【发布时间】:2019-05-30 00:03:46
【问题描述】:
我正在引导众所周知的mtcars 数据集并使用glm 和purrr::map 应用逻辑回归。但是我得到了
Error in eval(predvars, data, env) :
numeric 'envir' arg not of length one
错误
我已经尝试将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]>
当我尝试为每个引导数据框拟合模型时:
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
但是,当我尝试将 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
错误消息没有帮助,我不确定自己做错了什么。我觉得它与purrr::map 功能有关,但我不确定。
感谢任何帮助。
【问题讨论】:
-
map应该可以工作map(sample10$data_sample, ~ glm(vs ~ wt + disp, data = .x, family = binomial))在上面,您正在尝试创建模型输出而不包含在列表中