Worked Examples
worked examples, npreg, regression examples, local linear, local constant
Regression estimates the conditional mean \(g(x)=E(Y\mid X=x)\). The examples below show local-constant and local-linear estimates of \(g(x)\), followed by a mixed-data bandwidth workflow. They are teaching examples rather than reproductions of published empirical estimates.
A first conditional-mean example
This is a streamlined version of regression_intro_a.R. The bundled cps71 data contain 205 male grade-13 graduates sampled from the 1971 Canadian Census Public Use Tapes. The data were supplied by Aman Ullah and are documented in Pagan and Ullah (1999).
## Estimate g(age) with local-constant and local-linear regressions
library(np)
data(cps71, package = "np")
fit_lc <- npreg(logwage ~ age, data = cps71)
fit_ll <- npreg(logwage ~ age, data = cps71, regtype = "ll")
## Plot the first fit, then add the second fit and a legend
plot(fit_lc)
age_order <- order(cps71$age)
lines(cps71$age[age_order], fitted(fit_ll)[age_order],
col = 2, lty = 2)
legend("topleft",
c("Local constant", "Local linear"),
lty = c(1, 2),
col = c(1, 2),
bty = "n")A small bandwidth-selection example
The next block is a Gallery-authored simulation. It isolates the workflow for mixed continuous and unordered predictors; it is not adapted from an empirical application.
## Simulate a small mixed-data example and choose the bandwidth explicitly
library(np)
set.seed(42)
n <- 1000
x <- runif(n)
z1 <- rbinom(n, 1, 0.5)
z2 <- rbinom(n, 1, 0.5)
y <- cos(2 * pi * x) + z1 + rnorm(n, sd = 0.25)
z1 <- factor(z1)
z2 <- factor(z2)
mydat <- data.frame(y, x, z1, z2)
## Fit from the saved bandwidth object, then inspect both summaries
bw <- npregbw(y ~ x + z1 + z2, regtype = "ll", bwmethod = "cv.aic", data = mydat)
fit <- npreg(bws = bw, data = mydat)
summary(bw)
summary(fit)Script library
- Simple univariate local constant and local linear regression: regression_intro_a.R
- Derivative estimation: regression_intro_b.R
- Plotting fitted objects and intervals: regression_intro_c.R
- Multivariate regression: regression_multivar_a.R
For a fuller multivariate workflow, see Multivariate Regression and Prediction.
Causal inference examples
Causal Inference with Local Polynomials uses npreg for a sharp regression-discontinuity estimate and a randomized-trial treatment-effect curve. The standalone scripts reproduce author-package results, including bias-corrected inference; the trial example adds a simultaneous band over a fixed evaluation grid.
Notes
- Use the full scripts when you want more commentary than the short code blocks provide.
- Some examples call for additional packages. Where that is the case, the script itself says so.
- The older
npglpregroute is deprecated. For a functional replacement, usenpreg(..., nomad = "auto")innpornpRmpi. - The
wage1multivariate example is available under Multivariate Regression and Prediction. - For plotting objects, gradients, and uncertainty summaries, see Plotting and Intervals.
Reference
Pagan, A. and Ullah, A. (1999), Nonparametric Econometrics, Cambridge University Press.