Video Demos

Short, reproducible video demonstrations for np and npRmpi, with captions, transcripts, and exact R scripts.
Keywords

video demos, RStudio, np, npRmpi, reproducible examples

These short demonstrations show the finished result quickly, then reveal the few essential R statements needed to reproduce it. Every entry includes captions, a transcript, the exact installed-build script, and direct download links. Longer installation and platform instructions remain on Install and Get Started.

Onboarding and first results

Function: npreg()
Takeaway: one call selects the smoothing parameter and fits the relationship.

R is the computational environment; RStudio is the interface used here to write and run the analysis. This walkthrough moves from loading np to a fitted curve using the package’s bundled wage data.

Open or download the MP4 · Download the captions · Read the transcript

Install the public package once:

install.packages("np")

Then run the exact demonstrated analysis:

## Your first np result.
## Last verified with the public CRAN np 0.70-5 build.

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

fit_first <- npreg(logwage ~ age, data = cps71)
summary(fit_first$bws)
plot(fit_first, main = "A first np regression")

Download the R script.

The example uses public CRAN np 0.70-5 and the 205 observations in the bundled cps71 data. No seed is needed because the example neither simulates nor resamples data. Least-squares cross-validation selects an age bandwidth of 1.892158 in the verified installed build.

Transcript

R is the computing environment, and RStudio is where we work.

Install np once, load it, and use the bundled wage data.

One npreg() call selects the smoothing parameter and fits the relationship.

The summary records the selected bandwidth. The plot shows the result.

Your first reproducible np analysis is complete, and the full script is on the Gallery Video Demos page.

Once this works, continue to Data Preparation to learn how continuous, unordered, and ordered variables should be represented before using your own data.

Analytical demonstrations

The Italy sequence uses the same public dataset to connect conditional density, conditional distribution, and direct conditional quantiles without turning any one short into a full theoretical treatment. See How the four targets connect for the population definitions and the important target-specific smoothing distinction. A fourth Italy demonstration isolates how the R class assigned to a predictor propagates through smoothing, plotting, and gradient semantics.

Function: npcdens()
Takeaway: the entire conditional density can change shape, not merely its average.

The bundled Italy data follow per-capita GDP in 21 regions over 48 observed years from 1951 through 1998. year is already an ordered factor, so the estimator respects its ordered discrete support. The estimated density is mainly unimodal after the war and develops two clear peaks in later years, corresponding descriptively to the wealthier North and less wealthy South.

Open or download the MP4 · Download the captions · Read the transcript

## Italy conditional-density short.
## Last verified with the public CRAN np 0.70-5 build.

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

class(Italy$year)
fit_cdens <- npcdens(gdp ~ year, data = Italy)
summary(fit_cdens$bws)
plot(fit_cdens, view = "fixed", theta = 90, phi = 45)

Download the R script.

The verified public CRAN np 0.70-5 build selected an ordered-year smoothing parameter of 0.613508 and a GDP bandwidth of 0.570879. No seed is needed. These values are selected for the conditional-density target.

Transcript

Per-capita GDP across 21 Italian regions begins mainly unimodal after the war.

Here, year is an ordered factor, so npcdens() smooths across observed year levels.

Across later years, two clear peaks emerge, corresponding to the wealthier North and less wealthy South.

One call reveals the whole conditional density, not merely an average.

The full script is on Gallery Video Demos.

Function: npcdist()
Takeaway: for each ordered year and GDP value, the surface estimates the share of regions at or below that value.

Integrating a conditional density gives the conditional distribution at the population level. This episode estimates the conditional distribution directly. It does not obtain the fitted surface by numerically integrating the preceding video’s density estimate.

Open or download the MP4 · Download the captions · Read the transcript

## Italy conditional-distribution short.
## Last verified with the public CRAN np 0.70-5 build.

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

class(Italy$year)
fit_cdist <- npcdist(gdp ~ year, data = Italy)
summary(fit_cdist$bws)
plot(fit_cdist, view = "fixed", theta = 90, phi = 45)

Download the R script.

The verified public build selected an ordered-year smoothing parameter of 0.687927 and a GDP bandwidth of 0.307366 for the conditional-distribution target. Five fresh installed runs were exactly identical for the selected bandwidth object and fitted result. No seed is needed.

Transcript

Integrating the conditional density produces the conditional distribution.

Here, for each ordered year, the surface gives the estimated share of Italian regions with per-capita GDP at or below each value.

One npcdist() call estimates the whole conditional distribution.

Invert the conditional distribution at a chosen probability and you obtain a conditional quantile.

The full script is on Gallery Video Demos.

Function: npqreg()
Takeaway: choose probabilities and invert the fitted conditional distribution to obtain the corresponding conditional quantiles.

This example requests the 25th, 50th, and 75th conditional quantiles of regional GDP for each ordered year. The default plot overlays those curves on the observed per-year distributions.

Open or download the MP4 · Download the captions · Read the transcript

