I have used smooth.spline
to estimate a cubic spline for my data. But when I calculate the 90% point-wise confidence interval using equation, the results seems to be a little bit off. Can someone please tell me if I did it wrongly? I am just wondering if there is a function that can automatically calculate a point-wise interval band associated with smooth.spline
function.
boneMaleSmooth = smooth.spline( bone[males,"age"], bone[males,"spnbmd"], cv=FALSE)
error90_male = qnorm(.95)*sd(boneMaleSmooth$x)/sqrt(length(boneMaleSmooth$x))
plot(boneMaleSmooth, ylim=c(-0.5,0.5), col="blue", lwd=3, type="l", xlab="Age",
ylab="Relative Change in Spinal BMD")
points(bone[males,c(2,4)], col="blue", pch=20)
lines(boneMaleSmooth$x,boneMaleSmooth$y+error90_male, col="purple",lty=3,lwd=3)
lines(boneMaleSmooth$x,boneMaleSmooth$y-error90_male, col="purple",lty=3,lwd=3)
Because I am not sure if I did it correctly, then I used gam()
function from mgcv
package.
It instantly gave a confidence band but I am not sure if it is 90% or 95% CI or something else. It would be great if someone can explain.
males=gam(bone[males,c(2,4)]$spnbmd ~s(bone[males,c(2,4)]$age), method = "GCV.Cp")
plot(males,xlab="Age",ylab="Relative Change in Spinal BMD")
I'm not sure the confidence intervals for
smooth.spline
have "nice" confidence intervals like those formlowess
do. But I found a code sample from a CMU Data Analysis course to make Bayesian bootstap confidence intervals.Here are the functions used and an example. The main function is
spline.cis
where the first parameter is a data frame where the first column are thex
values and the second column are they
values. The other important parameter isB
which indicates the number bootstrap replications to do. (See the linked PDF above for the full details.)And that gives something like
Actually it looks like there might be a more parametric way to calculate confidence intervals using the jackknife residuals. This code comes from the S+ help page for smooth.spline
And that results in
And as far as the
gam
confidence intervals go, if you read theprint.gam
help file, there is anse=
parameter with defaultTRUE
and the docs saySo you can adjust the confidence interval by adjusting this parameter. (This would be in the
print()
call.)