R语言 使用plot_model()显示包含选定项的多个图

s3fp2yjn  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(313)

我目前遵循此指南:https://mran.microsoft.com/snapshot/2020-03-10/web/packages/sjPlot/vignettes/plot_model_estimates.html
我为指南复制并粘贴了以下内容:

library(sjPlot)
library(sjlabelled)
library(sjmisc)
library(ggplot2)

data(efc)
theme_set(theme_sjplot())

# create binary response
y <- ifelse(efc$neg_c_7 < median(na.omit(efc$neg_c_7)), 0, 1)

# create data frame for fitting model
df <- data.frame(
  y = to_factor(y),
  sex = to_factor(efc$c161sex),
  dep = to_factor(efc$e42dep),
  barthel = efc$barthtot,
  education = to_factor(efc$c172code)
)

# set variable label for response
set_label(df$y) <- "High Negative Impact"

# fit model
m1 <- glm(y ~., data = df, family = binomial(link = "logit"))

我添加了两个模型:

# added
m2 <- glm(y ~ sex + dep, data = df, family = binomial(link = "logit"))

m3 <- glm(y ~ sex + dep + barthel, data = df, family = binomial(link = "logit"))

我希望图只显示某些系数:

plot_models(m1, m2, m3,
            terms = c("sex", "dep"))

我得到这个错误:

Error in (show.zeroinf && minfo$is_zero_inflated) || minfo$is_dispersion : 
  invalid 'y' type in 'x || y'
In addition: Warning message:
Could not access model information.

我想把不同的情节分面:

plot_models(m1, m2, m3,
            facet_grid = TRUE)

我得到了这个错误:

Error in (show.zeroinf && minfo$is_zero_inflated) || minfo$is_dispersion : 
  invalid 'y' type in 'x || y'
In addition: Warning message:
Could not access model information.

有谁能给我提供一些关于这里可能发生的事情的见解吗?谢谢!

6pp0gazn

6pp0gazn1#

你可能把plot_modelplot_models函数和sjPlot弄混了。当你使用未知参数时,它会返回错误。所以你必须确保你使用了正确的参数。例如,facet_grid参数不存在,你可以通过?plot_models来检查。下面是一些可复制的代码:

library(sjPlot)
library(sjlabelled)
library(sjmisc)
library(ggplot2)

all.models <- list()
all.models[[1]] <- m1
all.models[[2]] <- m2
all.models[[3]] <- m3

plot_models(all.models,
            rm.terms = c("barthel", "education"))

创建于2023年3月14日,使用reprex v2.0.2

相关问题