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

fit <- npreg(
  logwage ~ age,
  regtype = "ll",
  bwmethod = "cv.aic",
  gradients = TRUE,
  data = cps71
)

plot(cps71$age, cps71$logwage, cex = 0.1, xlab = "age", ylab = "log(wage)")
lines(cps71$age, fitted(fit), col = "blue")

Asymptotic intervals

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

plot(fit, plot.errors.method = "asymptotic")

Gradients

If the model was fitted with gradients = TRUE, you can also look at derivative information.

plot(fit, gradients = TRUE)

And again with asymptotic intervals:

plot(fit, 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(
  fit,
  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.

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(fit, 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