Code Catalog

A one-page catalog of working scripts and quickstarts for np, npRmpi, and crs.
Keywords

np, npRmpi, crs, code examples, scripts, quickstart

This page is meant to be the quickest route to working scripts. It groups the existing code by package, task, and runtime mode so that you can scan, copy, and then drill down only if you need more context.

How to use this page

  • If you want only the smallest starter scripts, go first to Quickstarts.
  • If you want something you can paste into R immediately, start with the short blocks below.
  • If you want a fuller script, use the tables further down.
  • If you know a function name, use the site search box or the Reference page.

Minimal starters you can copy immediately

np: kernel regression

Source file: np_regression_quickstart.R

rm(list = ls())

## Minimal np regression example.
##
## The intended workflow is:
## 1. compute a bandwidth object,
## 2. fit the regression estimator,
## 3. inspect the result and a simple fitted curve.

library(np)
options(np.messages = FALSE)

data(cps71, package = "np")
dat <- cps71[, c("logwage", "age")]

bw <- npregbw(logwage ~ age, data = dat, regtype = "ll", bwmethod = "cv.aic")
fit <- npreg(bws = bw, data = dat)

summary(bw)
summary(fit)

plot(dat$age, dat$logwage, cex = 0.25, col = "grey")
o <- order(dat$age)
lines(dat$age[o], fitted(fit)[o], col = 2, lwd = 2)

Start here if you want the core np workflow in its simplest form. For more context, see Kernel Primer or Quickstarts.

np: density estimation

Source file: np_density_quickstart.R

rm(list = ls())

## Minimal np density-estimation example.
##
## The intended workflow is:
## 1. compute a bandwidth object,
## 2. fit the density estimator,
## 3. inspect the result.

library(np)
options(np.messages = FALSE)

data(faithful, package = "datasets")
dat <- data.frame(waiting = faithful$waiting)

bw <- npudensbw(~ waiting, data = dat, bwmethod = "cv.ml")
fhat <- npudens(bws = bw, data = dat)

summary(bw)
summary(fhat)

Start here if the object of interest is a density rather than a regression mean. For distribution, conditional-density, conditional-distribution, and quantile starters, see Quickstarts and Density, Distribution, Quantiles.

npRmpi: same workflow, MPI initialized once

Source file: nprmpi_session_quickstart.R

rm(list = ls())

## Minimal modern npRmpi example.
##
## The intended workflow is:
## 1. initialize MPI once in session/spawn mode,
## 2. write ordinary np-style code,
## 3. quit cleanly at the end.

library(npRmpi)

npRmpi.init(mode = "spawn", nslaves = 1)
on.exit(npRmpi.quit(), add = TRUE)

options(npRmpi.autodispatch = TRUE, np.messages = FALSE)

set.seed(1)
x <- runif(200)
y <- sin(2 * pi * x) + rnorm(200, sd = 0.2)
dat <- data.frame(y, x)

bw <- npregbw(y ~ x, regtype = "ll", bwmethod = "cv.ls", data = dat)
fit <- npreg(bws = bw, data = dat)

summary(bw)
summary(fit)

plot(dat$x, dat$y, cex = 0.35, col = "grey")
o <- order(dat$x)
lines(dat$x[o], fitted(fit)[o], col = 2, lwd = 2)

This is the modern default instructional path for npRmpi: initialize MPI once, then write ordinary np-style code. For mode details, see MPI and Large Data.

crs: spline regression

Source file: crs_quickstart.R

rm(list = ls())

## Minimal crs spline-regression example.
##
## This is the smallest useful workflow:
## 1. fit a spline model,
## 2. inspect the summary,
## 3. optionally move on to plotting or tighter search control.

library(crs)
options(crs.messages = FALSE)

set.seed(42)
n <- 250
x1 <- runif(n)
x2 <- runif(n)
y <- sin(2 * pi * x1) + x2 + rnorm(n, sd = 0.2)
dat <- data.frame(y, x1, x2)

fit <- crs(y ~ x1 + x2, data = dat)

summary(fit)

Use this when regression splines are the natural first tool. For the conceptual route, see Spline Primer.

np: kernel methods in serial

