Plotting and Intervals

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

npplot, 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.

A simple fitted-curve plot

Start with the smallest possible case.

library(np)
data(cps71, package = "np")

model.ll <- npreg(logwage ~ age, regtype = "ll", data = cps71)
plot(
  model.ll,
  plot.errors.method = "bootstrap",
  plot.errors.boot.method = "inid",
  plot.errors.boot.num = 9999,
  plot.errors.type = "all"
)

Asymptotic intervals

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

plot(model.ll, plot.errors.method = "asymptotic")

Gradients

For derivative views, request gradients in the plot call.

plot(model.ll, gradients = TRUE)

And again with asymptotic intervals:

plot(model.ll, gradients = TRUE, plot.errors.method = "asymptotic")

Bootstrap intervals

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

plot(
  model.ll,
  plot.errors.method = "bootstrap",
  plot.errors.boot.num = 25
)

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 via plot.rug = TRUE.

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.

library(np)
data(wage1, package = "np")

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

plot(
  fit_rgl,
  view = "fixed",
  renderer = "rgl",
  plot.data.overlay = FALSE,
  plot.errors.method = "asymptotic",
  plot.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

library(np)
data(wage1, package = "np")

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

plot(
  fit_multi,
  plot.errors.method = "bootstrap",
  plot.errors.boot.num = 25
)

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.

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