B_M*_*ner 3 r lme4 mixed-models
在 R 中,我正在寻找一种方法来估计lmer使用 kenward-rogers 或 satterthwaite 自由度和 SE 的模型的线性对比的置信区间。
例如,我可以使用 t 值(来自 KR 的 df)和 SE 计算混合模型中固定效应参数的 CI,如带有 R 的 SAS。
mod<-lmerTest::lmer(y~time1+treatment+time1:treatment+(1|PersonID),data=data)
lmerTest::summary(mod,ddf = "Kenward-Roger")
Run Code Online (Sandbox Code Playgroud)
这个输出:
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 49.0768 1.0435 56.4700 47.029 < 2e-16 ***
time1 5.8224 0.5963 48.0000 9.764 5.51e-13 ***
treatment 1.6819 1.4758 56.4700 1.140 0.2592
time1:treatment 2.0425 0.8433 48.0000 2.422 0.0193 *
Run Code Online (Sandbox Code Playgroud)
允许时间 1 的 CI,例如:
5.8224+abs(qt(0.05/2, 48))*0.5963 #7.021342
5.8224-abs(qt(0.05/2, 48))*0.5963 #4.623458
Run Code Online (Sandbox Code Playgroud)
我想为固定系数的线性对比做同样的事情。这是 p 值,但没有 SE 输出。
pbkrtest::KRmodcomp(mod,matrix(c(0,0,1,0),nrow = 1))
stat ndf ddf F.scaling p.value
Ftest 1.2989 1.0000 56.4670 1 0.2592
Run Code Online (Sandbox Code Playgroud)
有没有办法从使用这种类型的 df 的 lmer 线性对比中获得 SE 或 CI?
小智 5
为此,您至少有两个选择:使用lsmeans包,或手动执行(使用函数vcovAdj.lmerMod和pbkrtest::get_Lb_ddf)。就我个人而言,如果要测试的对比度不是很“简单”,我会选择后者,因为我觉得语法lsmeans有点复杂。
例如,采用以下模型:
library(pbkrtest)
library(lme4)
library(nlme) # for the 'Orthodont' data
# 'age' is a numeric variable, while 'Sex' and 'Subject' are factors
model <- lmer(distance ~ age : Sex + (1 | Subject), data = Orthodont)
Linear mixed model fit by REML ['lmerMod']
Formula: distance ~ age:Sex + (1 | Subject)
…
Fixed Effects:
(Intercept) age:SexMale age:SexFemale
16.7611 0.7555 0.5215
Run Code Online (Sandbox Code Playgroud)
我们希望从中获得有关男性和女性年龄系数之间差异的统计数据(即age:SexMale - age:SexFemale)。
使用 lsmeans:
library(lsmeans)
# Evaluate the contrast at a value of 'age' set to 1,
# so that the resulting value is equal to the regression coefficient
lsm = lsmeans(model, pairwise ~ age : Sex, at = list(age = 1))$contrast
Run Code Online (Sandbox Code Playgroud)
产生:
contrast estimate SE df t.ratio p.value
1,Male - 1,Female 0.2340135 0.06113276 42.64 3.828 0.0004
Run Code Online (Sandbox Code Playgroud)
或者,手动进行计算:
# Specify the contrasts: age:SexMale - age:SexFemale
# Must have the same order as the fixed effects in the model
K = c("(Intercept)" = 0, "age:SexMale" = 1, "age:SexFemale" = -1)
# Retrieve the adjusted variance-covariance matrix, to calculate the SE
V = pbkrtest::vcovAdj.lmerMod(model, 0)
# Point estimate, SE and df
point_est = sum(K * fixef(model))
SE = sqrt(sum(K * (V %*% K)))
df = pbkrtest::get_Lb_ddf(model, K)
alpha = 0.05 # significance level
# Calculate confidence interval for the difference between the 'age' coefficients for males and females
Delta_age_CI = point_est + SE * qt(c(0.5 * alpha, 1 - 0.5 * alpha), df)
Run Code Online (Sandbox Code Playgroud)
将导致点估计等于0.2340135、 SE 0.06113276、 df42.63844和置信区间[0.1106973, 0.3573297]