# t test and ANOVA for continuous variables

Hopefully you can help me lift some of my confusion, why do I not get the same P-value when comparing two continuous variable using a two way t test and an ANOVA test.

Here is my example:

r<-rnorm(1000,mean=0,sd=1)
r2<-rnorm(1000,mean=0.2,sd=1)

## test1
t.test(r2,r,var.equal = T,paired = F)

## test2
summary(l<-lm(r2~r))
anova(l)

## test3
summary(l<-lm(r2~0+r))
summary(l0<-lm(r2~0))
anova(l,l0)


None of the tests gives the same results although test2 and test3 are quite similar in P-value. So where is my misunderstanding?

I would think these would give the same result, but alas they do not...

• The difference can be expressed as a function of the (empirical) correlation of the datasets r and r2. They almost surely will not have zero correlation even though they are independent!
– whuber
Jul 1, 2020 at 15:17

The model test2 is not equivalent to test1. In test2, you're using r as independent variable, whereas it should be the dependent variable, together with r2. Because r and r2 are independent, the $$p$$-value of test2 will be uniformly distributed under the null hypothesis.

In order to calculate the equivalent test, you have to combine the two variables r and r2 together and create an indicator variable for the group (e.g. $$0$$ for group r and $$1$$ for group r2). Here is an example using R:

set.seed(142857)

r<-rnorm(1000,mean=0,sd=1)
r2<-rnorm(1000,mean=0.2,sd=1)

## test1
t.test(r2,r,var.equal = T,paired = F)

## test2
r_comb <- c(r, r2)
group <- factor(rep(0:1, each = length(r)))

summary(l<-lm(r_comb~group))
anova(l)


Now the $$p$$-values match exactly.