meanResiduals: Average Residuals within Factor Levels

View source: R/meanResiduals.R

meanResidualsR Documentation

Average Residuals within Factor Levels

Description

Computes the mean working residuals from a model fitted using Iterative Weighted Least Squares for each level of a factor or interaction of factors.

Usage

meanResiduals(object, by, standardized=TRUE, as.table=TRUE, ...)

Arguments

object

model object for which object$residuals gives the working residuals and object$weights gives the working weights.

by

either a formula specifying a factor or interaction of factors (recommended), or a list of factors (the elements of which must correspond exactly to observations in the model frame). When a list of factors is specified, their interaction is used to specify the grouping factor.

standardized

logical: if TRUE, the mean residuals are standardized to be approximately standard normal.

as.table

logical: logical: if TRUE and by specifies an interaction of factors, the result is returned as a table cross-classified by these factors.

...

currently ignored

Details

For level i of the grouping factor A the mean working residual is defined as

\frac{r_{ij} * w_{ij}}{\sum_{j = 1}^{n_i} w_{ij}}

where r_{ij} is the j'th residual for level i, w_{ij} is the corresponding working weight and n_i is the number of observations for level i. The denominator gives the weight corresponding to mean residual.

For non-aggregated residuals, i.e. when the factor has one level per observation, the residuals are the same as Pearson residuals.

Value

An object of class "meanResiduals", for which print and summary methods are provided. A "meanResiduals" object is a list containing the following elements:

call

the call used to create the model object from which the mean residuals are derived.

by

a label for the grouping factor.

residuals

the mean residuals.

df

the degrees of freedom associated with the mean residuals.

standardized

the standardized argument.

weights

the weights corresponding to the mean residuals.

Author(s)

Heather Turner

Examples

## Fit a conditional independence model, leaving out
## the uninformative subtable for dest == 7:
CImodel <- gnm(Freq ~ educ*orig + educ*dest, family = poisson,
               data = yaish, subset = (dest != 7))

## compute mean residuals over origin and destination
meanRes <- meanResiduals(CImodel, ~ orig:dest)
meanRes
summary(meanRes)

## Not run: 
## requires vcdExtra package
## display mean residuals for origin and destination
library(vcdExtra)
mosaic(CImodel, ~orig+dest)

## End(Not run)

## non-aggregated residuals
res1 <- meanResiduals(CImodel, ~ educ:orig:dest)
res2 <- residuals(CImodel, type = "pearson")
all.equal(as.numeric(res1), as.numeric(res2))

gnm documentation built on Sept. 16, 2023, 5:06 p.m.