Rooirokbokkie
Rooirokbokkie

Reputation: 252

ggplot2 residuals with ezANOVA

I ran a three way repeated measures ANOVA with ezANOVA.

anova_1<-ezANOVA(data = main_data, dv = .(rt), wid.(id), 
           within = .(A,B,C), type = 3, detailed = TRUE)

I'm trying to see what's going on with the residuals via a qqplot but I don't know how to get to them or if they'r even there. With my lme models I simply extract them from the model

main_data$model_residuals <- as.numeric(residuals(model_1))

and plot them

   residuals_qq<-ggplot(main_data, aes(sample = main_data$model_residuals)) +       
        stat_qq(color="black", alpha=1, size =2) + 
        geom_abline(intercept = mean(main_data$model_residuals), slope = sd(main_data$model_residuals))

I'd like to use ggplot since I'd like to keep a sense of consistency in my graphing.

EDIT

Maybe I wasn't clear in what I'm trying to do. With lme models I can simply create the variable model_residuals from the residuals object in the main_data data.frame that then contains the residuals I plot in ggplot. I want to know if something similar is possible for the residuals in ezAnova or if there is a way I can get hold of the residuals for my ANOVA.

Upvotes: 1

Views: 2241

Answers (3)

Vis
Vis

Reputation: 31

Also potentially adding to an old post, but I butted up against this problem as well and as this is the first thing that pops up when searching for this question I thought I might add how I got around it.

I found that if you include the return_aov = TRUE argument in the ezANOVA setup, then the residuals are in there, but ezANOVA partitions them up in the resulting list it produces within each main and interaction effect, similar to how base aov() does if you include an Error term for subject ID as in this case.

These can be pulled out into their own list with purrr by mapping the residual function over this aov sublist in ezANOVA, rather than the main output. So from the question example, it becomes:

anova_1 <- ezANOVA(data = main_data, dv = .(rt), wid = .(id), 
           within = .(A,B,C), type = 3, detailed = TRUE, return_aov = TRUE)

ezanova_residuals <- purrr::map(anova_1$aov, residuals)

This will produce a list where each entry is the residuals from a part of the ezANOVA model for effects and interactions, i.e. $(Intercept), $id, id:a, id:b, id:a:b etc.

I found it useful to then stitch these together in a tibble using enframe and unnest (as the list components will probably be different lengths) to very quickly get them in a long format, that can then be plotted or tested:

ezanova_residuals_tbl <- enframe(ezanova_residuals) %>% unnest

hist(ezanova_residuals_tbl$value)

shapiro.test(ezanova_residuals_tbl$value)

I've not used this myself but the mapping idea also works for the coefficients and fitted.values functions to pull them out of the ezANOVA results, if needed. They might come out in some odd formats and need some extra manipulation afterwards though.

Upvotes: 3

Luis
Luis

Reputation: 1584

A quite old post I know, but it's possible to use ggplot to plot the residuals after modeling your data with ez package by using this function:

proj(ez_outcome$aov)[[3]][, "Residuals"]

then:

qplot(proj(ez_outcome$aov)[[3]][, "Residuals"])

Hope it helps.

Upvotes: 3

Floris
Floris

Reputation: 146

I had the same trouble with ezANOVA. The solution I went for was to switch to ez.glm (from the afex package). Both ezANOVA and ez.glm wrap a function from a different package, so you should get the same results. This would look like this for your example:

anova_1<-ez.glm("id", "rt", main_data, within=c("A","B","C"), return="full")
nice.anova(anova_1$Anova) # show the ANOVA table like ezANOVA does.

Then you can pull out the lm object and get your residuals in the usual way:

residuals(anova_1$lm)

Hope that helps.


Edit: A few changes to make it work with the last version

anova_1<-aov_ez("id", "rt", main_data, within=c("A","B","C"))
print(m1)
print(m1$Anova)
summary(m1$Anova)
summary(m1)

Then you can pull out the lm object and get your residuals in the usual way:

residuals(anova_1$lm)

Upvotes: 3

Related Questions