Effect Displays for Linear and Generalized Linear Models

Effect Displays for Linear and Generalized Linear Models [R logo]

Help pages for package `effects' version 1.0-10

all.effects | Functions For Constructing Effect Plots
Arrests | Arrests for Marijuana Possession
as.data.frame.eff | Functions For Constructing Effect Plots
Cowles | Cowles and Davis's Data on Volunteering
effect | Functions For Constructing Effect Plots
plot.eff | Summarizing, Printing, and Plotting Effects
Prestige | Prestige of Canadian Occupations
print.eff | Summarizing, Printing, and Plotting Effects
print.summary.eff | Summarizing, Printing, and Plotting Effects
summary.eff | Summarizing, Printing, and Plotting Effects


Arrests for Marijuana Possession

Description

Data on police treatment of individuals arrested in Toronto for simple possession of small quantities of marijuana. The data are part of a larger data set featured in a series of articles in the Toronto Star newspaper.

Usage

Arrests

Format

A data frame with 5226 observations on the following 8 variables.

released
Whether or not the arrestee was released with a summons; a factor with levels: No; Yes.
colour
The arrestee's race; a factor with levels: Black; White.
year
1997 through 2002; a numeric vector.
age
in years; a numeric vector.
sex
a factor with levels: Female; Male.
employed
a factor with levels: No; Yes.
citizen
a factor with levels: No; Yes.
checks
Number of police data bases (of previous arrests, previous convictions, parole status, etc. – 6 in all) on which the arrestee's name appeared; a numeric vector

Source

Personal communication from Michael Friendly, York University.

[Package effects version 1.0-10 Index]


Cowles and Davis's Data on Volunteering

Description

The Cowles data frame has 1421 rows and 4 columns. These data come from a study of the personality determinants of volunteering for psychological research.

Usage

Cowles

Format

This data frame contains the following columns:

neuroticism
scale from Eysenck personality inventory.
extraversion
scale from Eysenck personality inventory.
sex
a factor with levels: female; male.
volunteer
volunteeing, a factor with levels: no; yes.

Source

Cowles, M. and C. Davis (1987) The subject matter of psychology: Volunteers. British Journal of Social Psychology 26, 97–102.


Functions For Constructing Effect Plots

Description

effect constructs an "effect" object for a term (usually a high-order term) in a linear or generalized linear model, absorbing the lower-order terms marginal to the term in question, and averaging over other terms in the model.

all.effects identifies all of the high-order terms in a model and returns a list of "effect" objects (i.e., an object of type "effect.list").

Usage

effect(term, mod, ...)

## S3 method for class 'lm':
effect(term, mod, xlevels=list(), default.levels=10, se=TRUE,
    confidence.level=.95,
    transformation=list(link=family(mod)$linkfun, inverse=family(mod)$linkinv),
    typical=mean, ...)

all.effects(mod, ...)

## S3 method for class 'eff':
as.data.frame(x, row.names=NULL, optional=TRUE, ...)

Arguments

term | the quoted name of a term, usually, but not necessarily, a high-order term in the model.
mod | an object of class "lm" or "glm".
xlevels | an optional list of values at which to set covariates, with components of the form covariate.name = vector.of.values.
default.levels | number of values for covariates that are not specified explicitly via xlevels; covariate values set by default are evenly spaced between the minimum and maximum values in the data.
se | if TRUE, the default, calculate standard errors and confidence limits for the effects.
confidence.level | level at which to compute confidence limits based on the standard-normal distribution; the default is 0.95.
transformation | a two-element list with elements link and inverse. For a generalized linear model, these are by default the link function and inverse-link (mean) function. For a linear model, these default to NULL. If NULL, the identify function, I, is used; this effect can also be achieved by setting the argument to NULL. The inverse-link may be used to transform effects when they are printed or plotted; the link may be used in positioning axis labels (see below). If the link is not given, an attempt will be made to approximate it from the inverse-link.
typical | a function to be applied to the columns of the model matrix over which the effect is "averaged"; the default is mean.
... | arguments to be passed down.
x | an object of type "effect".
row.names, optional | not used.

Details

Normally, the functions to be used directly are all.effects, to return a list of high-order effects, and the generic plot function to plot the effects. (see plot.eff.list and plot.eff). Plots are drawn using the xyplot function in the lattice package. Effects may also be printed (implicitly or explicitly via print) or summarized (using summary) (see print.eff.list, summary.eff.list, print.eff, and summary.eff).

If asked, the effect function will compute effects for terms that have higher-order relatives in the model, averaging over those terms (which rarely makes sense), or for terms that do not appear in the model but are higher-order relatives of terms that do. For example, for the model Y ~ A*B + A*C + B*C, one could compute the effect corresponding to the absent term A:B:C, which absorbs the constant, the A, B, and C main effects, and the three two-way interactions. In either of these cases, a warning is printed.

In calculating effects, the strategy for `safe' prediction described in Hastie (1992: Sec. 7.3.3) is employed.

