s-news
[Top] [All Lists]

Re: 1) Creating Tables in Splus 2) Looping

To: "angela g." <angelaeic@hotmail.com>
Subject: Re: 1) Creating Tables in Splus 2) Looping
From: Spencer Graves <spencer.graves@pdf.com>
Date: Mon, 20 Oct 2003 11:15:38 -0700
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <BAY9-F29CWIg6nvGMiU0000c054@hotmail.com>
References: <BAY9-F29CWIg6nvGMiU0000c054@hotmail.com>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Regarding the second question first: "summary" produces an object, of which a summary is printed by default when it is executed as a stand-alone command but not when executed in a loop as you have it. To get what you want, you can put "print" inside the loop, as follows:
DF <- data.frame(group=rep(1:3, length=12), x1=1:12, x2=rep(0:1, length=12))

for(i in 2:3){
   tmp <- aov(DF[,i]~factor(group) )
   print(summary(tmp))
}
             Df Sum of Sq Mean Sq   F Value     Pr(F)
factor(group)  2         8       4 0.2666667 0.7717734
Residuals 9 135 15 Df Sum of Sq Mean Sq F Value Pr(F)
factor(group)  2         0 0.0000000 9.766615e-032     1
Residuals 9 3 0.3333333
###################
Regarding your first question: You can get and store almost anything you want. S is an object-oriented language for statistics. By playing around a bit, I found that summary(tmp) in this example is actually a 2 x 3 matrix. Suppose I wanted to store the degrees of freedom and mean square for both the factor effect and residuals from each analysis in an 3-dimensional array. I might do the following:
aovSum <- array(NA, dim=c(2,2,2))
for(i in 2:3){
   tmp <- aov(DF[,i]~factor(group) )
   aovSum[i-1,,] <- as.matrix(summary(tmp)[,c(1,3)])
}
aovSum

I can dress aovSum up with dimnames, etc. There are several introductory treatments that cover this kind of thing. One of my favorits is Venables and Ripley (2002) Modern Applied Statistics with S, 4th ed. (Springer). Also, you might check out "www.r-project.org"; they offer similar treatments for free. hope this helps. spencer graves

angela g. wrote:

Hi, Two questions:

1) Can I program Splus to tabulate results of tests neatly in a table? Does Splus have something similar to SAS's "proc tabulate"?


2) I am looping through a datamatrix performing a test on each column and I want Splus to report the results of the test each iteration. This is what I did (mydata is a 100x15 matrix)
> for(i in 3:dim(mydata)[2]) { # for columns 3 through 15

+      tmp <- aov(mydata[, i] ~ factor(group), na.action = na.exclude)

+      summary(tmp)

}

But Splus returns no results at all. When I type summary(tmp) on the command line after executing the above statements, it spits out the correct result (ie. the test result for the last column in my dataset). So, this loop seems to be doing what I want it to do. However I would like Splus to report the intermediate results before overwriting it with the next test.

If anyone has some ideas, please email me. Thanks!


------------------------------------------------------------------------
See when your friends are online with MSN Messenger 6.0. Download it now FREE! <http://g.msn.com/8HMBENUS/2740??PS=>



<Prev in Thread] Current Thread [Next in Thread>