【问题标题】:Mapping a model to a permuted data set sometimes returns the model equation, rather than model output将模型映射到置换数据集有时会返回模型方程,而不是模型输出
【发布时间】:2019-05-15 21:24:16
【问题描述】:

我正在尝试使用带有purrr map的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 的单行数据帧,我该如何让它做到这一点?

谢谢。

【问题讨论】:

    标签: r purrr modelr


    【解决方案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
    .....
    
    

    【讨论】:

      【解决方案2】:

      置换返回类型&lt;S3: permutation&gt;不是数据框。

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

      所以要做你想做的事,只需在mean_of_vs() 函数的第一步中访问data 元素:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 2010-12-17
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多