如何使用purrr中的map和dplyr中的mutate生成glm摘要表? [英] How to use map from purrr and mutate from dplyr to produce a glm summary table?

查看:136
本文介绍了如何使用purrr中的map和dplyr中的mutate生成glm摘要表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用purrr和broom软件包生成一系列的glm,并构建一个包含模型信息的表,以便可以对其进行比较。

I am using the packages purrr and broom to produce a series of glm's and build a table with information of the models so I can compare them.

代码是从purrr调用map函数时失败。我认为问题与mutate和map的组合有关。我想为每个glm生成一个表格,并为该glm的组件创建一列。

The code is failing when I call map function from purrr. I think the problem relates to the combination of mutate and map. I want to generate a table with a row for each glm and columns for the glm's components.

DATA&代码

library(broom)
library(tidyverse)

# Produce a dummy dataset
set.seed(123)
dummy <- tibble(ID = 1:50,
                A = sample(x = 1:200, size = 50, replace = T),
                B = as.factor(sample(x = c("day", "night"), size = 50, replace = T)),
                C = as.factor(sample(x = c("blue", "red", "green"), size = 50, replace = T)))

# Nest the data
nested <- dummy %>% select(-ID) %>% nest()

# Define a function for a generalized linear model with a poisson family
mod_f <- function(x, df = nested) {glm(formula = as.formula(x), family = poisson, data = df)}

# Make a list of formulas as a column in a new dataframe
# A is our response variable that we try to predict using B and C
formulas <- c("A ~ 1", "A ~ B", "A ~ C", "A ~ B + C")
tbl <- tibble(forms = formulas)

# Fit the glm's using each of the formulas from the formulas vector
tbl_2 <- tbl %>% mutate(mods = map(formulas, mod_f))
        #gla = mods %>% map(glance),
        #tid = mods %>% map(tidy),
        #aug = mods %>% map(augment),
        #AIC = gla %>% map_dbl("AIC"))

错误


mutate_impl(.data,点)中的错误:评估错误:未找到对象'A'

Error in mutate_impl(.data, dots): Evaluation error: object 'A' not found


推荐答案

另一个Stackoverflow用户提供的最终答案:

Final answer as provided by another Stackoverflow's user:

library(broom)
library(tidyverse)

# Produce a dummy dataset
set.seed(123)
dummy <- tibble(ID = 1:50,
                A = sample(x = 1:200, size = 50, replace = T),
                B = as.factor(sample(x = c("day", "night"), size = 50, replace = T)),
                C = as.factor(sample(x = c("blue", "red", "green"), size = 50, replace = T)))

# Define a function for a generalized linear model with a poisson family
mod_f <- function(x) {glm(formula = as.formula(x), family = poisson, data = dummy)}

# Make a list of formulas as a column in a new dataframe
# A is yhe response variable we try to predict using B and C
formulas <- c("A ~ 1", "A ~ B", "A ~ C", "A ~ B + C")
tbl <- tibble(forms = formulas)

# Fit the glm using each of the formulas stored in the formulas vector
tbl_2 <- tbl %>% mutate(all = map(formulas, mod_f),
                        gla = all %>% map(glance),
                        tid = all %>% map(tidy),
                        aug = all %>% map(augment),
                        AIC = all%>% map_dbl("AIC"))

这篇关于如何使用purrr中的map和dplyr中的mutate生成glm摘要表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