diff --git a/04_NE.Rmd b/04_NE.Rmd
new file mode 100644
index 0000000..e4ef72a
--- /dev/null
+++ b/04_NE.Rmd
@@ -0,0 +1,35 @@
+# (PART) Natural Experiments {-}
+
+# Natural Experiments
+
+## Finding Experiments in Nature
+
+## Instrumental Variables
+
+```{r, eval=TRUE, echo=FALSE}
+if (knitr:::is_latex_output()) {
+ knitr::asis_output('\\url{http://nickchk.com/anim/Animation%20of%20IV.gif}')
+} else {
+ knitr::include_graphics("animations/IV.gif")
+}
+```
+
+## Difference-in-Differences
+
+```{r, eval=TRUE, echo=FALSE}
+if (knitr:::is_latex_output()) {
+ knitr::asis_output('\\url{http://nickchk.com/anim/Animation%20of%20DID.gif}')
+} else {
+ knitr::include_graphics("animations/DID.gif")
+}
+```
+
+## Regression Discontinuity
+
+```{r, eval=TRUE, echo=FALSE}
+if (knitr:::is_latex_output()) {
+ knitr::asis_output('\\url{NOLINKYET}')
+} else {
+ knitr::include_graphics("animations/RDDSmooth.gif")
+}
+```
diff --git a/05_OM.Rmd b/05_OM.Rmd
new file mode 100644
index 0000000..fec2f14
--- /dev/null
+++ b/05_OM.Rmd
@@ -0,0 +1,233 @@
+
+# (PART) Observational Methods {-}
+
+# The Purpose of Observational Methods
+
+In the previous chapter on [Randomized Controlled Trials]('03_RCT.html'), we showed that, by randomizing treatment, we could identify causal effects because the random variation sidestepped the impact of omitted variables or sample selection that could drive a non-causal correlation between treatement and outcome. In [Natural Experiments]('04_NE.html'), we mimicked that strategy by finding sources of near-random variation in treatment, and isolating the part of treatment that was driven by the near-random variation.
+
+Using Randomized Controlled Trials or Natural Experiments, if they are properly applied, it is possible to ignore *any* confounding variables, whether or not we can measure them in our data or even imagine what they are.
+
+However, it is not always possible to run an experiment or find a natural experiment. In this chapter, we will consider ways of identifying causal effects using *Observational* methods, relying only on the data we have actually collected and observed. Natural Experiments often make use of additional Observational methods, correcting for confounding variables that the design of the natural experiment cannot account for.
+
+This chapter contains four sections: the first two cover multiple regression and matching, respectively, which are two common ways of adjusting for observed variables. The third covers the sufficient adjustment set, which allows you to determine which variables should and should not be adjusted for to identify a causal effect. The fourth covers fixed effects, which is a method that allows you to adjust for some unmeasured confounding variables.
+
+## Multiple Regression
+
+```{r, eval=TRUE, echo=FALSE}
+if (knitr:::is_latex_output()) {
+ knitr::asis_output('\\url{http://nickchk.com/anim/Animation%20of%20Control.gif}')
+} else {
+ knitr::include_graphics("animations/Control.gif")
+}
+```
+
+
+## Matching
+
+```{r, eval=TRUE, echo=FALSE}
+if (knitr:::is_latex_output()) {
+ knitr::asis_output('\\url{http://nickchk.com/anim/Animation%20of%20Matching.gif}')
+} else {
+ knitr::include_graphics("animations/Matching.gif")
+}
+```
+
+## Sufficient Adjustment Set
+
+## Fixed Effects
+
+### Between and Within Variation
+
+Fixed effects is a method that can be applied when you have *panel data*: multiple observations of the same individual over time.
+
+When you have multiple observations per individual, variation in the outcome variable $Y$ comes from two places: *between variation* and *within variation*. Between variation is the difference *between* individuals in their average level of $Y$. Within variation is the difference *within* a given individual comparing one time period to another.
+
+For example, consider the below data.
+
+```{r, eval=TRUE, echo=FALSE}
+knitr::kable(data.frame(Person = c('Anna','Anna','Bill','Bill'),
+ Time = c(1,2,1,2),
+ Y = c(4,8,3,1),
+ D = c(0,1,1,0)),
+ caption='Y over Two Time Periods for Two People')
+```
+
+Anna's average $Y$ is `(4+8)/2 = 6`, and Bill's average is `(3+1)/2 = 2`. The difference between the `6` and the `2` is the difference *between* Anna and Bill. And Anna's change from 4 to 8, and Bill's change from 3 to 1 are the differences *within* Anna and Bill.
+
+Fixed effects eliminates *all* between variation and uses only within variation. In effect, you are *controlling for individual identity*. For this reason it is also known as the "within estimator".
+
+The reason fixed effects does this is that it in effect controls for *all* differences between people that are constant across time, whether or not we control for them.
+
+### Demonstration of Fixed Effects
+
+Fixed effects works by simply removing all the between variation in each variable before performing an analysis of choice.
+
+Using the above example with Anna and Bill, we can subtract all between variation in $Y$ and $D$ by calculating the average of $Y$ and $D$ for Anna and Bill, separately, and subtracting it out. Notice that $\bar{Y}_{Anna}=6, \bar{Y}_{Bill}=2, \bar{D}_{Anna}=.5, \bar{D}_{Bill}=.5$.
+
+```{r, eval=TRUE, echo=FALSE}
+knitr::kable(data.frame(Person = c('Anna','Anna','Bill','Bill'),
+ Time = c(1,2,1,2),
+ Y = c(4,8,3,1),
+ D = c(0,1,1,0),
+ Within.Y = c(-2,2,1,-1),
+ Within.D = c(-.5,.5,.5,-.5)),
+ caption='Within Variation in Y and D')
+```
+
+With this modified data, the relationship between `Within.Y` and `Within.D` will tell us about the effect of `D` on `Y`, as long as all confounding variables were a part of the between variation.
+
+This process is demonstrated in animation below.
+
+```{r, eval=TRUE, echo=FALSE}
+if (knitr:::is_latex_output()) {
+ knitr::asis_output('\\url{http://nickchk.com/anim/Animation%20of%20Fixed%20Effects.gif}')
+} else {
+ knitr::include_graphics("animations/FixedEffects.gif")
+}
+```
+
+### Fixed Effects with Regression
+
+Fixed effects estimation is usually performed using regression. In the below model, each individual $i$ has their own intercept $\alpha_{i}$.
+
+\begin{align}
+ Y_{it} = \alpha_i + \beta D_{it} + \varepsilon_{it} (\#eq:fe1)
+\end{align}
+
+where $\varepsilon_{it}$ is an error term. This can also be seen as a regression of $Y_{it}$ on $D_{it}$ and a series of binary variables, one for each individual.
+
+\begin{align}
+ Y_{it} = \alpha + \sum_{i=0^N}\alpha_iI(i) + \beta D_{it} + \varepsilon_{it} (\#eq:fe2)
+\end{align}
+
+where $I(i)$ is a function equal to 1 for individual $i$ and 0 otherwise.
+
+Either Equation \@ref(eq:fe1) or Equation \@ref(eq:fe2) will provide identical estimates of $\beta$ as though you had run the regression
+
+\begin{align}
+ Within.Y_{it} = \alpha + \beta Within.D_{it} + \varepsilon_{it} (\#eq:fe3)
+\end{align}
+
+Any variable that is *constant within individual* will be controlled for using fixed effects, whether or not it is measured. For example, imagine that $i$ is an index of people, $D_{it}$ is being exposed to a pollution-reduction policy, and $Y_{it}$ is a measure of health. The variable $G_i$ measures a genetic endowment at birth, which may affect health and also whether you live in the area with the policy. Notice that $G_i$ does not have a $t$ subscript, indicating that it is *constant over time* for each person. Even without measuring $G_i$, it will be controlled for, since $G_i$ is just $I(i)$ multiplied by some constant, and we're already controlling for $I(i)$.
+
+Equation \@ref(eq:fe2) also makes clear that we are not controlling for anything that varies within person over time. For example, $Inc_{it}$ is income, which may affect health, and also whether you can afford to live in the area with the policy. Fixed effects alone does not control for $Inc_{it}$, and so we may need to add it as a control.
+
+Similarly, fixed effects assumes that identity has a linear and constant effect. For example, if genetic endowment $G_i$ has a stronger effect in some years than others, fixed effects will not account for this. Or, if genetic endowment modifies how effective the policy is, fixed effects will not account for this unless $G_i$ can be measured and the nonlinearity can be directly modeled.
+
+However, if all confounding variables are between-individual, linearity holds, and several other assumptions hold (see Treatment Effect Heterogeneity below), then fixed effects will identify the Average Treatment Effect.
+
+Notice that the modeling in this section is very similar to *Random Effects*, not addressed in this chapter, which are similar to fixed effects but combine both between and within variation.
+
+### Common Extensions
+
+#### Clustered Standard Errors
+
+The standard approach to calculating standard errors for Equations \@ref(eq:fe1), \@ref(eq:fe2), or \@ref(eq:fe3) makes the assumption that the error term $\varepsilon_{it}$ is independent and identically distributed. However, it may be reasonable to assume that $\varepsilon_{it}$ is correlated within individual.
+
+Under this condition, standard errors will be underestimated. For this reason, it is common to estimate fixed effects regressions using clustered standard errors. See [Cameron & Miller 2013](http://cameron.econ.ucdavis.edu/research/Cameron_Miller_Cluster_Robust_October152013.pdf) (or the published version [Cameron & Mill 2015](http://jhr.uwpress.org/content/50/2/317.short)) for a practitioner's guide to whether clustering is necessary in a given fixed effects context, and how it can be performed.
+
+#### Two-Way Fixed Effects
+
+In many fixed effects contexts, some of the within variation in $Y_{it}$ may not just be specific to person $i$, but may be shared across all individuals. For example, if the sample includes many individuals in the same economy, the overall health of the economy changes over time and would affect everyone. If the treatment variable $D_{it}$ is correlated with time as well, then these shared time effects will bias our estimate of the causal effect.
+
+In cases like this it is common to include two sets of fixed effects - one for individual, $\alpha_i$, and one for time, $\alpha_t$. The regression equation is then
+
+\begin{align}
+ Y_{it} = \alpha_{i} + \alpha_{t} + \beta D_{it} + \varepsilon_{it} (\#eq:fe4)
+\end{align}
+
+#### Treatment Effect Heterogeneity
+
+Under the assumptions discussed above, the estimate produced by a fixed effects regression will be a weighted average of the treatment effect for each individual. To see this, consider a data set that has only two time periods in it, and each individual is treated in exactly one of these periods, like our example above:
+
+```{r, eval=TRUE, echo=FALSE}
+knitr::kable(data.frame(Person = c('Anna','Anna','Bill','Bill'),
+ Time = c(1,2,1,2),
+ Y = c(4,8,3,1),
+ D = c(0,1,1,0),
+ Within.Y = c(-2,2,1,-1),
+ Within.D = c(-.5,.5,.5,-.5)),
+ caption='Within Variation in Y and D')
+```
+
+Anna sees an increase of 4 when the treatment is applied, and Bill sees an increase of 2. The fixed effects estimate will be `(2+1)/2 - (-2 + -1)/2 = 1.5 - -1.5 = 3`, which is also `(4+2)/3 = 3`, the average treatment effect in the sample.
+
+In this case, Anna and Bill received equal weight. That is because Anna and Bill had the exact same amount of variation in $D$. Fixed effects will more heavily weight individuals with more variation in $D$. For example, imagine that Bill always has $D = 0$ and thus no variation in $D$:
+
+```{r, eval=TRUE, echo=FALSE}
+knitr::kable(data.frame(Person = c('Anna','Anna','Bill','Bill'),
+ Time = c(1,2,1,2),
+ Y = c(4,8,1,1),
+ D = c(0,1,0,0),
+ Within.Y = c(-2,2,0,0),
+ Within.D = c(-.5,.5,0,0)),
+ caption='Within Variation in Y and D, where Bill Always Has D = 0')
+```
+
+The fixed effects estimate is now `2 - -2 = 4`, which is Anna's treatment effect. Bill has no variation in $D$ and thus receives no weight. We have failed to estimate the Average Treatment Effect.
+
+One way to adjust for this is to use weighted least squares, where each observation is weighted by the inverse standard deviation of treatment within individual $(\sigma^D_i)^{-1}$, where
+
+\begin{align}
+ \sigma^D_i = (\sum_t(D_{it} - \bar{D}_i)^2)^{1/2} (\#eq:fe5)
+\end{align}
+
+While this will not account for observations with no variation in $D_{it}$, this will otherwise recover the average treatment effect ([Gibbons, Serrato, & Urbancic, 2018](https://www.degruyter.com/view/j/jem.2019.8.issue-1/jem-2017-0002/jem-2017-0002.xml)).
+
+### Coding Up Fixed Effects
+
+Each of the following coding examples uses `D` as a treatment variable of interest, `Y` as the outcome variable, `id` as a variable encoding the individual, and `t` as a variable with the time period. In the R examples, all variables are stored in the data frame `df`.
+
+Fixed effects is easy to implement using regression by simply including a set of dummy variables, one for each individual. This method can be implemented as:
+
+```{r, echo=TRUE, eval = FALSE}
+#in R:
+fe <- lm(Y~D+factor(id), data = df)
+summary(fe)
+
+#in Stata:
+regress Y D i.id
+```
+
+Many statistics packages also allow you to identify the panel structure of the data, and have a specific command for implementing fixed effects.
+
+```{r, echo=TRUE, eval = FALSE}
+#in R:
+library(plm)
+df.p <- pdata.frame(df,index=c("id","t"))
+fe <- plm(Y~D,data=df.p,model="within")
+summary(fe)
+#Add clustered standard errors
+library(lmtest)
+fe.cluster <- coeftest(fe,vcov=vcovHC(model,type = "HC1", cluster="group"))
+summary(fe.cluster)
+
+#in Stata:
+xtset id t
+xtreg Y D, fe
+#Add clustered standard errors
+xtreg Y D, fe vce(cluster id)
+```
+
+Two-way fixed effects can be handled, as normal, by adding dummy variables for time period. There are also more formal ways of handling two-way fixed effects that work more quickly or handle standard errors in a more accurate way.
+
+```{r, echo=TRUE, eval = FALSE}
+#in R:
+#Just using dummies
+fe <- lm(Y~D+factor(id)+factor(t), data = df)
+#Using PLM
+library(plm)
+df.p <- pdata.frame(df,index=c("id","t"))
+fe.twoways <- plm(Y~D,data=df.p,model="twoways")
+summary(fe.twoways)
+
+#in Stata:
+#Using dummies and xtreg
+xtset id t
+xtreg Y D i.t, fe
+#Using reghdfe
+ssc install reghdfe
+#You may also have to install gtools
+#and in some cases do "reghdfe, compile" before use
+reghdfe Y D, absorb(id t)
+```
diff --git a/STCI.Rproj b/STCI.Rproj
new file mode 100644
index 0000000..827cca1
--- /dev/null
+++ b/STCI.Rproj
@@ -0,0 +1,15 @@
+Version: 1.0
+
+RestoreWorkspace: Default
+SaveWorkspace: Default
+AlwaysSaveHistory: Default
+
+EnableCodeIndexing: Yes
+UseSpacesForTab: Yes
+NumSpacesForTab: 2
+Encoding: UTF-8
+
+RnwWeave: Sweave
+LaTeX: pdfLaTeX
+
+BuildType: Website
diff --git a/_bookdown_files/STCI_files/figure-epub3/histpotout-1.png b/_bookdown_files/STCI_files/figure-epub3/histpotout-1.png
new file mode 100644
index 0000000..e222285
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-epub3/histpotout-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-epub3/histyb-1.png b/_bookdown_files/STCI_files/figure-epub3/histyb-1.png
new file mode 100644
index 0000000..6993e2d
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-epub3/histyb-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-epub3/ploty1y0yB-1.png b/_bookdown_files/STCI_files/figure-epub3/ploty1y0yB-1.png
new file mode 100644
index 0000000..6a362fc
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-epub3/ploty1y0yB-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-epub3/plotyyB-1.png b/_bookdown_files/STCI_files/figure-epub3/plotyyB-1.png
new file mode 100644
index 0000000..6d58581
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-epub3/plotyyB-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-html/histpotout-1.png b/_bookdown_files/STCI_files/figure-html/histpotout-1.png
new file mode 100644
index 0000000..f2b0c39
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-html/histpotout-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-html/histyb-1.png b/_bookdown_files/STCI_files/figure-html/histyb-1.png
new file mode 100644
index 0000000..77af70a
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-html/histyb-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-html/ploty1y0yB-1.png b/_bookdown_files/STCI_files/figure-html/ploty1y0yB-1.png
new file mode 100644
index 0000000..840954d
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-html/ploty1y0yB-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-html/plotyyB-1.png b/_bookdown_files/STCI_files/figure-html/plotyyB-1.png
new file mode 100644
index 0000000..e757c95
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-html/plotyyB-1.png differ
diff --git a/_bookdown_files/STCI_files/figure-latex/histpotout-1.pdf b/_bookdown_files/STCI_files/figure-latex/histpotout-1.pdf
new file mode 100644
index 0000000..8fd2ab2
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-latex/histpotout-1.pdf differ
diff --git a/_bookdown_files/STCI_files/figure-latex/histyb-1.pdf b/_bookdown_files/STCI_files/figure-latex/histyb-1.pdf
new file mode 100644
index 0000000..ceb9375
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-latex/histyb-1.pdf differ
diff --git a/_bookdown_files/STCI_files/figure-latex/ploty1y0yB-1.pdf b/_bookdown_files/STCI_files/figure-latex/ploty1y0yB-1.pdf
new file mode 100644
index 0000000..63ed5d6
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-latex/ploty1y0yB-1.pdf differ
diff --git a/_bookdown_files/STCI_files/figure-latex/plotyyB-1.pdf b/_bookdown_files/STCI_files/figure-latex/plotyyB-1.pdf
new file mode 100644
index 0000000..e8e2708
Binary files /dev/null and b/_bookdown_files/STCI_files/figure-latex/plotyyB-1.pdf differ
diff --git a/animations/Control.gif b/animations/Control.gif
new file mode 100644
index 0000000..4e9877f
Binary files /dev/null and b/animations/Control.gif differ
diff --git a/animations/DID.gif b/animations/DID.gif
new file mode 100644
index 0000000..9544550
Binary files /dev/null and b/animations/DID.gif differ
diff --git a/animations/FixedEffects.gif b/animations/FixedEffects.gif
new file mode 100644
index 0000000..d21894e
Binary files /dev/null and b/animations/FixedEffects.gif differ
diff --git a/animations/IV.gif b/animations/IV.gif
new file mode 100644
index 0000000..989a5b8
Binary files /dev/null and b/animations/IV.gif differ
diff --git a/animations/Matching.gif b/animations/Matching.gif
new file mode 100644
index 0000000..ddfa3db
Binary files /dev/null and b/animations/Matching.gif differ
diff --git a/animations/RDDSmooth.gif b/animations/RDDSmooth.gif
new file mode 100644
index 0000000..98ada00
Binary files /dev/null and b/animations/RDDSmooth.gif differ
diff --git a/docs/STCI.epub b/docs/STCI.epub
index 1c9949d..46aa9f6 100644
Binary files a/docs/STCI.epub and b/docs/STCI.epub differ
diff --git a/docs/STCI.pdf b/docs/STCI.pdf
index 160f888..e1a4200 100644
Binary files a/docs/STCI.pdf and b/docs/STCI.pdf differ
diff --git a/docs/STCI.tex b/docs/STCI.tex
index 9826f1a..99518b2 100644
--- a/docs/STCI.tex
+++ b/docs/STCI.tex
@@ -107,22 +107,23 @@
\usepackage{titling}
% Create subtitle command for use in maketitle
-\newcommand{\subtitle}[1]{
+\providecommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
+
\title{Statistical Tools for Causal Inference}
- \pretitle{\vspace{\droptitle}\centering\huge}
+ \pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
- \author{The SKY Community}
- \preauthor{\centering\large\emph}
+ \author{The SKY Community}
+ \preauthor{\centering\large\emph}
\postauthor{\par}
- \predate{\centering\large\emph}
+ \predate{\centering\large\emph}
\postdate{\par}
- \date{2019-04-13}
+ \date{2019-04-13}
\usepackage{dsfont}
\newcommand{\uns}[1]{\mathds{1}[ #1 ]}
@@ -131,10 +132,11 @@
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{lemma}{Lemma}[chapter]
-\theoremstyle{definition}
-\newtheorem{definition}{Definition}[chapter]
\newtheorem{corollary}{Corollary}[chapter]
\newtheorem{proposition}{Proposition}[chapter]
+\newtheorem{conjecture}{Conjecture}[chapter]
+\theoremstyle{definition}
+\newtheorem{definition}{Definition}[chapter]
\theoremstyle{definition}
\newtheorem{example}{Example}[chapter]
\theoremstyle{definition}
@@ -400,11 +402,11 @@ \subsubsection{The sharp cutoff rule}\label{the-sharp-cutoff-rule}
\end{Highlighting}
\end{Shaded}
-\begin{table}
+\begin{table}[t]
\caption{\label{tab:tableDsharp}Treatment allocation with sharp cutoff rule}
\centering
-\begin{tabular}[t]{lr}
+\begin{tabular}{lr}
\toprule
0 & 771\\
1 & 229\\
@@ -689,5 +691,410 @@ \subsection{The switching equation}\label{the-switching-equation}
\ref{fig:plotyyB} only shows observed outcomes, and is thus silent on
the values of the missing potential outcomes.
+\part{Natural Experiments}\label{part-natural-experiments}
+
+\chapter{Natural Experiments}\label{natural-experiments}
+
+\section{Finding Experiments in
+Nature}\label{finding-experiments-in-nature}
+
+\section{Instrumental Variables}\label{instrumental-variables}
+
+\url{http://nickchk.com/anim/Animation%20of%20IV.gif}
+
+\section{Difference-in-Differences}\label{difference-in-differences}
+
+\url{http://nickchk.com/anim/Animation%20of%20DID.gif}
+
+\section{Regression Discontinuity}\label{regression-discontinuity}
+
+\url{NOLINKYET}
+
+\part{Observational
+Methods}\label{part-observational-methods}
+
+\chapter{The Purpose of Observational
+Methods}\label{the-purpose-of-observational-methods}
+
+In the previous chapter on \href{'03_RCT.html'}{Randomized Controlled
+Trials}, we showed that, by randomizing treatment, we could identify
+causal effects because the random variation sidestepped the impact of
+omitted variables or sample selection that could drive a non-causal
+correlation between treatement and outcome. In
+\href{'04_NE.html'}{Natural Experiments}, we mimicked that strategy by
+finding sources of near-random variation in treatment, and isolating the
+part of treatment that was driven by the near-random variation.
+
+Using Randomized Controlled Trials or Natural Experiments, if they are
+properly applied, it is possible to ignore \emph{any} confounding
+variables, whether or not we can measure them in our data or even
+imagine what they are.
+
+However, it is not always possible to run an experiment or find a
+natural experiment. In this chapter, we will consider ways of
+identifying causal effects using \emph{Observational} methods, relying
+only on the data we have actually collected and observed. Natural
+Experiments often make use of additional Observational methods,
+correcting for confounding variables that the design of the natural
+experiment cannot account for.
+
+This chapter contains four sections: the first two cover multiple
+regression and matching, respectively, which are two common ways of
+adjusting for observed variables. The third covers the sufficient
+adjustment set, which allows you to determine which variables should and
+should not be adjusted for to identify a causal effect. The fourth
+covers fixed effects, which is a method that allows you to adjust for
+some unmeasured confounding variables.
+
+\section{Multiple Regression}\label{multiple-regression}
+
+\url{http://nickchk.com/anim/Animation%20of%20Control.gif}
+
+\section{Matching}\label{matching}
+
+\url{http://nickchk.com/anim/Animation%20of%20Matching.gif}
+
+\section{Sufficient Adjustment Set}\label{sufficient-adjustment-set}
+
+\section{Fixed Effects}\label{fixed-effects}
+
+\subsection{Between and Within
+Variation}\label{between-and-within-variation}
+
+Fixed effects is a method that can be applied when you have \emph{panel
+data}: multiple observations of the same individual over time.
+
+When you have multiple observations per individual, variation in the
+outcome variable \(Y\) comes from two places: \emph{between variation}
+and \emph{within variation}. Between variation is the difference
+\emph{between} individuals in their average level of \(Y\). Within
+variation is the difference \emph{within} a given individual comparing
+one time period to another.
+
+For example, consider the below data.
+
+\begin{table}[t]
+
+\caption{\label{tab:unnamed-chunk-11}Y over Two Time Periods for Two People}
+\centering
+\begin{tabular}{l|r|r|r}
+\hline
+Person & Time & Y & D\\
+\hline
+Anna & 1 & 4 & 0\\
+\hline
+Anna & 2 & 8 & 1\\
+\hline
+Bill & 1 & 3 & 1\\
+\hline
+Bill & 2 & 1 & 0\\
+\hline
+\end{tabular}
+\end{table}
+
+Anna's average \(Y\) is \texttt{(4+8)/2\ =\ 6}, and Bill's average is
+\texttt{(3+1)/2\ =\ 2}. The difference between the \texttt{6} and the
+\texttt{2} is the difference \emph{between} Anna and Bill. And Anna's
+change from 4 to 8, and Bill's change from 3 to 1 are the differences
+\emph{within} Anna and Bill.
+
+Fixed effects eliminates \emph{all} between variation and uses only
+within variation. In effect, you are \emph{controlling for individual
+identity}. For this reason it is also known as the ``within estimator''.
+
+The reason fixed effects does this is that it in effect controls for
+\emph{all} differences between people that are constant across time,
+whether or not we control for them.
+
+\subsection{Demonstration of Fixed
+Effects}\label{demonstration-of-fixed-effects}
+
+Fixed effects works by simply removing all the between variation in each
+variable before performing an analysis of choice.
+
+Using the above example with Anna and Bill, we can subtract all between
+variation in \(Y\) and \(D\) by calculating the average of \(Y\) and
+\(D\) for Anna and Bill, separately, and subtracting it out. Notice that
+\(\bar{Y}_{Anna}=6, \bar{Y}_{Bill}=2, \bar{D}_{Anna}=.5, \bar{D}_{Bill}=.5\).
+
+\begin{table}[t]
+
+\caption{\label{tab:unnamed-chunk-12}Within Variation in Y and D}
+\centering
+\begin{tabular}{l|r|r|r|r|r}
+\hline
+Person & Time & Y & D & Within.Y & Within.D\\
+\hline
+Anna & 1 & 4 & 0 & -2 & -0.5\\
+\hline
+Anna & 2 & 8 & 1 & 2 & 0.5\\
+\hline
+Bill & 1 & 3 & 1 & 1 & 0.5\\
+\hline
+Bill & 2 & 1 & 0 & -1 & -0.5\\
+\hline
+\end{tabular}
+\end{table}
+
+With this modified data, the relationship between \texttt{Within.Y} and
+\texttt{Within.D} will tell us about the effect of \texttt{D} on
+\texttt{Y}, as long as all confounding variables were a part of the
+between variation.
+
+This process is demonstrated in animation below.
+
+\url{http://nickchk.com/anim/Animation%20of%20Fixed%20Effects.gif}
+
+\subsection{Fixed Effects with
+Regression}\label{fixed-effects-with-regression}
+
+Fixed effects estimation is usually performed using regression. In the
+below model, each individual \(i\) has their own intercept
+\(\alpha_{i}\).
+
+\begin{align}
+ Y_{it} = \alpha_i + \beta D_{it} + \varepsilon_{it} \label{eq:fe1}
+\end{align}
+
+where \(\varepsilon_{it}\) is an error term. This can also be seen as a
+regression of \(Y_{it}\) on \(D_{it}\) and a series of binary variables,
+one for each individual.
+
+\begin{align}
+ Y_{it} = \alpha + \sum_{i=0^N}\alpha_iI(i) + \beta D_{it} + \varepsilon_{it} \label{eq:fe2}
+\end{align}
+
+where \(I(i)\) is a function equal to 1 for individual \(i\) and 0
+otherwise.
+
+Either Equation \eqref{eq:fe1} or Equation \eqref{eq:fe2} will provide
+identical estimates of \(\beta\) as though you had run the regression
+
+\begin{align}
+ Within.Y_{it} = \alpha + \beta Within.D_{it} + \varepsilon_{it} \label{eq:fe3}
+\end{align}
+
+Any variable that is \emph{constant within individual} will be
+controlled for using fixed effects, whether or not it is measured. For
+example, imagine that \(i\) is an index of people, \(D_{it}\) is being
+exposed to a pollution-reduction policy, and \(Y_{it}\) is a measure of
+health. The variable \(G_i\) measures a genetic endowment at birth,
+which may affect health and also whether you live in the area with the
+policy. Notice that \(G_i\) does not have a \(t\) subscript, indicating
+that it is \emph{constant over time} for each person. Even without
+measuring \(G_i\), it will be controlled for, since \(G_i\) is just
+\(I(i)\) multiplied by some constant, and we're already controlling for
+\(I(i)\).
+
+Equation \eqref{eq:fe2} also makes clear that we are not controlling for
+anything that varies within person over time. For example, \(Inc_{it}\)
+is income, which may affect health, and also whether you can afford to
+live in the area with the policy. Fixed effects alone does not control
+for \(Inc_{it}\), and so we may need to add it as a control.
+
+Similarly, fixed effects assumes that identity has a linear and constant
+effect. For example, if genetic endowment \(G_i\) has a stronger effect
+in some years than others, fixed effects will not account for this. Or,
+if genetic endowment modifies how effective the policy is, fixed effects
+will not account for this unless \(G_i\) can be measured and the
+nonlinearity can be directly modeled.
+
+However, if all confounding variables are between-individual, linearity
+holds, and several other assumptions hold (see Treatment Effect
+Heterogeneity below), then fixed effects will identify the Average
+Treatment Effect.
+
+Notice that the modeling in this section is very similar to \emph{Random
+Effects}, not addressed in this chapter, which are similar to fixed
+effects but combine both between and within variation.
+
+\subsection{Common Extensions}\label{common-extensions}
+
+\subsubsection{Clustered Standard
+Errors}\label{clustered-standard-errors}
+
+The standard approach to calculating standard errors for Equations
+\eqref{eq:fe1}, \eqref{eq:fe2}, or \eqref{eq:fe3} makes the assumption that
+the error term \(\varepsilon_{it}\) is independent and identically
+distributed. However, it may be reasonable to assume that
+\(\varepsilon_{it}\) is correlated within individual.
+
+Under this condition, standard errors will be underestimated. For this
+reason, it is common to estimate fixed effects regressions using
+clustered standard errors. See
+\href{http://cameron.econ.ucdavis.edu/research/Cameron_Miller_Cluster_Robust_October152013.pdf}{Cameron
+\& Miller 2013} (or the published version
+\href{http://jhr.uwpress.org/content/50/2/317.short}{Cameron \& Mill
+2015}) for a practitioner's guide to whether clustering is necessary in
+a given fixed effects context, and how it can be performed.
+
+\subsubsection{Two-Way Fixed Effects}\label{two-way-fixed-effects}
+
+In many fixed effects contexts, some of the within variation in
+\(Y_{it}\) may not just be specific to person \(i\), but may be shared
+across all individuals. For example, if the sample includes many
+individuals in the same economy, the overall health of the economy
+changes over time and would affect everyone. If the treatment variable
+\(D_{it}\) is correlated with time as well, then these shared time
+effects will bias our estimate of the causal effect.
+
+In cases like this it is common to include two sets of fixed effects -
+one for individual, \(\alpha_i\), and one for time, \(\alpha_t\). The
+regression equation is then
+
+\begin{align}
+ Y_{it} = \alpha_{i} + \alpha_{t} + \beta D_{it} + \varepsilon_{it} \label{eq:fe4}
+\end{align}
+
+\subsubsection{Treatment Effect
+Heterogeneity}\label{treatment-effect-heterogeneity}
+
+Under the assumptions discussed above, the estimate produced by a fixed
+effects regression will be a weighted average of the treatment effect
+for each individual. To see this, consider a data set that has only two
+time periods in it, and each individual is treated in exactly one of
+these periods, like our example above:
+
+\begin{table}[t]
+
+\caption{\label{tab:unnamed-chunk-14}Within Variation in Y and D}
+\centering
+\begin{tabular}{l|r|r|r|r|r}
+\hline
+Person & Time & Y & D & Within.Y & Within.D\\
+\hline
+Anna & 1 & 4 & 0 & -2 & -0.5\\
+\hline
+Anna & 2 & 8 & 1 & 2 & 0.5\\
+\hline
+Bill & 1 & 3 & 1 & 1 & 0.5\\
+\hline
+Bill & 2 & 1 & 0 & -1 & -0.5\\
+\hline
+\end{tabular}
+\end{table}
+
+Anna sees an increase of 4 when the treatment is applied, and Bill sees
+an increase of 2. The fixed effects estimate will be
+\texttt{(2+1)/2\ -\ (-2\ +\ -1)/2\ =\ 1.5\ -\ -1.5\ =\ 3}, which is also
+\texttt{(4+2)/3\ =\ 3}, the average treatment effect in the sample.
+
+In this case, Anna and Bill received equal weight. That is because Anna
+and Bill had the exact same amount of variation in \(D\). Fixed effects
+will more heavily weight individuals with more variation in \(D\). For
+example, imagine that Bill always has \(D = 0\) and thus no variation in
+\(D\):
+
+\begin{table}[t]
+
+\caption{\label{tab:unnamed-chunk-15}Within Variation in Y and D, where Bill Always Has D = 0}
+\centering
+\begin{tabular}{l|r|r|r|r|r}
+\hline
+Person & Time & Y & D & Within.Y & Within.D\\
+\hline
+Anna & 1 & 4 & 0 & -2 & -0.5\\
+\hline
+Anna & 2 & 8 & 1 & 2 & 0.5\\
+\hline
+Bill & 1 & 1 & 0 & 0 & 0.0\\
+\hline
+Bill & 2 & 1 & 0 & 0 & 0.0\\
+\hline
+\end{tabular}
+\end{table}
+
+The fixed effects estimate is now \texttt{2\ -\ -2\ =\ 4}, which is
+Anna's treatment effect. Bill has no variation in \(D\) and thus
+receives no weight. We have failed to estimate the Average Treatment
+Effect.
+
+One way to adjust for this is to use weighted least squares, where each
+observation is weighted by the inverse standard deviation of treatment
+within individual \((\sigma^D_i)^{-1}\), where
+
+\begin{align}
+ \sigma^D_i = (\sum_t(D_{it} - \bar{D}_i)^2)^{1/2} \label{eq:fe5}
+\end{align}
+
+While this will not account for observations with no variation in
+\(D_{it}\), this will otherwise recover the average treatment effect
+(\href{https://www.degruyter.com/view/j/jem.2019.8.issue-1/jem-2017-0002/jem-2017-0002.xml}{Gibbons,
+Serrato, \& Urbancic, 2018}).
+
+\subsection{Coding Up Fixed Effects}\label{coding-up-fixed-effects}
+
+Each of the following coding examples uses \texttt{D} as a treatment
+variable of interest, \texttt{Y} as the outcome variable, \texttt{id} as
+a variable encoding the individual, and \texttt{t} as a variable with
+the time period. In the R examples, all variables are stored in the data
+frame \texttt{df}.
+
+Fixed effects is easy to implement using regression by simply including
+a set of dummy variables, one for each individual. This method can be
+implemented as:
+
+\begin{Shaded}
+\begin{Highlighting}[]
+\CommentTok{#in R:}
+\NormalTok{fe <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(Y}\OperatorTok{~}\NormalTok{D}\OperatorTok{+}\KeywordTok{factor}\NormalTok{(id), }\DataTypeTok{data =}\NormalTok{ df)}
+\KeywordTok{summary}\NormalTok{(fe)}
+
+\CommentTok{#in Stata:}
+\NormalTok{regress Y D i.id}
+\end{Highlighting}
+\end{Shaded}
+
+Many statistics packages also allow you to identify the panel structure
+of the data, and have a specific command for implementing fixed effects.
+
+\begin{Shaded}
+\begin{Highlighting}[]
+\CommentTok{#in R:}
+\KeywordTok{library}\NormalTok{(plm)}
+\NormalTok{df.p <-}\StringTok{ }\KeywordTok{pdata.frame}\NormalTok{(df,}\DataTypeTok{index=}\KeywordTok{c}\NormalTok{(}\StringTok{"id"}\NormalTok{,}\StringTok{"t"}\NormalTok{))}
+\NormalTok{fe <-}\StringTok{ }\KeywordTok{plm}\NormalTok{(Y}\OperatorTok{~}\NormalTok{D,}\DataTypeTok{data=}\NormalTok{df.p,}\DataTypeTok{model=}\StringTok{"within"}\NormalTok{)}
+\KeywordTok{summary}\NormalTok{(fe)}
+\CommentTok{#Add clustered standard errors}
+\KeywordTok{library}\NormalTok{(lmtest)}
+\NormalTok{fe.cluster <-}\StringTok{ }\KeywordTok{coeftest}\NormalTok{(fe,}\DataTypeTok{vcov=}\KeywordTok{vcovHC}\NormalTok{(model,}\DataTypeTok{type =} \StringTok{"HC1"}\NormalTok{, }\DataTypeTok{cluster=}\StringTok{"group"}\NormalTok{))}
+\KeywordTok{summary}\NormalTok{(fe.cluster)}
+
+\CommentTok{#in Stata:}
+\NormalTok{xtset id t}
+\NormalTok{xtreg Y D, fe}
+\CommentTok{#Add clustered standard errors}
+\NormalTok{xtreg Y D, fe }\KeywordTok{vce}\NormalTok{(cluster id)}
+\end{Highlighting}
+\end{Shaded}
+
+Two-way fixed effects can be handled, as normal, by adding dummy
+variables for time period. There are also more formal ways of handling
+two-way fixed effects that work more quickly or handle standard errors
+in a more accurate way.
+
+\begin{Shaded}
+\begin{Highlighting}[]
+\CommentTok{#in R:}
+\CommentTok{#Just using dummies}
+\NormalTok{fe <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(Y}\OperatorTok{~}\NormalTok{D}\OperatorTok{+}\KeywordTok{factor}\NormalTok{(id)}\OperatorTok{+}\KeywordTok{factor}\NormalTok{(t), }\DataTypeTok{data =}\NormalTok{ df)}
+\CommentTok{#Using PLM}
+\KeywordTok{library}\NormalTok{(plm)}
+\NormalTok{df.p <-}\StringTok{ }\KeywordTok{pdata.frame}\NormalTok{(df,}\DataTypeTok{index=}\KeywordTok{c}\NormalTok{(}\StringTok{"id"}\NormalTok{,}\StringTok{"t"}\NormalTok{))}
+\NormalTok{fe.twoways <-}\StringTok{ }\KeywordTok{plm}\NormalTok{(Y}\OperatorTok{~}\NormalTok{D,}\DataTypeTok{data=}\NormalTok{df.p,}\DataTypeTok{model=}\StringTok{"twoways"}\NormalTok{)}
+\KeywordTok{summary}\NormalTok{(fe.twoways)}
+
+\CommentTok{#in Stata:}
+\CommentTok{#Using dummies and xtreg}
+\NormalTok{xtset id t}
+\NormalTok{xtreg Y D i.t, fe}
+\CommentTok{#Using reghdfe}
+\NormalTok{ssc install reghdfe}
+\CommentTok{#You may also have to install gtools}
+\CommentTok{#and in some cases do "reghdfe, compile" before use}
+\NormalTok{reghdfe Y D, }\KeywordTok{absorb}\NormalTok{(id t)}
+\end{Highlighting}
+\end{Shaded}
+
\end{document}
diff --git a/docs/STCI_files/figure-html/histpotout-1.png b/docs/STCI_files/figure-html/histpotout-1.png
index 93a5c1b..f2b0c39 100644
Binary files a/docs/STCI_files/figure-html/histpotout-1.png and b/docs/STCI_files/figure-html/histpotout-1.png differ
diff --git a/docs/STCI_files/figure-html/histyb-1.png b/docs/STCI_files/figure-html/histyb-1.png
index 928b725..77af70a 100644
Binary files a/docs/STCI_files/figure-html/histyb-1.png and b/docs/STCI_files/figure-html/histyb-1.png differ
diff --git a/docs/STCI_files/figure-html/ploty1y0yB-1.png b/docs/STCI_files/figure-html/ploty1y0yB-1.png
index f128e0a..840954d 100644
Binary files a/docs/STCI_files/figure-html/ploty1y0yB-1.png and b/docs/STCI_files/figure-html/ploty1y0yB-1.png differ
diff --git a/docs/STCI_files/figure-html/plotyyB-1.png b/docs/STCI_files/figure-html/plotyyB-1.png
index d03f249..e757c95 100644
Binary files a/docs/STCI_files/figure-html/plotyyB-1.png and b/docs/STCI_files/figure-html/plotyyB-1.png differ
diff --git a/docs/animations/Animation of Control.gif b/docs/animations/Animation of Control.gif
new file mode 100644
index 0000000..4e9877f
Binary files /dev/null and b/docs/animations/Animation of Control.gif differ
diff --git a/docs/animations/Animation of Fixed Effects.gif b/docs/animations/Animation of Fixed Effects.gif
new file mode 100644
index 0000000..d21894e
Binary files /dev/null and b/docs/animations/Animation of Fixed Effects.gif differ
diff --git a/docs/animations/Animation of Matching.gif b/docs/animations/Animation of Matching.gif
new file mode 100644
index 0000000..ddfa3db
Binary files /dev/null and b/docs/animations/Animation of Matching.gif differ
diff --git a/docs/animations/Control.gif b/docs/animations/Control.gif
new file mode 100644
index 0000000..4e9877f
Binary files /dev/null and b/docs/animations/Control.gif differ
diff --git a/docs/animations/DID.gif b/docs/animations/DID.gif
new file mode 100644
index 0000000..9544550
Binary files /dev/null and b/docs/animations/DID.gif differ
diff --git a/docs/animations/FixedEffects.gif b/docs/animations/FixedEffects.gif
new file mode 100644
index 0000000..d21894e
Binary files /dev/null and b/docs/animations/FixedEffects.gif differ
diff --git a/docs/animations/IV.gif b/docs/animations/IV.gif
new file mode 100644
index 0000000..989a5b8
Binary files /dev/null and b/docs/animations/IV.gif differ
diff --git a/docs/animations/Matching.gif b/docs/animations/Matching.gif
new file mode 100644
index 0000000..ddfa3db
Binary files /dev/null and b/docs/animations/Matching.gif differ
diff --git a/docs/animations/RDDSmooth.gif b/docs/animations/RDDSmooth.gif
new file mode 100644
index 0000000..98ada00
Binary files /dev/null and b/docs/animations/RDDSmooth.gif differ
diff --git a/docs/basic-observational-methods.html b/docs/basic-observational-methods.html
new file mode 100644
index 0000000..a8ccfd8
--- /dev/null
+++ b/docs/basic-observational-methods.html
@@ -0,0 +1,232 @@
+
+
+
+
This chapter contains four sections: the first two cover multiple regression and matching, respectively, which are two common ways of adjusting for observed variables. The third covers the sufficient adjustment set, which allows you to determine which variables should and should not be adjusted for to identify a causal effect. The fourth covers fixed effects, which is a method that allows you to adjust for some unmeasured confounding variables.
In the previous chapter on Randomized Controlled Trials, we showed that, by randomizing treatment, we could identify causal effects because the random variation sidestepped the impact of omitted variables or sample selection that could drive a non-causal correlation between treatement and outcome. In Natural Experiments, we mimicked that strategy by finding sources of near-random variation in treatment, and isolating the part of treatment that was driven by the near-random variation.
+
Using Randomized Controlled Trials or Natural Experiments, if they are properly applied, it is possible to ignore any confounding variables, whether or not we can measure them in our data or even imagine what they are.
+
However, it is not always possible to run an experiment or find a natural experiment. In this chapter, we will consider ways of identifying causal effects using Observational methods, relying only on the data we have actually collected and observed. Natural Experiments often make use of additional Observational methods, correcting for confounding variables that the design of the natural experiment cannot account for.
+
This chapter contains four sections: the first two cover multiple regression and matching, respectively, which are two common ways of adjusting for observed variables. The third covers the sufficient adjustment set, which allows you to determine which variables should and should not be adjusted for to identify a causal effect. The fourth covers fixed effects, which is a method that allows you to adjust for some unmeasured confounding variables.
+
+
3.1 Multiple Regression
+
+
+
+
3.2 Matching
+
+
+
+
3.3 Sufficient Adjustment Set
+
+
+
3.4 Fixed Effects
+
+
3.4.1 Between and Within Variation
+
Fixed effects is a method that can be applied when you have panel data: multiple observations of the same individual over time.
+
When you have multiple observations per individual, variation in the outcome variable \(Y\) comes from two places: between variation and within variation. Between variation is the difference between individuals in their average level of \(Y\). Within variation is the difference within a given individual comparing one time period to another.
+
For example, consider the below data.
+
+
Table 3.1: Y over Two Time Periods for Two People
+
+
+
Person
+
Time
+
Y
+
D
+
+
+
+
+
Anna
+
1
+
4
+
0
+
+
+
Anna
+
2
+
8
+
1
+
+
+
Bill
+
1
+
3
+
1
+
+
+
Bill
+
2
+
1
+
0
+
+
+
+
Anna’s average \(Y\) is (4+8)/2 = 6, and Bill’s average is (3+1)/2 = 2. The difference between the 6 and the 2 is the difference between Anna and Bill. And Anna’s change from 4 to 8, and Bill’s change from 3 to 1 are the differences within Anna and Bill.
+
Fixed effects eliminates all between variation and uses only within variation. In effect, you are controlling for individual identity. For this reason it is also known as the “within estimator”.
+
The reason fixed effects does this is that it in effect controls for all differences between people that are constant across time, whether or not we control for them.
+
+
+
3.4.2 Demonstration of Fixed Effects
+
Fixed effects works by simply removing all the between variation in each variable before performing an analysis of choice.
+
Using the above example with Anna and Bill, we can subtract all between variation in \(Y\) and \(D\) by calculating the average of \(Y\) and \(D\) for Anna and Bill, separately, and subtracting it out. Notice that \(\bar{Y}_{Anna}=6, \bar{Y}_{Bill}=2, \bar{D}_{Anna}=.5, \bar{D}_{Bill}=.5\).
+
+
Table 3.2: Within Variation in Y and D
+
+
+
Person
+
Time
+
Y
+
D
+
Within.Y
+
Within.D
+
+
+
+
+
Anna
+
1
+
4
+
0
+
-2
+
-0.5
+
+
+
Anna
+
2
+
8
+
1
+
2
+
0.5
+
+
+
Bill
+
1
+
3
+
1
+
1
+
0.5
+
+
+
Bill
+
2
+
1
+
0
+
-1
+
-0.5
+
+
+
+
With this modified data, the relationship between Within.Y and Within.D will tell us about the effect of D on Y, as long as all confounding variables were a part of the between variation.
+
This process is demonstrated in animation below.
+
+
+
+
3.4.3 Fixed Effects with Regression
+
Fixed effects estimation is usually performed using regression. In the below model, each individual \(i\) has their own intercept \(\alpha_{i}\).
where \(\varepsilon_{it}\) is an error term. This can also be seen as a regression of \(Y_{it}\) on \(D_{it}\) and a series of binary variables, one for each individual.
Any variable that is constant within individual will be controlled for using fixed effects, whether or not it is measured. For example, imagine that \(i\) is an index of people, \(D_{it}\) is being exposed to a pollution-reduction policy, and \(Y_{it}\) is a measure of health. The variable \(G_i\) measures a genetic endowment at birth, which may affect health and also whether you live in the area with the policy. Notice that \(G_i\) does not have a \(t\) subscript, indicating that it is constant over time for each person. Even without measuring \(G_i\), it will be controlled for, since \(G_i\) is just \(I(i)\) multiplied by some constant, and we’re already controlling for \(I(i)\).
+
Equation (3.2) also makes clear that we are not controlling for anything that varies within person over time. For example, \(Inc_{it}\) is income, which may affect health, and also whether you can afford to live in the area with the policy. Fixed effects alone does not control for \(Inc_{it}\), and so we may need to add it as a control.
+
Similarly, fixed effects assumes that identity has a linear and constant effect. For example, if genetic endowment \(G_i\) has a stronger effect in some years than others, fixed effects will not account for this. Or, if genetic endowment modifies how effective the policy is, fixed effects will not account for this unless \(G_i\) can be measured and the nonlinearity can be directly modeled.
+
However, if all confounding variables are between-individual, linearity holds, and several other assumptions hold (see Treatment Effect Heterogeneity below), then fixed effects will identify the Average Treatment Effect.
+
Notice that the modeling in this section is very similar to Random Effects, not addressed in this chapter, which are similar to fixed effects but combine both between and within variation.
+
+
+
3.4.4 Common Extensions
+
+
3.4.4.1 Clustered Standard Errors
+
The standard approach to calculating standard errors for Equations (3.1), (3.2), or (3.3) makes the assumption that the error term \(\varepsilon_{it}\) is independent and identically distributed. However, it may be reasonable to assume that \(\varepsilon_{it}\) is correlated within individual.
+
Under this condition, standard errors will be underestimated. For this reason, it is common to estimate fixed effects regressions using clustered standard errors. See Cameron & Miller 2013 (or the published version Cameron & Mill 2015) for a practitioner’s guide to whether clustering is necessary in a given fixed effects context, and how it can be performed.
+
+
+
3.4.4.2 Two-Way Fixed Effects
+
In many fixed effects contexts, some of the within variation in \(Y_{it}\) may not just be specific to person \(i\), but may be shared across all individuals. For example, if the sample includes many individuals in the same economy, the overall health of the economy changes over time and would affect everyone. If the treatment variable \(D_{it}\) is correlated with time as well, then these shared time effects will bias our estimate of the causal effect.
+
In cases like this it is common to include two sets of fixed effects - one for individual, \(\alpha_i\), and one for time, \(\alpha_t\). The regression equation is then
Under the assumptions discussed above, the estimate produced by a fixed effects regression will be a weighted average of the treatment effect for each individual. To see this, consider a data set that has only two time periods in it, and each individual is treated in exactly one of these periods, like our example above:
+
+
Table 3.3: Within Variation in Y and D
+
+
+
Person
+
Time
+
Y
+
D
+
Within.Y
+
Within.D
+
+
+
+
+
Anna
+
1
+
4
+
0
+
-2
+
-0.5
+
+
+
Anna
+
2
+
8
+
1
+
2
+
0.5
+
+
+
Bill
+
1
+
3
+
1
+
1
+
0.5
+
+
+
Bill
+
2
+
1
+
0
+
-1
+
-0.5
+
+
+
+
Anna sees an increase of 4 when the treatment is applied, and Bill sees an increase of 2. The fixed effects estimate will be (2+1)/2 - (-2 + -1)/2 = 1.5 - -1.5 = 3, which is also (4+2)/3 = 3, the average treatment effect in the sample.
+
In this case, Anna and Bill received equal weight. That is because Anna and Bill had the exact same amount of variation in \(D\). Fixed effects will more heavily weight individuals with more variation in \(D\). For example, imagine that Bill always has \(D = 0\) and thus no variation in \(D\):
+
+
Table 3.4: Within Variation in Y and D, where Bill Always Has D = 0
+
+
+
Person
+
Time
+
Y
+
D
+
Within.Y
+
Within.D
+
+
+
+
+
Anna
+
1
+
4
+
0
+
-2
+
-0.5
+
+
+
Anna
+
2
+
8
+
1
+
2
+
0.5
+
+
+
Bill
+
1
+
1
+
0
+
0
+
0.0
+
+
+
Bill
+
2
+
1
+
0
+
0
+
0.0
+
+
+
+
The fixed effects estimate is now 2 - -2 = 4, which is Anna’s treatment effect. Bill has no variation in \(D\) and thus receives no weight. We have failed to estimate the Average Treatment Effect.
+
One way to adjust for this is to use weighted least squares, where each observation is weighted by the inverse standard deviation of treatment within individual \((\sigma^D_i)^{-1}\), where
While this will not account for observations with no variation in \(D_{it}\), this will otherwise recover the average treatment effect (Gibbons, Serrato, & Urbancic, 2018).
+
+
+
+
3.4.5 Coding Up Fixed Effects
+
Each of the following coding examples uses D as a treatment variable of interest, Y as the outcome variable, id as a variable encoding the individual, and t as a variable with the time period. In the R examples, all variables are stored in the data frame df.
+
Fixed effects is easy to implement using regression by simply including a set of dummy variables, one for each individual. This method can be implemented as:
+
#in R:
+fe <-lm(Y~D+factor(id), data = df)
+summary(fe)
+
+#in Stata:
+regress Y D i.id
+
Many statistics packages also allow you to identify the panel structure of the data, and have a specific command for implementing fixed effects.
+
#in R:
+library(plm)
+df.p <-pdata.frame(df,index=c("id","t"))
+fe <-plm(Y~D,data=df.p,model="within")
+summary(fe)
+#Add clustered standard errors
+library(lmtest)
+fe.cluster <-coeftest(fe,vcov=vcovHC(model,type ="HC1", cluster="group"))
+summary(fe.cluster)
+
+#in Stata:
+xtset id t
+xtreg Y D, fe
+#Add clustered standard errors
+xtreg Y D, fe vce(cluster id)
+
Two-way fixed effects can be handled, as normal, by adding dummy variables for time period. There are also more formal ways of handling two-way fixed effects that work more quickly or handle standard errors in a more accurate way.
+
#in R:
+#Just using dummies
+fe <-lm(Y~D+factor(id)+factor(t), data = df)
+#Using PLM
+library(plm)
+df.p <-pdata.frame(df,index=c("id","t"))
+fe.twoways <-plm(Y~D,data=df.p,model="twoways")
+summary(fe.twoways)
+
+#in Stata:
+#Using dummies and xtreg
+xtset id t
+xtreg Y D i.t, fe
+#Using reghdfe
+ssc install reghdfe
+#You may also have to install gtools
+#and in some cases do "reghdfe, compile" before use
+reghdfe Y D, absorb(id t)