Interactive Interpretation of Multiple Linear Regression Models

Description

This method facilitates the interpretation of multiple linear regression models and is a byproduct of the method fxInteractive.glm for generalized linear models.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## S3 method for class 'lm'
 fxInteractive(model, initial.values = as.list(NULL),
    preselect.var = NA, preselect.type = "effect", preselect.groups = NULL,
    dev.height = 18, dev.width = 18, dev.width.legend = 8, dev.pointsize = 10,
    dev.defined = FALSE, ylim = NA, col = NA, lty = 1, lwd = 1, 
    main = NA, main.line = 1.5, xlab = NA, ylab = NA, 
    legend.add = TRUE, legend.space = legend.add, legend.only = FALSE,
    legend.pos = "center", legend.cex = 1, legend.width.factor = 1, 
    rug.ticksize = 0.02, rug.col = "black", vline.actual = TRUE, 
    pos.hlines = c(0, 0), n.effects = 100, 
    autosave.plot = FALSE, snapshot.plot = FALSE, 
    graphics.filename = "LinRegIntPlot", graphics.numbering = !autosave.plot,
    graphics.type = "pdf", factor.sep = "|", level.sep = ".", 
    latex2console = FALSE, xtable.big.mark = ".", xtable.decimal.mark = ",", 
    xtable.digits = NULL, xtable.display = NULL, xtable.booktabs = FALSE,                 
    panel.title = "Linear Model", label.button = "Snapshot",
    label.slider.act = "Variable displayed: ", label.box.type = "Type",
    label.types = c("effect", "marginal effect"),
    label.box.groups = "Groups", 
    slider.width = 200, slider.height = 60, button.height = 30,
    box.type.height = 70, box.group.character.width = 7, 
    box.group.line.height = 28, dist.obj.width = 20,
    dist.obj.height = 10, ...)

Arguments

model

Object of class lm (mandatory).

initial.values

Initial values for the metric covariates in a named list, default to the means. See section 4 of the vignette and examples below.

preselect.var

Name of continuous variable to be displayed as character or NA (default) for menu selection.

preselect.type

The type of the initial plot to be displayed. Must be one of the values "effect" (default) or "marginal".

preselect.groups

Numeric vector with the index of the groups which are displayed in the initial plot. If NULL (the default) all groups are displayed.

dev.height

Height of graphic device in cm, default to 18.

dev.width

Width of plot area in graphic device in cm, default to 18.

dev.width.legend

Width of legend area in graphic device in cm, default to 8.

dev.pointsize

Character pointsize of graphic device, default to 10.

dev.defined

Graphic device predefined? Default to FALSE, see section 6.3 of the vignette for usage.

ylim

With a numeric vector of length 2 the plot limits in y-direction can be set. If NA (the default) these are determined automatically.

col

Vector of color specifications to represent different groups. Passed to the line commands and the legend. Actual palette and consecutive sequence if NA (default).

lty

Vector of line type specifications to represent different groups. Passed to the line commands and the legend, default to solid lines.

lwd

Vector of line width specifications to represent different groups. Passed to the line commands and the legend, default to 1.

main

Title for the plot, default to NA.

main.line

Height for plot title in lines which is passed to title(), default to 1.5.

xlab

Label for the x-axis. Name of the selected covariate, if NA (the default).

ylab

Label for the y-axis. Name of the selected plot type (see argument label.types), if NA (the default).

legend.add

Should a legend be added to the plot? Default to TRUE.

legend.space

Should the space for the legend be reserved? Default to the value of legend.add. Setting legend.add to FALSE and legend.space to TRUE plots white space instead of the legend. This can be useful when different plots are arranged in a document to ensure exact alignments and sizes, see section 6.2 of the vignette for details.

legend.only

Should just the legend be plotted? Default to FALSE. A plot with the legend alone can be useful when different plots are arranged in a document, see section 6.2 of the vignette for details.

legend.pos

Position of the legend as character, see legend for details. Default to "center".

legend.cex

Relative size of legend text. Can be reduced if the model contains many groups. Default to 1.

legend.width.factor

Factor by which the width of the legend is increased. Default to 1. Increasing this can solve the problem that the legend annotations do not fit in the surrounding box when the plots are saved as PDF or EPS files, see section 5 of the vignette for details.

rug.ticksize

Length of rugplot tickmarks, dafault to 0.02. Set to 0 or NA, if no rugplot should be drawn. For many observations the rug considerably slows down the rebuild of the plot.

rug.col

Color of rugplot tickmarks, default to "black".

vline.actual

Add vertical line at actual position of selected metric covariate? Default to TRUE.

pos.hlines

Positions of the horizontal lines for [1] the plot of the effects and [2] the plot of marginal effects. NA for no lines, default to c(0,0).

n.effects

Number of equally spaced points over the span of the selected metric covariate to calculate the effects for plotting, default to 100. Increase, if lines are not smooth.

autosave.plot

Directly save the initial plot? Default to FALSE. If set to TRUE the GUI-panel is immediately closed after initialization.

snapshot.plot

Save plot when snapshot button is pressed? Default to FALSE, see section 5 of the vignette for details.

graphics.filename

Filename (optionally including a path) as character for graphic file.

graphics.numbering

If TRUE (the default) a 3 digits number is automatically appended to the filenname to avoid that existing graphic files are overwritten.

graphics.type

Graphics file type argument, default to "pdf". On Windows systems all file types accepted by savePlot work. Under non Windows systems allowed values are "pdf", "eps", "png", "jpeg", "jpg", "tiff" and "bmp".

factor.sep

Character separating the factor-factor level combinations in the group names (default to "|").

level.sep

Character separating the factor name and the corresponding factor levels in the group names (default to ".").

latex2console

Should the textoutput triggered by the snapshot button be printed as LaTeX-code? Default to FALSE.

xtable.big.mark

Bigmark character for LaTeX output passed to print.xtable, default to ",".

xtable.decimal.mark

Decimal character for LaTeX output passed to print.xtable, default to ".".

xtable.digits

Number of digits for LaTeX output passed to xtable, default to NULL.

xtable.display

Display style for LaTeX output passed to xtable, default to NULL.

xtable.booktabs

Use the LaTeX package booktabs for horizontal lines in LaTeX tables, default to FALSE. Passed to print.xtable.

panel.title

Title used in the title bar of the GUI-panel of type character.

label.button

Label for the snapshot-button of type character.

label.slider.act

Additional label for the slider of the selected metric covariate of type character.

label.box.type

Title for the radiogroup box of type character.

label.types

Labels for radiogroup buttons (character vector of length 2). By default these are also used as corresponding annotations for the y-axis.

label.box.groups

Title for the checkbox of type character.

slider.width

Width of each slider in points (default to 200).

slider.height

Height of each slider in points (default to 60).

button.height

Height of snapshot button in points (default to 30).

box.type.height

Height of radiobox for type selection in points (default to 90).

box.group.character.width

The width of the boxes is basically a this value times the number of characters in points (default to 7).

box.group.line.height

The height of the checkbox is this value times the number of groups in points (default to 28).

dist.obj.width

Vertical distance between sliders and boxes and vertical margins in points (default to 20).

dist.obj.height

Horizontal distance between panel objects in points (default to 10).

...

Other graphical parameters passed to par.

Details

The only mandatory argument of the function is a fitted-model object of class lm. For this object the following prerequisites must be met:

  • The model must contain at least one metric covariate.

  • The model must be specified with the formula interface and the data frame containing the variables must be passed with the data argument.

  • The categorical variables must be factors (ordered or unordered).

Please refer to the documentation of fxInteractive or the vignette for more details.

Value

No object is returned, please refer to the documentation of fxInteractive or the vignette for more details.

Author(s)

Martin Meermeyer m.meermeyer@gmail.com

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
### Model specification ###
data("munichrent03")

## Treat ordered factors as unordered factors.
options(contrasts=c("contr.treatment","contr.treatment"))

model.rent <- lm(rent ~ yearc + I(yearc^2) + rooms + area*location + upkitchen,
    data=munichrent03)

## Not run: 

### Basic usage ###

## RStudio users may need to change the graphic device, see details.
options(device = "x11")

## Using defaults
fxInteractive(model.rent)

## Switch text output to LaTeX
fxInteractive(model.rent, latex2console = TRUE)

## Continental European number format in LaTeX output
fxInteractive(model.rent, latex2console = TRUE, xtable.decimal.mark = ",", xtable.big.mark = ".")

## Save plot as PDF to current working directory when 'Snapshot' is clicked
fxInteractive(model.rent, snapshot.plot = TRUE)

## Change color scheme and line types
fxInteractive(model.rent, col = rep(c(1,2,4),each = 2), lty = c(1,5))

## Change separation characters
fxInteractive(model.rent, factor.sep = "-", level.sep = ">")

## Suppress legend
fxInteractive(model.rent, legend.add = FALSE)

## Suppress rug plot
fxInteractive(model.rent, rug.ticksize = NA)

## Set initial values of metric covatiates 
fxInteractive(model.rent, initial.values = list(yearc=1990, rooms=3, area=80))

## Preselect covariate, plot type and groups
fxInteractive(model.rent, preselect.var = "yearc", preselect.type = "marginal",
    preselect.groups = c(2,4,6))

## Preselect covariate and plot type and change axis annotations
fxInteractive(model.rent, preselect.var = "yearc", preselect.type = "effect", 
    xlab = "year of construction", ylab = "net rent (EUR)")


### Visualization of statistical concepts ###

## Nonlinear effect 
fxInteractive(model.rent, preselect.var = "yearc")

## Nonparametric effect 
require("splines")
model.rent.bs <- lm(rent ~ bs(yearc) + rooms + area*location + upkitchen,
    data = munichrent03)
model.rent.bs$data <- munichrent03
fxInteractive(model.rent.bs, preselect.var = "yearc")
# Note that the data must be attached to the fitted-model object
# afterwards, see section 7 of the vignette for details.

## Interaction effect (directly)
fxInteractive(model.rent, preselect.var = "area")

## Interaction effect (indirectly)
fxInteractive(model.rent, preselect.var = "yearc")
# manipulate slider for 'area'


### Additional examples ###

## Customize device for printing
fxInteractive(model.rent, 
    dev.width = 6,
    dev.width.legend = 4,
    dev.height = 6,
    dev.pointsize = 6,
    col = c("red","darkred","blue","darkblue","green","darkgreen"),
    legend.width.factor = 1.1,
    vline.actual = FALSE,
    snapshot.plot = TRUE,
    graphics.filename = "munichrent-termplot",
    mar = c(2.5,2.5,1,1)+0.1,
    mgp = c(1.5,0.5,0),
    tcl =  -0.3)

## Save predefined plot automatically
fxInteractive(model.rent,
    initial.values = list(yearc=1990, rooms=3, area=80), 
    preselect.var = "area",
    preselect.type = "marginal",
    autosave.plot = TRUE,
    graphics.filename = "fig-rent-area-marg",
    legend.width.factor = 1.05)

## Modifications for models with many groups
# Increase space for legend and squeeze panel controls
model.rent.moregroups <- lm(rent ~ yearc + I(yearc^2) + rooms + area*location 
    + upkitchen + bathtile, data = munichrent03)
fxInteractive(model.rent.moregroups, 
    dev.width.legend = 9,
    legend.cex = 1,
    box.type.height = 65,
    box.group.character.width = 6,
    box.group.line.height = 28,
    dist.obj.height = 2)

# Squeeze legend and  panel controls
model.rent.manygroups <- lm(rent ~ yearc + I(yearc^2) + rooms + area 
    + district + upkitchen, data = munichrent03)
fxInteractive(model.rent.manygroups, 
    dev.width.legend = 6,
    legend.cex = 0.65,
    box.type.height = 90,
    box.group.character.width = 6,
    box.group.line.height = 25,
    dist.obj.height=2)
# Note that checkbox for groups grows beyond screen

# Preselect groups and specifiy color and line types directly
model.rent.manygroups <- lm(rent ~ yearc + I(yearc^2) + rooms + area 
    + district + upkitchen, data = munichrent03)
index.groups <- c(3,4,17,18,49,50)
vec.col <- NULL
vec.col[index.groups] <- c(1:6)
vec.lty <- NULL
vec.lty[index.groups] <- rep(c(1,2), each = 3)
fxInteractive(model.rent.manygroups,
preselect.var = "area",
preselect.groups = index.groups,
col = vec.col,
lty = vec.lty)

## End(Not run)