r - 编写成对 t 检验的函数

标签 r function for-loop t-test

我目前正在尝试在 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.test 函数中。这个过程是孤立进行的,但我在尝试迭代它时遇到了问题。

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))

结果为空。我显然错过了一些东西,但我不知道如何继续。如有任何帮助,我们将不胜感激。

最佳答案

combn 的第三个参数(不是 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 

关于r - 编写成对 t 检验的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70525102/

相关文章:

c - --i 在平方根函数中做什么?

javascript - angularjs 中非常慢的循环

r - 如何绘制等高线,显示 95% 的值落在 R 和 ggplot2 中的位置

r - 用日期扩展 x 轴

c - 函数返回类型语法

c - union 和通用功能

regex - R中data.table中一列的子字符串字符

r - R中最有效的kmeans聚类包是什么?

c++ - 如果输出无关紧要,则不会调用函数

java - 关于增量的几次迭代的解释?