Apply t-test on many columns in a dataframe split by factor
Asked Answered
U

3

8

I have a dataframe with one factor column with two levels, and many numeric columns. I want to split the dataframe by the factor column and do t-test on the colunm pairs.

Using the example dataset Puromycin I want the result to look something like this:

Variable    Treated Untreated   p-value    Test-statistic CI of difference**** 
Conc        0.3450  0.2763          XXX     T           XX - XX
Rate        141.58  110.7272        xxx     T           XX - XX

I think I am looking for a solution using PLYR that can an output the above results in a nice dataframe.

(The Puromycin only contains two numeric variables, but the solution I am looking for would work on a dataframe with many numeric variables)

UPDATE - I will try to clarify what i mean.

I would like to go from data that look like this:

Grouping variable   var1    var2    var3    var4    var5
1           3   5   7   3   7
1           3   7   5   9   6
1           5   2   6   7   6
1           9   5   7   0   8
1           2   4   5   7   8
1           2   3   1   6   4
2           4   2   7   6   5
2           0   8   3   7   5
2           1   2   3   5   9
2           1   5   3   8   0
2           2   6   9   0   7
2           3   6   7   8   8
2           10  6   3   8   0

To a result dataframe that look like this:

"Mean in group 1"   "Mean in group 2"  "P-value of difference" "N"

var1            ##          ##          ##          ##      
var2            ##          ##          ##          ##  
var3            ##          ##          ##          ##  
var4            ##          ##          ##          ##  
var5            ##          ##          ##          ##

Maybe it is something with mapply I am looking for because i want to split up my dataframe into dataframe1 and dataframe2 by a two-level factor, and apply a function( t-test) to the first parts of dataframe1 and dataframe2, and then a t-test on the second parts of dataframe1 and dataframe2, and then a t-test to the third parts of dataframe1 and dataframe2, and so on on all the column-pairs generated by the split by factor.

Uncaused answered 9/12, 2012 at 18:55 Comment(6)
Thank you for the editing help.Uncaused
What did you try with plyr?Gritty
I dont remember what I tried in plyr only that I could it did not work. I have read the plyr article from J stat software, but the examples used where too complicated for me to understand it.Uncaused
I don't think you need plyr to do a paired t.test. This can maybe help you.wiki.stdout.org/rcookbook/Statistical%20analysis/t-testGritty
To split the data, use split and the variable you want to split on. eg: split(Puromycin, Puromycin$state)Oncoming
@agstudy, I don't think the OP wants a "paired t-test"; they want t-tests on the pairs of columns.Edmonson
E
10

Maybe this produces the result you are looking for:

df <- read.table(text="Group   var1    var2    var3    var4    var5
1           3   5   7   3   7
1           3   7   5   9   6
1           5   2   6   7   6
1           9   5   7   0   8
1           2   4   5   7   8
1           2   3   1   6   4
2           4   2   7   6   5
2           0   8   3   7   5
2           1   2   3   5   9
2           1   5   3   8   0
2           2   6   9   0   7
2           3   6   7   8   8
2           10  6   3   8   0", header = TRUE)


t(sapply(df[-1], function(x) 
     unlist(t.test(x~df$Group)[c("estimate","p.value","statistic","conf.int")])))

The result:

     estimate.mean in group 1 estimate.mean in group 2   p.value statistic.t conf.int1 conf.int2
var1                 4.000000                 3.000000 0.5635410   0.5955919 -2.696975  4.696975
var2                 4.333333                 5.000000 0.5592911  -0.6022411 -3.104788  1.771454
var3                 5.166667                 5.000000 0.9028444   0.1249164 -2.770103  3.103436
var4                 5.333333                 6.000000 0.7067827  -0.3869530 -4.497927  3.164593
var5                 6.500000                 4.857143 0.3053172   1.0925986 -1.803808  5.089522
Ellsworth answered 10/12, 2012 at 13:48 Comment(0)
U
4

Maybe you can find this useful

res <- sapply(split(Puromycin[,-3],  Puromycin$state), t.test)[c(1:3,5),]
conf.level <- sapply(sapply(split(Puromycin[,-3],  Puromycin$state), t.test)[4, ], '[', 1:2)
res <- rbind(res, conf.level.lower=conf.level[1,], conf.level.upper=conf.level[2,])
res
                 treated    untreated   
statistic        4.297025   4.206221    
parameter        23         21          
p.value          0.00026856 0.0003968191
estimate         70.96417   55.50182    
conf.level.lower 36.80086   28.06095    
conf.level.upper 105.1275   82.94268    
Unisexual answered 9/12, 2012 at 20:27 Comment(1)
Thanks but i need the output to be like in the examples i gave. This is because i need to do it on many numeric variables, and I need them to be presented rowwise.Uncaused
T
0

You can also use a custom made package matrixTests for this. Example using the data.frame prepared by @Sven below:

df <- read.table(text="Group   var1    var2    var3    var4    var5
1           3   5   7   3   7
1           3   7   5   9   6
1           5   2   6   7   6
1           9   5   7   0   8
1           2   4   5   7   8
1           2   3   1   6   4
2           4   2   7   6   5
2           0   8   3   7   5
2           1   2   3   5   9
2           1   5   3   8   0
2           2   6   9   0   7
2           3   6   7   8   8
2           10  6   3   8   0", header = TRUE)

library(matrixTests)

col_t_welch(df[df$Group==1,-1], df[df$Group==2,-1])
     obs.x obs.y obs.tot   mean.x   mean.y  mean.diff     var.x     var.y   stderr        df  statistic    pvalue  conf.low conf.high alternative mean.null conf.level
var1     6     7      13 4.000000 3.000000  1.0000000  7.200000 11.333333 1.679002 10.963146  0.5955919 0.5635410 -2.696975  4.696975   two.sided         0       0.95
var2     6     7      13 4.333333 5.000000 -0.6666667  3.066667  5.000000 1.106976 10.938135 -0.6022411 0.5592911 -3.104788  1.771454   two.sided         0       0.95
var3     6     7      13 5.166667 5.000000  0.1666667  4.966667  6.666667 1.334226 10.995151  0.1249164 0.9028444 -2.770103  3.103436   two.sided         0       0.95
var4     6     7      13 5.333333 6.000000 -0.6666667 10.666667  8.333333 1.722862 10.146824 -0.3869530 0.7067827 -4.497927  3.164593   two.sided         0       0.95
var5     6     7      13 6.500000 4.857143  1.6428571  2.300000 13.142857 1.503624  8.285649  1.0925986 0.3053172 -1.803808  5.089522   two.sided         0       0.95
Triecious answered 29/8, 2018 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.