Solved – confidence band around a smoothed function

confidence intervalrsmoothing

I am using earth packageearth: Multivariate Adaptive Regression Spline Models regression to get a constant piecewise approximation of my data. I want to plot a band of confidence around it. Does this make sense to estimate a confidence interval of the smoothed function? If yes how can I do this?

I know that confidence intervals cannot be calculated directly, since it is a non parametric regression but I want to have a coherent result like (plot a band of confidence around my smoothed function) what I can get using lm or loess smoothing.

EDIT

I add some code to clarify my idea, Here what I would do if I am using linear regression or loess.

set.seed(1)
x <- rnorm(15)
dat <- data.frame(x = x, y = c(x + rnorm(15)))

Using lm

mod <- lm(y ~ x,data=dat)
predfit <- predict(mod,se=TRUE,interval="confidence")$fit

Using loess

level=0.95
mod <- loess(y~x,data=dat)
pred <- predict(mod, se = TRUE)
y = pred$fit
    ci <- pred$se.fit * qt(level / 2 + .5, pred$df)
data.frame(ymin = y - ci,
           ymax = y + ci)

My question how to get ymin and ymax if I use earth :

library(earth)
mod = earth(y~x,data=dat)

Best Answer

Earth will estimate prediction intervals for you. Do it like this:

mod = earth(y~x,data=dat, ncross=30, nfold=3, varmod.method="lm")
plotmo(mod, pt.col=1, level=.95) # show prediction intervals

summary(mod) will print this:

varmod: method "lm"    min.sd 0.124    iter.rsq 0.016

stddev of predictions:
            coefficients iter.stderr iter.stderr%
(Intercept)       1.2265      0.2059           17
y                 0.0883      0.1918          217

                          mean   smallest   largest   ratio
95% prediction interval   4.86       3.91       5.1     1.3

The above code illustrates the principle, but a problem with your small sample size is that there is not enough data to estimate prediction intervals reliably. (We know this because the iter.stderr% are very big, and the iter.rsq is very small. There is a vignette that comes with the earth package that explains in detail how to get prediction intervals, and some of the potential pitfalls.)