我正在使用purrr和broom软件包来生成一系列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”
mods
称为mos
,glan
称为gla
。干杯! - NelsonGon