R - loess forecast NA

I am struggling with out-of-sample prediction using loess . I get the NA values ​​for the new x, which are outside of the original sample. Can I get these forecasts?

 x <- c(24,36,48,60,84,120,180) y <- c(3.94,4.03,4.29,4.30,4.63,4.86,5.02) lo <- loess(y~x) x.all <- seq(3,200,3) predict(object = lo,newdata = x.all) 

I need to simulate a full yield curve, i.e. interest rates for different maturities.

+6
source share
2 answers

On the predict.loess page:

When the fit was done using the surface = "interpolation" (default), pred.less will not be extrapolated - therefore, points outside the hypercube aligned on the axis will not contain predictions (NA) and standard errors by applying the source data

If you change the surface parameter to "direct", you can extrapolate the values.

For example, this will work (on the side of the note: after building the prediction, I feel that you need to slightly increase the span parameter in the loess call):

 lo <- loess(y~x, control=loess.control(surface="direct")) predict(lo, newdata=x.all) 
+11
source

In addition to nico's answer: I would suggest installing gam (which uses floating point fines). However, extrapolation is not recommended unless you have a science-based model.

 x <- c(24,36,48,60,84,120,180) y <- c(3.94,4.03,4.29,4.30,4.63,4.86,5.02) lo <- loess(y~x, control=loess.control(surface = "direct")) plot(x.all <- seq(3,200,3), predict(object = lo,newdata = x.all), type="l", col="blue") points(x, y) library(mgcv) fit <- gam(y ~ s(x, bs="cr", k=7, fx =FALSE), data = data.frame(x, y)) summary(fit) lines(x.all, predict(fit, newdata = data.frame(x = x.all)), col="green") 

resulting plot

+4
source

Source: https://habr.com/ru/post/980575/


All Articles