Script Main topic Functions/features Start reading here
np_regression_quickstart.R Minimal kernel-regression workflow npregbw, npreg Kernel Primer
np_density_quickstart.R Minimal density-estimation workflow npudensbw, npudens Density, Distribution, Quantiles
np_distribution_quickstart.R Minimal distribution-estimation workflow npudistbw, npudist Density, Distribution, Quantiles
np_conditional_density_quickstart.R Minimal conditional-density workflow npcdensbw, npcdens Density, Distribution, Quantiles
np_conditional_distribution_quickstart.R Minimal conditional-distribution workflow npcdistbw, npcdist Density, Distribution, Quantiles
np_quantile_quickstart.R Minimal quantile-regression workflow npcdistbw, npqreg Density, Distribution, Quantiles
np_classification_quickstart.R Minimal classification / conditional-mode workflow npconmode Classification and Modes
np_plotting_quickstart.R Minimal plotting and interval workflow npreg, plot, predict Plotting and Intervals
np_entropy_quickstart.R Minimal entropy/testing workflow npunitest Entropy and Testing
np_semiparametric_quickstart.R Minimal semiparametric workflow npplreg Semiparametric Models
np_significance_quickstart.R Minimal significance-testing workflow npreg, npsigtest Significance and Specification
np_specification_quickstart.R Minimal model-specification workflow lm, npcmstest Significance and Specification

Additional serial scripts

Script Main topic Functions/features Start reading here
regression_intro_a.R Univariate regression and plotting npreg, local constant, local linear Kernel Primer
regression_intro_b.R Derivative estimation npreg, derivative output Plotting and Intervals
regression_intro_c.R Plotting fitted objects and intervals plot(), asymptotic and bootstrap error handling Plotting and Intervals
regression_multivar_a.R Multivariate regression mixed predictors, multivariate fit Multivariate Regression and Prediction
demo_poly.R Generalized local polynomial comparison npglpreg, crs, local polynomial comparison Worked Examples

npRmpi: modern session-mode quick start

If you want one current downloadable script before anything else, start here.

Script Main topic Functions/features Start reading here
nprmpi_session_quickstart.R Modern session / spawn workflow npRmpi.init, npregbw, npreg, autodispatch MPI and Large Data
nprmpi_attach_quickstart.R Modern attach-mode workflow npRmpi.init(mode = \attach\), npregbw, npreg MPI and Large Data
nprmpi_profile_quickstart.R Modern profile/manual-broadcast workflow mpi.bcast.cmd, np.mpi.initialize, npregbw, npreg MPI and Large Data

Inline source for common scripts

The blocks below are pulled directly from the script files in www/, so they stay aligned with the downloadable versions.

Source file: np_regression_quickstart.R

rm(list = ls())

## Minimal np regression example.
##
## The intended workflow is:
## 1. compute a bandwidth object,
## 2. fit the regression estimator,
## 3. inspect the result and a simple fitted curve.

library(np)
options(np.messages = FALSE)

data(cps71, package = "np")
dat <- cps71[, c("logwage", "age")]

bw <- npregbw(logwage ~ age, data = dat, regtype = "ll", bwmethod = "cv.aic")
fit <- npreg(bws = bw, data = dat)

summary(bw)
summary(fit)

plot(dat$age, dat$logwage, cex = 0.25, col = "grey")
o <- order(dat$age)
lines(dat$age[o], fitted(fit)[o], col = 2, lwd = 2)

Source file: np_density_quickstart.R

rm(list = ls())

## Minimal np density-estimation example.
##
## The intended workflow is:
## 1. compute a bandwidth object,
## 2. fit the density estimator,
## 3. inspect the result.

library(np)
options(np.messages = FALSE)

data(faithful, package = "datasets")
dat <- data.frame(waiting = faithful$waiting)

bw <- npudensbw(~ waiting, data = dat, bwmethod = "cv.ml")
fhat <- npudens(bws = bw, data = dat)

summary(bw)
summary(fhat)

Source file: np_distribution_quickstart.R

rm(list = ls())

## Minimal np distribution example.
##
## This mirrors the usual density workflow but targets the
## unconditional distribution function instead.

library(np)
options(np.messages = FALSE)

data(faithful, package = "datasets")
dat <- data.frame(waiting = faithful$waiting)

