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

2019-09-17 08:19发布

问题:

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



标签: r tapply