2

I've run an anova using the following code:

aov2 <- aov(amt.eaten ~ salt + Error(bird / salt),data)

If I use view(aov2) I can see the residuals within the structure of aov2, but I would like to extract them in a way that doesn't involve cutting and pasting. Can someone help me out with the syntax?

Various versions of residuals(aov2) I have been using only produce NULL

Luke
  • 4,769
  • 5
  • 28
  • 36
  • Which packages are you sourcing? Do any of them alias the aov object because the residuals method works for anova objects in base R. – AdamO Apr 15 '13 at 23:12
  • No packages: the whole script is base R – Luke Apr 15 '13 at 23:13
  • Can you add the output of `dput(head(data))`? Running the example `utils::data(npk, package="MASS"); a <- aov(terms(yield ~ block + N * P + K, keep.order=TRUE), npk)` gives me an ANOVA object for which residual extraction works. – AdamO Apr 15 '13 at 23:15

2 Answers2

2

I just learn that you can use proj:

x1 <- gl(8, 4)             
block <- gl(2, 16)    
y <- as.numeric(x1) + rnorm(length(x1))        
d <- data.frame(block, x1, y)       

m <- aov(y ~ x1 + Error(block), d)         
m.pr <- proj(m)          
m.pr[['Within']][,'Residuals']         
toto_tico
  • 17,977
  • 9
  • 97
  • 116
  • it gives me subscript out of bound – HelpASisterOut May 27 '18 at 15:24
  • what is your model in `aov`. Out of bound usually means that you are accessing an index that does not exists, my guess `m.pr[[3]]` which depends on your formula. You can always display `m.pr` – toto_tico May 27 '18 at 15:38
  • 1
    I displayed `m.pr` as you suggested. It's a list of 13 elements. The variables in my ANOVA are way more than in your case, i think in your case you have 2? Do you suggest inside of writing `m.pr[[3]]` I extract `m.pr["Within"]` which is the last item in the list ? – HelpASisterOut May 27 '18 at 17:05
  • maybe you can post another question in stackoverflow, I would need to see the full model in `aov` and the output in `m.pr`. Generally speaking, you can access the content of `m.pr` using the appropriate indexes, take a look [here](https://www.rdocumentation.org/packages/stats/versions/3.4.3/topics/proj) – toto_tico May 27 '18 at 19:58
  • 1
    this might actually work: `m.pr[['Within']][,'Residuals']` – toto_tico May 27 '18 at 20:04
1

The reason that you cannot extract residuals from this model is that you have specified a random effect due to the bird salt ratio (???). Here, each unique combination of bird and salt are treated like a random cluster having a unique intercept value but common additive effect associated with a unit difference in salt and the amount eaten.

I can't conceive of why we would want to specify this value as a random effect in this model. But in order to sensibly analyze residuals, you may want to calculate fitted differences in each stratum according to the fitted model and optimal intercept. I think this is tedious work and not very informative, however.

AdamO
  • 4,283
  • 1
  • 27
  • 39