我希望使用R中的nlme包拟合混合模型,该模型等价于以下SAS代码:
proc mixed data = one;
class var1 var2 year loc rep;
model yld = var1 * var2;
random loc year(loc) rep*year(loc);
编辑:实验内容解释
同样的 var1 和 var2 组合进行了多次重复测试(rep- 重复测试编号为 1:3)。重复测试(rep)是随机的。这组实验在不同地点(loc)和年份(year)中重复。虽然为方便起见,在每个地点和年份中,重复测试都被编号为 1:3,但一个地点和一年内的重复测试 1 与其他地点和年份内的重复测试 1 没有相关性。
我尝试了以下代码:
require(nlme)
fm1 <- lme(yld ~ var1*var2, data = one, random = loc + year / loc + rep * year / loc)
我的代码是否正确?
编辑:根据建议修改了数据和模型。您可以从以下链接下载示例数据文件: https://sites.google.com/site/johndatastuff/mydata1.csv
data$var1 <- as.factor(data$var1)
data$var2 <- as.factor(data$var2)
data$year <- as.factor(data$year)
data$loc <- as.factor(data$loc)
data$rep <- as.factor(data$rep)
following suggestions from the comments below:
fm1 <- lme(yld ~ var1*var2, data = data, random = ~ loc + year / loc + rep * year / loc)
Error in getGroups.data.frame(dataMix, groups) :
Invalid formula for groups
根据SAS输出预期
Type 3 tests of fixed effects
var1*var2 14 238 F value 16.12 Pr >F = < 0.0001
Covariance parameters:
loc = 0, year(loc) = 922161, year*rep(loc) = 2077492, residual = 1109238
我试过下面这个模型,但还是遇到了一些错误:
Edits: Just for information I tried the following model
require(lme4)
fm1 <- lmer(yld ~ var1*var2 + (1|loc) + (1|year / loc) + (1|rep : (year / loc)),
data = data)
Error in rep:`:` : NA/NaN argument
In addition: Warning message:
In rep:`:` : numerical expression has 270 elements: only the first used