Question

I am trying to find a less convoluted way to extract data from an aov object. Suppose I have a dataset a as shown below, and I ran an ANOVA based on the data, resulting in an object named a.model. I tried to locate the data by using str(a.model), but haven't been able to find them. Since I know how to extract data from lm objects, what I did was using lm(a.model)$model$score, which works. But is it possible to directly extract data from a.model without first converting an aov object to an lm object? - I guess this is more out of curiosity than anything because the "extra" step of conversion is not that much more work.

a=data.frame(factor1 = rep(letters[1:2], each=10), 
             factor2 = rep(letters[c(1,2,1,2)], each=5), 
             score=sort(rlnorm(20)))
a.model = aov(score~factor1*factor2, data=a)
Was it helpful?

Solution

Output from aov also have a component called model which contains the data, ie. a.model$model$score is identical to lm(a.model)$model$score.

Function names is useful:

> names(a.model)
 [1] "coefficients"  "residuals"    
 [3] "effects"       "rank"         
 [5] "fitted.values" "assign"       
 [7] "qr"            "df.residual"  
 [9] "contrasts"     "xlevels"      
[11] "call"          "terms"        
[13] "model"

Another way which is perhaps more convienient and works in more general cases, is to use functions model.matrix and model.frame which give the desing matrix and the whole model used in formula. In your second example (in comments) you can use model.frame to get the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top