问题描述
限时送ChatGPT账号..我使用嵌套的 tidyr 数据框创建了一些这样的模型:
I create some models like this using a nested tidyr dataframe:
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
像这样创建一些快速边际效应图
I would like to use this data to create some quick marginal effects plots with sjPlot::plot_models
like this
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
可接受的格式.
I've played around a bit with the nesting of the data but I've been unable to get it into a format that sjPlot::plot_models
will accept.
我期望得到的是多元回归模型的森林图";如帮助文件中所述.最终,目标是按组绘制回归模型的边际效应,我希望 plot_models 可以做到(如果我错了,请纠正我).
What I was expecting to get is a "Forest plot of multiple regression models" as described in the help file. Ultimately, the goal is to plot the marginal effects of regression models by group, which I was hoping the plot_models will do (please correct me if I'm wrong).
推荐答案
认为原代码和数据都有问题.plot_models
不支持函数调用中的 plot_model
参数.我首先展示了一个示例,该示例展示了如何使用 {ggplot2} 的 diamonds
数据集调用 plot_models
并将其与嵌套的 tibble
一起使用.然后我将这种方法应用于 OP 的样本数据,这不会产生可用的结果*.最后,我创建了一些新的玩具数据来展示如何将该方法应用于二项式模型.
It think there are some issues with the original code as well as with the data. There are arguments from plot_model
in the function call which are not supported in plot_models
. I first show an example that shows how plot_models
can be called and used with a nested tibble
using {ggplot2}'s diamonds
data set. Then I apply this approach to the OP's sample data, which doesn't yield useable results*. Finally, I create some new toy data to show how the approach could be applied to a binominal model.
(* 在原始玩具数据中,因变量在每个模型中总是 0
或总是 1
,因此这不太可能产生可用的结果.
(* In the original toy data the dependent variable is either always 0
or always 1
in each model so this is unlikely to yield useable results).
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 包 (v0.3.0) 于 2021 年 1 月 23 日创建上>
Created on 2021-01-23 by the reprex package (v0.3.0)
这篇关于如何从嵌套的小标题绘制 sjPlots?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论