将模型映射到排列的数据集有时会返回模型方程,而不是模型输出

时间:2019-05-15 21:24:16

标签: r purrr modelr

我正在尝试使用具有purrr映射的Modelr中的permute函数来计算置换下两类数据的平均值。

如果我要根据排列的数据集计算线性模型,则该函数的行为将与预期的一样,根据modelr :: permute的示例文件(尽管我正在自定义函数中运行线性模型):

library(tidyverse) 
library(modelr)

perms <- permute(mtcars,  1000, mpg)
jlm <- function(df){lm(mpg ~ wt, data = df)}
models3 <- map(perms$perm, jlm)
models3[[1]]
Call:
lm(formula = mpg ~ wt, data = df)

Coefficients:
(Intercept)           wt  
     28.211       -2.524

现在,我不是线性模型,而是想要该数据集中两个类别的平均值。我尝试运行如下。

mean_of_vs <- function(df){
  df %>% group_by(vs) %>% summarize(mean(mpg)) %>% spread(vs, `mean(mpg)`) %>%
    rename(zero = `0`, one = `1`)
}

models4 <- map(perms$perm, ~mean_of_vs)

models4[[1]]

但这只是返回函数方程,而不是函数的输出

function(df){
  df %>% group_by(vs) %>% summarize(mean(mpg)) %>% spread(vs, `mean(mpg)`) %>%
    rename(zero = `0`, one = `1`)
}

等式本身在一个简单的数据帧上起作用。

test <- perms %>% pull(perm) %>% .[[1]] %>% as.data.frame

mean_of_vs(test)
# A tibble: 1 x 2
   zero   one
  <dbl> <dbl>
1  16.6  24.5

所以我的问题是,为什么我的自定义函数不返回一堆平均值为vs = 0和vs = 1的单行数据帧,我将如何获得它呢?

谢谢。

2 个答案:

答案 0 :(得分:1)

置换返回类型<S3: permutation>不是一个数据帧。

> perms
# A tibble: 1,000 x 2
   perm              .id
   <list>            <chr>
 1 <S3: permutation> 0001
 2 <S3: permutation> 0002
 3 <S3: permutation> 0003
 4 <S3: permutation> 0004
 5 <S3: permutation> 0005
 6 <S3: permutation> 0006
 7 <S3: permutation> 0007
 8 <S3: permutation> 0008
 9 <S3: permutation> 0009
10 <S3: permutation> 0010
# ... with 990 more rows

检查它会发现数据帧存储为命名列表中的第一个元素:

> glimpse(perms[[1,1]])
List of 3
 $ data   :'data.frame':    32 obs. of  11 variables:
  ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
  ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
  ..$ disp: num [1:32] 160 160 108 258 360 ...
  ..$ hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
  ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
  ..$ wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
  ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
  ..$ vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
  ..$ am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
  ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
  ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
 $ columns: Named chr "mpg"
  ..- attr(*, "names")= chr "mpg"
 $ idx    : int [1:32] 1 30 21 12 27 14 17 2 15 32 ...
 - attr(*, "class")= chr "permutation"

要执行所需的操作,只需在data函数的第一步中访问mean_of_vs()元素:

mean_of_vs <- function(df) {
  df$data %>% 
    group_by(vs) %>% 
    summarize(mean(mpg)) %>% 
    spread(vs, `mean(mpg)`) %>%
    rename(zero = `0`, one = `1`)
}

现在一切正常:

> models4 <- map(perms$perm, mean_of_vs)
> models4[[1]]
# A tibble: 1 x 2
   zero   one
  <dbl> <dbl>
1  16.6  24.6

答案 1 :(得分:1)

很高兴见到你。

modelr::permute产生其类别为“置换”的数据

> class(perms[[1]][1][[1]])

[1] "permutation"

permutation类具有3个属性

数据

此变量中的数据

您要置换的列

idx

指示已选择哪些行的索引

我认为permutation仅采用某些公式(例如lmetc。我不确定公式列表)。

因此,如果要使用功能,则必须转换为data.frame / data.table / tibble,如下所示

mean_of_vs <- function(df){
   df %>%as.data.frame() %>% group_by(vs) %>% summarize(mean(mpg)) %>% spread(vs, `mean(mpg)`) %>%
     rename(zero = `0`, one = `1`)
}

然后,以map表示法执行~函数。

models4 <- map(perms$perm, mean_of_vs)

然后您将得到结果


.....

[[97]]

# A tibble: 1 x 2
   zero   one

  <dbl> <dbl>
1  21.4  18.4




[[98]]

# A tibble: 1 x 2
   zero   one

  <dbl> <dbl>
1  20.4  19.7
.....