遍历purrr中的公式 [英] iterating over formulas in purrr

查看:79
本文介绍了遍历purrr中的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一堆使用一堆公式(例如字符串),最好一次使用tidyverse函数.这就是我现在的位置.

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 

我想使用列表中的所有可能公式运行glm.我尝试这样做,如下.

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.

推荐答案

问题是您使用的是data.frame(),默认情况下(stringAsFactors=TRUE)将data.frame()转换为公式向量.

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

data.frame更改为data_frame对我有用. (data_frame来自tibble包,也通过dplyr导出,因此应该在library("tidyverse")之后可用)

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

这篇关于遍历purrr中的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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