Learn R Programming

afex (version 1.4-1)

residuals.afex_aov: Extract Residuals and Fitted Values from afex_aov objects

Description

Extract Residuals and Fitted Values from afex_aov objects.

Usage

# S3 method for afex_aov
residuals(object, append = FALSE, colname_residuals = ".residuals", ...)

# S3 method for afex_aov fitted(object, append = FALSE, colname_fitted = ".fitted", ...)

Value

A vector of residuals/fitted values corresponding to the data in

object$data$long, or if append = TRUE a data frame with an additional column of residuals/fitted values.

Arguments

object

afex_aov object.

append

If set to TRUE returns the residuals/fitted values appended as an additional column to the long data. Recommended when data was aggregated across within conditions.

colname_residuals, colname_fitted

Name of the appended column when append = TRUE.

...

Additional arguments passed to residuals.lm/fitted.lm.

Author

Mattan S. Ben-Shachar

Examples

Run this code
### Setup ANOVAs
data(obk.long, package = "afex")
between <- aov_car(value ~ treatment*gender + Error(id), data = obk.long)
within <- aov_car(value ~ 1 + Error(id/(phase*hour)), data = obk.long)
mixed <- aov_car(value ~ treatment * gender + Error(id/(phase*hour)), data = obk.long)

# All residuals call produce the message that the data was changed during calculation.
residuals(within)
residuals(mixed)
residuals(between)

## Get residuals plus data used for fitting: 
residuals(within, append = TRUE)
residuals(mixed, append = TRUE)
residuals(between, append = TRUE)

### in case data is correctly ordered before fitting, this message is not shown

## between data:
obk2 <- aggregate(value ~ gender + treatment + id , data = obk.long, FUN = mean)
between2 <- aov_car(value ~ treatment*gender + Error(id), data = obk2)

residuals(between2) ## no message
all.equal(obk2, between2$data$long[,colnames(obk2)]) ## TRUE

# Therefore okay:
obk2$residuals <- residuals(between2)

## within data
obk3 <- obk.long[with(obk.long, order(id, phase, hour)), ]
within2 <- aov_car(value ~ 1 + Error(id/(phase*hour)), data = obk3)
residuals(within2) ## no message, because order is correct
# Therefore okay:
obk3$residuals <- residuals(within2)

## Same for fitted values:
# (show message)
fitted(within)
fitted(mixed)
fitted(between)

## Get fitted values plus data used for fitting: 
fitted(within, append = TRUE)
fitted(mixed, append = TRUE)
fitted(between, append = TRUE)

## No message:
fitted(between2)
fitted(within2)

#### residuals() and fitted() methods can be used for plotting
### requires package ggResidpanel
if (require("ggResidpanel")) {
  resid_auxpanel(residuals = residuals(mixed), predicted = fitted(mixed))
  
  if (FALSE) {
  ## suppress Messages:
  suppressMessages(
    resid_auxpanel(residuals = residuals(mixed), predicted = fitted(mixed))
  )
  }
}

Run the code above in your browser using DataLab