Skip to content

Multiple t.test

5 messages · C.H., Raphael Saldanha, Uwe Ligges +2 more

#
Dear R experts,

Suppose I have an data frame likes this:
age height disease
1   1    100    TRUE
2   2    110    TRUE
3   3    120    TRUE
4   4    130   FALSE
5   5    140   FALSE
6   6    150   FALSE

Is there anyway to compare the age and height between those with
disease=TRUE and disease=FALSE using t.test and extract the p-values
quickly?

I can do this individually

t.test(example$age~example$disease)[3]

But when the number of variable grow to something like 200 it is not
easy any more.

Thanks!

Regards,

CH
#
On 12.09.2011 13:16, Raphael Saldanha wrote:
Hmmm, I think the actual answer to the question is something along this 
line:

sapply(example[names(example)!="disease"],
        function(x) t.test(x ~ example[["disease"]])[[3]])


Uwe Ligges
#
Here is another approach.  A linear regression with a single binomial predictor will give the same results as a pooled t-test (if you insist on non-pooled then use sapply as previously suggested).  The lm function will do multiple regressions if given a matrix as the y-variable, so you can do a whole bunch of t-tests like:
Response age Response height 
     0.02131164      0.02131164
But note that you will be dealing with a bunch of tests and could have the standard problems that go with that.  Also note that what you are doing (while common) is really backwards, you are seeing if disease status is predictive of height and age when what would be more interesting is if height or age is predictive of disease status, you can do this individually like:
Warning message:
glm.fit: fitted probabilities numerically 0 or 1 occurred
[1] 0.003925917 0.003925917
Warning messages:
1: glm.fit: fitted probabilities numerically 0 or 1 occurred 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred

(the warnings are because of the small unrealistic sample data, for real data they are much less likely).