1

基本上,我有一个混合效应模型,其中包含 2 个预测变量、一个交互项和一个随机效应,如下所示:

x1<-abs(rnorm(300)) #continuous variable
x2<-sample(c(1,2,3), 300, replace = T) #discrete variable 
x3<-sample(c("A","B","C"), 300, replace = T) #categorical variable 
y<-abs(rnorm(300)) #continuous variable
df<-as.data.frame(cbind(as.numeric(x1),as.numeric(x2),x3,as.numeric(y)))

m1=lmer(y~x1*x2+(1|x3),data=df)
summary(m1)

我使用 lme4 来创建这样的模型。我想在 x2 的不同水平(这是一个具有 3 个水平的因素)上找到 x1 对 y(贝塔系数)的边际效应。

我还想获得每个级别的 beta 置信区间并绘制这样的结果。

4

1 回答 1

1

你可能想试试这个marginaleffects 包。 (免责声明:我是维护者。)

library(lme4)
library(marginaleffects)

x1<-abs(rnorm(300)) #continuous variable
x2<-sample(c(1,2,3), 300, replace = T) #discrete variable 
x3<-sample(c("A","B","C"), 300, replace = T) #categorical variable 
y<-abs(rnorm(300)) #continuous variable
df<-as.data.frame(cbind(as.numeric(x1),as.numeric(x2),x3,as.numeric(y)))
m1 <- lmer(y~x1*x2+(1|x3),data=df)

marginaleffects(m1, variables = "x1", newdata = typical(x2 = 1:3, x3 = "B"))
##   rowid     type term        dydx  std.error        x1 x2 x3 predicted
## 1     1 response   x1 -0.04269402 0.08814210 0.7792904  1  B 0.9280228
## 2     2 response   x1  0.03476429 0.05850354 0.7792904  2  B 0.8708736
## 3     3 response   x1  0.11222260 0.09286114 0.7792904  3  B 0.8137245
于 2021-11-22T02:04:50.343 回答