有沒有辦法通過包中的map()函式進行 Levene 測驗purrr?或者是否有另一種簡單的方法來計算各種變數的 Levene 檢驗?
我的資料框包含各種因子和數字列,所以我嘗試使用map_if(),它作業正常,例如,用于 Shapiro 測驗。但是,我不知道如何指定公式。我想針對“治療”因素測驗我所有的數字變數。
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))
有什么建議?謝謝你的幫助!
現在還有一個可重復的例子;)
uj5u.com熱心網友回復:
這是一個替代方案:首先轉向長資料,
然后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
uj5u.com熱心網友回復:
您還可以更直接地使用匯總。然后旋轉并取消嵌套結果。
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)
另一種選擇是將變數名稱輸入地圖并創建公式。
library(purrr)
names(data)[map_lgl(data, is.numeric)] %>%
set_names() %>%
map_dfr(~ levene_test(data, as.formula(paste(.x, "~ Treatment"))), .id = "flux")
結果(對于兩者):
# 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
uj5u.com熱心網友回復:
問題是map在列上回圈,它不再是 data.frame 而levene_test期望data.frame/tibble. 根據?levene_test
資料 - 用于評估公式或模型的資料框
因此,不是map_if直接使用select數字 ( select(where(is.numeric)))列,而是獲取列名 ( names),回圈遍歷 中的那些map,select只有資料中的“處理”和回圈列,創建公式reformulate并應用levene_test
library(rstatix)
library(dplyr)
library(purrr)
data %>%
select(where(is.numeric)) %>%
names %>%
map_dfr(~ data %>%
select(Treatment, all_of(.x)) %>%
{levene_test(reformulate("Treatment", response = names(.)[2]), data = .)
})
-輸出
# 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
它也可以用做across雖然-即環across是列numeric在summarise中,使用data如cur_data(),創建公式reformulate,應用levene_test,回傳輸出的list,unclass與使用bind_rows(因為unclass會從洗掉data.frame屬性list)
data %>%
summarise(across(where(is.numeric),
~ list(cur_data() %>%
levene_test(reformulate("Treatment", response = cur_column()))))) %>%
unclass %>%
unname %>%
bind_rows
# 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
如果我們需要“flux”列識別符號,請使用該summarise步驟而不將輸出包裝在 a 中list,然后使用bind_rowswith.id
data %>%
summarise(across(where(is.numeric),
~ cur_data() %>%
levene_test(reformulate("Treatment", response = cur_column())))) %>%
unclass %>%
bind_rows(.id = 'flux')
# A tibble: 3 × 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
或者另一種選擇是 OPmap_if本身
map_if(data, is.numeric,
~ levene_test(. ~ Treatment,
data = tibble(.x, Treatment = data$Treatment) ), .else = ~ NULL) %>%
bind_rows(.id = 'flux')
# A tibble: 3 × 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/390427.html
上一篇:如何迭代不同長度的串列?
