phia (version 0.2-1)

interactionMeans: Calculate and Plot Adjusted Means for Interactions

Description

Creates a data frame with the adjusted means of a fitted model or the slopes associated to its covariates, plus the standard error of those values, for all the interactions of given factors, including intra-subjects factors in multivariate linear models with intra-subjects designs. These interactions may be plotted by pairs of factors.

Usage

interactionMeans(model, factors=names(xlevels), slope=NULL, ...)
# S3 method for interactionMeans
plot(x, atx=attr(x,"factors"), traces=atx,
  multiple=TRUE, y.equal=FALSE, legend=TRUE, legend.margin=0.2,
  cex.legend=1, abbrev.levels=FALSE, type="b", pch=0:6, errorbar, ...)

Value

interactionMeans returns an object of class "interactionMeans", that contains a data frame with the factor interactions and the means or slopes of the model adjusted to them, and some attributes used for plotting.

Arguments

model

fitted model. Currently supported classes include "lm", "glm", "mlm", "lme", and "mer" or "merMod" (excluding models fitted by nlmer).

factors

character vector with the names of interacting factors. All the factors of the model are used by default; use NULL for calculating the overall mean.

slope

character vector with the names of the interacting covariates associated to the slope that will be represented; if it is NULL (the default value), the function will calculate adjusted mean values.

x

an object created by the function interactionMeans.

atx

character vector with the names of factors that will be represented in the horizontal axis of the plots. All the factors represented in x are used by default.

traces

character vector with the names of the factors that will be represented as different traces in a plot (the same as atx by default).

multiple

logical indicating if the plots shall be combined in multi-panel figures.

y.equal

logical indicating if all the plots should have the same range in the y-axis (all plots would be expanded to the maximum range).

legend

logical indicating if legends should be drawn in case of plots with multiple traces.

legend.margin

fraction of the combined plotting area that shall be reserved for drawing the legends, if they are required.

cex.legend

character expansion for the legend, relative to the value of par("cex").

abbrev.levels

A logical indicating whether the factor levels are to be abbreviated in the plot, or an integer that specifies the minimum length of such abbreviations. See abbreviate.

type, pch

type of plot and point characters, as used by matplot.

errorbar

definition of the error bars in the plots (see Details).

...

further arguments passed to testFactors or matplot.

Author

Helios De Rosario-Martinez, helios.derosario@gmail.com

Details

This function calculates the adjusted values of the model and their standard errors for interactions between factors, at fixed values of covariates, if they exist. The main or crossed effect of covariates is represented by their “slope”, i.e. the variation rate of the response with respect to the product of the specified covariates. The default value of the covariates (and of the offset, if any) is their average in the model data frame, and it can be changed by the arguments covariates or offset, passed down to testFactors. Note that in generalized linear models, standard errors and slopes are referred to the link function, not to the mean (see testFactors for details, and how to force the calculation of the link function instead of the response for adjusted means).

In multivariate linear models, the adjusted means or slopes are calculated separately for each column by default, but it is possible to define an intra-subjects design of factors across columns, and put all columns in one. This may be defined by the argument idata passed down to testFactors (see Anova or linearHypothesis in package car for further details). If such transformation is done, it is also possible to include the factors of the intra-subjects design in factors, for calculating their main effects or interactions.

The generic plot function creates matrices of interaction plots, with the main effects of each factor represented in the diagonal, and the interactions between each pair of factors in the rest of panels. For multivariate models without intra-subjects design, a new device for each variable will be created. By default it also prints error bars around the means, plus/minus their standard errors. The size of the error bars can be adjusted by the argument errorbar. Currently supported definitions are strings with the pattern ciXX, where XX is a number between 01 and 99, standing for the $XX

The adjusted means and error bars of generalized models (fitted with glm or glmer) are plotted on the scale of the link function, although the y-axis is labelled on the scale of the response (unless the link function had been forced in the calculation of the means).

If the interactions involve many factors, it may be convenient to plot each panel in a different device (with multiple=FALSE), or select a subset of factors to be plotted with the arguments atx and traces. A panel will be plotted for each pair of factors defined by crossing these arguments; if the crossed factors are the same one, that panel will show its main effect.

See Also

testFactors, interactionMeans.

Examples

Run this code
# Interaction between two factors
# See ?Adler for a description of the data set

mod.adler <- lm(rating ~ instruction * expectation, data=Adler)
(means.adler <- interactionMeans(mod.adler))
plot(means.adler, abbrev.levels=TRUE)


# Effect of factors on the slopes of the model
# See ?SLID for a description of the data set

SLID$logwages <- log2(SLID$wages)
mod.slid <- lm(logwages ~ education + age * (sex  * language), data=SLID)
(slopes.slid <- interactionMeans(mod.slid, slope="age"))
plot(slopes.slid)


# Include intra-subjects factors
# See ?OBrienKaiser for a description of the data set

mod.ok <- lm(cbind(pre.1, pre.2, pre.3, pre.4, pre.5, 
                     post.1, post.2, post.3, post.4, post.5, 
                     fup.1, fup.2, fup.3, fup.4, fup.5) ~  treatment*gender, 
                data=OBrienKaiser)

# Intra-subjects data:
phase <- factor(rep(c("pretest", "posttest", "followup"), each=5))
hour <- ordered(rep(1:5, 3))
idata <- data.frame(phase, hour)

# Calculate all interactions, but plot only the interactions between
# hour (represented in the x-axis) and the other factors (in traces)

means.ok <- interactionMeans(mod.ok, idata=idata)
plot(means.ok, atx="hour", traces=c("gender","treatment","phase"))

Run the code above in your browser using DataCamp Workspace