Skip to content

Performing repeated T tests in R

2 messages · John Smith, Rui Barradas

#
I need to perform 10000 T tests

#I have two populations with different means
Popc1<-rnorm(100000,10,2)
Popc2<-rnorm(100000,8,2)

#I created two sets of samples - each set has 10000 samples, and I made a
matrix of 20 rows and 10000 columns to fit the data

sampc1<-matrix(,20,10000)
for(j in 1:10000){sampc1[1:20,j]<-sample(Popc1,20)}
sampc2<-matrix(,20,10000)
for(j in 1:10000){sampc2[1:20,j]<-sample(Popc2,20)}

#I wrote a blank matrix with 1 row and 10000 columns. I am planning to fill
this matrix with the t values that i calculate for each pair of samples
t<-matrix(,1,10000)

#What i need to do is to calculate the t value for each pair of samples: So
if i were to do it manually, for the first sample in each set, I would do
something like:

t.test(sampc1[,1],sampc2[,1])
#And i would continue doing this all the way until the 10000th pair of
samples
so t.test(sampc1[,2],sampc2[,2])
    t.test(sampc1[,3],sampc2[,3]) and so on

#But i need to do this for all 10000 pairs of samples:
#I tried to write a loop

for(j in 1:10000) {t[,1:10000]<-t.test(sampc1[, j],sampc2[, j])$statistic}

#but this fills each column in the matrix with the same exact t value.  

#Can someone tell me how to write the code to perform each pair of t tests?
I think i am missing something in my loop

Thanks 

--
View this message in context: http://r.789695.n4.nabble.com/Performing-repeated-T-tests-in-R-tp4571860p4571860.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,
Why not a vector? (It would be a column vector, which might not fit your
needs.)
And though not really a problem, the matrix is called 't', the name of R's
transpose function.
I can become confusing, maybe you could call it 'tstat' or something like
that.
And that value is the test statistic of the last test.
This is because the index of the results matrix/row vector should be

for(j in 1:10000) {t[, j]<-t.test(sampc1[, j],sampc2[, j])$statistic}

Or, since it has only one row,  t[1, j]

Or, with the suggestions above,

tstat <- double(10000)
for(j in 1:10000) {tstat[j]<-t.test(sampc1[, j],sampc2[, j])$statistic}


Hope this helps

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Performing-repeated-T-tests-in-R-tp4571860p4571923.html
Sent from the R help mailing list archive at Nabble.com.