## Italy conditional-quantile short.
## Last verified with the public CRAN np 0.70-5 build.

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

class(Italy$year)
fit_qreg <- npqreg(gdp ~ year,
                   data = Italy,
                   tau = c(0.25, 0.50, 0.75))
summary(fit_qreg$bws)
plot(fit_qreg)

Download the R script.

npqreg() first computes bandwidths optimized for the conditional-distribution target, estimates that distribution, and then inverts it at the requested \(\tau\in(0,1)\) values. With the same data and selection settings, the verified bandwidth values match the preceding npcdist() example. This is a distribution-based quantile route. nplsqreg() is a separate, regression-based location-scale method. No seed is needed.

Transcript

Choose a probability, tau, and invert the fitted conditional distribution. The result is the GDP value where that distribution reaches tau. Here, one npqreg() call directly traces the 25th, 50th, and 75th conditional quantiles across Italy’s ordered years. The curves show the lower, middle, and upper parts of the distribution. The full script is on Gallery Video Demos.

Function: npreg()
Takeaway: give the predictor the R class that represents its statistical meaning; np then carries that type through kernel choice, cross-validation, the fitted plot, and the meaning of a gradient.

The bundled Italy$year is an ordered factor. For comparison, this episode stores the same displayed year values as numeric in a second data frame. The response observations are unchanged: only the statistical meaning assigned to the predictor differs.

Open or download the MP4 · Download the captions · Read the transcript

## Italy ordered-factor versus numeric gradient short.
## Last verified with the public CRAN np 0.70-5 build.

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

# The bundled data correctly stores year as an ordered factor.
class(Italy$year)

# Re-express the same displayed years as numeric for comparison.
italy_numeric <- transform(
  Italy,
  year = as.numeric(as.character(year))
)
class(italy_numeric$year)

fit_ordered <- npreg(gdp ~ year, data = Italy)
fit_numeric <- npreg(gdp ~ year, data = italy_numeric)

summary(fit_ordered$bws)
summary(fit_numeric$bws)

options(plot.par.mfrow = FALSE)
par(mfrow = c(2, 2))

plot(fit_ordered,
     main = "Ordered year: fitted mean",
     xlab = "Year", ylab = "GDP")
plot(fit_numeric,
     main = "Numeric year: fitted mean",
     xlab = "Year", ylab = "GDP")
plot(fit_ordered, gradients = TRUE,
     main = "Ordered year: finite difference",
     xlab = "Year")
plot(fit_numeric, gradients = TRUE,
     main = "Numeric year: derivative",
     xlab = "Year")

Download the R script.

No kernel or gradient switch is required in either npreg() call. The variable class determines the appropriate route automatically:

  • Ordered factor: the verified fit uses the Li–Racine ordered kernel, cross-validates the categorical smoothing parameter lambda = 0.7046792, plots level-specific fitted values, and reports Delta as an adjacent-ordered-level finite difference.
  • Numeric: the verified fit uses a second-order Gaussian kernel, cross-validates the continuous bandwidth h = 3.337488, plots a smooth fitted curve, and reports the analytic derivative with respect to year.

The categorical smoothing parameter and continuous bandwidth have different meanings and scales; their numerical magnitudes should not be compared. The numeric version is included to expose the contrast, not to recommend coercing ordered variables. Choose the class from the predictor’s substantive meaning: integer coding alone does not determine whether a variable is continuous, unordered, or ordered. See Data Preparation for the mixed-data workflow and Plotting and Intervals for the broader gradient interface.

Five seedless installed runs produced exactly identical bandwidths, fitted values, gradients, and four-panel graphics without warnings or messages. No seed is needed for this deterministic example.

Transcript

Give a predictor its correct R class, and np carries that meaning through the analysis.

With year ordered, cross-validation tunes a discrete Li–Racine kernel; the fitted values are level-specific, and Delta is an adjacent-level difference.

With year numeric, it tunes a continuous Gaussian kernel and returns an analytic derivative.

Give the variable the right class; np handles the rest.

The next pair uses the built-in faithful data to compare two unconditional bivariate targets and to demonstrate the interactive rgl renderer in RStudio’s Viewer.

Function: npudens()
Takeaway: target-specific smoothing reveals two distinct eruption cycles in a manipulable joint-density surface.

The datasets::faithful data contain 272 observations of Old Faithful eruption duration in minutes and the waiting time to the next eruption. The two peaks correspond to short eruptions followed by shorter waits and long eruptions followed by longer waits.

Open or download the MP4 · Download the captions · Read the transcript

## Old Faithful joint-density short.
## Last verified with the public CRAN np 0.70-5 build.

library(np)
data(faithful, package = "datasets")

fit_udens <- npudens(~ eruptions + waiting, data = faithful)
summary(fit_udens$bws)
plot(fit_udens, renderer = "rgl")

Download the R script.

