knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-" )
library(npiv) set.seed(42)
This is the R package npiv (Nonparametric Instrumental Variables Estimation and Inference) written by Jeffrey S. Racine (racinej@mcmaster.ca) and co-authored and maintained by Timothy Christensen (timothy.christensen@yale.edu).
This package implements methods introduced in Chen, Christensen, and Kankanala (2024) for estimating and constructing uniform confidence bands for nonparametric structural functions using instrumental variables, including data-driven choice of tuning parameters. It also provides functionality to construct uniform confidence bands using the method of Chen and Christensen (2018). All methods in this package apply to nonparametric regression as a special case.
You can install by either downloading the zip ball or tar ball, decompress and run R CMD INSTALL
on it.
Alternatively, you can install the development version but before doing so Windows users have to first install Rtools, while OS X users have to first install Xcode and the command line tools (in OS X 10.9 or higher, once you have Xcode installed, open a terminal and run xcode-select --install). Note also that versions of e.g. Rtools are paired with versions of R so ensure you have the latest version of R installed prior to commencing this process.
After installing Rtools/Xcode and devtools (via install.packages("devtools")
), install the development package using the following command:
library(devtools); install_github('JeffreyRacine/npiv')
We present a simple example to Engel curve estimation using data from the British Family Expenditure Survey. We first load the data and sort on log household expenditure for plotting purposes. We generate a grid of log expenditure from 4.5 to 6.5 for plotting.
data("Engel95", package = "npiv") Engel95 <- Engel95[order(Engel95$logexp),] attach(Engel95) logexp.eval <- seq(4.5,6.5,length=100)
To nonparametrically regress Y
on X
using W
as an instrument, use npiv(Y ~ X | W)
or npiv(Y, X, W)
. By default, npiv
uses a data-driven choice of sieve dimension based on the method of Chen, Christensen, and Kankanala (2024). We include X.eval = logexp.eval
since we want to plot over a smaller region than the support of log expenditure.
food_engel <- npiv(food, logexp, logwages, X.eval = logexp.eval)
We plot the estimated function and 95% uniform confidence bands using the command plot
. By default, the confidence bands are generated using the method of Chen, Christensen, and Kankanala (2024). We include showdata = TRUE
to overlay the data points.
plot(food_engel, showdata = TRUE)
Confidence bands for the derivative of the structural function are plotted by including the argument type = "deriv"
.
plot(food_engel, type = "deriv")
We can estimate a conditional mean function by nonparametric regression with data-driven choice of sieve dimension simply by passing the regressor as the instrument. The following example estimates the Engel curve for food by regression of food
on logexp
:
food_engel_reg_uniform <- npiv(food, logexp, logexp, X.eval = logexp.eval) plot(food_engel_reg_uniform, showdata = TRUE)
The plot is wiggly, indicating that the algorithm has selected a fairly large sieve dimension. This can sometimes happen when the data are far from uniform, because the default method uses splines with knots placed uniformly over the support of the data. We can restore good performance by including knots = "quantiles"
to use splines with knots placed at the quantiles of the data.
food_engel_reg_quantiles <- npiv(food, logexp, logexp, X.eval = logexp.eval, knots = "quantiles") plot(food_engel_reg_quantiles, showdata = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.