gpt4 book ai didi

r - 如何获得 smooth.spline 的置信区间?

转载 作者:行者123 更新时间:2023-12-01 22:56:43 24 4
gpt4 key购买 nike

我使用smooth.spline来估计我的数据的三次样条。但是当我使用方程计算 90% 逐点置信区间时,结果似乎有点偏差。有人可以告诉我我是否做错了吗?我只是想知道是否有一个函数可以自动计算与 smooth.spline 函数关联的逐点间隔带

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)

enter image description here

因为我不确定我是否正确执行,所以我使用了 mgcv 包中的 gam() 函数。

它立即给出了一个置信区间,但我不确定它是 90% 还是 95% CI 还是其他。如果有人能解释一下就太好了。

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")

enter image description here

最佳答案

我不确定 smooth.spline 的置信区间是否像 lowess 那样具有“良好”的置信区间。但我从 CMU Data Analysis course 找到了一个代码示例建立贝叶斯引导置信区间。

以下是使用的函数和示例。主要函数是 spline.cis,其中第一个参数是数据框,其中第一列是 x 值,第二列是 y 值> 值(value)观。另一个重要参数是 B,它指示要执行的引导复制数量。 (有关完整详细信息,请参阅上面链接的 PDF。)

# Helper functions
resampler <- function(data) {
n <- nrow(data)
resample.rows <- sample(1:n,size=n,replace=TRUE)
return(data[resample.rows,])
}

spline.estimator <- function(data,m=300) {
fit <- smooth.spline(x=data[,1],y=data[,2],cv=TRUE)
eval.grid <- seq(from=min(data[,1]),to=max(data[,1]),length.out=m)
return(predict(fit,x=eval.grid)$y) # We only want the predicted values
}

spline.cis <- function(data,B,alpha=0.05,m=300) {
spline.main <- spline.estimator(data,m=m)
spline.boots <- replicate(B,spline.estimator(resampler(data),m=m))
cis.lower <- 2*spline.main - apply(spline.boots,1,quantile,probs=1-alpha/2)
cis.upper <- 2*spline.main - apply(spline.boots,1,quantile,probs=alpha/2)
return(list(main.curve=spline.main,lower.ci=cis.lower,upper.ci=cis.upper,
x=seq(from=min(data[,1]),to=max(data[,1]),length.out=m)))
}

#sample data
data<-data.frame(x=rnorm(100), y=rnorm(100))

#run and plot
sp.cis <- spline.cis(data, B=1000,alpha=0.05)
plot(data[,1],data[,2])
lines(x=sp.cis$x,y=sp.cis$main.curve)
lines(x=sp.cis$x,y=sp.cis$lower.ci, lty=2)
lines(x=sp.cis$x,y=sp.cis$upper.ci, lty=2)

这给出了类似的东西

bootstrap confidence intervals

实际上,看起来可能有一种更参数化的方法来使用折刀残差计算置信区间。此代码来自S+ help page for smooth.spline

  fit <- smooth.spline(data$x, data$y)      # smooth.spline fit
res <- (fit$yin - fit$y)/(1-fit$lev) # jackknife residuals
sigma <- sqrt(var(res)) # estimate sd

upper <- fit$y + 2.0*sigma*sqrt(fit$lev) # upper 95% conf. band
lower <- fit$y - 2.0*sigma*sqrt(fit$lev) # lower 95% conf. band
matplot(fit$x, cbind(upper, fit$y, lower), type="plp", pch=".")

结果是

residual CI estimate

gam 置信区间而言,如果您阅读 print.gam 帮助文件,就会发现有一个 se= 参数默认为 TRUE 并且文档说

when TRUE (default) upper and lower lines are added to the 1-d plots at 2 standard errors above and below the estimate of the smooth being plotted while for 2-d plots, surfaces at +1 and -1 standard errors are contoured and overlayed on the contour plot for the estimate. If a positive number is supplied then this number is multiplied by the standard errors when calculating standard error curves or surfaces. See also shade, below.

所以可以通过调整这个参数来调整置信区间。 (这将在 print() 调用中进行。)

关于r - 如何获得 smooth.spline 的置信区间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23852505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com