我在纵向数据集中拟合混合效应模型,并注意到指定相同的结构会导致不同的输出,具体取决于我使用的是“混合”(来自 afex 包)还是 lme4。我在下面提供了可重现的代码(R 版本 4.2.2)
library(lme4)
library(afex)
library(tidyverse)
library(effectsize)
# Pull in toy data and convert vars to factors
dset_test1 <- mtcars %>%
mutate(car_id = row.names(mtcars)) %>%
mutate(vs = as.factor(vs)) %>%
mutate(am = as.factor(am))
# Create simulated longitudinal dataset
increase_longitudinal <- mtcars %>%
dplyr::select(am, wt) %>%
mutate(car_id = row.names(mtcars)) %>%
mutate(wt2 = (wt)*2)
# Merge and make long form
dset_test = merge(dset_test1, increase_longitudinal, on='car_id') %>%
pivot_longer(cols = c(wt, wt2), names_to = 'year', values_to = 'weight') %>%
mutate(year = as.factor(year))
# Set reference levels
dset_test$year <- relevel(dset_test$year, "wt")
# fit model with mixed
mix1 <- mixed(mpg~year * weight + hp +(1 | car_id), data=dset_test)
summary(mix1)
parameters::standardize_parameters(mix1)
# fit model with lme4
lmer1 <- lmer(mpg~year * weight + hp +(1 | car_id), data=dset_test)
summary(lmer1)
parameters::standardize_parameters(lmer1)
这会产生以下标准化输出:
# model with mixed:
Parameter | Std. Coef. | 95% CI
------------------------------------------
(Intercept) | -0.29 | [-0.40, -0.18]
year1 | -0.81 | [-0.98, -0.63]
weight | -1.12 | [-1.36, -0.88]
hp | -0.39 | [-0.53, -0.25]
year1:weight | -0.37 | [-0.45, -0.29]
# Model with lme4:
Parameter | Std. Coef. | 95% CI
--------------------------------------------
(Intercept) | -1.05 | [-1.29, -0.81]
yearwt2 | 1.54 | [ 1.21, 1.86]
weight | -1.42 | [-1.72, -1.12]
hp | -0.40 | [-0.54, -0.26]
yearwt2:weight | 0.71 | [ 0.56, 0.86]
正如您所看到的,模型系数有很大不同,特别是对于交互项,系数的符号甚至不同。感谢您对这里可能发生的情况有任何见解!
afex::mixed()
默认情况下自动使用和为零对比,因为这通常可以更轻松地解释具有交互作用的模型中的主效应(但 R 的默认设置是使用 treatment 对比)。
如果您在运行
options(contrasts = c("contr.sum", "contr.poly"))
拟合之前设置 lmer()
,您将获得相同的系数。 (还有其他方法来设置对比度,但这可能是最简单的。)
顺便说一句,我希望这不能代表您的真实数据——当我运行此代码时,我收到了很多警告,并且输出中出现了很多危险信号(例如,残留标准误差非常接近于零,与原始
yearwt2
拟合中的原始 lmer
估计一样)。如果可以的话,通常最好生成更合理的测试数据,这样人们(像我)就不会分心/认为问题可能是一些无法识别的问题......