iterating over formulas in purrr

妖精的绣舞 提交于 2020-01-03 05:06:46

问题


I have a bunch of formulas, as strings, that I'd like to use, one at a time in a glm, preferably using tidyverse functions. Here's where I am at now.

library(tidyverse)
library(broom)

mtcars %>% dplyr::select(mpg:qsec) %>% colnames -> targcols
paste('vs ~ ', targcols) -> formulas
formulas

#> 'vs ~  mpg' 'vs ~  cyl' 'vs ~  disp' 'vs ~  hp' 'vs ~  drat' 'vs ~  wt' 'vs ~  qsec' 

I can run a general linear model with any one of these formulas as

glm(as.formula(formulas[1]), family = 'binomial', data = mtcars) %>% glance

#>  null.deviance,  df.null,    logLik, AIC,    BIC,    deviance,   df.residual
#> 43.86011,    31,     -12.76667,  29.53334,   32.46481,   25.53334,   30 

I'd like to run the glm with every possible formula in the list. I tried doing that as follows.

data.frame(formulas = formulas) %>%
    mutate(mod = map(formulas, function(fs){
        glm(as.formula(fs), family = 'binomial', data = mtcars)
    }))

But then I get the following error message:

Error in mutate_impl(.data, dots): Evaluation error: invalid formula. Traceback:

1. data.frame(formulas = formulas) %>% mutate(mod = map(formulas,   .     function(fs) {  .         glm(as.formula(fs), family =
       "binomial", data = mtcars)  .     }))
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. eval(quote(`_fseq`(`_lhs`)), env, env)
4. eval(quote(`_fseq`(`_lhs`)), env, env)
5. `_fseq`(`_lhs`)
6. freduce(value, `_function_list`)
7. withVisible(function_list[[k]](value))
8. function_list[[k]](value)
9. mutate(., mod = map(formulas, function(fs) {  .     glm(as.formula(fs), family = "binomial", data = mtcars)  . }))
10. mutate.data.frame(., mod = map(formulas, function(fs) {   .     glm(as.formula(fs), family = "binomial", data = mtcars)   . }))
11. as.data.frame(mutate(tbl_df(.data), ...))
12. mutate(tbl_df(.data), ...)
13. mutate.tbl_df(tbl_df(.data), ...)
14. mutate_impl(.data, dots)

Could somebody tell me what I am missing here? Thanks for any advice.


回答1:


The problem is that you're using data.frame(), which by default (stringAsFactors=TRUE) converts your formula vector to a factor.

Changing data.frame to data_frame works for me. (data_frame is from the tibble package, also exported via dplyr, so it should be available after library("tidyverse"))

You can shorten your code a little bit:

data_frame(formulas) %>%
    mutate(mod = map(formulas, 
                      ~  glm(as.formula(.),
                             family = 'binomial', data = mtcars)))


来源:https://stackoverflow.com/questions/48450308/iterating-over-formulas-in-purrr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!