如何解决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
在做什么,则它是根据您的情况估计参数a
和b
的值。因此,不需要变量Volume
和Height
的起始值。这些只是数据集中的值。如果您将它们从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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。