【发布时间】:2016-12-24 23:25:12
【问题描述】:
我正在尝试使用 NLS 在 R 中评估不同人群是否达到不同的渐近线。这里我有两个 data.frames df1 只有一个人群(由站点表示)
df1<- structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("ALT01",
"ALT02", "ALT03", "Cotton", "Deep", "Eckhardt", "Green", "Johnson",
"Kissinger", "Marsh", "Sand", "Shypoke", "Sora", "Spike", "Tamora",
"WRP01", "WRP05", "WRP08", "WRP10", "WRP11", "WRP12", "WRP14",
"WRP15", "WRP18"), class = "factor"), Nets = 1:18, Cumulative.spp = c(12L,
13L, 15L, 17L, 17L, 17L, 17L, 19L, 19L, 19L, 19L, 20L, 22L, 22L,
22L, 22L, 22L, 22L)), .Names = c("Site", "Nets", "Cumulative.spp"
), row.names = c(NA, 18L), class = "data.frame")
而 df2 必须有种群(再次由 Site 表示)
df2 <- structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("ALT01",
"ALT02", "ALT03", "Cotton", "Deep", "Eckhardt", "Green", "Johnson",
"Kissinger", "Marsh", "Sand", "Shypoke", "Sora", "Spike", "Tamora",
"WRP01", "WRP05", "WRP08", "WRP10", "WRP11", "WRP12", "WRP14",
"WRP15", "WRP18"), class = "factor"), Nets = c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L), Cumulative.spp = c(12L, 13L, 15L, 17L, 17L,
17L, 17L, 19L, 19L, 19L, 19L, 20L, 22L, 22L, 22L, 22L, 22L, 22L,
7L, 10L, 11L, 12L, 13L, 14L, 14L, 14L, 15L, 15L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L)), .Names = c("Site", "Nets", "Cumulative.spp"
), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L), class = "data.frame")
当我为一个人群建模时,一切看起来都很棒:
Model1<-nls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0), data = df1)
我正在尝试做的是看看我是否可以将多个群体添加到同一个模型并添加一个站点变量,我已经尝试过:
Model2<-nls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0) + Site , data = df2)
还有这个:
Model2<-nls(Cumulative.spp ~ SSasympOff(Nets + Site , A, lrc, c0), data = df2)
但到目前为止没有运气,任何帮助将不胜感激。
【问题讨论】:
-
查看
nlme::gnls(),尤其是params参数... -
谢谢@BenBolker 我试图理解
params的文档,虽然它说每个参数都有不同的模型我还没有找到任何关于如何要做到这一点,你能帮我多一点吗? -
类似于
gnls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0), params = Nets ~ Site, data = df2)(如果您希望Nets的效果在不同站点之间有所不同)或params = Nets+A+lrc+c0~Site如果您希望所有参数都不同(尽管在这种情况下您可以细分数据并分别拟合每个站点;唯一的(?)差异将是残差方差的汇总与站点特定估计) -
感谢@BenBolker,因为您看到有人提出了使用 nls 的解决方案,但我仍然想学习
gnls方式,我尝试了gnls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0), params = Nets ~ Site, data = df2)并给了我这个错误:错误在 eval(expr, envir, enclos) 中:找不到对象'A';gnls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0), params = Nets+A+lrc+c0~Site, data = df2)给了我另一个错误,这个:gnls(Cumulative.spp ~ SSasympOff(Nets, A, lrc, c0), data = df2)仍然有效