-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
70 lines (53 loc) · 2.46 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file and run rmarkdown::render("README.Rmd") -->
```{r, include = FALSE}
knitr::opts_chunk$set(
fig.path = "man/figures/README-"
)
```
## Two-Stage Least-Squares Regression with Diagnostics
An implementation of instrumental variables regression using two-stage least-squares
(2SLS) estimation, based on the `ivreg()` function previously in the
[AER](https://CRAN.R-project.org/package=AER) package. In addition to standard regression
functionality (parameter estimation, inference, predictions, etc.) the package provides
various regression diagnostics, including hat values, deletion diagnostics such as
studentized residuals and Cook's distances; graphical diagnostics such as
component-plus-residual plots and added-variable plots; and effect plots with partial
residuals.
**Instrumental variables regression:**
```
library("ivreg")
ivreg(Q ~ P + D | D + F + A, data = Kmenta)
```
**Via two-stage least squares (2SLS):**
```{r 2sls, echo=FALSE, results="hide"}
exams::tex2image("\\begin{eqnarray*}
y & = & X \\beta + \\varepsilon \\\\
\\widehat{X} & = & Z (Z^\\top Z)^{-1} Z^\\top X \\\\
\\widehat{\\beta}_{\\mathrm{2SLS}} & = & (\\widehat{X}^\\top \\widehat{X})^{-1} \\widehat{X}^\\top y
\\end{eqnarray*}",
format = "svg", header = "", pt = 14,
dir = file.path(getwd(), "man", "figures"), name = "README-2sls")
```
<img alt="2SLS" src="man/figures/README-2sls.svg" style="border:10px solid transparent">
**With diagnostics:**
```{r effects, echo=FALSE, results="hide", message=FALSE, fig.height=4.7, fig.width=5, fig.show="hide"}
library("ivreg")
library("effects")
library("car")
deq <- ivreg(Q ~ P + D | D + F + A, data = Kmenta)
plot(predictorEffect("P", deq, residuals = TRUE), partial.residuals = list(span = 1))
```
```{r qqplot, echo=FALSE, results="hide", message=FALSE, fig.height=4.7, fig.width=5, fig.show="hide"}
qqPlot(deq, main = "QQ plot")
## mtext("QQ plot", line = 1.5, cex = 1.2, font = 2)
```
```{r influenceplot, echo=FALSE, results="hide", message=FALSE, fig.height=4.7, fig.width=5, fig.show="hide"}
influencePlot(deq, main = "")
mtext("Influence plot", line = 2, cex = 1.2, font = 2)
```
<img alt="Effects plot" src="man/figures/README-effects-1.png" width="33%">
<img alt="QQ plot" src="man/figures/README-qqplot-1.png" width="33%">
<img alt="Influence plot" src="man/figures/README-influenceplot-1.png" width="33%">