我使用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代码,因此无法获得绘图方差的估计值。
答案 0 :(得分:3)
我找到了问题的答案:我需要添加模型的一个组件,为我用nlsLM回归的函数生成一个渐变。
log.model = function(t.RedventedBulbul, Asym, xmid, scal) {
numericDeriv(quote(Asym/(1 + exp((xmid - t.RedventedBulbul)/scal))),
c("Asym", "xmid", "scal"), parent.frame())
}