【问题标题】:How to add geom_pointrange() and ggplot2 aesthetics to plot_cme?如何将 geom_pointrange() 和 ggplot2 美学添加到 plot_cme?
【发布时间】:2023-01-18 05:16:55
【问题描述】:

我正在复制这个情节:

这是使用的线性模型:

feber <- lm(sdptv~antiimm*expcondition,data=subset(test, expcondition != "Adversarial"))

为了绘制 expcondtion 的边际效应如何随着 antimm 变量值的变化而变化,我使用 plot_cme:

 plot_cme(feber, effect = "expcondition", condition = "antiimm")

由此我得到以下情节: enter image description here

我的问题是: 如何将 geom_pointranges 添加到我的绘图中?我觉得我需要创建一个 data.frame。但不确定如何。以前没用过边际效应。

提前致谢。

我的数据集的前 20 行:

test <- structure(list(sdimmpos = c(0.666666666666667, 0, 0.555555555555556, 0.666666666666667,           0.444444444444444, 0.111111111111111, 0.666666666666667, 0.666666666666667, 0.666666666666667,     0.222222222222222, 0.777777777777778, 0.666666666666667, 0.111111111111111, 0.777777777777778, 0.333333333333333, 0.555555555555556, 0.666666666666667, 0.444444444444444, 0.444444444444444, 0.444444444444444), sdptv = c(0.222222222222222, 1, 0.444444444444444, 0.444444444444444, 0.777777777777778, 0, 0, 0.111111111111111, 1, 0.333333333333333, 0, 1, NA, 0.777777777777778, 0.222222222222222, 0.777777777777778, 0.222222222222222, 0.444444444444444, NA, NA), rvptv = c(0, 0, 0.777777777777778, 0.888888888888889, 0.333333333333333, 0, 0.888888888888889, 0.111111111111111, 0, 0, 0.666666666666667, 0.666666666666667, NA, 0.222222222222222, 0, 0.111111111111111, 1, 0.555555555555556, NA, NA), sfptv = c(0, 0, 0.555555555555556, 0.888888888888889, 0.888888888888889, 0, 0.666666666666667, 0, 0.666666666666667, 0, 1, 0.555555555555556, NA, 0.111111111111111, 0, 0.111111111111111, 0.666666666666667, 0.555555555555556, NA, NA), elptv = c(0, 0, 0.666666666666667, 0.777777777777778, 0.333333333333333, 0, 0.555555555555556, 0, 0.666666666666667, 0, 1, 0.222222222222222, NA, 0, 0, 0, 0.555555555555556, 0.777777777777778, NA, NA), veptv = c(0.444444444444444, 0, 0.444444444444444, 0.888888888888889, 0.222222222222222, 0.222222222222222, 0, 0.666666666666667, 0.444444444444444, 0.444444444444444, 0, 0.555555555555556, NA, 0.777777777777778, 0.444444444444444, 0.888888888888889, 0.222222222222222, 0.222222222222222, NA, NA), dfptv = c(0.111111111111111, 0.111111111111111, 0.111111111111111, 0.555555555555556, 0, 1, 0, 0.666666666666667, 0.444444444444444, 0, 0, 0.111111111111111, NA, 0, 0.444444444444444, 0, 0, 0, NA, NA), nbptv = c(0, 0.444444444444444, 0, NA, 0.444444444444444, 0.777777777777778, 0, 0.444444444444444, 0.444444444444444, 0, 0, 0, NA, 0, 0.444444444444444, 0, 0, 0, NA, NA), maxlbptv = c(0.222222222222222, 1, 0.777777777777778, 0.888888888888889, 0.888888888888889, 0, 0.888888888888889, 0.111111111111111, 1, 0.333333333333333, 1, 1, NA, 0.777777777777778, 0.222222222222222, 0.777777777777778, 1, 0.777777777777778, NA, NA), expcondition = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 2L, 2L, 1L, 1L, 3L, 2L, 1L, 3L), .Label = c("Control", "Accommodative", "Adversarial" ), class = "factor"), expcondition_refadv = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 3L, 2L, 1L, 3L, 3L, 1L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 1L), .Label = c("Adversarial", "Control", "Accommodative" ), class = "factor"), gender = structure(c(2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L), .Label = c("1", "2"), class = "factor"), education = structure(c(6L, 6L, 2L, 7L, 1L, 5L, 8L, 6L, 4L, 1L, 6L, 1L, 7L, 7L, 7L, 6L, 3L, 6L, 1L, 3L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9"), class = "factor"), occupation = structure(c(1L, 5L, 4L, 3L, 5L, 1L, 2L, 1L, 1L, 6L, 1L, 1L, 8L, 7L, 5L, 1L, 4L, 4L, 3L, 5L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), agegrp = structure(c(3L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 3L, 1L, 3L, 3L, 3L, 1L, 1L, 2L, 2L), .Label = c("1", "2", "3"), class = "factor"), hhincome = structure(c(5L, 4L, 13L, 12L, 3L, 5L, 9L, 6L, 5L, 1L, 6L, 3L, 12L, 12L, 4L, 5L, 1L, 2L, 2L, 9L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"), class = "factor"), region = structure(c(4L, 4L, 1L, 1L, 2L, 5L, 1L, 1L, 3L, 1L, 4L, 5L, 1L, 5L, 4L, 4L, 4L, 1L, 3L, 5L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), att_imm1 = c(0.75, 1, 0.25, 0.75, 0.75, 1, 0, 1, 0.75, 0.75, 0, 0.5, 1, 0.5, 1, 0.5, 0.5, 0, 0, 0.5), att_imm2 = c(0.25, 1, NA, 0.25, 0.75, 1, 0, 1, 0.5, 1, 0, 0.75, 1, 0.5, 1, 0.5, 0.5, 0.25, 0.5, 0.5), antiimm = c(0.5, 1, 0.25, 0.5, 0.75, 1, 0, 1, 0.625, 0.875, 0, 0.625, 1, 0.5, 1, 0.5, 0.5, 0.125, 0.25, 0.5), partygroup15 = structure(c(3L, 2L, 4L, 2L, 2L, 1L, 4L, 1L, 2L, 2L, 2L, 2L, 4L, 3L, 4L, 1L, 2L, 4L, 1L, 3L ), .Label = c("DPP", "Left bloc", "Other right", "Other/DK" ), class = "factor")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

    标签: r ggplot2 marginal-effects r-marginaleffects


    【解决方案1】:

    默认情况下,只要 x 轴变量为连续数值变量,plot_cme() 就会画一条线。如果您使用 x 轴上的因子变量拟合模型,您将获得点范围。此行为不可自定义。但是,marginaleffects 仍然可以让您完成 97% 的工作。

    您所要做的就是使用 draw=FALSE 参数返回一个数据框,其中包含用于绘制您当前获得的绘图的基础数据。然后,您可以将该数据框提供给ggplot2,并根据您的喜好自定义绘图:

    library(marginaleffects)
    library(ggplot2)
    
    p <- plot_cme(feber, effect = "expcondition", condition = "antiimm", draw = FALSE)
    
    ggplot(p, aes(
        x = condition1,
        y = comparison,
        ymin = conf.low,
        ymax = conf.high,
        color = contrast)) +
        geom_pointrange(position = position_dodge(width = .2)) +
        theme_classic()
    

    请参阅此小插图以获取更多自定义选项(需要 marginaleffects 的 0.8.0 版):https://vincentarelbundock.github.io/marginaleffects/articles/plot.html

    【讨论】:

    • 非常感谢,先生。现在,你知道为什么我们得到这么多点范围吗?条件 (antiimm) 只有 9 个值,那么为什么绘图显示 25 个点范围?最好
    • 对于数值变量,该函数在整个范围内绘制一组等距点。这是为了确保情节看起来不错。使用 0.8.0 版,您可以在参数中使用向量或函数指定确切的值:condition = list(antiimm = unique) 请参阅上面链接的插图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多