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

Plot methods display a fitted statistical object and, where supported, its derivatives or finite differences and uncertainty summaries. These operations can require additional fitting, influence calculations, or bootstrap work.

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-2 and np 0.80-2 releases, both available on CRAN.

  • 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

The former dotted spellings of compound public plot controls are retired rather than silently translated. For example, use data_rug, not data.rug, and render_control, not render.control. Plot-facing derivative order is gradient_order; estimator calls continue to use gradient.order.

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. The retired output = "both" route is replaced by output = "plot-data".

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

Save, inspect, and replay plotted data

Plot methods now return a common np_plot_data object when you request output = "data" or output = "plot-data". The former computes without drawing; the latter draws and returns the same numerical schema. Interval columns are absolute endpoints rather than offsets from the estimate.

## Compute the plotted curve once, inspect it, then replay it without refitting
plot_data <- plot(model.ll, output = "data")
curve_data <- as.data.frame(plot_data)
head(curve_data)
plot(plot_data)

For plots with several panels, targets, or bands, select them explicitly in as.data.frame(plot_data, panel = ..., target = ..., band = ...). Replaying with plot(plot_data) uses the retained geometry and does not refit, resample, or consume random numbers.

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.

WarningBias-corrected density intervals remain experimental

Version 0.80-2 extends corrected asymptotic and bootstrap plotting routes for selected fixed-bound Gaussian, beta, uniform, and Epanechnikov density kernels. These bias-corrected intervals and bands are experimental pre-beta research, incomplete, and not recommended for general-purpose inference. Their availability depends on the kernel, support, dimension, target grid, and calibration requested; an unavailable joint band is reported rather than silently replaced by another method. The underlying density estimates and ordinary no-error plots are not redefined by these opt-in interval routes.

Gradients

For a regression function \(g(x)=E(Y\mid X=x)\), numeric-predictor panels show \(\beta_j(x)=\partial g(x)/\partial x_j\) (or the requested higher-order derivative). Unordered factors compare each level with the first retained level; ordered factors use adjacent-level differences. The categorical panels therefore show \(\Delta_j g(x)\), not a derivative with respect to an artificial numeric coding.

## 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 intervals use simulated sampling variation and can be substantially more expensive than asymptotic intervals.

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