Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aov and t.test deliver different results

Tags:

r

anova

From my knowledge, a t-test should deliver identical results (the same p-value) as ANOVA when applied to data with one explanatory variable. To test this, I ran the below to compare the results:

df <- structure(list(y = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1), x = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("FP", "WP" ), class = "factor")), class = "data.frame", row.names = c(NA,-11L))

summary(aov(y ~ x, data = df))
             Df Sum Sq Mean Sq F value Pr(>F)
x            1 0.3068  0.3068   1.473  0.256
Residuals    9 1.8750  0.2083               

t.test(y ~ x, data = df)

Welch Two Sample t-test

data:  y by x
t = -2.0494, df = 7, p-value = 0.0796
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.80768193  0.05768193
sample estimates:
mean in group FP mean in group WP 
           1.000            1.375 

As one can see, the p-value in case of ANOVA is 0.256 and 0.0796 in case of a t-test.

To understand the reason for this deviation, I calculated the test statistics myself, using the formulas for a t-test and for ANOVA. It looks like the t-test function gives wrong resuls when the sizes of the groups are different.

Is there a setting to make t-test to correctly work with different group sizes?

like image 402
DatamineR Avatar asked Jan 24 '23 23:01

DatamineR


1 Answers

The results are not wrong, the t-test function just applies the Welch-correction if the variances of the two groups are unequal. You can suppress this like so:

t.test(y ~ x, data = df, var.equal = TRUE)

    Two Sample t-test

data:  y by x
t = -1.2136, df = 9, p-value = 0.2558
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.0740253  0.3240253
sample estimates:
mean in group FP mean in group WP 
           1.000            1.375 

Which gives the same p-value as the anova (also notice the title of the output is now not "Welch Two Sample t-test" but simply "Two Sample t-test").

like image 131
Dominik S. Meier Avatar answered Feb 05 '23 03:02

Dominik S. Meier