Skip to content

Problem with differences between S+ and R in parsing output tables with $

5 messages · RexBryan, Ben Bolker, Douglas Bates

#
R-wizards

I have successfully run with S+ a statistical power calculation for
non-normal distributions as presented in M. Crawley?s new book.? When I
tried the newest version of R on the same code, the $ parse statement
doesn't seem to retrieve the appropriate number from a table. Note that
some of the cosmetic differences in the two tables have to be dealt with
by the parser. Any ideas what's happening?
REX

# Begin R -------------------------------------------------------------
#
Df 	Sum Sq 	Mean Sq 	F value 	Pr(>F)
fa            	1   	11.1    	11.1  	0.9327 	0.3345
Residuals   	698 	8279.3    	11.9

# R: ... trying to parse the table gives a NULL for the probability of
F...
NULL

# End R ---------------------------------------------------------------

# Begin S+ ------------------------------------------------------------
Df 	Sum of Sq  	Mean Sq   	F Value
Pr(F) 
fa   			1    	11.063 	11.06286 	0.9326708
0.3345044
Residuals 		698  	8279.314 	11.86148          

# S+ ... using $ to parse the table gives the right answer for the
probability of F...

(summary(aov(model))$"Pr(F)")[1]
[1] 0.3345044

# End S+ --------------------------------------------------------------
#
"RexBryan" <rexbryan1 at attbi.com> writes:
Hmm - that's a peculiar way of trying to extract the information but
if you really want to do it like that you should first use str() to
determine the structure of the returned value.  It turns out that the
value is a list and the first component of the list is a data frame
with one column labelled "Pr(>F)".
List of 1
 $ :Classes anova  and `data.frame':	3 obs. of  5 variables:
  ..$ Df     : num [1:3] 3 5 15
  ..$ Sum Sq : num [1:3] 13445  1428   781
  ..$ Mean Sq: num [1:3] 4482  286   52
  ..$ F value: num [1:3] 86.11  5.49    NA
  ..$ Pr(>F) : num [1:3] 1.11e-09 4.55e-03       NA
 - attr(*, "class")= chr [1:2] "summary.aov" "listof"

This means you would need to put [[1]] before the $
[1] 1.110419e-09 4.550074e-03           NA
#
In R, it looks like the "summary.aov" type actually contains the data 
frame you're trying to extract as the first (and only) element of a list, 
so 

summary(aov(model))[[1]]

is more or less equivalent to the summary(aov(model)) object in S-PLUS, 
and

summary(aov(model))[[1]]$"Pr(>F)"[1]

gets the specific number you're looking for.

str()  and class() are the tools for disentangling this kind of problem.  
It is a shame that it's so hard to extract the value in this case, but the
authors have to be able to structure things internally however they find
convenient, and unless they provide accessor methods for everything anyone 
could conceivably want, people are going to have to dig at some point ...
On Mon, 9 Dec 2002, RexBryan wrote:

            

  
    
#
On 9 Dec 2002, Douglas Bates wrote:

            
How would you normally go about extracting such information?  Other than 
doing it via summary(model(aov))[[1]]$"Pr(>F)"[1] [which is admittedly 
pretty horrible], or extracting the raw information from the aov object 
and computing the F-prob oneself, I can't see how to do it ...

  Ben
#
Ben Bolker <ben at zoo.ufl.edu> writes:
I think this means I should learn not to make editorial comments and
just stick to the answer.  On reflection I think you are correct in
what you said in your earlier posting.  This is not a particularly
elegant way of getting the p-value but it seems to be the only way of
doing so.  There is no accessor method for it.  Having an accessor
would be a more elegant design.