我有一個資料框,其中包含同一測驗的幾個子量表的分數(列:參與者、會話、組、總分、每個子量表一列)。我正在嘗試對每個子量表的雙向混合方差分析進行假設檢查。為方便起見,我想為每個假設檢查撰寫一個回圈,這給了我所有子尺度的輸出。這對于檢查例外值、運行 Box 的 M 檢驗和生成實際的 ANOVA 輸出非常有效。但是,在使用 Levene 的測驗嘗試相同的事情時出現錯誤。請參閱下面的代碼和錯誤:
subscales <- c("awareness", "clarity", "impulse", "goals", "nonacceptance",
"strategies") # these correspond to the column names in the df
for (scale in subscales) {
ders %>%
group_by(session) %>%
levene_test(scale ~ group) %>%
kable(caption = scale) %>% print()
}
mutate(., data = map(.data$data, .f, ...)) 中的錯誤:由錯誤引起
model.frame.default()
:!可變長度不同(為“組”找到)
如何在我的 df 中對所有列運行 Levene 的測驗,而無需一遍又一遍地重復相同的代碼?我是 R 的新手,所以也許我正在嘗試一種過于 Python 的方式,應該使用類似 lapply() 的東西?
uj5u.com熱心網友回復:
reformulate
使用將被參考的字串創建公式,scale
因此,它需要使用reformulate
或構造公式paste
for (scale in subscales) {
ders %>%
group_by(session) %>%
levene_test(reformulate('group', response = scale)) %>%
kable(caption = scale) %>% print()
}
這也可以用across
library(dplyr)
library(stringr)
library(tidyr)
library(rstatix)
data(mtcars)
mtcars %>%
mutate(carb = factor(carb)) %>%
group_by(cyl) %>%
summarise(across(c(mpg, disp),
~ levene_test(cur_data(),
reformulate('carb', response = cur_column())) %>%
rename_with(~ str_c(cur_column(), .x), everything()) )) %>%
unpack(where(is.tibble))
-輸出
# A tibble: 3 × 9
cyl mpgdf1 mpgdf2 mpgstatistic mpgp dispdf1 dispdf2 dispstatistic dispp
<dbl> <int> <int> <dbl> <dbl> <int> <int> <dbl> <dbl>
1 4 1 9 0.975 0.349 1 9 1.32e- 1 7.24e- 1
2 6 2 4 2.52 0.196 2 4 7.44e 29 7.23e-60
3 8 3 10 1.60 0.251 3 10 1.18e 1 1.27e- 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491082.html