How to perform t-tests for each level of a factor with tapply

六眼飞鱼酱① 提交于 2019-12-25 02:59:30

问题


My data and code are like this:

my_vector <- rnorm(150)
my_factor1 <- gl(3,50)
my_factor2 <- gl(2,75)

tapply(my_vector, my_factor1, function(x)
  t.test(my_vector~my_factor2, paired=T))

I want to do a separate t-test for each level of my_factor1, to test my_vector for both levels of my_factor2.

However, with my code the t-test is not splitting the levels of my_factor1, and the results are equal for each level because my_vector is entirely included in each t.test.

This is the output of my code:

$`1`

Paired t-test

data:  my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.2866512  0.3669667
sample estimates:
mean of the differences 
         0.04015775 


$`2`

Paired t-test

data:  my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.2866512  0.3669667
sample estimates:
mean of the differences 
         0.04015775 


$`3`

Paired t-test

data:  my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.2866512  0.3669667
sample estimates:
mean of the differences 
         0.04015775 

What am I missing or doing wrong?


回答1:


Your example is slightly problematic, since if you set:

df <- data.frame(my_vector = rnorm(150),
                 my_factor1 = gl(3,50),
                 my_factor2 = gl(2,75)
                )

You will have only one unique value for my_factor2 when my_factor1 = 1 or 3 because of how your repetitions overlap. See ?gl. So do:

df <- data.frame(my_vector = rnorm(150),
                 my_factor1 = gl(3,1,150),
                 my_factor2 = gl(2,1,150)
                )
with(df,
       by(df, my_factor1,
          function(x) t.test(my_vector ~ my_factor2, data=x)
       )
     )

Which appears to produce your desired output.

As a side note-- consider correction for multiple comparisons: https://stats.stackexchange.com/questions/16779/when-is-multiple-comparison-correction-necessary



来源:https://stackoverflow.com/questions/30332752/how-to-perform-t-tests-for-each-level-of-a-factor-with-tapply

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!