【问题标题】:Marginal effect of beta at different values with corresponding confidence intervals具有相应置信区间的不同值的 beta 的边际效应
【发布时间】:2020-12-27 13:08:33
【问题描述】:

基本上,我有一个混合效应模型,其中包含 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 置信区间并绘制这样的结果。

【问题讨论】:

    标签: r interaction marginal-effects


    【解决方案1】:

    你可能想试试the marginaleffects package. (免责声明:我是维护者。)

    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
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2022-08-20
      • 2014-04-25
      • 2023-01-31
      • 1970-01-01
      相关资源
      最近更新 更多