r - How to extrapolate beyond the x points passed to `ksmooth`? -
i have kernel function so:
x <- 1:100 y <- rnorm(100, mean=(x/2000)^2) plot(x,y) kernel <- ksmooth(x,y, kernel="normal", bandwidth=10) print(kernel$y)
if try predict @ point outside of range of x values, give me nan
, because attempting extrapolate beyond data:
x <- 1:100 y <- rnorm(100, mean=(x/2000)^2) plot(x,y) kernel <- ksmooth(x,y, kernel="normal", bandwidth=10, x.points=c(130)) print(kernel$y) > print(kernel$y) [1] na
even when change range.x
doesn't budge:
x <- 1:100 y <- rnorm(100, mean=(x/2000)^2) plot(x,y) kernel <- ksmooth(x,y, kernel="normal", bandwidth=10, range.x=c(1,200) , x.points=c(130)) print(kernel$y) > print(kernel$y) [1] na
how ksmooth
function extrapolate beyond data? know bad idea in theory, in practice issue comes time.
to answer side question, looking @ code of ksmooth
, range.x
used when x.points
not provided explains why not see used. let's @ code in ksmooth
:
function (x, y, kernel = c("box", "normal"), bandwidth = 0.5, range.x = range(x), n.points = max(100l, length(x)), x.points) { if (missing(y) || is.null(y)) stop("numeric y must supplied.\nfor density estimation use density()") kernel <- match.arg(kernel) krn <- switch(kernel, box = 1l, normal = 2l) x.points <- if (missing(x.points)) seq.int(range.x[1l], range.x[2l], length.out = n.points) else { n.points <- length(x.points) sort(x.points) } ord <- order(x) .call(c_ksmooth, x[ord], y[ord], x.points, krn, bandwidth) }
from see need not provide x.points
make sure range.x
used. if run:
x <- 1:100 y <- rnorm(100, mean=(x/2000)^2) plot(x,y) kernel <- ksmooth(x,y, kernel="normal", bandwidth=10, range.x=c(1,200)) plot(kernel$x, kernel$y)
now you'll see kernel evaluated beyond 100 (although not 200). increasing bandwidth parameter allows further away 100.
Comments
Post a Comment