The verified public CRAN np 0.70-5 build selected bandwidths of 0.146981 for eruptions and 2.925689 for waiting, optimized for the joint-density target. No seed is needed. With renderer = "rgl", the fitted surface opens in RStudio’s Viewer, where it can be rotated, zoomed, and resized.

Transcript

Old Faithful’s 272 observations contain measurements of eruption duration and waiting time. One npudens call selects bandwidths optimized for the joint-density target and reveals two distinct peaks: short eruptions with shorter waits, and long eruptions with longer waits. The surface opens in RStudio’s Viewer, where you can drag to rotate, scroll to zoom, and resize the pane. Find the full script in Gallery Video Demos.

Function: npudist()
Takeaway: the same data can be viewed cumulatively with smoothing selected for the joint-distribution target.

At each eruption-duration and waiting-time pair, the fitted surface estimates the probability of observing values at or below both coordinates. npudist() estimates this joint distribution directly; it does not integrate the fitted joint-density object from the preceding video.

Open or download the MP4 · Download the captions · Read the transcript

## Old Faithful joint-distribution short.
## Last verified with the public CRAN np 0.70-5 build.

library(np)
data(faithful, package = "datasets")

fit_udist <- npudist(~ eruptions + waiting, data = faithful)
summary(fit_udist$bws)
plot(fit_udist, renderer = "rgl")

Download the R script.

The verified public build selected bandwidths of 0.0576794 for eruptions and 0.186285 for waiting, optimized for the joint-distribution target. Five fresh installed runs were exactly identical for each estimator’s selected bandwidth object and fitted result. No seed is needed.

Transcript

The same 272 Old Faithful observations can be viewed cumulatively. One npudist call selects bandwidths optimized for the joint-distribution target and estimates the probability at or below each eruption-duration and waiting-time pair. This object is estimated directly, not by integrating the density. It opens in RStudio’s Viewer, where you can rotate, zoom, and resize the surface. Find the full script in Gallery Video Demos.

Function: npreg()
Takeaway: the recommended automatic pathway can select polynomial complexity and smoothing for the target, then display the fitted relationship and its derivatives with complementary bootstrap variability bounds.

The first cps71 short used npreg()’s default local-constant estimator, equivalent to polynomial degree 0. Here, the recent and recommended nomad = "auto" convenience route selects the local-polynomial degree and bandwidth together for the Old Faithful waiting-time relationship. In this inexpensive one-predictor search, the automatic policy uses exhaustive degree search with Powell bandwidth optimization; the verified public build selects degree 4 and a fixed bandwidth of 556.863008.

Open or download the MP4 · Download the captions · Read the transcript

## Old Faithful automatic local-polynomial regression and bootstrap bands.
## Verified with the public CRAN np 0.70-5 build.

library(np)
data(faithful, package = "datasets")

fit_npreg <- npreg(waiting ~ eruptions, data = faithful, nomad = "auto")
summary(fit_npreg$bws)

options(plot.par.mfrow = FALSE)
par(mfrow = c(2, 2))

plot(fit_npreg, errors = "bootstrap", band = "all", B = 9999)
plot(fit_npreg,
     errors = "bootstrap", band = "all", B = 9999,
     gradients = TRUE, gradient_order = 1)
plot(fit_npreg,
     errors = "bootstrap", band = "all", B = 9999,
     gradients = TRUE, gradient_order = 2)
plot(fit_npreg,
     errors = "bootstrap", band = "all", B = 9999,
     gradients = TRUE, gradient_order = 3)

Download the R script.

band = "all" overlays three bootstrap variability bounds on the fitted function and each requested derivative:

  • Pointwise bounds address uncertainty separately at each displayed evaluation point.
  • Simultaneous bounds use the bootstrap joint distribution to cover the displayed function as a whole.
  • Bonferroni bounds adjust the pointwise level across the displayed evaluation points.

These are estimate-centred variability bounds. Smoothing bias can matter, so confidence-band interpretation requires the usual negligible-bias or bias-correction conditions. See Plotting and Intervals for the broader plotting interface and bias-corrected centering options.

Five seedless installed runs with B = 9999 selected the same degree, bandwidth, and objective and produced byte-identical four-panel graphics without warnings or messages. No seed is needed for this example. The automatic routing policy is described in Runtime and Scaling, and the recommended convenience workflow appears in the Kernel Primer. The higher-order, large-bandwidth result illustrates the behavior studied by Hall and Racine (2015), Infinite Order Cross-Validated Local Polynomial Regression.

Transcript

Old Faithful’s waiting-time relationship need not be local constant or local linear.

Automatic cross-validation selects a degree-four local polynomial and a very large bandwidth for this target.

The fitted curve and its first three derivatives appear together, with pointwise, simultaneous, and Bonferroni bootstrap variability bounds.

The complete reproducible script is in Gallery Video Demos.

The next reviewed analytical episodes will continue balancing estimator families, tests, interval routes, and plotting capabilities before the serial to npRmpi session-mode comparison.

Back to top