首页 > 解决方案 > 在 R 中使用内核回归执行残差引导

问题描述

核回归是一种非参数技术,它希望估计随机变量的条件期望。它使用响应值 Y 的局部平均来找到 X 和 Y 之间的一些非线性关系。

我已经使用引导程序进行内核密度估计,现在也想将其用于内核回归。我被告知要使用残差引导进行内核回归,并且已经阅读了几篇关于此的论文。但是,我不确定如何执行此操作。使用 FKSUM 包在 R 中完成了编程。我尝试在内核回归上使用标准重采样:

library(FKSUM)
set.seed(1)

n <- 5000
sample.size <- 500
B.replications <- 200
x <- rbeta(n, 2, 2) * 10
y <- 3 * sin(2 * x) + 10 * (x > 5) * (x - 5)
y <- y + rnorm(n) + (rgamma(n, 2, 2) - 1) * (abs(x - 5) + 3)

#taking x.y to be the population
x.y <- data.frame(x, y)

xs <- seq(min(x), max(x), length = 1000)
ftrue <- 3 * sin(2 * xs) + 10 * (xs > 5) * (xs - 5)

#Sample from the population
seqx<-seq(1,5000,by=1)
sample.ind <- sample(seqx, size = sample.size, replace = FALSE)
sample.reg<-x.y[sample.ind,]
x_s <- sample.reg$x
y_s <- sample.reg$y

fhat_loc_lin.pop <- fk_regression(x, y)
fhat_loc_lin.sample <- fk_regression(x = x_s, y = y_s)

plot(x, y, col = rgb(.7, .7, .7, .3), pch = 16, xlab = 'x',
     ylab = 'x', main = 'Local linear estimator with amise bandwidth')
lines(xs, ftrue, col = 2, lwd = 3)
lines(fhat_loc_lin, lty = 2, lwd = 2)

#Bootstrap
n.B.sample = sample.size  # sample bootstrap size

boot.reg.mat.X <- matrix(0,ncol=B.replications, nrow=n.B.sample)
boot.reg.mat.Y <- matrix(0,ncol=B.replications, nrow=n.B.sample)
fhat_loc_lin.boot <- matrix(0,ncol = B.replications, nrow=100)

Temp.reg.y <- matrix(0,ncol = B.replications,nrow = 1000)


for(i in 1:B.replications){
  sequence.x.boot <- seq(from=1,to=n.B.sample,by=1)
  sample.ind.boot <- sample(sequence.x.boot, size = sample.size, replace = TRUE)
  boot.reg.mat <- sample.reg[sample.ind.boot,]

  boot.reg.mat.X <- boot.reg.mat$x
  boot.reg.mat.Y <- boot.reg.mat$y

  fhat_loc_lin.boot <- fk_regression(x = boot.reg.mat.X ,
                                     y = boot.reg.mat.Y, 
                                     h = fhat_loc_lin.sample$h)

  lines(y=fhat_loc_lin.boot$y,x= fhat_loc_lin.sample$x, col =c(i) )

Temp.reg.y[,i] <- fhat_loc_lin.boot$y  

}

quan.reg.l <- vector()
quan.reg.u <- vector()

for(i in 1:length(xs)){
  quan.reg.l[i] <- quantile(x = Temp.reg.y[i,],probs = 0.025)
  quan.reg.u[i] <- quantile(x = Temp.reg.y[i,],probs = 0.975)
}

# Lower Bound
Temp.reg.2  <-  quan.reg.l 
lines(y=Temp.reg.2,x=fhat_loc_lin.boot$x ,col="red",lwd=4,lty=1)

# Upper Bound
Temp.reg.3  <-  quan.reg.u
lines(y=Temp.reg.3,x=fhat_loc_lin.boot$x ,col="navy",lwd=4,lty=1)

因为我没有收到任何关于简历的回复,所以现在在这里问这个问题。任何帮助将不胜感激!

标签: rkernel-densitystatistics-bootstrap

解决方案


推荐阅读