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

View source: R/residuals.R

residuals.afex_aovR Documentation

Extract Residuals and Fitted Values from afex_aov objects

Description

Extract Residuals and Fitted Values from afex_aov objects.

Usage

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

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

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.

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.

Author(s)

Mattan S. Ben-Shachar

Examples

### 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))
  
  ## Not run: 
  ## suppress Messages:
  suppressMessages(
    resid_auxpanel(residuals = residuals(mixed), predicted = fitted(mixed))
  )
  
## End(Not run)
}

afex documentation built on Sept. 11, 2024, 7:59 p.m.