将混合模型公式从 SAS 转换为 R
我想在 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 的相同组合的实验
在重复中进行测试(重复重复编号为 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
I want to fit a mixed model using nlme package in R which is equivalent to following SAS codes:
proc mixed data = one;
class var1 var2 year loc rep;
model yld = var1 * var2;
random loc year(loc) rep*year(loc);
EDITS: Explanation of what is experiment about
the same combination of var1 and var2 were tested in replicates (rep- replicates are numbered 1:3). The replicates (rep) is considered random. This set of experiment is repeated over locations (loc) and years (year). Although replicates are numbered 1:3 within each location and year for covinience because they do not have any name, replication 1 within a location and a year doesnot have correlation replication 1 within other location and other year
I tried the following codes:
require(nlme)
fm1 <- lme(yld ~ var1*var2, data = one, random = loc + year / loc + rep * year / loc)
Is my codes correct?
EDITS: data and model based on suggestions
you can download the example data file from the following link:
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
EXPECTED BASED ON SAS OUTPUT
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
I tried the following model, I still getting some errors:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您提供更详细的信息。我将数据存储在
d
中,以避免与data
函数和参数混淆;这些命令可以以任何方式工作,但这种避免数据通常被认为是良好的做法。请注意,由于
var
和var2
之间缺乏平衡,交互很难适应;供参考的是交叉表:通常为了适合交互(并且没有主要效果),您会使用
:
而不是*
,但在这里最好制作一个因子,如下所示:然后使用
nlme
,尝试使用
lme4
,尝试另请注意,因为
nlme
和lme4
它们的函数名称有重叠,您只需加载R 会话中一次一次;要切换,您需要关闭 R 并重新启动。 (还存在其他方法,但这是最容易解释的方法。)Thanks for the more detailed information. I stored the data in
d
to avoid confusion with thedata
function and parameter; the commands works either way but this avoidingdata
is generally considered good practice.Note that the interaction is hard to fit because of the lack of balance between
var
andvar2
; for reference here's the crosstabs:Normally to just fit the interaction (and no main effects) you'd use
:
instead of*
, but here it works best to make a single factor, like this:Then with
nlme
, tryand with
lme4
, tryAlso note that because
nlme
andlme4
have overlap in their function names you need to only load one at time into your R session; to switch you need to close R and restart. (Other ways exist but that's the simplest to explain.)