## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(panglm)

## ----copd---------------------------------------------------------------------
idx <- c("id", "visit")
att <- attgt(cost ~ 1, data = copd, index = idx, treat = "adopt",
             control = "notyet")
summary(att)

tp <- twopart(cost ~ exacerbations + hospitalized + smoker + treatment + crp +
                visit + programme + factor(clinic),
              binary_formula = ~ exacerbations + hospitalized + smoker +
                treatment + crp,
              data = copd, index = idx, positive = "lognormal")
coef(tp$positive)[["programme"]]

## ----docvisits----------------------------------------------------------------
early <- subset(docvisits, year <= 1988)
f <- doctor ~ age + hhninc + hhkids + married + working
pooled <- panglm(f, data = early, index = c("id", "year"), model = "pooling",
                 family = "probit", dynamic = TRUE, vcov = "cluster")
random <- panglm(f, data = early, index = c("id", "year"), model = "random",
                 family = "probit", dynamic = TRUE, R = 8)
cbind(pooled = coef(pooled)[c("lag_doctor", "initial_doctor")],
      random = coef(random)[c("lag_doctor", "initial_doctor")])

## ----meps---------------------------------------------------------------------
tp <- twopart(totexp ~ insurance + I(age / 10) + sex + nchronic + povcat,
              data = meps, index = c("id", "year"), positive = "gamma")
partialeffects(tp, variables = "insurance")

pos <- subset(meps, totexp > 0)
pooled <- panglm(totexp ~ insurance + povcat + I(age / 10) + sex + nchronic,
                 data = pos, index = c("id", "year"), family = "gamma",
                 model = "pooling")
within <- panglm(totexp ~ insurance + povcat, data = pos,
                 index = c("id", "year"), family = "gamma", model = "within")
exp(c(pooled = coef(pooled)[["insuranceuninsured"]],
      within = coef(within)[["insuranceuninsured"]])) - 1

## ----medicaid-----------------------------------------------------------------
ix <- c("state", "year")
cs <- attgt(uninspov ~ 1, data = medicaid, index = ix, treat = "adopt",
            control = "never")
summary(cs, type = "dynamic")

tw <- did(uninspov ~ 1, data = medicaid, index = ix, treat = "adopt")
ew <- etwfe(uninspov ~ 1, data = medicaid, index = ix, treat = "adopt")
c(attgt = summary(cs)$overall$estimate, etwfe = summary(ew)$overall$estimate,
  twfe = coef(tw)[["treated"]])

# first stage: Medicaid coverage of the same adults
summary(attgt(medicaidpov ~ 1, data = medicaid, index = ix,
              treat = "adopt", control = "never"))$overall

## ----healthspend--------------------------------------------------------------
hs <- transform(healthspend, lche = log(chereal), lgdp = log(gdppc))
ix <- c("iso3c", "year")
fe <- panglm(lche ~ lgdp, data = hs, index = ix, model = "within")
re <- panglm(lche ~ lgdp, data = hs, index = ix, model = "random")
fe2 <- panglm(lche ~ lgdp, data = hs, index = ix, model = "within",
              effect = "twoways", vcov = "cluster")
cbind(within = coef(fe), random = coef(re)[-1], twoways = coef(fe2))
panhausman(fe, method = "mundlak", data = hs)

## ----cigar--------------------------------------------------------------------
ix <- c("state", "year")
f <- lnc ~ lag(lnc, 1) + lnp + lnpn + lny
ols <- panglm(f, data = cigar, index = ix, model = "pooling")
fe <- panglm(f, data = cigar, index = ix, model = "within", effect = "twoways")
ab <- pangmm(lnc ~ lag(lnc, 1) + lnp + lnpn + lny | lag(lnc, 2:99),
             data = cigar, index = ix, transformation = "d", model = "onestep")
tab <- rbind(OLS = coef(ols)[-1], Within = coef(fe), GMM = coef(ab)[1:4])
round(tab, 3)
round(tab[, "lnp"] / (1 - tab[, 1]), 2)  # long-run price elasticity