bw <- npudistbw(~ waiting, data = dat, nmulti = 1)
Fhat <- npudist(bws = bw, data = dat)

summary(bw)
summary(Fhat)

Source file: np_conditional_density_quickstart.R

rm(list = ls())

## Minimal np conditional-density example.
##
## This keeps the first run small enough to be practical while still
## showing the standard two-step workflow:
## 1. compute a bandwidth object,
## 2. fit the conditional-density estimator.

library(np)
options(np.messages = FALSE)

data(faithful, package = "datasets")
dat <- faithful[seq_len(120), c("eruptions", "waiting")]

bw <- npcdensbw(eruptions ~ waiting, data = dat, nmulti = 1)
fhat <- npcdens(bws = bw, data = dat)

summary(bw)
summary(fhat)

Source file: np_conditional_distribution_quickstart.R

rm(list = ls())

## Minimal np conditional-distribution example.
##
## This uses a small first run so the standard two-step workflow stays
## copyable and practical.

library(np)
options(np.messages = FALSE)

data(faithful, package = "datasets")
dat <- faithful[seq_len(120), c("eruptions", "waiting")]

bw <- npcdistbw(eruptions ~ waiting, data = dat, nmulti = 1)
Fhat <- npcdist(bws = bw, data = dat)

summary(bw)
summary(Fhat)

Source file: np_quantile_quickstart.R

rm(list = ls())

## Minimal np quantile-regression example.
##
## The key idea is to compute a conditional-distribution bandwidth once
## and then reuse it for more than one quantile.

library(np)
options(np.messages = FALSE)

data(faithful, package = "datasets")
dat <- faithful[seq_len(120), c("eruptions", "waiting")]

bw <- npcdistbw(eruptions ~ waiting, data = dat, nmulti = 1)
q25 <- npqreg(bws = bw, tau = 0.25)
q50 <- npqreg(bws = bw, tau = 0.50)
q75 <- npqreg(bws = bw, tau = 0.75)

summary(bw)
summary(q50)

plot(dat$waiting, dat$eruptions, cex = 0.35, col = "grey")
o <- order(dat$waiting)
lines(dat$waiting[o], q25$quantile[o], col = 2, lty = 2, lwd = 2)
lines(dat$waiting[o], q50$quantile[o], col = 4, lty = 1, lwd = 2)
lines(dat$waiting[o], q75$quantile[o], col = 2, lty = 3, lwd = 2)

Source file: np_classification_quickstart.R

rm(list = ls())

## Minimal np classification / conditional-mode example.
##
## This keeps the first run compact while still showing the basic
## nonparametric classification route.

library(np)
data(birthwt, package = "MASS")

birthwt$low <- factor(birthwt$low)
birthwt$smoke <- factor(birthwt$smoke)
birthwt$race <- factor(birthwt$race)

fit <- npconmode(low ~ smoke + race + age + lwt, data = birthwt, nmulti = 1)

summary(fit)
fit$confusion.matrix

Source file: np_plotting_quickstart.R

rm(list = ls())

## Minimal np plotting and interval example.
##
## This fits a simple model, saves one asymptotic-interval plot, and
## shows the prediction route on a small evaluation grid.

library(np)
options(np.messages = FALSE)

data(cps71, package = "np")

fit <- npreg(
  logwage ~ age,
  regtype = "ll",
  bwmethod = "cv.aic",
  gradients = TRUE,
  data = cps71
)

plot_path <- file.path(tempdir(), "np_plotting_quickstart.png")
png(plot_path, width = 700, height = 500)
plot(fit, plot.errors.method = "asymptotic", plot.errors.style = "band")
dev.off()

pred_grid <- data.frame(age = seq(20, 60, by = 10))
predict(fit, newdata = pred_grid)

cat("Saved plot to:", plot_path, "\n")

Source file: np_entropy_quickstart.R

rm(list = ls())

## Minimal np entropy/testing example.
##
## This uses the univariate density-equality test because it is a
## compact, fast first run.

library(np)
options(np.messages = FALSE)

set.seed(1234)
n <- 300
x <- rnorm(n)
y <- rnorm(n)

test_out <- npunitest(x, y, bootstrap = FALSE)

summary(test_out)

Source file: np_semiparametric_quickstart.R

rm(list = ls())

