car (version 1.0-0)

effect: Functions For Constructing Effect Plots

Description

effect constructs an "effect" object for 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, xlevels=list(), default.levels=10, se=TRUE, 
    confidence.level=.95, transformation=family(mod)$linkinv, typical=mean)
    
all.effects(mod, ...)

## S3 method for class 'effect':
print(x, ...)## S3 method for class 'effect':
summary(object, ...)## S3 method for class 'effect':
plot(x, x.var=which.max(levels), 
    z.var=which.min(levels), multiline=is.null(x$se), rug=TRUE,
    ylab=x$response, colors=palette(), symbols=1:10, lines=1:10,
    factor.names=TRUE, ...)
## S3 method for class 'effect.list':
plot(x, selection, ...)
as.data.frame.effect(x, row.names=NULL, optional=TRUE)

Arguments

term
the quoted name of 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 function to be applied to the calculated effects and confidence limits; the default is the inverse-link function for the model, which is a the identity function for a linear model (i.e., no transformation.)
typical
a function to be applied to the columns of the model matrix over which the effect is "averaged"; the default is mean.
x, object
an object of type "effect" or "effect.list", as appropriate.
x.var
the index (number) 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.
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 defi
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.
z.var
the index (number) 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 multi
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.
factor.names
a logical value, default TRUE, that controls the inclusion of factor names in conditioning-variable labels.
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.
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.
...
arguments to be passed down.
row.names, optional
not used.

Value

  • effect returns a list with the following components:
  • termthe high-order term to which the effect pertains.
  • formulathe complete model formula.
  • responsea character string giving the response variable.
  • variablesa list with information about each predictor, including its name, whether it is a factor, and its levels or values.
  • effecta one-column matrix of fitted values, representing the effect on the original scale; this is a ravelled table, representing all combinations of predictor values.
  • fita one-column matrix of fitted values, representing the effect on the transformed scale.
  • xa data frame, the columns of which are the predictors, and the rows of which give all combinations of values of the predictors.
  • model.matrixthe model matrix from which the effect was calculated.
  • data
  • {a data frame with the data on which the fitted model was based.}
  • discrepancythe percentage discrepancy for the 'safe' predictions of the original fit; should be very close to 0.
  • sea vector or standard errors for the effect, on the original scale.
  • lower, upperone-column matrices of confidence limits, on the transformed scale.
  • confidence.levelcorresponding to the confidence limits.

Details

Normally, the functions to be used directly are all.effects, to return a list of effects, and the generic plot function to plot the effects. 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). In calculating effects, the strategy for "safe" prediction described in Hastie (1992: Sec. 7.3.3) is employed.

References

Fox, J. (1987) Effect displays for generalized linear models. Sociological Methodology 17, 347--361. Hastie, T. J. (1992) Generalized additive models. In Chambers, J. M., and Hastie, T. J. (eds.) Statistical Models in S, Wadsworth.

See Also

xyplot

Examples

Run this code
data(Cowles)
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
    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
plot(eff.cowles, 'sex', ylab="Prob(Volunteer)")
    Loading required package: lattice 
    Loading required package: grid
plot(eff.cowles, 'neuroticism:extraversion', ylab="Prob(Volunteer)")
plot(eff.cowles, 'neuroticism:extraversion', multiline=TRUE, 
    ylab="Prob(Volunteer)")
    
plot(effect('sex:neuroticism:extraversion',
    glm(volunteer ~ sex*neuroticism*extraversion, data=Cowles, family=binomial),
    xlevels=list(neuroticism=0:24, extraversion=seq(0, 24, 6))), multiline=TRUE)
    
data(Prestige)
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)

Run the code above in your browser using DataLab