[R] Questions of t.test {stats}

Marta Rufino mrufino at ipimar.ualg.pt
Tue Oct 19 10:50:29 CEST 2004


Dear colegue,

I am not sure if it is this what you want, but to apply the t.test to all
rows in a data frame, you can do like:

apply(dataframe, 1, t.test)

If you want to store the results in a nice data frame, to us after, then you
should do a function, for example:

#################################### wilcox CI plus median
median.ci.wilcox=function(y){
# print("example: use to construct graphs with 95% CI, using xYplot, from
Hmisc. use summarize to compute the CI in function of factors")
if (is.R()) {require(ctest)}
if(length(y)>5){
c=t.test(y, conf.int=T, conf.level=.95)
res=c(mean=c$estimate, Lower=c(c$conf.int[1]), Upper=c(c$conf.int[2]))}
else {res=c(median=NA, Lower=NA, Upper=NA)}
res
}



hope this helps,
All the best,
Marta


----- Original Message ----- 
From: "pcscan" <s938611 at mail.yzu.edu.tw>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, October 19, 2004 4:04 AM
Subject: [R] Questions of t.test {stats}


> We are currently using the t-test in Package stats,
>
> t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"),
>        mu = 0, paired = FALSE, var.equal = FALSE,
>        conf.level = 0.95, ...)
>
> but have some troubles :
> 1. why does the t-test take so a long time to perform a single test on a
row
> of a data.frame ? Is there any alternative function to perform t-test on
all
> the rows of a data.frame ?
> 2. We got different results on the following data with the argument
> var.equal  setting as TRUE and FALSE respectively.
> We are curious why the  "Welch Two Sample t-test" couldn't distinguish
these
> two vectors well.
>
> Any help is greatly appreciated.
>
>
> Sincerely. Liu Yu Ting
>
>
============================================================================
> ===============
>
> x <-
>
c(-0.299611385,-0.164028986,-0.225545128,-0.244473171,-0.276619985,-0.276362
>
81,-0.289633015,-0.298994167,-0.27908886,-0.265612916,-0.262321082,-0.295753
>
768,-0.235677803,-0.283872306,-0.282174954,-0.241592817,-0.274716893,-0.2886
>
55752,-0.262166777,-0.263298345,-0.252239841,-0.298274078,-0.28958158,-0.187
>
174691,-0.26628157,-0.252034102,-0.248793703,-0.267207398,-0.289838754,-0.28
>
4283785,-0.118097619,-0.27898599,-0.265818655,-0.295085114,-0.246839177,-0.2
>
76105636,-0.293336328,-0.294210721,-0.259543597,0.18181929,-0.276311375,-0.2
>
48948008,-0.212583533,-0.247147786,-0.269573403,-0.27636281,-0.295445158,-0.
>
281146256,-0.27636281,-0.255840285,-0.292513369,-0.21664689,-0.228014003,-0.
>
238249548,-0.238300983,-0.238506723,-0.242004296,-0.213869405,-0.272916672,-
>
0.293233458,-0.239483986,-0.147672687,-0.289941624,-0.233774712,-0.237940939
>
,-0.276517115,-0.22431069,-0.217469848,0.461573717,-0.218858591,-0.280271863
>
,-0.290867452,-0.177144886,-0.179150847,-0.258463465,-0.269470533,-0.2482279
>
19,-0.221327466,-0.217418413,-0.290044494,-0.290610278,-0.260006512,-0.22261
>
3338,-0.275951331,0.015118775,0.116959879,-0.24509039,-0.092894518,0.5274618
> 26)
>
> y <-
>
c(0.963784092,-0.266641614,4.623274441,1.6857758,-0.251159709,-0.090631382,1
> .380355357,-0.117840445,7.213998979,3.404935937,1.444648983)
>
> > t.test(x,y)
>
>         Welch Two Sample t-test
>
> data:  x and y
> t = -2.8369, df = 10.009, p-value = 0.01763
> alternative hypothesis: true difference in means is not equal to 0
> 95 percent confidence interval:
>  -3.6338688 -0.4369566
> sample estimates:
>  mean of x  mean of y
> -0.2180945  1.8173182
>
> > t.test(x,y,var.equal=TRUE)
>
>         Two Sample t-test
>
> data:  x and y
> t = -8.2473, df = 98, p-value = 7.507e-13
> alternative hypothesis: true difference in means is not equal to 0
> 95 percent confidence interval:
>  -2.525175 -1.545650
> sample estimates:
>  mean of x  mean of y
> -0.2180945  1.8173182
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list