Michael Rennie
2004-Jul-29 05:57 UTC
[R] extracting the t-statistic: just the numbers, please
Hi, there I am quite sure there is an easy answer to this, but I am unsure how to gather a bunch of t-statistics in an organized format. I am trying to generate a list of t-statistics for a randomization routine. If I try to collect a bunch of t-statistics from a run, this is what happens:> M <- 10 ; simt <- NULL > for(i in 1:M)+ { + perm<-sample(site,replace=F) + + permute<-cbind(perm, site, a, b, c) + + m<- order(perm) + + m1<-cbind(perm[m], site[m], a[m], b[m], c[m]) + + black<-c((m1[1:5,5]),(m1[11:15,5])) + #black + + white<-c((m1[6:10,5]),(m1[16:20,5])) + #white + + sims <- t.test(black,white,var.equal=FALSE,mu=0)$statistic + simt<-c(simt,sims) + #simt + } # Next i (next simulation)> > simtt t t t t t t 0.3474150 0.1542973 -0.4044992 1.2466663 -0.2933944 -0.5809257 0.7799080 t t t -1.4132713 1.2048335 -0.6596936 Which gives me a list, but not in a form that I can do anything with. This is in stark contrast to what happens when requesting p-values, which gives output like this: M <- 10 ; simt <- NULL> for(i in 1:M)+ { + perm<-sample(site,replace=F) + + permute<-cbind(perm, site, a, b, c) + + m<- order(perm) + + m1<-cbind(perm[m], site[m], a[m], b[m], c[m]) + + black<-c((m1[1:5,5]),(m1[11:15,5])) + #black + + white<-c((m1[6:10,29]),(m1[16:20,5])) + #white + + sims <- t.test(black,white,var.equal=FALSE,mu=0)$p.value + simt<-c(simt,sims) + #simt + } # Next i (next simulation)> > simt[1] 0.6763749 0.7480091 0.9447851 0.3342029 0.7852635 0.3199006 0.5272153 [8] 0.3863616 0.7333693 0.7268907 Now THAT'S what I'd like to get for my t-statistics- a nice vector (simt) that I can deal with later, rather than the output I am currently getting (the first output above). Does anyone know a way to extract JUST the t-statistics from the t.test, without the "t" character header, so I can generate a nice little vector? Alternatively, can I manipulate the output I am currently getting for the t- statistics so that I can isolate just the numbers? -- Michael Rennie Ph.D. Candidate University of Toronto at Mississauga 3359 Mississauga Rd. N. Mississauga ON L5L 1C6 Ph: 905-828-5452 Fax: 905-828-3792
Christian Schulz
2004-Jul-29 06:19 UTC
[R] extracting the t-statistic: just the numbers, please
> data(sleep) > t.test(extra ~ group, data = sleep)$statistic[1]t -1.860813> t.test(extra ~ group, data = sleep)$statistic[[1]][1] -1.860813 hope this helps, christian Am Donnerstag, 29. Juli 2004 07:57 schrieb Michael Rennie:> Hi, there > > I am quite sure there is an easy answer to this, but I am unsure how to > gather a bunch of t-statistics in an organized format. > > I am trying to generate a list of t-statistics for a randomization routine. > If > > I try to collect a bunch of t-statistics from a run, this is what happens: > > M <- 10 ; simt <- NULL > > for(i in 1:M) > > + { > + perm<-sample(site,replace=F) > + > + permute<-cbind(perm, site, a, b, c) > + > + m<- order(perm) > + > + m1<-cbind(perm[m], site[m], a[m], b[m], c[m]) > + > + black<-c((m1[1:5,5]),(m1[11:15,5])) > + #black > + > + white<-c((m1[6:10,5]),(m1[16:20,5])) > + #white > + > + sims <- t.test(black,white,var.equal=FALSE,mu=0)$statistic > + simt<-c(simt,sims) > + #simt > + } # Next i (next simulation) > > > simt > > t t t t t t > t 0.3474150 0.1542973 -0.4044992 1.2466663 -0.2933944 -0.5809257 > 0.7799080 t t t > -1.4132713 1.2048335 -0.6596936 > > Which gives me a list, but not in a form that I can do anything with. This > is in stark contrast to what happens when requesting p-values, which gives > output like this: > > > > M <- 10 ; simt <- NULL > > > for(i in 1:M) > > + { > + perm<-sample(site,replace=F) > + > + permute<-cbind(perm, site, a, b, c) > + > + m<- order(perm) > + > + m1<-cbind(perm[m], site[m], a[m], b[m], c[m]) > + > + black<-c((m1[1:5,5]),(m1[11:15,5])) > + #black > + > + white<-c((m1[6:10,29]),(m1[16:20,5])) > + #white > + > + sims <- t.test(black,white,var.equal=FALSE,mu=0)$p.value > + simt<-c(simt,sims) > + #simt > + } # Next i (next simulation) > > > simt > > [1] 0.6763749 0.7480091 0.9447851 0.3342029 0.7852635 0.3199006 0.5272153 > [8] 0.3863616 0.7333693 0.7268907 > > Now THAT'S what I'd like to get for my t-statistics- a nice vector (simt) > that I can deal with later, rather than the output I am currently getting > (the first output above). > > Does anyone know a way to extract JUST the t-statistics from the t.test, > without the "t" character header, so I can generate a nice little vector? > Alternatively, can I manipulate the output I am currently getting for the > t- statistics so that I can isolate just the numbers?
Spencer Graves
2004-Jul-29 06:20 UTC
[R] extracting the t-statistic: just the numbers, please
The function "as.vector" will remove the names: > t.test(1:9)$statistic t 5.477226 > as.vector(t.test(1:9)$statistic) [1] 5.477226 Is this what you want? spencer graves Michael Rennie wrote:>Hi, there > >I am quite sure there is an easy answer to this, but I am unsure how to gather >a bunch of t-statistics in an organized format. > >I am trying to generate a list of t-statistics for a randomization routine. If >I try to collect a bunch of t-statistics from a run, this is what happens: > > > > >>M <- 10 ; simt <- NULL >>for(i in 1:M) >> >> >+ { >+ perm<-sample(site,replace=F) >+ >+ permute<-cbind(perm, site, a, b, c) >+ >+ m<- order(perm) >+ >+ m1<-cbind(perm[m], site[m], a[m], b[m], c[m]) >+ >+ black<-c((m1[1:5,5]),(m1[11:15,5])) >+ #black >+ >+ white<-c((m1[6:10,5]),(m1[16:20,5])) >+ #white >+ >+ sims <- t.test(black,white,var.equal=FALSE,mu=0)$statistic >+ simt<-c(simt,sims) >+ #simt >+ } # Next i (next simulation) > > >>simt >> >> > t t t t t t t > 0.3474150 0.1542973 -0.4044992 1.2466663 -0.2933944 -0.5809257 0.7799080 > t t t >-1.4132713 1.2048335 -0.6596936 > >Which gives me a list, but not in a form that I can do anything with. This is >in stark contrast to what happens when requesting p-values, which gives output >like this: > > > > M <- 10 ; simt <- NULL > > >>for(i in 1:M) >> >> >+ { >+ perm<-sample(site,replace=F) >+ >+ permute<-cbind(perm, site, a, b, c) >+ >+ m<- order(perm) >+ >+ m1<-cbind(perm[m], site[m], a[m], b[m], c[m]) >+ >+ black<-c((m1[1:5,5]),(m1[11:15,5])) >+ #black >+ >+ white<-c((m1[6:10,29]),(m1[16:20,5])) >+ #white >+ >+ sims <- t.test(black,white,var.equal=FALSE,mu=0)$p.value >+ simt<-c(simt,sims) >+ #simt >+ } # Next i (next simulation) > > >>simt >> >> > [1] 0.6763749 0.7480091 0.9447851 0.3342029 0.7852635 0.3199006 0.5272153 > [8] 0.3863616 0.7333693 0.7268907 > >Now THAT'S what I'd like to get for my t-statistics- a nice vector (simt) that >I can deal with later, rather than the output I am currently getting (the first >output above). > >Does anyone know a way to extract JUST the t-statistics from the t.test, >without the "t" character header, so I can generate a nice little vector? >Alternatively, can I manipulate the output I am currently getting for the t- >statistics so that I can isolate just the numbers? > > >
Peter Dalgaard
2004-Jul-29 06:31 UTC
[R] extracting the t-statistic: just the numbers, please
Michael Rennie <mrennie at utm.utoronto.ca> writes:> > simt > t t t t t t t > 0.3474150 0.1542973 -0.4044992 1.2466663 -0.2933944 -0.5809257 0.7799080 > t t t > -1.4132713 1.2048335 -0.6596936 > > Which gives me a list, but not in a form that I can do anything with. This is > in stark contrast to what happens when requesting p-values, which gives output > like this:..> > simt > [1] 0.6763749 0.7480091 0.9447851 0.3342029 0.7852635 0.3199006 0.5272153 > [8] 0.3863616 0.7333693 0.7268907 > > Now THAT'S what I'd like to get for my t-statistics- a nice vector (simt) that > I can deal with later, rather than the output I am currently getting (the first > output above). > > Does anyone know a way to extract JUST the t-statistics from the t.test, > without the "t" character header, so I can generate a nice little vector? > Alternatively, can I manipulate the output I am currently getting for the t- > statistics so that I can isolate just the numbers?It *is* a nice little vector! It's just that it has names. names(simt) <- NULL gets rid of them. BTW, simt <- c(simt, sims) is going to kill performance with large repeat counts since it copies all previous results every time around. Preallocate the result, or consider using replicate() or sapply(). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907