Patrick Ayscue
2008-Jun-19 15:01 UTC
[R] how to extract object from stats test output (cor.test)?
Hello, Is there a way to extract output objects from a stats test without viewing the entire output? I am trying to do so in the following: define a vector of length j for( i in 1: length (vector)) { vector[i] = cor.test (datavector1, datavector2[i], method=("spearman")) } I would like the reported Spearman's rho to be saved in a vector. I have tried a few different ways of doing this but seem unable to figure out how to get only that output without looking at each report and copying by hand. Any help would be appreciated. Thanks, Patrick [[alternative HTML version deleted]]
Abhijit Dasgupta
2008-Jun-19 15:36 UTC
[R] how to extract object from stats test output (cor.test)?
First of all, you need to store as a list not a vector if you;re storing the entire output. Otherwise, you can just store, for example, cor.test(...)$p.value On Thu, 19 Jun 2008 11:01:42 -0400 "Patrick Ayscue" <payscue at gmail.com> wrote:> Hello, > > Is there a way to extract output objects from a stats test without viewing > the entire output? I am trying to do so in the following: > > define a vector of length j > > for( i in 1: length (vector)) { > vector[i] = cor.test (datavector1, datavector2[i], method=("spearman")) > } > > I would like the reported Spearman's rho to be saved in a vector. I have > tried a few different ways of doing this but seem unable to figure out how > to get only that output without looking at each report and copying by hand. > > > Any help would be appreciated. > > > Thanks, > Patrick > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Abhijit Dasgupta
Jorge Ivan Velez
2008-Jun-19 15:45 UTC
[R] how to extract object from stats test output (cor.test)?
Dear Patrick, Assuming that datavector1 is the first column of your data set, you can try: # Data set set.seed(123) y=matrix(rnorm(100*6),ncol=6) rownames(y)=1:100 colnames(y)=c('datavector1',paste('X',1:5,sep="")) y[1:10,] # Correlations RES=apply(y[,-1],2,function(y,x) cor.test(x,y,method="spearman")$estimate,y[,1]) RES RES contains the (k-1) spearman correlation coefficient, being k the number of columns of your data. HTH, Jorge On Thu, Jun 19, 2008 at 11:01 AM, Patrick Ayscue <payscue@gmail.com> wrote:> Hello, > > Is there a way to extract output objects from a stats test without viewing > the entire output? I am trying to do so in the following: > > define a vector of length j > > for( i in 1: length (vector)) { > vector[i] = cor.test (datavector1, datavector2[i], method=("spearman")) > } > > I would like the reported Spearman's rho to be saved in a vector. I have > tried a few different ways of doing this but seem unable to figure out how > to get only that output without looking at each report and copying by hand. > > > Any help would be appreciated. > > > Thanks, > Patrick > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Duncan Murdoch
2008-Jun-19 15:49 UTC
[R] how to extract object from stats test output (cor.test)?
On 6/19/2008 11:01 AM, Patrick Ayscue wrote:> Hello, > > Is there a way to extract output objects from a stats test without viewing > the entire output? I am trying to do so in the following: > > define a vector of length j > > for( i in 1: length (vector)) { > vector[i] = cor.test (datavector1, datavector2[i], method=("spearman")) > } > > I would like the reported Spearman's rho to be saved in a vector. I have > tried a few different ways of doing this but seem unable to figure out how > to get only that output without looking at each report and copying by hand.See the Value section of the ?cor.test man page. It says that the result is a list containing (among other things) the components statistic: the value of the test statistic. estimate: the estimated measure of association, with name '"cor"', '"tau"', or '"rho"' corresponding to the method employed. I think "estimate" is what you want, which you can get as cor.test (datavector1, datavector2[i], method=("spearman"))$estimate This is a numeric vector with one element named "rho", in the example on the ?cor.test page. Duncan Murdoch> > > Any help would be appreciated. > > > Thanks, > Patrick > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.