首页 文章

可视化二元逻辑模型在ggplot中双向交互的边际效应

提问于
浏览
0

有人可以使用ggplot建议一种快速且易于理解的方法,用于可视化glm模型/二元逻辑回归模型的双向交互吗?我对边际效应感兴趣!

我看过其他帖子,但并不是真的了解它们 . 另一个问题是我不能使用ggpredict / gginteraction,因为R版本(3.4.2) .

我的数据结构如下(简化):

region_AB   motive   voter_attribute  vote_for_party_XY
1           1        1                1
1           0        1                1
1           1        0                0
0           0        0                0
0           0        1                0
0           1        0                0

而且我声称(并且实际发现)那个区域调解了给定动机对XY方投票的影响 .

现在我知道这不是一个可重复的例子 . 但也许有人可以提出一个适合所有解决方案(至少对于glm模型的双向交互的情况) . 如果有必要并且有帮助,也许 mtcars 数据集可以用于示例目的:there's even an example for an interaction-term model using this dataset .

我希望有人有一个很好的解决方案 . 这可能是可视化双向互动的边际效应的一般指南......

1 回答

  • 0

    您可以使用ggeffects-package来计算边际效应 . 返回值是一个数据框,但是有一个创建/返回ggplot对象的 plot() -method . 这是一个带有二进制结果的人工示例,但您可以在上面引用的网站的"Articles"中找到更多详细信息 .

    library(ggeffects)
    library(sjmisc) # to preserve labels
    data(efc)
    
    # prepare data, create binary outcome and make
    # numeric variables categorical
    efc$neg_c_7d <- dicho(efc$neg_c_7)
    efc$c161sex <- to_factor(efc$c161sex)
    efc$c172code <- to_factor(efc$c172code)
    
    # fit logistic regression
    m <- glm(
      neg_c_7d ~ c12hour + c161sex * c172code,
      data = efc,
      family = binomial(link = "logit")
    )
    
    # compute and plot marginal effects
    ggpredict(m, c("c172code", "c161sex")) %>% plot()
    

    enter image description here

    请注意,我使用的数据集is labelled,这就是轴用"proper"值和变量标签注释的原因 .

相关问题