Quickstarts

Small runnable scripts for the most common np, npRmpi, and crs entry points.
Keywords

quickstart, np, npRmpi, crs, examples, starter scripts

This page is the shortest route from “I need an example” to a small runnable script. The scripts below are the modern quickstarts in the gallery, and each block is pulled directly from the underlying source file.

Choose a starting point

If you want to… Start here
fit a first kernel regression np_regression_quickstart.R
estimate a density with np np_density_quickstart.R
estimate a distribution with np np_distribution_quickstart.R
estimate a conditional density with np np_conditional_density_quickstart.R
estimate a conditional distribution with np np_conditional_distribution_quickstart.R
fit nonparametric conditional quantiles with np np_quantile_quickstart.R
run a nonparametric classification / mode example np_classification_quickstart.R
save a fitted curve with asymptotic intervals np_plotting_quickstart.R
run an entropy/testing function with np np_entropy_quickstart.R
fit a semiparametric model with np np_semiparametric_quickstart.R
run a nonparametric significance test np_significance_quickstart.R
run a model-specification test np_specification_quickstart.R
use npRmpi interactively on macOS/Linux nprmpi_session_quickstart.R
use npRmpi under mpiexec nprmpi_attach_quickstart.R
use npRmpi with explicit broadcast control nprmpi_profile_quickstart.R
fit a first spline model with crs crs_quickstart.R

np quickstarts

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)

npRmpi quickstarts

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

crs quickstart

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)

Where to go next

Back to top