Learn R Programming

lokern (version 1.1-12)

varNPreg: Nonparametric Variance Estimator

Description

Estimates the error variance \(\sigma^2\) nonparametrically in the model $$Y_i = m(x_i) + E_i,$$ where \(E_i \sim (0,\sigma^2)\), i.i.d.

Computes leave-one-out residuals (local linear approximation followed by reweighting) and their variance.

Usage

varNPreg(x, y)

Value

A list with components

res

numeric; residuals at x[] of length n.

snr

explained variance of the true curve, i.e., an \(R^2\), defined as \(1 - \hat{\sigma^2}/ \hat{\sigma_0^2}\), where \(\hat{\sigma^2} = \)sigma2, and \(\hat{\sigma_0^2} := var(Y) = E[Y^2] - (E[Y])^2\), see the example below.

sigma2

estimation of residual variance, \(\hat{\sigma^2}\).

Arguments

x

abscissae values, ordered increasingly.

y

observations at y[i] at x[i].

Author

Martin Maechler

See Also

lokerns, glkerns.

Examples

Run this code
n <- 100
x <- sort(runif(n))
y <- sin(pi*x) + rnorm(n)/10
str(ve <- varNPreg(x,y))

plot(x, y)
## "fitted" = y - residuals:
lines(x, y - ve$res, col=adjustcolor(2, 1/2), lwd=3)
segments(x,y,x,y-ve$res, col=3:4, lty=2:3, lwd=1:2)

## sigma2 :=  1/n  sum_i res_i^2  :
          with(ve,         c(sigma2, sum(res^2)/n))
stopifnot(with(ve, all.equal(sigma2, sum(res^2)/n)))

## show how 'snr' is computed, given 'sigma2'  { in ../src/auxkerns.f }
dx2 <- diff(x, 2) # (x[i+1] - x[i-1]) i= 2..{n-1}
dx.n <- c(x[2]-x[1], dx2, x[n]-x[n-1])
SY  <- sum(dx.n * y)
SY2 <- sum(dx.n * y^2)
rx <- 2*(x[n]-x[1]) # 'dn'
(sigm2.0 <- SY2/rx - (SY/rx)^2)
(R2 <- 1 - ve$sigma2 / sigm2.0)
stopifnot(all.equal(ve$snr, R2))

Run the code above in your browser using DataLab