简体   繁体   English

将水平线添加到每个面板具有不同截距的“效果图”

[英]Add horizontal line to an "effects plot" with different intercepts per panel

I'm using the effects package to plot the effects from an ordinal logistic regression and I'd like to add a different horizontal line (represented the expected baseline probabilities if the predictor has no effect) to the multi-panel plot with a different baseline probability for each panel of the plot.我正在使用效果 package 到 plot 顺序逻辑回归的效果,我想向具有不同基线的多面板 plot 添加一条不同的水平线(表示如果预测变量没有效果,则表示预期的基线概率) plot 的每个面板的概率。

Here's an example dataset:这是一个示例数据集:

# Simulate some data
data = data.frame(predictor_x = rnorm(100, mean = 5, sd = 1),
                    response_y = as.factor(rbinom(100, size = 2, prob = 0.5)))
# Make a model
library(MASS)
model = polr(response_y ~ predictor_x, data = data, Hess= TRUE)
# Plot
library(effects)
p1 = plot(Effects(focal.predictor = "predictor_x", model))
# View
p1
# Try to add a red dashed line for expected probabilities
p1 + abline(h = c(0.25, 0.5, 0.25), col = "red", lty = 3) #failed

Given the plots, I'd like to add a horizontal line at y =.25, for the upper plot, y =.5, for the middle plot, and y =.25, for the lower plot.考虑到这些图,我想在 y =.25 处添加一条水平线,用于上部 plot,y =.5,用于中间 plot,y =.25,用于下部 plot。

Also, I know the + is usually ggplot format so I'm also not sure if that will work either or if you can/how to add more information (like a line) to a saved plot object?另外,我知道 + 通常是 ggplot 格式,所以我也不确定这是否有效,或者您是否可以/如何向已保存的 plot object 添加更多信息(如一行)?

The plot method for Effect objects uses the lattice plotting system, which is neither base R nor ggplot based. Effect对象的plot方法使用lattice绘图系统,它既不是基于 R 也不是基于 ggplot。 However, the ggeffect package can effectively produce the same plot using the ggplot framework, which is a bit easier to work with.然而, ggeffect package 可以使用 ggplot 框架有效地生成相同的 plot,这更容易使用。 In your case, we can do:对于您的情况,我们可以这样做:

library(MASS)
library(effects)
library(ggeffects)

data <- data.frame(predictor_x = rnorm(100, mean = 5, sd = 1),
                  response_y = as.factor(rbinom(100, size = 2, prob = 0.5)))

model <- polr(response_y ~ predictor_x, data = data, Hess= TRUE)

p1 <- plot(ggeffect(model))$predictor_x

p1$facet$params$nrow <- 3

p1 + 
  geom_hline(data = data.frame(response.level = c("X0", "X1", "X2"),
                               y = c(0.25, 0.5, 0.25)),
             aes(yintercept = y), color = "red3", linetype = 2) +
  geom_rug(data = within(data, response.level <- paste0("X", response_y)),
                         aes(x = predictor_x, y = 0.5), sides = "b")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM