【问题标题】:How to plot sjPlots from a nested tibble?如何从嵌套的小标题中绘制 sjPlots?
【发布时间】:2021-01-18 22:35:44
【问题描述】:

我使用嵌套的 tidyr 数据框创建了一些这样的模型:

set.seed(1)
library(tidyr)
library(dplyr)
library(sjPlot)
library(tibble)
library(purrr)

fits <- tribble(~group, ~colA, ~colB, ~colC,
        sample(c("group1", "group2"), 10, replace = T), 0, sample(10, replace = T), sample(10, replace = T),
        sample(c("group1", "group2"), 10, replace = T), 1, sample(10, replace = T), sample(10, replace = T)) %>% 
    unnest(cols = c(colB, colC)) %>%
    nest(data=-group) %>%
    mutate(fit= map(data, ~glm(formula = colA ~ colB + colC, data = .x, family="binomial"))) %>%
    dplyr::select(group, fit) %>%
    tibble::column_to_rownames("group")

我想使用这些数据创建一些带有sjPlot::plot_models 的快速边际效应图,就像这样

plot_models(as.list(fits), type = "pred", terms = c("colB", "colA", "colC"))

不幸的是,我得到了错误

Error in if (fam.info$is_linear) tf <- NULL else tf <- "exp" : 
  argument is of length zero
In addition: Warning message:
Could not access model information. 

我对数据的嵌套进行了一些尝试,但我无法将其转换为sjPlot::plot_models 可以接受的格式。

我期望得到的是帮助文件中描述的“多个回归模型的森林图”。最终,目标是按组绘制回归模型的边际效应,我希望 plot_models 能做到这一点(如果我错了,请纠正我)。

【问题讨论】:

  • 您生成fits 的示例代码给出“错误:在.data 中找不到列rowname”?
  • 现在应该可以了,忘记指定组了。
  • @TeaTree 请尝试编辑您的问题,甚至回答您的问题,因为我想我得到了类似的东西,我很想知道答案。赞成。谢谢
  • 我在代码中添加了 purrr 包,这是我之前忘记的。我不确定你的意思@MohamedRahouma。我不知道我的问题的答案,但 MWE 现在应该可以工作了。
  • 想我可能会用unnest(cols = group) 代替column_to_rownames(group) - 后者使用列表元素作为行名,而先前的未列出向量。还从后续的mutate(models = map(fit, plot_models)) 中得到一些东西——我在文档中看不到你的其他论点。那是在做某事,但绝对不是它应该做的。

标签: r tidyr sjplot


【解决方案1】:

它认为原始代码和数据存在一些问题。函数调用中有来自plot_model 的参数,plot_models 不支持这些参数。我首先展示了一个示例,说明如何使用 {ggplot2} 的 diamonds 数据集调用和使用嵌套的 tibble 并使用 plot_models。然后我将这种方法应用于 OP 的样本数据,它不会产生可用的结果*。最后,我创建了一些新的玩具数据来展示如何将该方法应用于二项式模型。

(* 在原始玩具数据中,因变量在每个模型中始终为 0 或始终为 1,因此这不太可能产生可用结果。

set.seed(1)
library(tidyr)
library(dplyr)
library(sjPlot)
library(tibble)
library(ggplot2)

# general example
fits <- tibble(id = c("x", "y", "z")) %>%
  rowwise() %>% 
  mutate(fit = list(glm(reformulate(
    termlabels = c("cut", "color", "depth", "table", "price", id),
    response = "carat"),
    data = diamonds)))

plot_models(fits$fit)

# OP's example data
fits2 <- tribble(~group, ~colA, ~colB, ~colC,
                 sample(c("group1", "group2"), 10, replace = T), 0,
                 sample(10, replace = T), sample(10, replace = T),
                 sample(c("group1", "group2"), 10, replace = T), 1,
                 sample(10, replace = T),
                 sample(10, replace = T)) %>% 
  unnest(cols = c(colB, colC)) %>%
  nest(data = -group) %>%
  rowwise() %>% 
  mutate(fit = list(glm(formula = colA ~ colB + colC, data = data, family="binomial")))

plot_models(fits2$fit)
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Warning: Removed 4 rows containing missing values (geom_point).

# new data for binominal model
n <- 500
g <- round(runif(n, 0L, 1L), 0)
x1 <- runif(n,0,100)
x2 <- runif(n,0,100)
y <- (x2 - x1 + rnorm(n,sd=20)) < 0

fits3 <- tibble(g, y, x1, x2) %>% 
  nest_by(g) %>% 
  mutate(fit = list(glm(formula = y ~ x1 + x2, data = data, family="binomial")))

plot_models(fits3$fit)

reprex package (v0.3.0) 于 2021-01-23 创建

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 2019-08-12
    • 2018-01-20
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多