## Minimal np semiparametric example.
##
## This uses a partially linear model because it is the clearest
## lightweight entry point into the semiparametric family.

library(np)
options(np.messages = FALSE)

set.seed(42)
n <- 200
x1 <- rnorm(n)
z <- runif(n)
y <- 1 + 2 * x1 + sin(2 * pi * z) + rnorm(n, sd = 0.2)
dat <- data.frame(y, x1, z)

fit <- npplreg(y ~ x1 | z, data = dat)

summary(fit)

Source file: np_significance_quickstart.R

rm(list = ls())

## Minimal np significance-testing example.
##
## The idea is to fit a model with one irrelevant regressor and then
## ask whether the nonparametric significance test detects that.

library(np)
options(np.messages = FALSE)

set.seed(42)
n <- 200
z <- factor(rbinom(n, 1, 0.5))
x1 <- rnorm(n)
x2 <- runif(n, -2, 2)
y <- x1 + x2 + rnorm(n)
dat <- data.frame(z, x1, x2, y)

fit <- npreg(
  y ~ z + x1 + x2,
  regtype = "ll",
  bwmethod = "cv.aic",
  data = dat
)

test_out <- npsigtest(fit)

summary(fit)
summary(test_out)

Source file: np_specification_quickstart.R

rm(list = ls())

## Minimal np model-specification test example.
##
## The idea is to fit a simple linear model to nonlinear data and then
## ask whether the parametric specification looks too restrictive.

library(np)
options(np.messages = FALSE)

set.seed(42)
n <- 120
x <- runif(n, -2, 2)
y <- x + x^2 + rnorm(n, sd = 0.25)

model_ols <- lm(y ~ x, x = TRUE, y = TRUE)
X <- data.frame(x = x)

test_out <- npcmstest(
  model = model_ols,
  xdat = X,
  ydat = y,
  nmulti = 1
)

summary(model_ols)
summary(test_out)

Source file: regression_intro_a.R

rm(list=ls())

## Here is a simple illustration to help you get started with
## univariate kernel regression.

## First, let's grab some data from the np package

## Load the np package

library(np)

## Load the cps71 dataset contained in the np package

data(cps71)

## Attach the data so that the variables `logwage' and `age' can be
## called directly

attach(cps71)

## Plot the data (note it is sorted on age already which helps when
## plotting the lines below, and note the cex=0.25 uses circles that
## are 1/4 the standard size that are grey not black which is the
## default)

plot(age,logwage,cex=0.25,col="grey")

## Fit a (default) local constant model (since we do not explicitly
## call npregbw() which conducts least-squares cross-validated
## bandwidth selection by default it is automatically invoked when we
## call npreg())

model.lc <- npreg(logwage~age)

## Plot the fitted values (the colors and linetypes allow us to
## distinguish different plots on the same figure)

lines(age,fitted(model.lc),col=1,lty=1)

## Fit a local linear model (we use the arguments regtype="ll" to do
## this)

model.ll <- npreg(logwage~age,regtype="ll")

## Plot the fitted values with a different color and linetype

lines(age,fitted(model.ll),col=2,lty=2)

## Add a legend

legend("topleft",
       c("Local Constant","Local Linear"),
       lty=c(1,2),
       col=c(1,2),
       bty="n")

Source file: regression_multivar_a.R

rm(list=ls())

## Here is a simple illustration to help you get started with
## multivariate kernel regression and plotting via R's `plot' function
## (which calls the np function `npplot')

## First, let's grab some data from the np package

## Load the np package

library(np)

## Load the wage1 dataset contained in the np package

data(wage1)

## Attach the data so that the variables `lwage', `female', `educ' and
## `exper' can be called directly

attach(wage1)

## Fit a local linear model (since we do not explicitly call npregbw()
## which conducts least-squares cross-validated bandwidth selection by
## default it is automatically invoked when we call npreg()). Here we
## have a `factor' (female) and two `numeric' predictors (educ and
## exper - see ?wage1 for details)

model <- npreg(lwage~female+educ+exper,regtype="ll")

