0

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)
Alex
  • 4,030
  • 8
  • 40
  • 62

1 Answers1

1

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.

Jouni Helske
  • 6,427
  • 29
  • 52
  • 1
    Does it matter whether or not the model is repeated measures? Because if it is repeated measures, the output of `names()` is different. `a=data.frame(factor1 = rep(letters[1:2], each=40), factor2 = rep(letters[c(1,2,1,2)], each=20), id = rep(paste("subj", 1:4, sep=""), 5), score=sort(rlnorm(80))) a.model = aov(score~factor1*factor2 + Error(id/(factor1*factor2)), data=a) names(a.model)` – Alex Feb 26 '13 at 20:42
  • 1
    No but using the Error function in the formula causes the the aov work differently. I updated my answer. – Jouni Helske Feb 26 '13 at 20:50
  • An add up to the answer above and the @Alex's comment the that Alex is right. When we add the 'Error()' in our model, the options above are not valid. What one can do is 'a <- do.call(cbind, lapply(model, summary))' or simply 'unclass(summary(model))'. they give out a list of lists where one can easily access the stats, say by 'a[[2]][['F value]]'. – CAOC Sep 23 '18 at 05:23