generated from mbg-unsw/neweywest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivreg-20221207.Rtex
executable file
·224 lines (190 loc) · 6.06 KB
/
ivreg-20221207.Rtex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
% Brief workshop on instrumental variable regression in R
% (c) 2022 Malcolm Gillies <[email protected]>
% https://github.com/mbg-unsw/ivreg
%
% This work is licensed under a
% Creative Commons Attribution-NonCommercial-ShareAlike 4.0
% International Licence
\documentclass[aspectratio=169,12pt]{beamer} % XXXX fix AR here
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usefonttheme{serif} % need this with Charter font
\usetheme{Berlin} % using default now
\usecolortheme{beaver} % using default now
\usepackage[libertine]{libertine} % not using osf (old-style figures)
\usepackage[scale=0.9]{tgheros} % scale to match libertine
\usepackage[varqu,varl]{inconsolata}
\usepackage[libertine]{newtxmath}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usepackage{natbib}
\usepackage{gitinfo2}
\renewcommand{\gitMark}{\color{gray}\texttt{\tiny\gitBranch\,@\,\gitAbbrevHash\,\gitAuthorDate}}
\setbeamertemplate{navigation symbols}{} % remove navigation symbols
\setbeamercolor*{item}{fg=darkred}
%% begin.rcode setup, include=FALSE
% library(knitr)
% opts_chunk$set(fig.path='figure/latex-', cache.path='cache.latex-', size='small')
%% end.rcode
\title{Intro to instrumental variable regression in R}
\author{Malcolm Gillies}
\date{7 December 2022}
\usebackgroundtemplate{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=south west,scale=1,rotate=90] at ([shift={(0cm,0cm)}]current page marginpar area.south east) {\gitMark};
\end{tikzpicture}%
}
\begin{document}
{
%\usebackgroundtemplate{}
\begin{frame}
\titlepage
\end{frame}
}
\begin{frame}{Acknowledgement}
The material in this presentation was adapted from:
\medskip
\textbf{Introduction to Econometrics with R}
\emph{Christoph Hanck, Martin Arnold, Alexander Gerber, and Martin Schmelzer}
2021-10-06
\url{https://www.econometrics-with-r.org/}
\medskip
Especially Chapter 12 / Section 12.1
\end{frame}
\begin{frame}{Note on terminology}
\begin{itemize}
\item Econometrics and (bio)statistics use different words for things
\item A glossary can help, e.g. \cite{gunasekara_2008}
\item \emph{Endogenous regressor} $\sim$ Confounded independent variable
\end{itemize}
\end{frame}
\begin{frame}{IV assumptions}
\begin{enumerate}
\item Strongly related to the independent variable
\item Uncorrelated with confounders and only related to
outcome via the independent variable
\end{enumerate}
\end{frame}
\begin{frame}{Notation}
\begin{itemize}
\item $X_i$ independent variable
\item $Z_i$ instrumental variable
\item $Y_i$ outcome variable
\end{itemize}
\end{frame}
\begin{frame}{Two-stage least-squares regression}
First stage: regress independent variable on IV and
predict ``good part'' of independent variable
\begin{gather}
X_i = \underbrace{\pi_0 + \pi_1 Z_i}_\text{good} + \underbrace{\nu_i}_\text{bad} \\
\hat{X}_i = \hat{\pi}_0 + \hat{\pi}_1 Z_i
\end{gather}
Second stage: regress outcome on predictions to yield effect $\beta_1$
\begin{equation}
Y_i = \beta_0 + \beta_1 \hat{X}_i + \epsilon_i
\end{equation}
\end{frame}
\begin{frame}{Example: how do cigarette sales vary with price?}
\begin{itemize}
\item Does increasing the price of cigarettes cause a decrease in sales?
\begin{itemize}
\item \emph{Elasticity of demand}
\end{itemize}
\item Not confounding per se
\begin{itemize}
\item \emph{Simultaneous causality} (two-way causality) in supply and demand
\end{itemize}
\item Instrumental variable analysis can remove bias
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Setup}
%% begin.rcode vars, warning=FALSE, message=FALSE
%#### load the data set and get an overview
%library(AER)
%data("CigarettesSW")
%
%#### compute real per capita prices
%CigarettesSW$rprice <- with(CigarettesSW, price / cpi)
%
%#### compute the sales tax
%CigarettesSW$salestax <- with(CigarettesSW, (taxs - tax) / cpi)
%
%#### generate a subset for the year 1995
%c1995 <- subset(CigarettesSW, year == "1995")
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{Scatterplot}
%% begin.rcode scatter, fig.height=1.7, fig.width=3.4
%library(ggplot2)
%ggplot(data=c1995, aes(x=log(rprice), y=log(packs))) +
% geom_smooth(method='lm', formula= y~x) + geom_point()
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{Naive estimate}
%% begin.rcode lm
%summary(lm(log(packs)~log(rprice), data=c1995))$coefficients
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{Scatterplot (instrumental variable)}
%% begin.rcode scatter2, fig.height=1.7, fig.width=3.4
%library(ggplot2)
%ggplot(data=c1995, aes(x=salestax, y=log(rprice))) +
% geom_smooth(method='lm', formula= y~x) + geom_point()
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{First stage regression (1)}
%% begin.rcode fs1
%cig_s1 <- lm(log(rprice) ~ salestax, data = c1995)
%
%coeftest(cig_s1, vcov = vcovHC, type = "HC1")
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{First stage regression (2)}
%% begin.rcode fs2
%#### inspect the R^2 of the first stage regression
%summary(cig_s1)$r.squared
%
%#### store the predicted values
%lcigp_pred <- cig_s1$fitted.values
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{Second stage regression}
%% begin.rcode ss
%cig_s2 <- lm(log(c1995$packs) ~ lcigp_pred)
%coeftest(cig_s2, vcov = vcovHC)
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{Automated TSLS using \texttt{ivreg()}}
%% begin.rcode ivreg
%cig_ivreg <- ivreg(log(packs) ~ log(rprice) | salestax, data = c1995)
%coeftest(cig_ivreg, vcov = vcovHC, type = "HC1")
%
%% end.rcode
\end{frame}
\begin{frame}[fragile]{Check strength of instrument using $F$ test}
%% begin.rcode ftest, size='scriptsize'
%linearHypothesis(cig_s1, "salestax=0", vcov = vcovHC, type = "HC1")
%
%% end.rcode
\end{frame}
\begin{frame}{Additional worked examples}
\begin{itemize}
\item \emph{Causal Inference in Education}
\item \url{https://bookdown.org/aschmi11/causal_inf/instrumental-variables-estimation.html}
\item A tutorial on the use of instrumental variables in pharamcoepidemiology \cite{ertefaie_2017}
\end{itemize}
\end{frame}
\begin{frame}{References}
\tiny\bibliography{ivreg.bib}
\bibliographystyle{abbrvnat}
\end{frame}
\end{document}