## model will be an object of class `npreg'. The generic R function
## `plot' will call `npplot' when it is deployed on an object of this
## type (see ?npplot for details on supported npplot
## arguments). Calling plot on a npreg object allows you to do some
## tedious things without having to write code such as including
## confidence intervals as the following example demonstrates. Note
## also that we do not explicitly have to specify `gradients=TRUE' in
## the call to npreg() as plot (npplot) will take care of this for
## us. Below we use the asymptotic standard error estimates and then
## take +- 1.96 standard error)

plot(model,plot.errors.method="asymptotic",plot.errors.style="band")

## We might also wish to use bootstrapping instead (here we bootstrap
## the standard errors and then take +- 1.96 standard error)

plot(model,plot.errors.method="bootstrap",plot.errors.style="band")

## Alternately, we might compute true nonparametric confidence
## intervals using (by default) the 0.025 and 0.975 percentiles of the
## pointwise bootstrap distributions

plot(model,plot.errors.method="bootstrap",plot.errors.type="quantiles",plot.errors.style="band")

## Note that adding the argument `gradients=TRUE' to the plot call
## will automatically plot the derivatives instead

plot(model,plot.errors.method="bootstrap",plot.errors.type="quantiles",plot.errors.style="band",gradients=TRUE)

Source file: nprmpi_session_quickstart.R

rm(list = ls())

## Minimal modern npRmpi example.
##
## The intended workflow is:
## 1. initialize MPI once in session/spawn mode,
## 2. write ordinary np-style code,
## 3. quit cleanly at the end.

library(npRmpi)

npRmpi.init(mode = "spawn", nslaves = 1)
on.exit(npRmpi.quit(), add = TRUE)

options(npRmpi.autodispatch = TRUE, np.messages = FALSE)

set.seed(1)
x <- runif(200)
y <- sin(2 * pi * x) + rnorm(200, sd = 0.2)
dat <- data.frame(y, x)

bw <- npregbw(y ~ x, regtype = "ll", bwmethod = "cv.ls", data = dat)
fit <- npreg(bws = bw, data = dat)

summary(bw)
summary(fit)

plot(dat$x, dat$y, cex = 0.35, col = "grey")
o <- order(dat$x)
lines(dat$x[o], fitted(fit)[o], col = 2, lwd = 2)

Source file: nprmpi_attach_quickstart.R

rm(list = ls())

## Minimal attach-mode npRmpi example.
##
## Launch with a pre-created MPI world, for example:
##   mpiexec -env R_PROFILE_USER "" -env R_PROFILE "" -n 2 \
##     Rscript --no-save nprmpi_attach_quickstart.R

library(npRmpi)

npRmpi.init(mode = "attach", comm = 1, autodispatch = TRUE)
options(np.messages = FALSE)

if (mpi.comm.rank(0L) == 0L) {
  set.seed(1)
  x <- runif(200)
  y <- sin(2 * pi * x) + rnorm(200, sd = 0.2)
  dat <- data.frame(y, x)

  bw <- npregbw(y ~ x, regtype = "ll", bwmethod = "cv.ls", data = dat)
  fit <- npreg(bws = bw, data = dat)

  summary(bw)
  summary(fit)

  npRmpi.quit(mode = "attach", comm = 1)
}

Source file: nprmpi_profile_quickstart.R

rm(list = ls())

## Minimal profile/manual-broadcast npRmpi example.
##
## Launch with an explicit profile source, for example:
##   RPROFILE=$(Rscript --no-save -e 'cat(system.file("Rprofile", package="npRmpi"))')
##   mpiexec -env R_PROFILE_USER "$RPROFILE" -env R_PROFILE "" -n 2 \
##     Rscript --no-save nprmpi_profile_quickstart.R

invisible(mpi.bcast.cmd(np.mpi.initialize(), caller.execute = TRUE))
invisible(mpi.bcast.cmd(options(np.messages = FALSE), caller.execute = TRUE))

set.seed(1)
x <- runif(200)
y <- sin(2 * pi * x) + rnorm(200, sd = 0.2)
dat <- data.frame(y, x)

invisible(mpi.bcast.Robj2slave(dat))

invisible(mpi.bcast.cmd(
  bw <- npregbw(y ~ x, regtype = "ll", bwmethod = "cv.ls", data = dat),
  caller.execute = TRUE
))

invisible(mpi.bcast.cmd(
  fit <- npreg(bws = bw, data = dat),
  caller.execute = TRUE
))

summary(bw)
summary(fit)