Value

effect returns an "eff" object with the following components:

term | the term to which the effect pertains.
formula | the complete model formula.
response | a character string giving the response variable.
variables | a list with information about each predictor, including its name, whether it is a factor, and its levels or values.
fit | a one-column matrix of fitted values, representing the effect on the scale of the linear predictor; this is a ravelled table, representing all combinations of predictor values.
x | a data frame, the columns of which are the predictors, and the rows of which give all combinations of values of the predictors.
model.matrix | the model matrix from which the effect was calculated.
data | a data frame with the data on which the fitted model was based.
discrepancy | the percentage discrepancy for the `safe' predictions of the original fit; should be very close to 0.
se | a vector of standard errors for the effect, on the scale of the linear predictor.
lower, upper | one-column matrices of confidence limits, on the scale of the linear predictor.
confidence.level | corresponding to the confidence limits.
transformation | a two-element list, with element link giving the link function, and element inverse giving the inverse-link (mean) function.

Author(s)

John Fox jfox@mcmaster.ca.

References

Fox, J. (1987) Effect displays for generalized linear models. Sociological Methodology 17, 347–361.

Fox, J. (2003) Effect displays in R for generalised linear models. Journal of Statistical Software 8:15, 1–27, <http://www.jstatsoft.org/counter.php?id=75&url=v08/i15/effect-displays-revised.pdf&ct=1>.

Hastie, T. J. (1992) Generalized additive models. In Chambers, J. M., and Hastie, T. J. (eds.) Statistical Models in S, Wadsworth.

See Also

print.eff, summary.eff, plot.eff, print.summary.eff, print.eff.list, summary.eff.list, plot.eff.list, xyplot

Examples

mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion,
    data=Cowles, family=binomial)
eff.cowles <- all.effects(mod.cowles, xlevels=list(neuroticism=0:24,
    extraversion=seq(0, 24, 6)))
eff.cowles
    ## Not run:
    model: volunteer ~ sex + neuroticism * extraversion

    sex effect
    sex
    female      male
    0.4409441 0.3811941

    neuroticism*extraversion effect
            extraversion
    neuroticism          0         6        12        18        24
            0  0.07801066 0.1871263 0.3851143 0.6301824 0.8225756
            1  0.08636001 0.1963396 0.3870453 0.6200668 0.8083638
            2  0.09551039 0.2058918 0.3889798 0.6098458 0.7932997
            3  0.10551835 0.2157839 0.3909179 0.5995275 0.7773775
           . . .
            23 0.51953129 0.4747277 0.4303273 0.3870199 0.3454282
            24 0.54709527 0.4895731 0.4323256 0.3768303 0.3243880

## End(Not run)
plot(eff.cowles, 'sex', ylab="Prob(Volunteer)")
    ## Not run:
    Loading required package: lattice

## End(Not run)

plot(eff.cowles, 'neuroticism:extraversion', ylab="Prob(Volunteer)",
    ticks=list(at=c(.1,.25,.5,.75,.9)))

plot(eff.cowles, 'neuroticism:extraversion', multiline=TRUE,
    ylab="Prob(Volunteer)")

plot(effect('sex:neuroticism:extraversion', mod.cowles,
    xlevels=list(neuroticism=0:24, extraversion=seq(0, 24, 6))), multiline=TRUE)
## Not run:
    Warning message:
    sex:neuroticism:extraversion does not appear in the model in:
      effect("sex:neuroticism:extraversion", mod.cowles,
      xlevels = list(neuroticism = 0:24,
## End(Not run)

mod.pres <- lm(prestige ~ log(income, 10) + poly(education, 3) + poly(women, 2),
    data=Prestige)
eff.pres <- all.effects(mod.pres, default.levels=50)
plot(eff.pres, ask=FALSE)


Prestige of Canadian Occupations

Description

The Prestige data frame has 102 rows and 6 columns. The observations are occupations.

Usage

Prestige

Format

This data frame contains the following columns:

education
Average education (years) of occupational incumbents, in 1971.
income
Average income (dollars) of incumbents, 1971.
women
Percentage of incumbents who are women, 1971.
prestige
Pineo-Porter prestige score for occupation, from a social survey conducted in the mid-1960s.
census
Canadian Census occupational code.
type
Type of occupation. A factor with levels (note: out of order): bc, Blue Collar; prof, Professional, Managerial, and Technical; wc, White Collar.

Source

Canada (1971) Census of Canada. Vol. 3, Part 6. Statistics Canada [pp. 19-1–19-21].

Personal communication from B. Blishen, W. Carroll, and C. Moore, Departments of Sociology, York University and University of Victoria.

References

Fox, J. (1997) Applied Regression, Linear Models, and Related Methods. Sage.


Summarizing, Printing, and Plotting Effects

summary.eff(effects) R Documentation

Summarizing Printing and Plotting Effects

Description

summary, print, and plot methods for eff and eff.list objects.

Usage

## S3 method for class 'eff':
print(x, type=c("response", "link"), ...)
## S3 method for class 'eff.list':
print(x, ...)
## S3 method for class 'summary.eff':
print(x, ...)
## S3 method for class 'eff':
summary(object, type=c("response", "link"), ...)
## S3 method for class 'eff.list':
summary(object, ...)
## S3 method for class 'eff':
plot(x, x.var=which.max(levels),
    z.var=which.min(levels), multiline=is.null(x$se), rug=TRUE, xlab,
    ylab=x$response, main=paste(effect, "effect plot"),
    colors=palette(), symbols=1:10, lines=1:10, cex=1.5, ylim,
    factor.names=TRUE, type=c("response", "link"), ticks=list(at=NULL, n=5),
    alternating=TRUE, rescale.axis=TRUE,
    row=1, col=1, nrow=1, ncol=1, more=FALSE, ...)
## S3 method for class 'eff.list':
plot(x, selection, ask=TRUE, ...)

Arguments

x | an object of type "effect", "summary.effect", or "effect.list", as appropriate.
object | an object of type "effect" or "effect.list", as appropriate.
type | if "response" (the default), effects are printed or the vertical axis is labelled on the scale of the response variable; if "link", effects are printed or the vertical axis labelled on the scale of the linear predictor.
x.var | the index (number) or quoted name of the covariate or factor to place on the horizontal axis of each panel of the effect plot. The default is the predictor with the largest number of levels or values.
z.var | the index (number) or quoted name of the covariate or factor for which individual lines are to be drawn in each panel of the effect plot. The default is the predictor with the smallest number of levels or values. This argument is only used if multiline = TRUE.
multiline | if TRUE, each panel of the display represents combinations of values of two predictors, with one predictor (corresponding to x.var) on the horzontal axis, and the other (corresponding to z.var) used to define lines in the graph; defaults to TRUE if there are no standard errors in the object being plotted, and FALSE otherwise.
rug | if TRUE, the default, a rug plot is shown giving the marginal distribution of the predictor on the horizontal axis, if this predictor is a covariate.
xlab | the label for the horizontal axis of the effect plot; if missing, the function will use the name of the predictor on the horizontal axis.
ylab | the label for the vertical axis of the effect plot; the default is the response variable for the model from which the effect was computed.
main | the title for the plot, printed at the top; the default title is constructed from the name of the effect.
colors | colors[1] is used to plot effects, colors[2] to plot confidence bands. In a mulitline plot, the successive colors correspond to the levels of the z.var covariate or factor.
symbols, lines | corresponding to the levels of the z.var covariate or factor on a multiline plot. These arguments are used only if multiline = TRUE; in this case a legend is drawn at the top of the display.
cex | character expansion for plotted symbols; default is 1.
ylim | 2-element vector containing the lower and upper limits of the vertical axes; if NULL, the default, then the vertical axes are scaled from the data.
factor.names | a logical value, default TRUE, that controls the inclusion of factor names in conditioning-variable labels.
ticks | a two-item list controlling the placement of tick marks on the vertical axis, with elements at and n. If at=NULL (the default), the program attempts to find `nice' locations for the ticks, and the value of n (default, 5) gives the approximate number of tick marks desired; if at is non-NULL, then the value of n is ignored.
alternating | if TRUE (the default), the tick labels alternate by panels in multi-panel displays from left to right and top to bottom; if FALSE, tick labels appear at the bottom and on the left.
rescale.axis | if TRUE (the default), the tick marks on the vertical axis are labelled on the response scale (e.g., the probability scale for effects computed on the logit scale for a binomial GLM).
row, col, nrow, ncol, more | These arguments are used to graph an effect as part of an array of plots; row, col, nrow, and ncol are used to compose the split argument and more the more argument to print.trellis. Normally these arguments are not set by the user, but by print.effect.list.
selection | the optional index (number) or quoted name of the effect in an effect list to be plotted; if not supplied, a menu of high-order terms is presented or all effects are plotted.
ask | if selection is not supplied and ask is TRUE (the default), a menu of high-order terms is presented; if ask is FALSE, effects for all high-order terms are plotted in an array.
... | arguments to be passed down.

