knitr::opts_knit$set(
        stop_on_error = 2L
)
knitr::opts_chunk$set(
    fig.height = 11,
    fig.width = 7
)

Examples

Basic example

Attach the sample turnout dataset:

data(turnout, package = "Zelig")

Estimating parameter values for the logistic regression:

m1 <- glm(vote ~ age + race, family = binomial(link = "logit"), data = turnout)

Summarize estimated parameters:

summary(m1)

For logit models you may wish to calculate odds ratios:

cbind(OR = exp(coef(m1)), exp(confint(m1)))

Set values for the explanatory variables and simulate quantities of interest from the posterior distribution:

library(smargins)
m.sm <- smargins(m1, age = 36, race = "white")
summary(m.sm)

Show the results graphically:

library(ggplot2)

ggplot(m.sm, aes(x = .smargin_qi)) +
    geom_density(aes(fill = interaction(age, race)))

First differences

Estimating the risk difference (and risk ratio) between low education (25th percentile) and high education (75th percentile) while all the other variables averaged over their observed values.

m2 <- glm(vote ~ educate, data = turnout, family = binomial(link = "logit"))
m2.sm <- smargins(m2, educate = quantile(educate, prob = c(0.25, 0.75)))

summary(m2.sm)
summary(scompare(m2.sm, var = "educate"))
ggplot(scompare(m2.sm, "educate")) +
    geom_density(aes(x = .smargin_qi, fill = factor(educate)))

ggplot(m2.sm, aes(x = .smargin_qi)) +
    geom_density(aes(fill = factor(educate)))


izahn/smargins documentation built on Sept. 11, 2019, 2:08 p.m.