繁体   English   中英

如何使用purrr中的map和dplyr中的mutate来生成glm汇总表?

[英]How to use map from purrr and mutate from dplyr to produce a glm summary table?

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

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

数据和代码

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,dots)出错:评估错误:找不到对象'A'

你在函数中犯了一个错误:你调用了df而不是dummy. 不确定你是否可以重构来概括它。 这里:

   mod_f <- function(x, df = nested) {glm(formula = as.formula(x), family = poisson, data = dummy)}

# 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))

这会产生:

forms     mods     
  <chr>     <list>   
1 A ~ 1     <S3: glm>
2 A ~ B     <S3: glm>
3 A ~ C     <S3: glm>
4 A ~ B + C <S3: glm>
    `Map(mod_f,formulas)` 

产量等等:

$`A ~ 1`

Call:  glm(formula = as.formula(x), family = poisson, data = dummy)

Coefficients:
(Intercept)  
      4.649  

Degrees of Freedom: 49 Total (i.e. Null);  49 Residual
Null Deviance:      1840 
Residual Deviance: 1840     AIC: 2154

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

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"))

暂无
暂无

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

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