Details

In a generalized linear model, by default, the print and summary methods for effect objects print the computed effects on the scale of the response variable using the inverse of the link function. In a logit model, for example, this means that the effects are expressed on the probability scale.

By default, effects in a GLM are plotted on the scale of the linear predictor, but the vertical axis is labelled on the response scale. This preserves the linear structure of the model while permitting interpretation on what is usually a more familiar scale. This approach may also be used with linear models, for example to display effects on the scale of the response even if the data are analyzed on a transformed scale, such as log or square-root.

Value

The summary method for "eff" objects returns a "summary.eff" object with the following components (those pertaining to confidence limits need not be present):

header | a character string to label the effect.
effect | an array containing the estimated effect.
lower.header | a character string to label the lower confidence limits.
lower | an array containing the lower confidence limits.
upper.header | a character string to label the upper confidence limits.
upper | an array containing the upper confidence limits.

Author(s)

John Fox jfox@mcmaster.ca.

See Also

effect, all.effects, xyplot

Examples

mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion,
    data=Cowles, family=binomial)
eff.cowles <- all.effects(mod.cowles, xlevels=list(neuroticism=0:24,
    extraversion=seq(0, 24, 6)))
eff.cowles
    ## Not run:
    model: volunteer ~ sex + neuroticism * extraversion

    sex effect
    sex
    female      male
    0.4409441 0.3811941

    neuroticism*extraversion effect
            extraversion
    neuroticism          0         6        12        18        24
            0  0.07801066 0.1871263 0.3851143 0.6301824 0.8225756
            1  0.08636001 0.1963396 0.3870453 0.6200668 0.8083638
            2  0.09551039 0.2058918 0.3889798 0.6098458 0.7932997
            3  0.10551835 0.2157839 0.3909179 0.5995275 0.7773775
           . . .
            23 0.51953129 0.4747277 0.4303273 0.3870199 0.3454282
            24 0.54709527 0.4895731 0.4323256 0.3768303 0.3243880

## End(Not run)
plot(eff.cowles, 'sex', ylab="Prob(Volunteer)")
    ## Not run:
    Loading required package: lattice

## End(Not run)

plot(eff.cowles, 'neuroticism:extraversion', ylab="Prob(Volunteer)",
    ticks=list(at=c(.1,.25,.5,.75,.9)))

plot(eff.cowles, 'neuroticism:extraversion', multiline=TRUE,
    ylab="Prob(Volunteer)")

plot(effect('sex:neuroticism:extraversion', mod.cowles,
    xlevels=list(neuroticism=0:24, extraversion=seq(0, 24, 6))), multiline=TRUE)
## Not run:
    Warning message:
    sex:neuroticism:extraversion does not appear in the model in:
      effect("sex:neuroticism:extraversion", mod.cowles,
      xlevels = list(neuroticism = 0:24,
## End(Not run)

mod.pres <- lm(prestige ~ log(income, 10) + poly(education, 3) + poly(women, 2),
    data=Prestige)
eff.pres <- all.effects(mod.pres, default.levels=50)
plot(eff.pres, ask=FALSE)