invisible(mpi.bcast.cmd(mpi.quit(), caller.execute = TRUE))

npRmpi: serial and MPI comparison scripts

These are older comparison scripts, but still useful if you want to see side-by-side serial and MPI routes. For new work, start with the session / spawn example above and then come back to these when you want route parity or legacy comparison.

Task Serial MPI Start reading here
Conditional density estimation npcdensls_serial.R npcdensls_npRmpi.R Density, Distribution, Quantiles
Model specification test npcmstest_serial.R npcmstest_npRmpi.R Significance and Specification
Conditional mode estimation npconmode_serial.R npconmode_npRmpi.R Classification and Modes
Density equality test npdeneqtest_serial.R npdeneqtest_npRmpi.R Entropy and Testing
Single index estimation npindexich_serial.R npindexich_npRmpi.R Semiparametric Models
Partially linear regression npplreg_serial.R npplreg_npRmpi.R Semiparametric Models
Local linear AIC bandwidth selection npregllaic_serial.R npregllaic_npRmpi.R Kernel Primer
Serial dependence test npsdeptest_serial.R npsdeptest_npRmpi.R Entropy and Testing
Significance test npsigtest_serial.R npsigtest_npRmpi.R Significance and Specification
Unconditional density estimation npudensml_serial.R npudensml_npRmpi.R Density, Distribution, Quantiles

npRmpi: local polynomial / constrained examples

Script Main topic
lp_k1_prodfunc.R Production-function style local polynomial example
lp_k1.R Local polynomial example
lp_radial_mean.R Constrained radial mean example
lp_radial_deriv.R Constrained radial derivative example

crs: spline examples

Script Main topic Notes
crs_quickstart.R Minimal spline-regression workflow modern quickstart
radial_rgl.R Bivariate radial spline surface requires rgl
sine_rgl.R Bivariate sine spline surface requires rgl
radial_constrained_mean.R Constrained spline mean estimation uses quadprog
radial_constrained_first_partial.R Constrained first derivative uses quadprog
radial_constrained_second_partial.R Constrained second derivative uses quadprog

Source file: crs_quickstart.R

rm(list = ls())

## Minimal crs spline-regression example.
##
## This is the smallest useful workflow:
## 1. fit a spline model,
## 2. inspect the summary,
## 3. optionally move on to plotting or tighter search control.

library(crs)
options(crs.messages = FALSE)

set.seed(42)
n <- 250
x1 <- runif(n)
x2 <- runif(n)
y <- sin(2 * pi * x1) + x2 + rnorm(n, sd = 0.2)
dat <- data.frame(y, x1, x2)

fit <- crs(y ~ x1 + x2, data = dat)

summary(fit)

Interactive demos for teaching and exploration

Script Main topic Notes
manipulate_density.R Univariate density with sliders RStudio + manipulate
manipulate_eruptions.R Old Faithful density RStudio + manipulate
manipulate_distribution.R Univariate distribution RStudio + manipulate
manipulate_bivariate_density.R Bivariate density RStudio + manipulate
manipulate_faithful_density.R Old Faithful bivariate density RStudio + manipulate
manipulate_bivariate_distribution.R Bivariate distribution RStudio + manipulate
manipulate_wage1.R Multivariate regression partial plots RStudio + manipulate
manipulate_npglpreg.R Generalized local polynomial regression RStudio + manipulate
manipulate_npglpreg_sin.R Sinusoidal GLP illustration RStudio + manipulate
manipulate_constrained_local_polynomial.R Constrained local polynomial regression requires quadprog
manipulate_constrained_local_polynomial_production.R Constrained production function requires quadprog
manipulate_constrained_spline.R Constrained spline surface requires quadprog
manipulate_constrained_spline_derivative.R Constrained spline derivative requires quadprog
manipulate_copula.R Copula plots requires mnormt
manipulate_socco.R SOCCO nonlinear-system demo requires data.RData and figures.R

Notes

  • The gallery still preserves the original scripts, but the site now aims to route you to them more directly.
  • The short blocks near the top are there so you can copy a minimal working pattern without downloading anything first.
  • For MPI-specific launch semantics, see MPI and Large Data.
  • For short copyable code blocks rather than full scripts, start with Worked Examples.
Back to top