afex icon indicating copy to clipboard operation
afex copied to clipboard

Residuals with `mixed`-fitted model

Open pablobernabeu opened this issue 3 years ago • 3 comments

Hi Henrik,

I would like to ask you if you could please advise me how to create condition-specific residuals plots for mixed-fitted models.

I have looked for possibilities but to little avail.

Thank you very much

pablobernabeu avatar Nov 28 '22 12:11 pablobernabeu

Are you looking for something like this?

library(afex)
#> Loading required package: lme4
#> Loading required package: Matrix
#> ************
#> Welcome to afex. For support visit: http://afex.singmann.science/
#> - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
#> - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
#> - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
#> - NEWS: emmeans() for ANOVA models now uses model = 'multivariate' as default.
#> - Get and set global package options with: afex_options()
#> - Set orthogonal sum-to-zero contrasts globally: set_sum_contrasts()
#> - For example analyses see: browseVignettes("afex")
#> ************
#> 
#> Attaching package: 'afex'
#> The following object is masked from 'package:lme4':
#> 
#>     lmer
library(ggeffects)
library(patchwork)

data(md_12.1)
a <- aov_ez("id", "rt", md_12.1, within = c("angle", "noise"))

p1 <- ggemmeans(a, "angle") |> 
  plot(add.data = TRUE, jitter = 0.1) + 
  ggplot2::ggtitle("Raw Data")

p2 <- ggemmeans(a, "angle") |> 
  plot(residuals = TRUE, jitter = 0.1) + 
  ggplot2::ggtitle("Residualised scores")
#> Data was changed during ANOVA calculation. Thus, residuals cannot be added to original data.
#> residuals(..., append = TRUE) will return data and residuals.

#Compare
p1 + p2 &
  ggplot2::coord_cartesian(ylim = c(300, 1000))

Created on 2022-11-28 with reprex v2.0.2

mattansb avatar Nov 28 '22 13:11 mattansb

Thank you very much. Would it be possible to get Q-Q plots, please?

I would also like to comment on the function I used to fit my model, namely, mixed(). I've noticed that a custom function that was available to plot residuals (kindly provided by yourself in a previous issue) doesn't seem to work with models fitted by mixed. I'm echoing this previous issue.

Since that issue, do you know if it has become possible to create Q-Q plots in a way similar to your residuals_qqplot function, but with a mixed-fitted model?

I have googled into this but to no avail so far.

pablobernabeu avatar Nov 28 '22 15:11 pablobernabeu

P.S. Even if there were a more cumbersome way, I would be very interested in it.

pablobernabeu avatar Nov 28 '22 15:11 pablobernabeu