我正在使用 nls() 为多个鸟类种群的数据拟合一个逻辑模型(自启动;SSlogis)。我的目标是为数据拟合一个预期函数(仅使用每个数据集的一部分),并在图表上显示关于预期的方差的度量。然后,我想拟合并绘制观察到的函数(使用每个总体的整个数据集)以确定观察到的动态是否在期望的方差范围内。这是我目前为实现此目的而编写的代码:
CE.mod = nls(CE.observed ~ SSlogis(t.CattleEgret, Asym, xmid, scal))
with(collapse.data, plot(CE.time, CE.obs))
CE.extrap = predict(CE.mod, data.frame(t.CattleEgret = CE.time))
lines(CE.time, CE.extrap)
CE.se.fit = sqrt(apply(attr(CE.extrap, "gradient"), 1, function(x)
sum(vcov(CE.mod)*outer(x,x))))
matplot(CE.time, CE.extrap+outer(CE.se.fit, qnorm(c(0.5, 0.025, 0.975))),
type = "l", lty = c(1,1,1), ylab = "Abundance (# per party hour)",
xlab = "Time (year)", main = "Cattle Egret Collapse Analysis",
pch = 15, font.lab = 2, font.axis = 2, cex = 4, cex.lab = 1.5,
cex.axis = 2, cex.main = 2, frame.plot = FALSE, lwd = 4, 10)
with(collapse.data, matpoints(CE.time, CE.obs, pch = 15, cex = 3))
lines(CE.time, predict(nls(CE.obs ~ SSlogis(log(CE.time),
Asym, xmid, scal))), lty = 3, lwd = 4)
哪里(来自“collapse.data”文件):
t.CattleEgret = c(1:20)
CE.time = c(1:45)
CE.obs = c(0.3061324, 0.0000100, 0.2361211, 0.5058240, 2.0685032, 2.1944544,
4.2689494, 4.9508297, 3.1334720, 3.6570752, 5.6753381, 10.9133183,
5.4518257, 20.4166979, 15.9741054, 19.0970426, 13.7559959, 14.1358153,
15.9986416, 29.6762828, 10.3760667, 8.4284488, 6.1060359, 3.7099982,
3.3584060, 2.5981386, 2.5697082, 2.8091952, 5.5487979, 1.6505442,
2.2696972, 2.1835692, 3.6747876, 4.8307886, 3.5019731, 2.8397137,
1.8605288, 11.1848738, 2.6268683, 4.1215127, 2.3996210, 2.6569938,
2.1987387, 3.0267252, 2.4420927)
CE.observed = c(0.3061324, 0.0000100, 0.2361211, 0.5058240, 2.0685032, 2.1944544,
4.2689494, 4.9508297, 3.1334720, 3.6570752, 5.6753381, 10.9133183,
5.4518257, 20.4166979, 15.9741054, 19.0970426, 13.7559959, 14.1358153,
15.9986416, 29.6762828)
该代码工作正常并产生如下图:
但是,如果我从代码的最后一行中删除“log()”,以便编写以下代码:
lines(CE.time, predict(nls(CE.obs ~ SSlogis(CE.time,
Asym, xmid, scal))), lty = 3, lwd = 4),
该线不会绘制,我收到此错误:
Error in nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid =
aux[1L], : step factor 0.000488281 reduced below 'minFactor' of 0.000976562
即使我使用 nls.controls 并更改“minFactor”值,我也无法更改。我还在为某些人群定义 mod(##.mod 部分)的初始行之后收到此错误消息。
此外,对于某些人群,我在报告此问题的最后一行代码之后收到一条错误消息:
Error in qr.solve(QR.B, cc) : singular matrix 'a' in solve
我想不出自然对数转换数据的合理性,我只能假设我只是以允许 predict() 和 SSlogis() 的方式更改了数据(在这种情况下是任意记录的)功能正常,但我不知道为什么。对于此类问题,我无法在任何论坛中找到任何合适的答案。任何帮助将不胜感激。
*更新:我已尝试按照 Roland 的建议实现 nlsLM 功能(如下)。这确实清理了使用令人困惑的 log() 的代码部分:
lines(CE.time, predict(nlsLM(CE.obs ~ Asym/(1 + exp((xmid - CE.time)/scal)), start
= list(Asym = max(CE.obs), xmid = popsizetime[1], scal = 1), control =
nls.lm.control(maxiter = 1000))
但是,对于其他人群,我在初始模型规范中遇到了与上述相同的错误消息:
ChMa.mod = nls(ChMa.observed ~ SSlogis(t.ChestnutMannikin, Asym, xmid, scal))
Error in nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid =
aux[1L], : step factor 0.000488281 reduced below 'minFactor' of 0.000976562
切换到:
ChMa.mod = nlsLM(ChMa.observed ~ Asym/(1 + exp((xmid - t.ChestnutMannikin)/
scal)), start = list(Asym = max(ChMa.obs), xmid = popsizetime[2],
scal = 1), control = nls.lm.control(maxiter = 1000))
在哪里
ChMa.observed = c(4.02785074, 0.33847154, 0.99029776, 2.86516540, 0.59588068,
0.01334333, 2.07693362, 0.62485994, 3.48979515, 3.67785202, 20.84180181)
t.ChestnutMannikin = c(1:11)
popsizetime[2] = 11
虽然此开关确实避免了错误消息,但 nlsLM 评估函数但不评估梯度。如果没有梯度评估,我无法使用 se.fit 代码,因此无法获得用于绘图的方差估计。