plot_coef: Plotting Coefficients

Description Usage Arguments Value References Examples

View source: R/plot_coef.R

Description

Drawing the coefficient plots from either model result or coefficient table.

Usage

 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
plot_coef(
  m,
  m.names = NULL,
  order.variable = "original",
  odds = FALSE,
  vcov.est = NULL,
  robust.type = "HC1",
  cluster.var = NULL,
  boot.sims = 300,
  boot.seed = 578,
  boot.ncores = 1,
  show.ci = TRUE,
  level.ci = 0.95,
  drop.intercept = FALSE,
  drop.intercept.names = "(Intercept)",
  drop.variable.names = NULL,
  point.shape = 16,
  point.size = 1.5,
  point.color = "black",
  ci.linetype = 1,
  ci.size = 0.5,
  ci.height = 0.2,
  overlap.names = "m.names",
  overlap.gapwidth = 0.5,
  overlap.shape.index = NULL,
  overlap.color.index = NULL,
  overlap.linetype.index = NULL,
  overlap.legend.position = "bottom",
  category.names = NULL,
  category.names.location = "left",
  category.names.angle = 0,
  facet.names = NULL,
  facet.x.scale = "fixed",
  title = NULL,
  y.title = NULL,
  custom.variable.names = NULL,
  custom.x.title = NULL,
  custom.x.lim = NULL,
  custom.x.breaks = NULL,
  custom.themes = NULL,
  footnote.gof = FALSE,
  footnote.gof.nobs = "right",
  footnote.gof.extracts = c("AIC", "Adj. R$^2$", "Log Likelihood"),
  footnote.gof.reorder = NULL,
  footnote.gof.linebreak = TRUE,
  custom.footnote = NULL,
  footnote.caption = FALSE,
  footnote.fontsize = NULL,
  footnote.fontcol = "black",
  footnote.align = "right",
  footnote.distance.from.side = 0.05,
  footnote.distance.from.bottom = 0.75,
  footnote.bottom.expand.rate = NULL,
  show.plot = TRUE,
  flip.plot = FALSE,
  ...
)

Arguments

m

Single or the list of model object or the matrix or data.frame object. If it's a list all elements of the list should be the same class. If model object, coef and coefci must be applicable to the object. If matrix or data.frame, then it should include following three columns and row names. Each row should correspond to each variable.

  • Row Names: Name of Variables

  • First Column: Coefficient

  • Second Column: Lower limit of confidence interval

  • Third Column: Upper limit of confidence interval

m.names

The set of names that identifies each element in m. Considered if m is a list of models. The length of the vector must correspond with the length of m. If NULL (default), each element i is temporarily named as Model i.

order.variable

