How to fix "Error in eval(predvars, data, env) : numeric 'envir' arg not of length one" in purrr

I'm bootstrapping well known mtcars dataset and applying logistic regression using glm and purrr::map. Howevever I'm getting

Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

error

I've already tried to apply glm to a single bootstrapped data and it works fine, but when I apply map function it fails

library(tidyverse)
library(rsample)
library(broom)

sample10 <- 
    bootstraps(mtcars, times=10) %>%  
    rowwise() %>% 
    mutate(data_sample=list(analysis(splits))) %>%
    select(id, data_sample)

sample10

Source: local data frame [10 x 2]
Groups: <by row>

# A tibble: 10 x 2
   id          data_sample        
   <chr>       <list>             
 1 Bootstrap01 <df[,11] [32 × 11]>
 2 Bootstrap02 <df[,11] [32 × 11]>
 3 Bootstrap03 <df[,11] [32 × 11]>
 4 Bootstrap04 <df[,11] [32 × 11]>
 5 Bootstrap05 <df[,11] [32 × 11]>
 6 Bootstrap06 <df[,11] [32 × 11]>
 7 Bootstrap07 <df[,11] [32 × 11]>
 8 Bootstrap08 <df[,11] [32 × 11]>
 9 Bootstrap09 <df[,11] [32 × 11]>
10 Bootstrap10 <df[,11] [32 × 11]>

when I try to fit a model for each bootstrap dataframe :

sample10 %>% 
    mutate(model_fit = map(data_sample, 
                           ~ glm(formula= vs ~ wt + disp,                    
                                 data=., 
                                 family=binomial)))

Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

However when I try to fit a glm to single Bootstrap data frame it is all good

glm(formula= vs ~ wt + disp, data=sample10$data_sample[[1]], family=binomial)


Call:  glm(formula = vs ~ wt + disp, family = binomial, data = sample10$data_sample[[1]])

Coefficients:
(Intercept)           wt         disp  
    5.54313     -1.19918     -0.01472  

Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
Null Deviance:      41.18 
Residual Deviance: 16.97    AIC: 22.97

The error message is not helpful, and I'm not sure what I'm doing wrong. I feel like it's something with purrr::map function but I'm not sure.

Any help is appreciated.

Upvotes: 0

Views: 1076

Answers (1)

lroha
lroha

Reputation: 34441

The issue I think is that the tibble is still grouped by row. If you add ungroup() as the last step of your chain then the code will work.

sample10 <- bootstraps(mtcars, times=10) %>%  
  rowwise() %>% 
  mutate(data_sample=(list(analysis(splits)))) %>%
  select(id, data_sample) %>%
  ungroup()

sample10 %>% 
  mutate(model_fit = map(data_sample, 
                         ~ glm(formula= vs ~ wt + disp,                    
                               data=., 
                               family=binomial)))


# A tibble: 10 x 3
   id          data_sample            model_fit
   <chr>       <list>                 <list>   
 1 Bootstrap01 <data.frame [32 x 11]> <S3: glm>
 2 Bootstrap02 <data.frame [32 x 11]> <S3: glm>
 3 Bootstrap03 <data.frame [32 x 11]> <S3: glm>
 4 Bootstrap04 <data.frame [32 x 11]> <S3: glm>
 5 Bootstrap05 <data.frame [32 x 11]> <S3: glm>
 6 Bootstrap06 <data.frame [32 x 11]> <S3: glm>
 7 Bootstrap07 <data.frame [32 x 11]> <S3: glm>
 8 Bootstrap08 <data.frame [32 x 11]> <S3: glm>
 9 Bootstrap09 <data.frame [32 x 11]> <S3: glm>
10 Bootstrap10 <data.frame [32 x 11]> <S3: glm>

Upvotes: 1

Related Questions