0

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?

Waldi
  • 39,242
  • 6
  • 30
  • 78
user9102437
  • 600
  • 1
  • 10
  • 24
  • You need to provide more background info and a reproducible example. not clear at all, what `rng` is, how you obtained `predicted` and `st.dev` values. etc. In general, I would recommend to use `mgcv` package with function `gam` to fit the spline, which will also provide many convenience functions for post-processing – adibender Mar 05 '21 at 14:23
  • @adibender, I have added an example, but I have to stick to ```smooth.spline``` – user9102437 Mar 05 '21 at 14:36

0 Answers0