I would like to compute a confidence interval for smooth.spline
, but there seems to be no such option in the predict
function. I tried just applying the formula like so:
lines(rng, predicted + 1.96*st.dev/sqrt(n), lty = 'dashed', lwd=2)
lines(rng, predicted - 1.96*st.dev/sqrt(n), lty = 'dashed', lwd=2)
But this is not the pointwise CI that I'm looking for. Usually, you write , interval = "confidence"
in the predict
function, and it returns the lower and upper bounds, but it is not the case here.
Here is an example:
library(splines)
x_ = c(0.3, 0.8, 1.6, 2.7)
y_ = c(3.5, 2.3, 3.2, 5.0)
n = 50 - length(x_)
set.seed(0)
x = seq(0,3, length.out=n) + runif(n,0,0.1)
y = x*sin(3*x) + runif(n)
x = c(x, x_)
y = c(y, y_)
st.dev <- sd(y)
z <- qnorm(1 - .05/2)
rng <- seq(min(x),max(x),length.out= 100)
df <- data.frame(x=x, y=y)
ft <- smooth.spline(x,y)
pred <- predict(ft, rng, interval = "confidence")$y
lines(rng, pred, col="green", lwd=3)
plot(x,y,col='red', pch=19)
lines(rng, pred, col="green", lwd=3)
lines(rng, pred + z*st.dev/sqrt(n), lty = 'dashed', lwd=2)
lines(rng, pred - z*st.dev/sqrt(n), lty = 'dashed', lwd=2)
How do I go about this then?