R 正在修复为x:y而不是仅为1:y运行的函数
我定义了一个函数来计算树的高度(h)和直径(dbh)之间的关系,基于从2份出版物中提取的方程。我的目标是使用论文1(香桃)中建立的关系来预测论文2(Marechaux和Chave)中方程中变量的值。我想测试一下纸张2生成的R 正在修复为x:y而不是仅为1:y运行的函数,r,function,plot,error-handling,R,Function,Plot,Error Handling,我定义了一个函数来计算树的高度(h)和直径(dbh)之间的关系,基于从2份出版物中提取的方程。我的目标是使用论文1(香桃)中建立的关系来预测论文2(Marechaux和Chave)中方程中变量的值。我想测试一下纸张2生成的nls()曲线适合纸张1的直径范围。目前,我一直收到一个错误(我相信plot()) xy.coords(x,y,xlabel,ylabel,log)中出现错误: “x”和“y”长度不同 如果我对[x:y]使用x=1以外的任何值,即胸径.min:dbh.max 我的职能如下: #
nls()
曲线适合纸张1的直径范围。目前,我一直收到一个错误(我相信plot()
)
xy.coords(x,y,xlabel,ylabel,log)中出现错误:
“x”和“y”长度不同
如果我对[x:y]
使用x=1以外的任何值,即胸径.min:dbh.max
我的职能如下:
# Plant.Functional.Type constants...
Dsb1 <- 2.09
Dsb2 <- 0.54
Db1 <- 0.93
Db2 <- 0.84
BDb1 <- 2.66
BDb2 <- 0.48
Eb1 <- 1.41
Eb2 <- 0.65
# # # # # # # # # # # # # # # # # # # # # # # # # # #
Generate.curve <- function(b1, b2, dbh.min, dbh.max){
# calculate Xiangtao's allometry...
tmp_h <- c(dbh.min:dbh.max)
for (dbh in dbh.min:dbh.max)
{
h = b1*dbh^(b2)
tmp_h[dbh] = h
}
# plot to check curve
plot(dbh.min:dbh.max, tmp_h)
# define secondary function for Marechaux and Chave allometry
h_fxn <- function(hlim,dbh,ah){
h = hlim * (dbh / (dbh + ah))
return(h)
}
# use nonlinear least squares model to solve for ah and hlim
# set model inputs
start.ah <- 1
start.hlim <- 5
tmp_v <- cbind(dbh.min:dbh.max,tmp_h)
tmp.fit <- nls(tmp_h ~ h_fxn(hlim,dbh.min:dbh.max,ah), start = list(hlim = start.hlim,
ah = start.ah), algorithm = "port", upper = list(hlim = 75, ah = 99))
# seems to be no way of extracting ah and hlim from tmp.fit via subset
# extract manually and then check fit with
# lines(dbh.min:dbh.max, hlim * (dbh.min:dbh.max/(dbh.min:dbh.max + ah)))
# for equation h = hlim * (dbh / (dbh + ah)) from Marechaux and Chave
return(tmp.fit)
}
# # # # # # # # # # # # # # # # # # # # # # # # # # #
但我也希望能够在[80:100]
等范围内检查曲线拟合。
我一直在试图弄清楚为什么
Generate.curve(Dsb1、Dsb2、80100)
现在大约3天都返回一个错误。感谢您的帮助。您的问题在于此部分:
tmp\u h这不太重要,因为我通常仍然可以获得所需的信息,但是当我使用dbhmin和dbhmax彼此保持一定距离时,例如Generate.curve(Dsb1,Dsb2,40,80)
I getnls中的错误(tmp\u h~h\u fxn(hlim,dbh.min:dbh.max,ah),start=list(hlim=start.hlim,:收敛失败:错误收敛(8)
知道原因吗?@sethparker我认为这只是因为数据点太少无法拟合模型。
Generate.curve(Dsb1,Dsb2,1,100)
lines(1:100, 36.75 * (1:100/(1:100 + 52.51)))