【发布时间】:2025-12-15 23:30:01
【问题描述】:
我正在使用 nlme 包在 R 中运行多元混合模型。假设 x 和 y 是纵向数据的响应变量,假设组内误差是相关的。残差矩阵表示为:
所以我的问题是如何将相关性纳入lme 函数?
我尝试了命令corr = corComSymm(from =~ 1 | x) 或corr = corAR1(from =~ 1 | x) 但没有奏效!
这里是例子:
# visiting time by months
time = rep(c(0,3,6,9),time = 4, 200)
# subjects
subject = rep(1:50, each = 4)
# first response variable "identity"
x = c(rep(0, 100), rep(1,100))
# second response variable "identity"
y = c(rep(1, 100), rep(0,100))
# values of both reponses variables (x_1, x_2)
value = c(rnorm(100,20,1),rnorm(100,48,1))
# variables refer to reponses variables (x_1, x_2)
variable = factor(c(rep(0,150),rep(1,50)), label=c("X","Y"))
df = data.frame(subject , time, x,y,value, variable)
library(nlme)
# fit the model that each response variable has intercept and slope (time) for each random and fixed effects
# as well as fixed effects slopes for sex and lesion, and each response has different variance
f= lme(value ~ -1 + x + y + x:time + y:time , random = ~ -1 + (x + y) + time:( x + y)|subject ,
weights = varIdent(form=~1| x),corr = corAR1(from = ~ 1|x), control=lmeControl(opt="optim"), data =df)
Error in corAR1(from = ~1 | x) : unused argument (from = ~1 | x)
有什么建议吗?
【问题讨论】:
-
首先,您将
form拼错为from。其次,form参数期望时间协变量为~ t或 ~ t |g ` 其中g是分组变量(请参阅corAR1的帮助页面。因为您已经定义了随机效应g作为主题,因此时间协变量必须是在每个主题内变化的变量。 -
谢谢@aosmith!你是对的,我拼写错误。但是,鉴于我正在尝试做的事情,我正在尝试为“nlme”调用的非固定部分得出正确的语法。根据'R'中的'nlme'函数,不清楚如何设置两个响应变量之间的相关性。
-
对不起@Alex。我相信“nlme”和“lme4”都用于混合模型,它们给出的结果几乎相似。这就是我标记两者的原因。
-
@R.Saeiti 是的,它们都可以用于混合模型。但是它们有不同的代码库,您只使用其中一个。请适当标记。