Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zeileis/ivreg

2SLS Regression with Diagnostics in R
https://github.com/zeileis/ivreg

instrumental-variables regression-diagnostics two-stage-least-squares-regression

Last synced: 2 months ago
JSON representation

2SLS Regression with Diagnostics in R

Awesome Lists containing this project

README

        

---
output: github_document
---

```{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")
```

2SLS

**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)
```

Effects plot
QQ plot
Influence plot