我目前正在嘗試在 R 中撰寫一個函式,該函式將允許我計算資料框中所有可能的成對 t 檢驗。(我知道存在可以實作此目的的函式,但我也想學習如何成功撰寫函式)。我遇到了一個我不知道如何解決的問題。
資料:
library(combinat) # for generating pairwise combinations of variables
apple <- rnorm(100)
banana <- rnorm(100)
pear <- rnorm(100)
orange <- rnorm(100)
pineapple <- rnorm(100)
data <- data.frame(apple, banana, pear, orange, pineapple)
我的想法是使用 for 回圈在列名組合表中查找每一對列名,使用 match 函式參考原始資料集中關聯的列號,然后呼叫關聯列名作為 t 中的元素.測驗功能。這個程序是獨立作業的,但我在嘗試迭代時遇到了問題。
combinations <- combn2(names(data)) # creates a 2x10 table of all the combinations of the 5 column names
a<-match(combinations[8,1],colnames(data))
a<-data[,a]
b<-match(combinations[8,2],colnames(data))
b<-data[,b]
t.test(a, b)
# This works as expected
這是我嘗試使用 for 回圈自動執行此程序:
test <- function(data) {
names <- names(data)
combinations <- combinat::combn2(names(data))
num_rows <- NROW(combinations)
for (i in 1:num_rows) {
x<- match(combinations[i,1],colnames(data))
x<-data[,x]
y<- match(combinations[i,2],colnames(data))
y<-data[,y]
t.test(x, y)
}
}
test(data)
summary(test(data))
結果是空的。我顯然錯過了一些東西,但我不確定如何繼續。任何幫助表示贊賞。
uj5u.com熱心網友回復:
combn(not combn2)的第三個引數采用一個可以應用于每個組合的函式。你可以簡單地做
combn(data, 2L, \(d) {
syms <- lapply(names(d), as.symbol)
names(syms) <- c("x", "y")
eval(bquote(t.test(.(x), .(y)), syms), d)
}, FALSE)
輸出
[[1]]
Welch Two Sample t-test
data: apple and banana
t = -0.11531, df = 197.6, p-value = 0.9083
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3017470 0.2684074
sample estimates:
mean of x mean of y
-0.03961686 -0.02294705
[[2]]
Welch Two Sample t-test
data: apple and pear
t = -0.78348, df = 197.86, p-value = 0.4343
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3841981 0.1657171
sample estimates:
mean of x mean of y
-0.03961686 0.06962364
[[3]]
Welch Two Sample t-test
data: apple and orange
t = -0.55681, df = 196.65, p-value = 0.5783
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3433412 0.1921482
sample estimates:
mean of x mean of y
-0.03961686 0.03597966
[[4]]
Welch Two Sample t-test
data: apple and pineapple
t = 0.038627, df = 197.99, p-value = 0.9692
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2739606 0.2849074
sample estimates:
mean of x mean of y
-0.03961686 -0.04509027
[[5]]
Welch Two Sample t-test
data: banana and pear
t = -0.64848, df = 196.99, p-value = 0.5174
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3740876 0.1889462
sample estimates:
mean of x mean of y
-0.02294705 0.06962364
[[6]]
Welch Two Sample t-test
data: banana and orange
t = -0.4234, df = 194.84, p-value = 0.6725
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3334116 0.2155582
sample estimates:
mean of x mean of y
-0.02294705 0.03597966
[[7]]
Welch Two Sample t-test
data: banana and pineapple
t = 0.15274, df = 197.7, p-value = 0.8788
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2637425 0.3080290
sample estimates:
mean of x mean of y
-0.02294705 -0.04509027
[[8]]
Welch Two Sample t-test
data: pear and orange
t = 0.25138, df = 197.38, p-value = 0.8018
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2302948 0.2975828
sample estimates:
mean of x mean of y
0.06962364 0.03597966
[[9]]
Welch Two Sample t-test
data: pear and pineapple
t = 0.82024, df = 197.79, p-value = 0.4131
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1610834 0.3905112
sample estimates:
mean of x mean of y
0.06962364 -0.04509027
[[10]]
Welch Two Sample t-test
data: orange and pineapple
t = 0.59521, df = 196.45, p-value = 0.5524
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1875381 0.3496780
sample estimates:
mean of x mean of y
0.03597966 -0.04509027
[[1]]
Welch Two Sample t-test
data: apple and banana
t = -0.11531, df = 197.6, p-value = 0.9083
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3017470 0.2684074
sample estimates:
mean of x mean of y
-0.03961686 -0.02294705
[[2]]
Welch Two Sample t-test
data: apple and pear
t = -0.78348, df = 197.86, p-value = 0.4343
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3841981 0.1657171
sample estimates:
mean of x mean of y
-0.03961686 0.06962364
[[3]]
Welch Two Sample t-test
data: apple and orange
t = -0.55681, df = 196.65, p-value = 0.5783
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3433412 0.1921482
sample estimates:
mean of x mean of y
-0.03961686 0.03597966
[[4]]
Welch Two Sample t-test
data: apple and pineapple
t = 0.038627, df = 197.99, p-value = 0.9692
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2739606 0.2849074
sample estimates:
mean of x mean of y
-0.03961686 -0.04509027
[[5]]
Welch Two Sample t-test
data: banana and pear
t = -0.64848, df = 196.99, p-value = 0.5174
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3740876 0.1889462
sample estimates:
mean of x mean of y
-0.02294705 0.06962364
[[6]]
Welch Two Sample t-test
data: banana and orange
t = -0.4234, df = 194.84, p-value = 0.6725
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3334116 0.2155582
sample estimates:
mean of x mean of y
-0.02294705 0.03597966
[[7]]
Welch Two Sample t-test
data: banana and pineapple
t = 0.15274, df = 197.7, p-value = 0.8788
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2637425 0.3080290
sample estimates:
mean of x mean of y
-0.02294705 -0.04509027
[[8]]
Welch Two Sample t-test
data: pear and orange
t = 0.25138, df = 197.38, p-value = 0.8018
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2302948 0.2975828
sample estimates:
mean of x mean of y
0.06962364 0.03597966
[[9]]
Welch Two Sample t-test
data: pear and pineapple
t = 0.82024, df = 197.79, p-value = 0.4131
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1610834 0.3905112
sample estimates:
mean of x mean of y
0.06962364 -0.04509027
[[10]]
Welch Two Sample t-test
data: orange and pineapple
t = 0.59521, df = 196.45, p-value = 0.5524
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1875381 0.3496780
sample estimates:
mean of x mean of y
0.03597966 -0.04509027
[[1]]
Welch Two Sample t-test
data: apple and banana
t = -0.11531, df = 197.6, p-value = 0.9083
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3017470 0.2684074
sample estimates:
mean of x mean of y
-0.03961686 -0.02294705
[[2]]
Welch Two Sample t-test
data: apple and pear
t = -0.78348, df = 197.86, p-value = 0.4343
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3841981 0.1657171
sample estimates:
mean of x mean of y
-0.03961686 0.06962364
[[3]]
Welch Two Sample t-test
data: apple and orange
t = -0.55681, df = 196.65, p-value = 0.5783
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3433412 0.1921482
sample estimates:
mean of x mean of y
-0.03961686 0.03597966
[[4]]
Welch Two Sample t-test
data: apple and pineapple
t = 0.038627, df = 197.99, p-value = 0.9692
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2739606 0.2849074
sample estimates:
mean of x mean of y
-0.03961686 -0.04509027
[[5]]
Welch Two Sample t-test
data: banana and pear
t = -0.64848, df = 196.99, p-value = 0.5174
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3740876 0.1889462
sample estimates:
mean of x mean of y
-0.02294705 0.06962364
[[6]]
Welch Two Sample t-test
data: banana and orange
t = -0.4234, df = 194.84, p-value = 0.6725
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3334116 0.2155582
sample estimates:
mean of x mean of y
-0.02294705 0.03597966
[[7]]
Welch Two Sample t-test
data: banana and pineapple
t = 0.15274, df = 197.7, p-value = 0.8788
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2637425 0.3080290
sample estimates:
mean of x mean of y
-0.02294705 -0.04509027
[[8]]
Welch Two Sample t-test
data: pear and orange
t = 0.25138, df = 197.38, p-value = 0.8018
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2302948 0.2975828
sample estimates:
mean of x mean of y
0.06962364 0.03597966
[[9]]
Welch Two Sample t-test
data: pear and pineapple
t = 0.82024, df = 197.79, p-value = 0.4131
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1610834 0.3905112
sample estimates:
mean of x mean of y
0.06962364 -0.04509027
[[10]]
Welch Two Sample t-test
data: orange and pineapple
t = 0.59521, df = 196.45, p-value = 0.5524
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1875381 0.3496780
sample estimates:
mean of x mean of y
0.03597966 -0.04509027
uj5u.com熱心網友回復:
您需要為輸出分配一個參考 t.test(x, y)
試試這個:
test <- function(data) {
names <- names(data)
combinations <- combinat::combn2(names(data))
num_rows <- nrow(combinations)
test_results <- vector(mode = "list", length = num_rows)
for (i in 1:num_rows) {
x <- match(combinations[i,1],colnames(data))
x <- data[,x]
y <- match(combinations[i,2],colnames(data))
y <- data[,y]
test_results[[i]] <- t.test(x, y)
}
return(test_results)
}
這將為您提供一個串列輸出,其中每個條目都是根據您的要求對特定欄位組合執行的不同 t 檢驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/397796.html
