lscv: Least Squares Cross-Validation criterion function

Description Usage Arguments Value See Also Examples

View source: R/cv.R

Description

This function computes the leave-one-out cross-validation criterion CV(h) for the trial bandwidths supplied in the vector h, where

CV(h) = \frac{1}{n}∑_{i=1}^{n} (Y_i-\hat{m}^{(-i)}_h(x_i))^2

Used in find_hcv to find an optimal data-driven bandwidth.

Usage

1
lscv(h, data, estimator, ...)

Arguments

h

a vector of bandwidths at which to evaluate CV(h)

data

the data, specified as a data frame containing two components x and y

estimator

an R function for the estimator, with inputs and return type similar to local_average and nw

...

additional arguments to be passed to the estimator (e.g. kernel, empty_nhood)

Value

The function returns a list with components

h

the vector of bandwidths used

cv

a vector containing the computed values of CV(h)

See Also

find_hcv

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  # simulate and plot some data
  m <- function(x) (x^2+1)*sin(2*pi*x*((1-x) + 4*x))
  x <- sort(runif(100))
  y <- m(x) + rnorm(length(x), sd=0.1)
  simdata <- data.frame(x=x,y=y)

  # create sequence of bandwidths, compute and plot CV function
  h <- seq(from=0,to=0.1,length=100)
  info <- lscv(h, simdata, nw, kernel=gauss)
  plot(info$h,info$cv, type="l", xlab="h", ylab="CV(h)")

timwaite/nprtw documentation built on Jan. 25, 2021, 1:50 a.m.