Order of coefficients in the plot(character/numeric vector). "original" (default) preserves the original order of the variables. "cfdescend" plots by the descending order of coefficient values. "cfascend" plots by the ascending order of coefficient values. Alternatively, you can also specify the order of variables by numeric vector (applied after drop.intercept and drop.variable.names are applied, thus you don't need the names for dropped variables).

odds

Use odds ratio instead of coefficient in the output (boulean). The default is FALSE. If TRUE, the exponent of the coefficients will be plotted.

vcov.est

Single or a list of alternative variance-covariance matrix. Each element must be one of raw variance-covariance matrix, "robust", "cluster", "boot", or NULL (default). If "robust", robust standard error (also see robust.type). If "cluster", cluster robust standard error (also see cluster.var). if "boot", bootstrapped standard error calculated by Boot function is used. Ignored if NULL (default) or the m is not model object. Must have the same length as m if it is a list.

robust.type

The type of leverage adjustment passed to vcovHC (applied only when vcov.est=="robust").

cluster.var

Single or a list of vector, matrix, or data.frame of cluster variables, where each column is a separate variable. Alternatively, a formula specifying the cluster variables to be used (see details in cluster.vcov. Applied only when vcov.est=="cluster".)

boot.sims

Number of iterations if bootstrap is used.

boot.seed

Random number seed if bootstrap method is used.

boot.ncores

Number of cores to parallelize bootstrap. The default is 1. Use "auto" to automatically detect number of cores in computer.

show.ci

Show confidence interval (boulean). The default is TRUE.

level.ci

The level used for confidence interval (numeric: 0-1). The default is 0.95.

drop.intercept

Drop the intercept from the plot (boulean). If FALSE (default), intercept included in the plot.

drop.intercept.names

The name(s) of intercept (character/character vector). Needed if drop.intercept is TRUE. This is used to identify and eliminate intercept variables from the output. Default value is "(Intercept)".

drop.variable.names

The name(s) of additional variables to drop (character/character vector) from the ouput. The default is NULL.

point.shape

Shape of the point outputs (numeric/character). The default is 16 (filled circle).

point.size

Size of point outputs (numeric). The default is 1.5.

point.color

Color of point (and ci) outputs (numeric). The default is "black".

ci.linetype

The line type of confidence interval outputs (numeric). The default is 1.

ci.size

The line width of confidence interval outputs (numeric). The default is 0.5.

ci.height

The height of the vertical line added to the edge of confidence interval outputs. The default is 0.2.

overlap.names

Model labels that controls overlapping (applied only when there are two or more ms). Default is "m.names" (use values specified in m.names). You can alternatively give a character vector of the same length as m.

overlap.gapwidth

The gap between overlapped ouputs (numeric). The default value is 0.5.

overlap.shape.index

The index of shapes for overlapped point ouputs. Must be in the same length as the list m. The first element of the vector is the shape for m, then from the second element, the order must correspond with the order in the list m. If NULL, point.shape is applied to all classes.

overlap.color.index

The index of colors for overlapped point ouputs. Must be in the same length as the list m. The first element of the vector is the shape for m, then from the second element, the order must correspond with the order in the list m. If NULL, point.shape is applied to all classes.

overlap.linetype.index

The index of line types for overlapped confidence interval ouputs. Must be in the same length as the list m. The first element of the vector is the shape for m, then from the second element, the order must correspond with the order in the list m. If NULL, the number corresponding with the order is assigned to each class.

overlap.legend.position

The position of the legend for overlapping classess. See legend.position in ggplot theme for possible values. The default is "bottom".

category.names

The categories of variables (factor). If not NULL, the output provides the separate panels for variables in each category. The length of the vector must much with the number of variables in m (applied after drop.intercept and drop.variable.names are applied, thus you don't need the names for dropped variables).

category.names.location

The location of category names. Either "left" or "right". The default is "left".

category.names.angle

The angle of category names (numeric). The default is 0 (horizontal).

facet.names

If not NULL, facet models by this identifier. Use "m.names" to facet each model separately (overlap.names is forced to NULL). Alternatively, assign character vector of the same length as m (number of models).

facet.x.scale

If "fixed" (default), all facetted models have same x axis limits. If "free" the scale is adjusted by each facetted panel.

title

Plot title (character). The default is to include no title.

y.title

Y axis title (character). The default is to include no axis title.

custom.variable.names

Set of alternative variable names in the output (character vector). The default is NULL. This is applied AFTER drop.intercept and drop.variable.names are applied, thus you don't need the names for dropped variables.

custom.x.title

Custom name for the X axis title (character). The default is NULL.

custom.x.lim

Custom limit for the X axis. The default is NULL.

custom.x.breaks

Custom breaks for the X axis. The default is NULL.

custom.themes

ggplot themes that overrides the default theme. The default is NULL.

footnote.gof

Include GOF measures in the footnote (boulean). The default is FALSE. If TRUE, footnote with GOF measures are added to the plot by extract_gofchr and plot_footnote function, and the function exports gtable object. Note that gtable object is less customizable than ggplot object.

footnote.gof.nobs

The position of "number of observation (N)" to be printed. The default is "right" (the end of the sentence). "left" (the start of the sentence) and "none" (do not print number of observation) are also available.

footnote.gof.extracts

GOF measures to be inluded if footnote.gof == TRUE. See extract_gofchr documentation for more details.

footnote.gof.reorder

The numeric vector specifying the alternative order of GOF (e.g., if there are three GOFs, and you want to flip the order of first and second GOF, then input c(2,1,3)). The length of the vector must correspond with the number of exported GOFs (it may not be the same as the length of footnote.gof.extracts). If NULL (default), the original order is used. The order does not include the number of observation (if exported).

footnote.gof.linebreak

Include linebreak between models (boulean). If TRUE (default), linebreak will be included between models. If FALSE, the ouput will be single line. Only applicable when there are more than one model.

custom.footnote

Custom footnote (character). The default is NULL. If assigned, footnote are added to the plot by plot_footnote function, and the function exports gtable object. Note that gtable object is less customizable than ggplot object. If it is also the case that footnote.gof == TRUE, custom footnote will be added as the new line after the GOF footnote.

footnote.caption

Use caption option in ggplot2 to add footnote. If TRUE, all footnote options below are ignored.

footnote.fontsize

The size of font. If NULL (default), the size is set to the the font size in text setting of ggplot theme - 1.

footnote.fontcol

The color of the font. The default is "black".

footnote.align

The alignment of the footnote text. Either "right" or "left".

footnote.distance.from.side

The horizontal distance of notes from the edge of graph space by the proportion of graph width (numeric: 0-1). The default is 0.05. The distance is measured from the side specified in align.

footnote.distance.from.bottom

The vertical distance of notes from the bottom of graph bottom by the proportion of bottom graph margin height (numeric: 0-1). The default is 0.75.

footnote.bottom.expand.rate

The expansion rate of the bottom margin of the graph to incorporate footnote (numeric). The value of 1 indicates no expansion. If NULL (default), it is set to the number of lines in the footnote + 1.

show.plot

Print the plot at the end of function (boulean). The default is TRUE.

flip.plot

Flip the x and y axis of the plot. The default is FALSE. If TRUE, the variable names will be printed as columns and coefficients will be printed as rows. The original design does not intended for flipping the axis, thus flipping it may cause problems in the layout.

...

Additional arguments passed to vcovHC, cluster.vcov, or Boot depending on the value of vcov.est.

Value

ggplot object without footnote (axis and theme settings can be added later). If footnote is added, then gtable object is the output. It is impossible to add ggplot elements to the gtable object. gtable plot can be viewed by using either grid.draw() or plot() function.

References

http://www.surefoss.org/dataanalysis/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/ is where my initial idea come from.

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
## Load Data
library(pscl)
data(vote92)

## Recode Variables
vote92$voteBush <- as.numeric(
  factor(vote92$vote,levels=c("Clinton","Bush")))*1 - 1
vote92$bushdis <- sqrt(vote92$bushdis)
vote92$clintondis <- sqrt(vote92$clintondis)

## Estimate Logistic Regression
fm <- formula(voteBush ~ dem + rep +
                clintondis + bushdis +
                persfinance + natlecon)
m <- glm(fm, data = vote92,
         family = binomial("logit"))

## Basic Graph
plot_coef(m)

## Change X-axis to Odds Ratio
plot_coef(m, odds = TRUE)

## Export the Coefficients Table First, and import directly
cfci <- data.frame(cbind(coef(m), confint(m, level = 0.95)))
plot_coef(cfci)

## Estimate Model by Male and Female Subset
m_male <- glm(fm, data = vote92[vote92$female==0,],
         family = binomial("logit"))
m_female <- glm(fm, data = vote92[vote92$female==1,],
              family = binomial("logit"))

## Overlap Subsetted Results
plot_coef(list(m_male, m_female),
          m.names = c("Male", "Female"))

## Add Title and Custom Variable Names
vn <- c("(Intercept)",
        "Democrat","Republican",
        "Ideological Distance from Clinton",
        "Ideological Distance from Bush",
        "Retrospective Personal Finance",
        "Retrospective National Economy")
plot_coef(list(m_male, m_female),
          m.names = c("Male", "Female"),
          title = "Vote for Bush (1992)",
          custom.variable.names = vn)

## Overlap the Third Model & Facet Variables by Category & Add GOF Footnote
fn <- c("Constant",rep("Preference",4),rep("Evaluation",2))
fn <- factor(fn,levels=unique(fn))
plot_coef(list(m_male, m_female, m),
          m.names = c("Male", "Female", "All"),
          category.names = fn,
          title = "Vote for Bush (1992)",
          custom.variable.names = vn,
          footnote.gof = TRUE)

## Further Adding Facets 
plot_coef(list(m_male, m_female, m),
          m.names = c("Male", "Female", "ALL"),
          overlap.linetype.index = c(1,2,1),
          category.names = fn,
          facet.names = c("Gender Subsets","Gender Subsets","All"),
          title = "Vote for Bush (1992)",
          custom.variable.names = vn,
          footnote.gof = TRUE)

gentok/estvis documentation built on April 2, 2020, 1:58 p.m.