bw_CV_fun: CV optimal bandwidth

Description Usage Arguments Value See Also Examples

View source: R/lpreba.R

Description

Selection of the bandwidth that yields the smallest leave-one-out cross-validation (LOOCV) error over a supplied grid.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
bw_CV_fun(
  X,
  Y,
  kernel = epanechnikov,
  bw_grid,
  degree = 1L,
  kernel_left = epanechnikov_left,
  boundary_left = NA,
  boundary_right = NA,
  boundary_adjustment = FALSE,
  plot = TRUE
)

Arguments

X

Data for the regressor (vector).

Y

Data for the regressand (vector).

kernel

Kernel (function). Default is epanechnikov.

bw_grid

Bandwidth grid to find global minimum of CV error (vector).

degree

Degree of the locally fitted polynomial (integer). Default is 1L.

kernel_left

Left boundary kernels (function). Default is epanechnikov_left.

boundary_left

Lower boundary of the support of X (scalar). Default is NA.

boundary_right

Upper boundary of the support of X (scalar). Default is NA

boundary_adjustment

Explicit boundary adjustment (boolean). Default is FALSE.

plot

Plot CV error over bandwidth grid (boolean). Default is TRUE.

Value

CV optimal bandwidth (scalar) and plot of CV errors if plot == TRUE.

See Also

CV_error_fun

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
m_fun <- function(x) {sin(2*pi*x)} # True regression function
n <- 100 # Sample size
X <- seq(0, 1, length.out = n) # Data for the regressor
m_X <- m_fun(X) # True values of regression function
epsilon <- rnorm(n, sd = 0.25) # Error term
Y <- m_X + epsilon # Data for the regressand
bw_grid <- seq(0.05, 0.2, length.out = 200)

# Local polynomial estimator
bw_CV_fun(X = X, Y = Y, kernel = epanechnikov, bw_grid = bw_grid, degree = 1L,
          kernel_left = epanechnikov_left,
          boundary_left = NA, boundary_right = NA,
          boundary_adjustment = FALSE,
          plot = TRUE)

# Boundary-adjusted Nadaraya-Watson estimator
bw_CV_fun(X = X, Y = Y, kernel = epanechnikov, bw_grid = bw_grid, degree = 0L,
          kernel_left = epanechnikov_left,
          boundary_left = 0, boundary_right = 1,
          boundary_adjustment = TRUE,
          plot = TRUE)

svjaco/lpreba documentation built on March 4, 2022, 12:42 a.m.