简体   繁体   English

在 purrr 中使用 map() function 中的 levene_test?

[英]use levene_test in map() function from purrr?

Is there a way to do a Levene Test via the map() function from the purrr package?有没有办法通过purrr package 中的map() function 进行 Levene 测试? Or is there another simple way to compute a Levene Test over various variables?还是有另一种简单的方法来计算各种变量的 Levene 测试?

My data frame contains various factor and numeric columns, so I tried with map_if() , which works fine, eg, for Shapiro tests.我的数据框包含各种因子和数字列,所以我尝试使用map_if() ,它可以正常工作,例如用于夏皮罗测试。 However, I do not know how to specify the formula.但是,我不知道如何指定公式。 I want to test all my numeric variables against the "Treatment" factor.我想根据“治疗”因素测试我所有的数字变量。

library("tidyverse")
library("rstatix")

data <- data.frame(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), 
                                    .Label = c("S1 ", "S2 ", "S3 "), class = "factor"), 
                   plot = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L), 
                                    .Label = c(" Tree 1 ", " Tree 2 ", " Tree 3 "), class = "factor"), 
                   Treatment = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("T1", "T2"), class = "factor"), 
                   flux1 = c(11.52188065, 8.43156699, 4.495312274, -1.866676811, 3.861102035, -0.814742373, 6.51039536, 4.767950345, 10.36544542, 1.065963875), 
                   flux2 = c(0.142259208, 0.04060245, 0.807631744, 0.060127596, -0.157762562, 0.062464942, 0.043147603, 0.495001652, 0.34363348, 0.134183704), 
                   flux3 = c(0.147506197, 1.131009714, 0.038860728, 0.0176834, 0.053191593, 0.047591306, 0.00573377, -0.034926075, 0.123379247, 0.018882469))

map_if(data, is.numeric, levene_test(. ~ Treatment))

Any suggestions?有什么建议么? Thanks for your help!谢谢你的帮助!

Now also with an reproducible example;)现在还有一个可重现的例子;)

Here is an alternative: First pivot to long data,这是一个替代方案:首先 pivot 到长数据,

Then group_by and apply the formula (here flux should be factor!)然后group_by并应用公式(这里通量应该是因素!)

library(tidyr)
library(dplyr)

data %>% 
  pivot_longer(
    cols = starts_with("flux"),
    names_to = "flux",
    values_to = "value"
  ) %>%
  mutate(flux = as.factor(flux)) %>% 
  group_by(flux) %>% 
  levene_test(value ~ Treatment)
  flux    df1   df2 statistic     p
  <fct> <int> <int>     <dbl> <dbl>
1 flux1     1     8     0.410 0.540
2 flux2     1     8     2.85  0.130
3 flux3     1     8     1.11  0.323

The issue is that map loops over the columns and it is no longer a data.frame whereas levene_test expects a data.frame/tibble问题是map在列上循环,它不再是 data.frame 而levene_test需要data.frame/tibble

data %>%  select(where(is.numeric)) %>% names %>% map_dfr(~ data %>% select(Treatment, all_of(.x)) %>% {levene_test(reformulate("Treatment", response = names(.)[2]), data = .)})

-output -输出

# A tibble: 3 × 4
    df1   df2 statistic     p
  <int> <int>     <dbl> <dbl>
1     1     8     0.410 0.540
2     1     8     2.85  0.130
3     1     8     1.11  0.323

You can also use summarize a bit more directly.您也可以更直接地使用汇总。 Then pivot and unnest the results.然后 pivot 并取消嵌套结果。

library(dplyr)
library(tidyr)

data %>% 
  summarize(across(where(is.numeric),
                   ~ list(levene_test(cur_data(), . ~ Treatment)))) %>% 
  pivot_longer(everything(), names_to = "flux", values_to = "levene_test") %>% 
  unnest(levene_test)

Result:结果:

# A tibble: 3 x 5
  flux    df1   df2 statistic     p
  <chr> <int> <int>     <dbl> <dbl>
1 flux1     1     8     0.410 0.540
2 flux2     1     8     2.85  0.130
3 flux3     1     8     1.11  0.323

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM