Description Usage Arguments Details Value Author(s) References Examples
View source: R/LinRegInteractive_functions.R
This method implements proposals for the interpretation of models for binary outcomes made by Hoetker (2007) but works for other types of generalized linear models as well. The method is also suitable for generalized additive models fitted with gam
(package gam and mgcv).
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 'glm'
fxInteractive(model, initial.values = as.list(NULL),
preselect.var = NA, preselect.type = "link", 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.5, 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 = "Generalized Linear Model", label.button = "Snapshot",
label.slider.act = "Variable displayed: ", label.box.type = "Type",
label.types = c("linear predictor", "response", "marginal effect"),
label.box.groups = "Groups",
slider.width = 200, slider.height = 60, button.height = 30,
box.type.height = 100, box.group.character.width = 7,
box.group.line.height = 28, dist.obj.width = 20,
dist.obj.height = 10, ...)
|
model |
Object of class |
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 |
preselect.type |
The type of the initial plot to be displayed. Must be one of the values |
preselect.groups |
Numeric vector with the index of the groups which are displayed in the initial plot. If |
dev.height |
Height of graphic device in cm, default to |
dev.width |
Width of plot area in graphic device in cm, default to |
dev.width.legend |
Width of legend area in graphic device in cm, default to |
dev.pointsize |
Character pointsize of graphic device, default to |
dev.defined |
Graphic device predefined? Default to |
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 |
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 |
main |
Title for the plot, default to |
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 |
ylab |
Label for the y-axis. Name of the selected plot type (see argument |
legend.add |
Should a legend be added to the plot? Default to |
legend.space |
Should the space for the legend be reserved? Default to the value of |
legend.only |
Should just the legend be plotted? Default to |
legend.pos |
Position of the legend as character, see |
legend.cex |
Relative size of legend text. Can be reduced if the model contains many groups. Default to |
legend.width.factor |
Factor by which the width of the legend is increased. Default to |
rug.ticksize |
Length of rugplot tickmarks, dafault to |
rug.col |
Color of rugplot tickmarks, default to |
vline.actual |
Add vertical line at actual position of selected metric covariate? Default to |
pos.hlines |
Positions of the horizontal lines for [1] the plot of the link function, [2] the plot of the response and [3] the plot of marginal effects. |
n.effects |
Number of equally spaced points over the span of the selected metric covariate to calculate the effects for plotting, default to |
autosave.plot |
Directly save the initial plot? Default to |
snapshot.plot |
Save plot when snapshot button is pressed? Default to |
graphics.filename |
Filename (optionally including a path) as character for graphic file. |
graphics.numbering |
If |
graphics.type |
Graphics file type argument, default to |
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 |
xtable.big.mark |
Bigmark character for LaTeX output passed to |
xtable.decimal.mark |
Decimal character for LaTeX output passed to |
xtable.digits |
Number of digits for LaTeX output passed to |
xtable.display |
Display style for LaTeX output passed to |
xtable.booktabs |
Use the LaTeX package |
panel.title |
Title used in the title bar of the GUI-panel of type |
label.button |
Label for the snapshot-button of type |
label.slider.act |
Additional label for the slider of the selected metric covariate of type |
label.box.type |
Title for the radiogroup box of type |
label.types |
Labels for radiogroup buttons ( |
label.box.groups |
Title for the checkbox of type |
slider.width |
Width of each slider in points (default to |
slider.height |
Height of each slider in points (default to |
button.height |
Height of snapshot button in points (default to |
box.type.height |
Height of radiobox for type selection in points (default to |
box.group.character.width |
The width of the boxes is basically a this value times the number of characters in points (default to |
box.group.line.height |
The height of the checkbox is this value times the number of groups in points (default to |
dist.obj.width |
Vertical distance between sliders and boxes and vertical margins in points (default to |
dist.obj.height |
Horizontal distance between panel objects in points (default to |
... |
Other graphical parameters passed to |
The only mandatory argument of the function is a fitted-model object of class glm
. 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 factor
s (ordered or unordered).
Please refer to the documentation of fxInteractive
or the vignette for more details.
No object is returned, please refer to the documentation of fxInteractive
or the vignette for more details.
Martin Meermeyer m.meermeyer@gmail.com
Hoetker, G. (2007). The use of logit and probit models in strategic management research: Critical issues. Strategic Management Journal, 28(4), 331-343.
Kleiber, C., Zeileis, A. (2008). Applied Econometrics with R. New York: Springer.
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 140 141 142 143 144 145 146 147 | ### Model specification ###
data("creditdata")
## Treat ordered factors as unordered factors.
options(contrasts=c("contr.treatment","contr.treatment"))
model.cd <- glm(credit ~ amount + I(amount^2) + age + duration*teleph + housing,
family=binomial, data=creditdata)
## Not run:
### Basic usage ###
## RStudio users may need to change the graphic device, see details.
options(device = "x11")
## Using defaults
fxInteractive(model.cd)
## Switch text output to LaTeX
fxInteractive(model.cd, latex2console = TRUE)
## Continental European number format in LaTeX output
fxInteractive(model.cd, latex2console = TRUE, decimal.mark = ",", big.mark = ".")
## Save plot as PDF to current working directory when 'Snapshot' is clicked
fxInteractive(model.cd, snapshot.plot = TRUE)
## Change color scheme and line types
fxInteractive(model.cd, col = rep(c(2,4),each = 3), lty = c(1,3,5))
## Change separation characters
fxInteractive(model.cd, factor.sep = "-", level.sep = ">")
## Suppress legend
fxInteractive(model.cd, legend.add = FALSE)
## Suppress rug plot
fxInteractive(model.cd, rug.ticksize = NA)
## Set initial values of metric covatiates
fxInteractive(model.cd, initial.values = list(amount=5000, age=30, duration=24))
## Preselect covariate, plot type and groups
fxInteractive(model.cd, preselect.var = "duration", preselect.type = "response",
preselect.groups = c(2,5))
## Preselect covariate and plot type and change axis annotations
fxInteractive(model.cd, preselect.var = "duration", preselect.type = "response",
xlab = "duration (months)", ylab = "probability of credit default")
### Visualization of statistical concepts ###
## Nonparametric effect
require("splines")
model.cd.bs <- glm(credit ~ bs(amount) + age + duration*teleph + housing,
family = binomial, data = creditdata)
fxInteractive(model.cd.bs, preselect.var = "amount")
## Generalized additive model
require("mgcv")
model.cd.mgcv <- gam(credit ~ s(amount) + age + duration*teleph + housing,
family = binomial, data = creditdata)
fxInteractive(model.cd.mgcv)
## Interaction effect (directly)
fxInteractive(model.cd, preselect.var = "duration")
## Interaction effect (indirectly)
fxInteractive(model.cd, preselect.var = "age")
# manipulate slider for 'duration'
## Quasi-complete separation
# example from Kleiber, Zeileis (2008), p. 130ff
require("AER")
data("MurderRates")
model.mr <- glm(I(executions > 0) ~ time + income + noncauc + lfp + southern,
family = binomial, data = MurderRates)
fxInteractive(model.mr, preselect.var = "income", preselect.type = "response")
### Additional examples ###
## Customize device for printing
fxInteractive(model.cd,
dev.width = 6,
dev.width.legend = 4,
dev.height = 6,
dev.pointsize = 6,
col = c("darkred","red","salmon","darkblue","blue","lightblue"),
legend.width.factor = 1.1,
vline.actual = FALSE,
snapshot.plot = TRUE,
graphics.filename = "creddefault-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.cd,
initial.values = list(amount=5000, duration=24, age=30),
preselect.var = "duration",
preselect.type = "link",
autosave.plot = TRUE,
graphics.filename = "fig-creddefault-duration-link",
legend.width.factor = 1.05)
## Modifications for models with many groups
# Increase space for legend and squeeze panel controls
model.cd.moregroups <- glm(credit ~ amount + I(amount^2) + age
+ duration*teleph + housing + job, family = binomial, data = creditdata)
fxInteractive(model.cd.moregroups,
dev.width.legend = 9,
legend.cex = 1,
box.type.height = 90,
box.group.character.width = 6,
box.group.line.height = 25,
dist.obj.height = 2)
# Squeeze legend and panel controls
model.cd.manygroups <- glm(credit ~ amount + I(amount^2) + age
+ duration*teleph + housing + intuse, family = binomial, data = creditdata)
fxInteractive(model.cd.manygroups,
dev.width.legend = 6,
legend.cex = 0.54,
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.cd.manygroups <- glm(credit ~ amount + I(amount^2) + age
+ duration*teleph + housing + intuse, family = binomial, data = creditdata)
index.groups <- c(1,11,21,31,41,51)
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.cd.manygroups,
preselect.var = "amount",
preselect.groups = index.groups,
col = vec.col,
lty = vec.lty)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.