我有两组数据,并且有理由相信它们遵循相同的渐近曲线,且具有偏移上限。假设所有观察结果都是独立的。我用 drc 库拟合了一个简单的模型:
library(drc)
library(nlme)
library(dplyr)
df <- structure(list(iv = c(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1,
2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4,
5, 6), dv = c(9.2, 8.5, 13.5, 15.8, 18.3, 17.7, 8.7, 10.8, 14.3,
15, 18, 15.3, 8.7, 14.6, 14.8, 16.8, 15.8, 15.8, 14.2, 13.5,
18.5, 20.8, 23.3, 22.7, 13.7, 15.8, 19.3, 20, 23, 20.3, 13.7,
19.6, 19.8, 21.8, 20.8, 20.8), group = c("1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2")), row.names = c(NA, -36L), class = c("tbl_df",
"tbl", "data.frame"))
drm_mod <- drm(
dv ~ iv,
curveid = group,
data = df,
fct = AR.2(names = c("Upper Limit", "Steepness"))
)
所以我尝试用 nlme 复制并失败:
nlme_mod <- nlme(dv ~ SSasympOrig(iv, Asm, lrc), data=df,
fixed= list(Asym + lrc ~ group),
groups = group,
random = ~ 1)
Error: object 'group' not found
2.
nlme.formula(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), groups = group, random = ~1)
1.
nlme(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), groups = group, random = ~1)
如果我删除组规范,我会得到以下信息:
nlme_mod <- nlme(dv ~ SSasympOrig(iv, Asm, lrc), data=df,
fixed= list(Asym + lrc ~ group),
# groups = group,
random = ~ 1)
Error in nlsList.formula(model = dv ~ SSasympOrig(iv, Asm, lrc), data = df) :
'data' must be a "groupedData" object if 'formula' does not include groups
8.
stop("'data' must be a \"groupedData\" object if 'formula' does not include groups")
7.
nlsList.formula(model = dv ~ SSasympOrig(iv, Asm, lrc), data = df)
6.
nlme::nlsList(model = dv ~ SSasympOrig(iv, Asm, lrc), data = df)
5.
eval(expr, p)
4.
eval(expr, p)
3.
eval.parent(nlsLCall)
2.
nlme.formula(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), random = ~1)
1.
nlme(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), random = ~1)
假设所有观察结果都是独立的。
那你为什么要尝试拟合混合效应模型?
df$group <- factor(df$group)
library(nlme)
#get starting values
mod0 <- nlsList(dv ~ SSasympOrig(iv, Asym, lrc) | group, data=df)
coef <- coef(mod0)
#due to treatment contrasts
start <- rbind(coef[1,], apply(coef, 2, diff))
mod <- gnls(dv ~ SSasympOrig(iv, Asym, lrc), data=df,
params = list(Asym + lrc ~ group), start = start)
plot(dv ~ iv, data = df, pch = as.integer(df$group))
curve(predict(mod, newdata = data.frame(iv = x, group = "1")), add = TRUE)
curve(predict(mod, newdata = data.frame(iv = x, group = "2")), add = TRUE, lty = 2)