将混合模型公式从 SAS 转换为 R

发布于 2024-12-13 21:34:30 字数 1772 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩一世无双 2024-12-20 21:34:30

感谢您提供更详细的信息。我将数据存储在d中,以避免与data函数和参数混淆;这些命令可以以任何方式工作,但这种避免数据通常被认为是良好的做法。

请注意,由于 varvar2 之间缺乏平衡,交互很难适应;供参考的是交叉表:

> xtabs(~var1 + var2, data=d)
    var2
var1  1  2  3  4  5
   1 18 18 18 18 18
   2  0 18 18 18 18
   3  0  0 18 18 18
   4  0  0  0 18 18
   5  0  0  0  0 18

通常为了适合交互(并且没有主要效果),您会使用 : 而不是 *,但在这里最好制作一个因子,如下所示:

d$var12 <- factor(paste(d$var1, d$var2, sep=""))

然后使用 nlme,尝试

fm1 <- lme(yld ~ var12, random = ~ 1 | loc/year/rep, data = d)
anova(fm1)

使用 lme4,尝试

fm1 <- lmer(yld ~ var12 + (1 | loc/year/rep), data=d)
anova(fm1)

另请注意,因为 nlmelme4它们的函数名称有重叠,您只需加载R 会话中一次一次;要切换,您需要关闭 R 并重新启动。 (还存在其他方法,但这是最容易解释的方法。)

Thanks for the more detailed information. I stored the data in d to avoid confusion with the data function and parameter; the commands works either way but this avoiding data is generally considered good practice.

Note that the interaction is hard to fit because of the lack of balance between var and var2; for reference here's the crosstabs:

> xtabs(~var1 + var2, data=d)
    var2
var1  1  2  3  4  5
   1 18 18 18 18 18
   2  0 18 18 18 18
   3  0  0 18 18 18
   4  0  0  0 18 18
   5  0  0  0  0 18

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:

d$var12 <- factor(paste(d$var1, d$var2, sep=""))

Then with nlme, try

fm1 <- lme(yld ~ var12, random = ~ 1 | loc/year/rep, data = d)
anova(fm1)

and with lme4, try

fm1 <- lmer(yld ~ var12 + (1 | loc/year/rep), data=d)
anova(fm1)

Also note that because nlme and lme4 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文