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

Examples

Simple first differences

Load Zelig and attach example data frame:

data(macro, package = "Zelig")

Estimate model:

m1 <- lm(unem ~ gdp + capmob + trade, data = macro)

Summarize regression coefficients:

summary(m1)

Set explanatory variables to their default (mean/mode) values, with high (80th percentile) and low (20th percentile) values for the trade variable:

library(smargins)
m1.sm <- smargins(m1, trade = quantile(trade, c(0.2, 0.8)))
summary(m1.sm)

Calculate first differences for the effect of high versus low trade on GDP:

summary(scompare(m1.sm, var = "trade"))

Plot the simulated distributions:

library(ggplot2)

ggplot(m1.sm, aes(x = .smargin_qi, fill = factor(trade))) +
    geom_density(alpha = 0.25)

ggplot(scompare(m1.sm, var = "trade"), aes(x = .smargin_qi, fill = factor(trade))) +
    geom_density()

Categorical variables, including fixed effects

Estimate a model with fixed effects for each country.

m2 <- lm(unem ~ gdp + trade + capmob + country, data = macro)

Calculate expected values for each country.

m2.sm <- smargins(m2, country = unique(country))

summary(m2.sm)

(m2.sm.comp <- summary(scompare(m2.sm, var = "country")))

Plot.

m2.sm.comp$country <- reorder(m2.sm.comp$country, m2.sm.comp$median)

ggplot(m2.sm.comp, aes(y = country, x = median)) +
    geom_point() +
    geom_errorbarh(aes(xmin = lower_2.5, xmax = upper_97.5), height = .2)


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