【问题标题】:Apply test (Jarque-Bera) to subgroups with map()使用 map() 将测试 (Jarque-Bera) 应用于子组
【发布时间】:2018-06-01 15:00:17
【问题描述】:

我正在尝试对我的数据集的子组运行测试 (Jarque-Bera)。我正在尝试为此使用purrrs 函数map(),但不知何故它对我来说失败了。

对于这里的示例,我将使用内置的ChickWeightdataset:

加载一些包:

library(dplyr)
library(ggplot2)
library(tidyr)
library(purrr)
library(tseries)

数据如下:

ggplot(ChickWeight, aes(weight, fill = Diet)) +
  geom_histogram() +
  facet_wrap(~Diet)

我现在尝试对饮食 1、2、3 和 4 亚组使用 Jarque-Bera 检验。

我知道如何在这样的组中使用它:

ChickWeight %>% 
  filter(Diet == 1) %>% 
  pull(weight) %>% 
  jarque.bera.test()

返回:

    Jarque Bera Test

data:  .
X-squared = 46.687, df = 2, p-value = 7.278e-11

但是现在,我想为所有子组执行此操作。所以我像这样嵌套数据:

nst <- ChickWeight %>% 
  nest(-Diet)

现在我想我可以立即应用 purrrs map() 函数,但有些失败:

tsts <- nst %>% 
  map(jb = map(data, jarque.bera.test(weight)))

返回:

as_mapper(.f, ...) 中的错误:缺少参数“.f”,没有默认值

我也试过了:

tsts <- nst %>% 
  mutate(jb = map(data, jarque.bera.test(weight)))

这就产生了:

mutate_impl(.data, dots) 中的错误: 评估错误:找不到对象“重量”。

【问题讨论】:

    标签: r dplyr tidyr purrr


    【解决方案1】:

    您的对象nst 是一个数据框;在引擎盖下,数据框是列的列表。因此,如果您将map 应用于nst,它将映射到列而不是映射到数据中的组。另外,请注意map 的第二个参数必须是一个函数(或一些其他类型的函数简写表达式——请参阅purrr::map 的文档)。

    有很多方法可以解决您的问题。我最喜欢使用 dplyr 中的 group_bydo 以及 broom 中的 tidy

    library(dplyr)
    library(tidyr)
    library(purrr)
    library(tseries)
    library(broom)
    
    ChickWeight %>%
        group_by(Diet) %>%
        do(tidy(jarque.bera.test(.$weight)))
    # # A tibble: 4 x 5
    # # Groups:   Diet [4]
    #   Diet  statistic  p.value parameter method          
    #   <fct>     <dbl>    <dbl>     <dbl> <fct>           
    # 1 1         46.7  7.28e-11         2 Jarque Bera Test
    # 2 2         18.8  8.36e- 5         2 Jarque Bera Test
    # 3 3         12.4  2.00e- 3         2 Jarque Bera Test
    # 4 4          5.17 7.54e- 2         2 Jarque Bera Test
    

    请注意,tidyjarque.bera.test 的输出转换为单行数据帧,do 函数对每个组重复此操作。这是一个纯粹的 dplyr 方法:

    ChickWeight %>%
        group_by(Diet) %>%
        summarize(p.value = jarque.bera.test(weight)$p.value)
    # # A tibble: 4 x 2
    #   Diet   p.value
    #   <fct>    <dbl>
    # 1 1     7.28e-11
    # 2 2     8.36e- 5
    # 3 3     2.00e- 3
    # 4 4     7.54e- 2
    

    接下来是一种使用nestmap 的方法。请注意,nest 创建的数据框的第二列是列表列。我们使用pull 仅拉取列表列,然后使用map 对每个组执行 Jarque-Bera 测试。

    ChickWeight %>%
        nest(-Diet) %>%
        pull(data) %>%
        map(~ jarque.bera.test(.$weight))
    # [[1]]
    # 
    #   Jarque Bera Test
    # 
    # data:  .$weight
    # X-squared = 46.687, df = 2, p-value = 7.278e-11
    # 
    # 
    # [[2]]
    # 
    #   Jarque Bera Test
    # 
    # data:  .$weight
    # X-squared = 18.779, df = 2, p-value = 8.36e-05
    # 
    # 
    # [[3]]
    # 
    #   Jarque Bera Test
    # 
    # data:  .$weight
    # X-squared = 12.426, df = 2, p-value = 0.002003
    # 
    # 
    # [[4]]
    # 
    #   Jarque Bera Test
    # 
    # data:  .$weight
    # X-squared = 5.1686, df = 2, p-value = 0.07545
    

    请注意,map 接受公式作为编写anonymous function 的紧凑方式。最后,这里有一些结合mapdplyr 的稍微笨拙的方法:

    map(1:4, ~ filter(ChickWeight, Diet == .) %>% pull(weight) %>% jarque.bera.test)
    
    map(1:4, ~ filter(ChickWeight, Diet == .)) %>%
        map(~ pull(., weight)) %>%
        map(~ jarque.bera.test(.))
    

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 2021-01-21
      • 2023-01-11
      • 1970-01-01
      相关资源
      最近更新 更多