[R] Multiple t.test
Greg Snow
Greg.Snow at imail.org
Mon Sep 12 19:00:56 CEST 2011
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:
> tmp <- summary( lm( as.matrix(example[,-3]) ~ example[,3] ) )
> sapply( tmp, function(x) coef(x)[2,4] )
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:
> fit <- glm( disease ~ 1, data=example, family=binomial )
> fit2 <- glm( disease ~ ., data=example, family=binomial )
Warning message:
glm.fit: fitted probabilities numerically 0 or 1 occurred
> add1( fit, fit2, test='Chisq' )[[5]][-1]
[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).
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of C.H.
> Sent: Monday, September 12, 2011 1:54 AM
> To: R-help
> Subject: [R] Multiple t.test
>
> Dear R experts,
>
> Suppose I have an data frame likes this:
>
> > example <- data.frame(age=c(1,2,3, 4,5,6),
> height=c(100,110,120,130,140,150), disease=c(TRUE, TRUE, TRUE, FALSE,
> FALSE, FALSE))
>
> > example
> 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
>
> --
> CH Chan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list