nls缺少值或产生无穷大

问题描述

我正在尝试使用Chapman-Richards函数对嵌套数据进行建模,如下所示: https://image.slideserve.com/575351/testing-eichhorn-s-rule35-n.jpg

我的原始数据如下:

structure(list(Site = c(1,1,4,4),Height = c(2.4,11.3,20.5,27.4,32,34.9,2.45,7.45,13.05,17.7,20.75,22.8),Volume = c(12,220,605,991,1288,1495,11,106,283,473,619,723)),row.names = c(NA,-12L),class = c("tbl_df","tbl","data.frame"))

我创建一个增长模型:

model <- function(data){
  nls(Volume~ a * (Height^b),data=data,start=c(Volume=200,Height=5,a=1,b=1))
}

当我嵌套数据以为每个嵌套创建模型时:

Silver %>% group_by(Site_class) %>% nest() %>% 
  mutate(mod= map(.x=data,model))

我收到以下错误:

fitting parameters ‘a’,‘b’ without any variables
Error in numericDeriv(form[[3L]],names(ind),env) : 
  Missing value or an infinity produced when evaluating the model
Called from: numericDeriv(form[[3L]],env)

我知道数据可能很少变化。

如果我尝试alg =“ plinear”,则会出现以下错误:

fitting parameters ‘a’,‘b’ without any variables
Error in qr.solve(QR.B,cc) : singular matrix 'a' in solve
Called from: qr.solve(QR.B,cc)

我尝试将其更改为robustbase :: nlrob,以防其表现更好。

这会将错误代码更改为:

Error in parse(text = x,keep.source = FALSE) : 
  <text>:2:0: unexpected end of input
1: ~ 
   ^
Called from: parse(text = x,keep.source = FALSE)

有人知道是什么原因导致我这个问题或如何解决?非常感谢!

解决方法

如果我了解nls在做什么,则它是根据您的情况估计参数ab的值。因此,不需要变量VolumeHeight的起始值。这些只是数据集中的值。如果您将它们从start参数中删除,错误就会消失,您会得到一个模型。

model <- function(data){
  nls(Volume~ a * (Height^b),data=data,start=c(a = 1,b = 1))
}
  
df %>% group_by(Site) %>% nest() %>% 
  mutate(mod= map(.x=data,model))

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...