---
output:
  pdf_document:
    citation_package: natbib
    latex_engine: pdflatex
    toc: false
    keep_tex: true
title: "Standard Errors in Shift-Share Regressions"
author: "Michal Kolesár"
date: "`r format(Sys.time(), '%B %d, %Y')`"
geometry: margin=1in
fontfamily: mathpazo
bibliography: library.bib
fontsize: 11pt
vignette: >
  %\VignetteIndexEntry{ShiftShareSE}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include=FALSE, cache=FALSE}
library("knitr")
knitr::opts_knit$set(self.contained = FALSE)
knitr::opts_chunk$set(tidy = TRUE, collapse=TRUE, comment = "#>",
                      tidy.opts=list(blank=FALSE, width.cutoff=55))
```

The package `ShiftShareSE` implements confidence intervals proposed by @akm18 for
inference in shift-share least squares and instrumental variables regressions,
in which the regressor of interest (or the instrument) has a shift-share
structure, as in @bartik91. A shift-share variable has the structure
$X_{i}=\sum_{s=1}^{S}w_{is}{X}_{s}$, where $i$ indexes regions, $s$
indexes sectors, ${X}_{s}$ are sectoral shifters (or shocks), and
$w_{is}$ are shares, such as initial share of region $i$'s employment in sector
$s$.

This vignette illustrates the use of the package using a dataset from @adh13
(ADH hereafter). The dataset is included in the package as the list `ADH`. The
first element of the list, `ADH$reg` is a data-frame with regional variables,
the second element, `ADH$sic` is a vector of SIC codes for the sectors, and
`ADH$W` is a matrix of shares. See `?ADH` for a description of the dataset.

# Examples

We now replicate column (1) of Table V in @akm18. First we load the package,
define the vector of controls, and define a vector of 3-digit SIC codes:
```{r}
library("ShiftShareSE")
ctrls <- paste("t2 + l_shind_manuf_cbp + l_sh_popedu_c +", "l_sh_popfborn + l_sh_empl_f + l_sh_routine33 + l_task_outsource", "+ division")
sic <- floor(ADH$sic/10)
```

We cluster the standard errors at the 3-digit SIC code (using the option
`sector_cvar`), and, following ADH, weight the data using the weights
`ADH$reg$weights`. See `?reg_ss` and `?ivreg_ss` for full description of the
options.

The first-stage regression:
```{r}
reg_ss(as.formula(paste("shock ~ ", ctrls)), W=ADH$W, X=IV,
               data=ADH$reg, weights=weights, region_cvar=statefip,
               sector_cvar=sic, method="all")
```

Note that for `"AKM0"`, `"Std. Error"` corresponds to the normalized standard
error, i.e. the length of the confidence interval divided by
$2z_{1-\alpha/2}$.


The reduced-form and IV regressions:
```{r}
reg_ss(as.formula(paste("d_sh_empl ~", ctrls)), W=ADH$W, X=IV,
                  data=ADH$reg, region_cvar=statefip, weights=weights,
                  sector_cvar=sic, method="all")
ivreg_ss(as.formula(paste("d_sh_empl ~", ctrls, "| shock")), W=ADH$W,
                  X=IV, data=ADH$reg, region_cvar=statefip,
                  weights=weights, sector_cvar=sic, method="all")
```

## Note on collinear sectors

Let $W$ denote the share matrix with the $(i,s)$ element given by $w_{is}$.
Suppose that columns of $W$ are collinear, so it that it has rank $S_{0}<S$.
Without loss of generality, suppose that the first $S_0$ columns of the matrix
are full rank, so that the collinearity is caused by the last $S-S_0$ sectors.
In this case, it is not possible to recover, $\tilde{X}_s$, the sectoral
shifters with the controls partialled out, and the `reg_ss` and `ivreg_ss`
functions will return an error message `"Share matrix is collinear"`. The
researcher can either (i) drop the collinear sectors, defining
$X_i=\sum_{s=1}^{S_0}w_{is}X_{s}$, (ii) aggregate the sectors, or (iii) if the
only controls are those with shift-share structure, and we have data on $Z_{s}$,
we can estimate $\tilde{X}_{s}$ by running a sector-level regression of $X_s$
onto $Z_s$, and taking the residual. This third option is not currently
implemented in this package. Note that options (i) and (ii) change the
definition of the estimand. Since they involve changing the shock vector
$X_{i}$, this has to be done before using the `reg_ss` and `ivreg_ss` functions.


# References
