Plotting and Intervals

Practical guidance on plotting fitted objects, gradients, intervals, and object-driven graphics in np.
Keywords

plot.np, plotting, confidence intervals, bootstrap intervals, gradients

Plotting is one of the places where np becomes especially useful, because the fitted object often carries much more than a point estimate. The package also supports asymptotic and bootstrap-based uncertainty summaries, but those come with real computational cost.

If you want a minimal downloadable script for this route, start with np_plotting_quickstart.R.

NoteCurrent plotting interface

This page tracks the current np / npRmpi 0.70-2 plotting interface on CRAN as of Friday, May 15, 2026.

  • use errors, bootstrap, band, alpha, and B for the main interval controls
  • use snake_case names such as data_rug, data_overlay, common_scale, and coef_index
  • use np_boot_control(), np_grid_control(), and np_render_control() only for the remaining detailed control groups

If you are adapting older code after installing the new release, the most common translation is from plot.errors.* names to these newer public arguments.

A simple fitted-curve plot

Start with the smallest possible case.

## Load the package and a simple example dataset
library(np)
data(cps71, package = "np")

## Fit a local-linear model, then ask plot() for bootstrap error bounds
model.ll <- npreg(logwage ~ age, regtype = "ll", data = cps71)
plot(model.ll,
  errors = "bootstrap",
  bootstrap = "inid",
  band = "all")

Asymptotic intervals

For many users, asymptotic intervals are the first reasonable uncertainty summary because they are much lighter than full bootstrap procedures.

## Add asymptotic error bounds to the same fitted object
plot(model.ll, errors = "asymptotic")

Gradients

For derivative views, request gradients in the plot call.

## Request derivative plots from the same fitted object
plot(model.ll, gradients = TRUE)

And again with asymptotic intervals:

## Combine derivative plots with asymptotic error bounds
plot(model.ll, gradients = TRUE, errors = "asymptotic")

Bootstrap intervals

Bootstrap-based intervals are often appealing because they are less tied to asymptotic formulas, but they can be slow.

## Use the bootstrap route when you want simulation-based intervals
plot(model.ll, errors = "bootstrap")

For serious work, this is exactly the sort of route where separating bandwidth selection from later estimation and plotting is especially worthwhile.

Interactive rgl rendering and rugs

When rgl is installed, plot methods can also render interactive 3D surfaces via renderer = "rgl". That gives you a manipulable display: drag with the mouse to rotate and use the scroll wheel to zoom. The same plotting surface now also supports 1D/2D rugs and data-overlay controls via the direct data_rug and data_overlay arguments.

These are best viewed in RStudio, where the interactive surface appears in the Viewer pane rather than the Plot pane.

If you want a minimal runnable script for this route, use np_rgl_quickstart.R.

## Fit a small bivariate model, then draw it with the interactive renderer
library(np)
data(wage1, package = "np")

fit_rgl <- npreg(lwage ~ educ + exper,
  regtype = "ll",
  data = wage1)

plot(fit_rgl,
  view = "fixed",
  renderer = "rgl",
  errors = "asymptotic",
  data_overlay = FALSE,
  data_rug = TRUE)

The same renderer is also useful for conditional density/distribution surfaces when you want to inspect shape changes interactively rather than through a fixed static viewpoint.

Partial regression plots

For multivariate models, plot() typically constructs partial regression plots: one covariate varies while the others are held fixed at representative values such as medians or modes.

That is why these plots are often smoother and easier to read than simply plotting raw data against one regressor at a time in a multivariate setting.

A multivariate example

## Build a multivariate fit, then let plot() create the partial views
library(np)
data(wage1, package = "np")

fit_multi <- npreg(lwage ~ female + married + educ + exper + tenure,
  regtype = "ll",
  data = wage1)

plot(fit_multi, errors = "bootstrap")

This is a high-value visualization, but it is not a cheap one.

Prediction

Once a model is fitted, prediction is straightforward. Supply new explanatory data via newdata.

## Predict on a small evaluation grid
cps_eval <- data.frame(age = seq(10, 70, by = 10))
predict(model.ll, newdata = cps_eval)

If you request se.fit = TRUE, the prediction route will also return asymptotic standard errors where supported.

A practical warning about runtime

Plotting can trigger real computation, especially when:

  • gradients are requested,
  • asymptotic errors are recovered,
  • bootstrap intervals are requested,
  • the model itself is multivariate or expensive.

So if a plot does not appear instantly, that does not necessarily mean anything is wrong.

One common source of confusion

The values returned by gradients(fit) are not the same object as the smooth partial-regression plots generated by plot(fit, gradients = TRUE) in multivariate settings. The plot method is constructing an evaluation path that is easier to interpret visually, while gradients(fit) returns derivative information tied to the model object’s evaluation structure.

Back to top