sim_glm: Simulate and plot quantities of interest from generalised...

Description Usage Arguments Value Source Examples

Description

Simulate and plot quantities of interest from generalised linear models

Usage

1
sim_glm(obj, newdata, x_coef, group_coef, n = 1000, model = "lm", col_pal)

Arguments

obj

fitted model object from lm or glm

newdata

data frame with fitted values for finding the quantities of interest. Column names must match coefficient names in obj. You do not need to specify fitted values for all coefficients. Unspecified coefficients will be fitted at 0.

x_coef

character string naming the variable from obj that will be plotted along the x-axis.

group_coef

optional character string specifying the values for the coefficient in obj along which the values of x_coef will be grouped in the plot.

n

numeric specifying the number of simulations to run.

model

character string or function specifying the type of estimation model for the quantity of interest. Currently must be the string 'lm' (for predicted values of a continuous response variable) or the string 'logit' (for predicted probabilities of a binary dependent response varible being 1). Experimental: you could also include a specify a function that takes a vector of values from your simulated point estimates and fitted values (e.g. alpha + beta1 * x1 + beta2 * x2) return a numeric vector with your custom quantity of interest.

col_pal

character string specifying the plot's colour palette.

Value

A gg ggplot2 object with predicted quantities represented by the simulation highest 50, 90, and 95 probability intervals. The central line is the median of the simulation interval.

Note: for predicted probabilities from logistic regression models, predictions outside of [0, 1] are discarded and so not included in the median or highest probability density intveral calculations.

Source

King, Gary, Michael Tomz, and Jason Wittenberg. 2000. "Making the Most of Statistical Analyses: Improving Interpretation and Presentation." American Journal of Political Science 44(2): 341-55.

Christopher Gandrud (2015). simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software, 65(3), 1-20. URL http://www.jstatsoft.org/v65/i03/.

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
# Normal Linear Model example
library(car) # Contains data
m1 <- lm(prestige ~ education + type,
         data = Prestige)

fitted_prestige <- expand.grid(education = 6:16, typewc = 1)

sim_glm(obj = m1, newdata = fitted_prestige, x_coef = 'education', n = 50)

fitted_prestige <- expand.grid(education = 6:16, typewc = 0:1)

sim_glm(obj = m1, newdata = fitted_prestige, x_coef = 'education',
        group_coef = 'typewc', n = 50)

# Logistic Model example
URL <- 'http://www.ats.ucla.edu/stat/data/binary.csv'
Admission <- read.csv(URL)
Admission$rank <- as.factor(Admission$rank)

m2 <- glm(admit ~ gre + gpa + rank,
          data = Admission, family = 'binomial')

fitted_admit_1 <- with(Admission,
                       expand.grid(gre = seq(220, 800, by = 10),
                                   gpa = mean(gpa),
                                   rank2 = 0:1))

sim_glm(obj = m2, newdata = fitted_admit_1, x_coef = 'gre',
        group_coef = 'rank2', model = 'logit', n = 50)

christophergandrud/simGLM documentation built on May 13, 2019, 7:03 p.m.