在逻辑回归后使用 +1sd/-1sd 在 ggplot 中绘制交互图

Ploting interaction plot in ggplot using +1sd/-1sd following logistic regression

我想绘制一个交互作用(一个自变量 -3 模态被视为分类-,一个调节变量 -7 模态被视为连续;最后,二元因变量 -0 或 1)。

具体来说,我打算制作一个图表,其中 y 轴为 DV,x 轴为分类 IV。现在,我想为我的连续调节变量绘制两条线,代表自变量 3 个水平的每个水平的平均值的 +1sd 和 -1sd(正如传统上在这种图表中所做的那样),而不是代表每种模式的七行。

如何让R软件用ggplot只在图表中计算并显示这两个特定的信息片段?

[编辑 1] 这是我的数据的一个子集:内容是分类 IV,Motivcentered,主持人(连续),resp 是我的 DV(二进制):

structure(list(content = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
resp = c(1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1), 
motivcentered = c(-0.25, 1.75, 1.75, -0.25, -2.25, 1.75, 1.75, -1.25, 0.75, -0.25, 0.75, -0.25, -4.25, -1.25, 1.75), 
id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), 
item = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 
.Names = c("content", "resp", "motivcentered", "id", "item"), 
row.names = c(NA, -15L), 
class = "data.frame", codepage = 65001L)

[编辑 2] 我正在尝试使用现在的 分类 ID(具有连续调节器和分类视频)。分类 ID 是与我之前的请求(见上文)的唯一区别。我在绘制这样的图表时遇到了问题(仍然为主持人绘制 +1sd、-1sd 和平均线),因为它没有显示我的 IV 的每种模式(3 种模式,应该出现在 x 轴上)。你们中有人知道如何使用提供的子集来处理该问题吗?

编辑 1: 在提供了一个小样本数据集后,我更新了我的答案。如果连续变量是你的调节器,我猜你想在 x 轴上有 content,在 y 轴上有预测概率,md 有不同的线,motivcentered 有 +1 和 -1 sd .

我将您的数据复制到名为 mydat:

的数据框中
# make categorical
mydat$content <- as.factor(mydat$content)
# fit model
fit <- glm(resp ~ content * motivcentered, 
           family = binomial("logit"), 
           data = mydat)
# load library
library(sjPlot)
# show plot
sjp.int(fit, type = "eff", 
        moderatorValues = "meansd", 
        swapPredictors = T)

moderatorValues 表示您想使用调节变量的哪些值,选项 meansd 是平均值,+1/-1 sd。默认情况下,sjp.int 假设具有较少唯一值的变量是调节器,但是,您希望相反。因此,swapPredictors 现在使用 content 作为沿 x 轴的 DV,并使用 motivcentered 作为调节器。

情节是这样的(有点奇怪,很可能是由于观察量有限):

对于逻辑回归,y 轴上只有值 0 和 1 几乎没有意义,因此显示了对预测概率的交互作用。

您有多种选择来更改情节外观,例如:

sjp.int(fit, 
        type = "eff", 
        moderatorValues = "meansd", 
        swapPredictors = T, 
        showCI = T, 
        facet.grid = T, 
        legendLabels = c("-1 sd", "mean", "+1 sd"))

(请注意,由于观察有限,该图再次看起来有点奇怪,尤其是置信区间)

编辑2:原post首先没有可重现的例子,所以我在这里尝试了"generic"猜测:可能是sjp.int函数的 sjPlot-package 对你有用吗?

假设您想要交互项的边际效应,均值和 +/- 1 sd,函数调用将如下所示:

library(sjmisc) # for sample data
data(efc)
mydf <- data.frame(usage = efc$tot_sc_e,
                   sex = efc$c161sex,
                   education = efc$c172code,
                   burden = efc$neg_c_7,
                   barthel = efc$barthtot)
# convert gender predictor to factor
mydf$sex <- relevel(factor(mydf$sex), ref = "2")
# fit samplemodel
fit <- lm(usage ~ .*., data = mydf)

library(sjPlot)
sjp.int(fit, 
        type = "eff",
        moderatorValues = "meansd")

结果图可能如下所示:

该图取自this package vignette部分效果显示情节类型的不同调节剂值;显示所有相关示例的部分称为 有意选择连续调节器的值