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.

The code blocks are Gallery-authored teaching examples built on two datasets bundled with np; they do not reproduce published empirical estimates. cps71 is the 1971 Canadian Census wage sample supplied by Aman Ullah and documented by Pagan and Ullah (1999). wage1 is the 1976 U.S. Current Population Survey wage sample supplied by Jeffrey M. Wooldridge and documented by Wooldridge (2000).

NoteCurrent plotting interface

This page reflects the npRmpi 0.80-1 and np 0.80-1 releases, both available on CRAN.

  • use errors, bootstrap, band, alpha, and B for the main interval controls
  • use dotted 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

The former underscore spellings of public plot controls are retired rather than silently translated. For example, use data.rug, not data_rug, and render.control, not render_control.

In estimator calls, uncertainty is requested with se = TRUE; predictions use se.fit = TRUE, and gradients(object, se = TRUE) requests gradient standard errors. Plot methods deliberately retain the method-valued control errors = "asymptotic" or errors = "bootstrap".

Older plotting code that uses plot.errors.* names should translate those controls to the public arguments above.

Fitted objects and their bandwidth objects now retain the prepared sample used for plotting, including subset and missing-data decisions. Copula grid size is the exception to ordinary post-fit grid adjustment: set neval or supply u when constructing npcopula(), because plot.npcopula() rejects neval.

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

Simultaneous asymptotic variability bands

Supported plots can calibrate a Gaussian simultaneous band over the finite evaluation grid within each panel. Use band = "all" to compare pointwise, simultaneous, and Bonferroni bands on the same display.

## Compare all supported asymptotic band constructions on one panel
plot(model.ll, errors = "asymptotic", band = "all")

This common interface now covers supported regression, single-index, smooth-coefficient, partially linear, density/distribution, conditional quantile, location-scale quantile, class-probability, and copula plots, including eligible level surfaces. gaussian.draws controls the Gaussian maximum simulation and defaults to 9,999; random.seed makes that calibration reproducible without changing the caller’s RNG state.

These are selected-smoothing variability bands over the displayed finite grid. They do not correct smoothing bias, include bandwidth- or degree-selection uncertainty, cover points between the grid, or provide joint coverage across separate panels, quantiles, or response levels. If a joint band is not identified for a nonregular, degenerate, or resource-limited request, the plot reports the simultaneous limits as unavailable while retaining any valid pointwise and Bonferroni results.

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

Current np and npRmpi releases also support bias-corrected bootstrap centering on the supported fixed-bandwidth plot families. Unsupported routes fail explicitly rather than silently drawing a different interval.

For fixed-bandwidth conditional density/distribution and gradient plots, the bias-corrected center uses a mixed-data pilot matched to the selected response and conditioning kernels and retains the selected LC/LL/LP estimator. These explicitly requested centers can differ from earlier releases; ordinary fitted values and estimate-centered bootstrap variability draws are unchanged.

Current bootstrap implementations avoid unnecessary public-facing quadratic storage on ordinary default grids. The default evaluation grids for single-index, conditional-mode, and partially-linear plots grow linearly with the training sample size; an explicit request such as neval = ntrain remains under user control and can still be substantially heavier. Fixed local-polynomial plot bootstraps use a shared count-compressed, bounded-ridge response solver across the supported regression and conditional families. Long bootstrap plots retain one call-scoped elapsed clock and provide an approximate overall ETA across scheduled targets.

Fixed and geometric plot-bootstrap draws are replication ordered, so a fixed seed no longer depends on bootstrap chunk size or the number of MPI workers. That stronger reproducibility contract can change seeded bands relative to an older package version.

## Ask supported bootstrap plots to use bias-corrected centers
plot(model.ll,
  errors = "bootstrap",
  center = "bias-corrected")

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.

The current widget uses a high-DPI-aware canvas and construction-aware interval legends while preserving ordinary rgl.* overrides.

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.

Data references

Pagan, A. and Ullah, A. (1999), Nonparametric Econometrics, Cambridge University Press. Wooldridge, J. M. (2000), Introductory Econometrics: A Modern Approach, South-Western College Publishing.

Back to top