问题 1:线性回归和混合效应模型如何处理因子变量?
A1:因子被编码为虚拟变量(1 = true,0 = false)。
例如,模型1的系数为:
coef(m1) #lm( distance ~ age + Sex)
#(Intercept) age SexFemale
# 17.7067130 0.6601852 -2.3210227
因此计算距离为:
距离 = 17.71 + 0.66*年龄 - 2.32*性别女性
其中 SexFemale 对于男性为 0,对于女性为 1。这简化为:
男: 距离 = 17.71 + 0.66*年龄
女:距离 = 15.39 + 0.66*年龄
如果模型有更多类别(例如超重、健康、体重不足),则相应地添加虚拟变量:
R代码:lm(距离~年龄+体重状态)
计算:距离 = 年龄 + 体重超过 (weightIsOver) + 体重 (healthy) + 体重 (weightIsUnder)
为每种体重类型创建三个单独的系数,并根据个人的体重类型乘以 0 或 1。
Q2:如果我对因子变量的每个级别都有一个单独的模型(m3
and m4
),这与模型有何不同m1
and m2
?
A2:斜率和截距根据您的模型而变化。
m1 是多元线性回归 (MLR),其中截距随性别而变化,但年龄的斜率相同。我们也可以将其称为随机斜率。线性混合效应 (LME) 模型 m2 还指定了随性别变化的截距(1|Sex
).
m3 和 m4 ~ 随机斜率和随机截距模型,因为数据是分开的。
让我们指定一个具有随机斜率和随机截距的 LME:
m2a <- lme(distance ~ age, data = Orthodont, random= ~ age | Sex,
control = lmeControl(opt="optim"))
#Changed the optimizer to achieve convergence
组合系数使我们能够检查模型的结构:
#Combine the model coefficients
coefs <- rbind(
coef(m1)[1:2],
coef(m1)[1:2] + c(coef(m1)[3], 0), #female coefficient added to intercept
coef(m2),
coef(m2a),
coef(m3),
coef(m4)); names(coefs) <- c("intercept", "age")
model.coefs <- data.frame(
model = paste0("m", c(1,1,2,2,"2a", "2a",3,4)),
type = rep(c("MLR", "LME randomIntercept", "LME randomSlopes",
"separate LM"), each=2),
Sex = rep(c("male","female"), 4),
coefs, row.names = 1:8)
model.coefs
# model model2 Sex intercept age #intercept & slope
#1 m1 MLR male 17.70671 0.6601852 #different same
#2 m1 MLR female 15.38569 0.6601852
#3 m2 LME randomIntercept male 17.67197 0.6601852 #different same
#4 m2 LME randomIntercept female 15.43622 0.6601852
#5 m2a LME randomSlopes male 16.65625 0.7540780 #different different
#6 m2a LME randomSlopes female 16.91363 0.5236138
#7 m3 separate LM male 16.34062 0.7843750 #different different
#8 m4 separate LM female 17.37273 0.4795455
Q3:哪一种是最好的模型/方法?
A3:这取决于具体情况,但可能是混合效应模型。
在您的示例中,m3 和 m4 彼此没有关系,并且每种性别本质上具有不同的斜率。可以检查 LME 模型以确定随机斜率是否合理(例如anova(m2, m2a)
)。当您有多个级别(例如学校内班级内的学生)和重复测量(针对同一主题或跨时间的多项测量)时,混合效应模型具有多种用途。您还可以指定协方差结构 http://www.theanalysisfactor.com/covariance-matrices/与这些模型。
为了可视化这些不同的模型,让我们看看Orthodont
data:
library(ggplot)
gg <- ggplot(Orthodont, aes(age, distance, fill=Sex)) + theme_bw() +
geom_point(shape=21, position= position_dodge(width=0.2)) +
stat_summary(fun.y = "mean", geom="point", size=8, shape=22, colour="black" ) +
scale_fill_manual(values = c("Male" = "black", "Female" = "white"))
Circles = raw data, Squares = means. Distance appears to increase linearly with age. Males have higher distances than females. The slopes may vary by sex too, with females having a smaller increase in distance with age compared to males. (Note: raw data have been slightly dodged on the x-axis to avoid overplotting.)
将我们的模型添加到数据中并放大:
gg1 <- gg +
geom_abline(data = model.coefs, size=1.5,
aes(slope = age, intercept = intercept, colour = type, linetype = Sex))
gg1 + coord_cartesian(ylim = c(21, 27)) #zoom in
在这里,我们看到具有随机截距的 LME 模型类似于 MLR 模型。具有随机截距和随机斜率的 LME 类似于子集数据上的单独 LM。
最后,这是如何制作等价的m2
使用lme4
包裹:
m2 <- lme(distance ~ age , data = Orthodont, random = ~ 1|Sex)
library(lme4)
m5 <- lmer(distance ~ age + (1|Sex), data = Orthodont) #same as m2
更多资源:
(广义)线性混合模型常见问题解答 http://glmm.wikidot.com/faq
比较nlme and lme4 https://freshbiostats.wordpress.com/2013/07/28/mixed-models-in-r-lme4-nlme-both/ using Orthodont
data.