[英]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
.我正在引导众所周知的
mtcars
数据集并使用glm
和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我已经尝试将
glm
应用于单个引导数据并且它工作正常,但是当我应用map
函数时它失败了
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 适合单个 Bootstrap 数据框时,一切都很好
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.我觉得它与
purrr::map
功能有关,但我不确定。
Any help is appreciated.任何帮助表示赞赏。
The issue I think is that the tibble is still grouped by row.我认为的问题是 tibble 仍然按行分组。 If you add
ungroup()
as the last step of your chain then the code will work.如果您将
ungroup()
添加为链的最后一步,则代码将起作用。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.