使用 bootstrap 的分位数回归的置信区间
Confidence interval for quantile regression using bootstrap
我正在尝试获取线性和分位数回归的五种 bootstrap 区间。我能够 bootstrap 并使用来自 car 和 boot.ci 的 Boot 找到线性回归的 5 个 boostrap 间隔(Quantile、Normal、Basic、Studentized 和 BCa)来自 启动。当我尝试使用来自 quantreg 的 rq 对分位数回归执行相同操作时,它会抛出一个错误。这是示例代码
创建模型
library(car)
library(quantreg)
library(boot)
newdata = Prestige[,c(1:4)]
education.c = scale(newdata$education, center=TRUE, scale=FALSE)
prestige.c = scale(newdata$prestige, center=TRUE, scale=FALSE)
women.c = scale(newdata$women, center=TRUE, scale=FALSE)
new.c.vars = cbind(education.c, prestige.c, women.c)
newdata = cbind(newdata, new.c.vars)
names(newdata)[5:7] = c("education.c", "prestige.c", "women.c" )
mod1 = lm(income ~ education.c + prestige.c + women.c, data=newdata)
mod2 = rq(income ~ education.c + prestige.c + women.c, data=newdata)
启动线性和分位数回归
mod1.boot <- Boot(mod1, R=999)
boot.ci(mod1.boot, level = .95, type = "all")
dat2 <- newdata[5:7]
mod2.boot <- boot.rq(cbind(1,dat2),newdata$income,tau=0.5, R=10000)
boot.ci(mod2.boot, level = .95, type = "all")
Error in if (ncol(boot.out$t) < max(index)) { :
argument is of length zero
1) 为什么 boot.ci 不适用于分位数回归
2) 使用我从 stackexchange 获得的这个解决方案,我能够找到分位数 CI。
rq 的分位数(百分位数 CI)的解决方案
t(apply(mod2.boot$B, 2, quantile, c(0.025,0.975)))
我如何获得 bootstrap 的其他 CI(普通、基础、学生化、BCa)。
3) 此外,我的 boot.ci 线性回归命令会产生此警告
Warning message:
In sqrt(tv[, 2L]) : NaNs produced
这是什么意思?
使用summary.rq
您可以计算模型系数的 boostrap 标准误差。
有五种 boostrap 方法 (bsmethods
) 可用(参见 ?boot.rq
)。
summary(mod2, se = "boot", bsmethod= "xy")
# Call: rq(formula = income ~ education.c + prestige.c + women.c, data = newdata)
#
# tau: [1] 0.5
#
# Coefficients:
# Value Std. Error t value Pr(>|t|)
# (Intercept) 6542.83599 139.54002 46.88860 0.00000
# education.c 291.57468 117.03314 2.49139 0.01440
# prestige.c 89.68050 22.03406 4.07009 0.00010
# women.c -48.94856 5.79470 -8.44712 0.00000
要计算 bootstrap 个置信区间,您可以使用以下技巧:
mod1.boot <- Boot(mod1, R=999)
set.seed(1234)
boot.ci(mod1.boot, level = .95, type = "all")
dat2 <- newdata[5:7]
set.seed(1234)
mod2.boot <- boot.rq(cbind(1,dat2),newdata$income,tau=0.5, R=10000)
# Create an object with the same structure of mod1.boot
# but with boostrap replicates given by boot.rq
mod3.boot <- mod1.boot
mod3.boot$R <- 10000
mod3.boot$t0 <- coef(mod2)
mod3.boot$t <- mod2.boot$B
boot.ci(mod3.boot, level = .95, type = "all")
# BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
# Based on 10000 bootstrap replicates
#
# CALL :
# boot.ci(boot.out = mod3.boot, type = "all", level = 0.95)
#
# Intervals :
# Level Normal Basic Studentized
# 95% (6293, 6838 ) (6313, 6827 ) (6289, 6941 )
#
# Level Percentile BCa
# 95% (6258, 6772 ) (6275, 6801 )
感谢所有提供帮助的人。我能够自己找出解决方案。我 运行 循环计算分位数回归的系数,然后分别使用 boot 和 boot.ci。这是代码
仅引导命令,根据问题创建模型
mod3 <- formula(income ~ education.c + prestige.c + women.c)
coefsf <- function(data,ind){
rq(mod3, data=newdata[ind,])$coef
}
boot.mod <- boot(newdata,coefsf,R=10000)
myboot.ci <- list()
for (i in 1:ncol(boot.mod$t)){
myboot.ci[[i]] <- boot.ci(boot.mod, level = .95, type =
c("norm","basic","perc", "bca"),index = i)
}
我按照我的意愿做了这个 CI 所有变量而不仅仅是截距。
我正在尝试获取线性和分位数回归的五种 bootstrap 区间。我能够 bootstrap 并使用来自 car 和 boot.ci 的 Boot 找到线性回归的 5 个 boostrap 间隔(Quantile、Normal、Basic、Studentized 和 BCa)来自 启动。当我尝试使用来自 quantreg 的 rq 对分位数回归执行相同操作时,它会抛出一个错误。这是示例代码
创建模型
library(car)
library(quantreg)
library(boot)
newdata = Prestige[,c(1:4)]
education.c = scale(newdata$education, center=TRUE, scale=FALSE)
prestige.c = scale(newdata$prestige, center=TRUE, scale=FALSE)
women.c = scale(newdata$women, center=TRUE, scale=FALSE)
new.c.vars = cbind(education.c, prestige.c, women.c)
newdata = cbind(newdata, new.c.vars)
names(newdata)[5:7] = c("education.c", "prestige.c", "women.c" )
mod1 = lm(income ~ education.c + prestige.c + women.c, data=newdata)
mod2 = rq(income ~ education.c + prestige.c + women.c, data=newdata)
启动线性和分位数回归
mod1.boot <- Boot(mod1, R=999)
boot.ci(mod1.boot, level = .95, type = "all")
dat2 <- newdata[5:7]
mod2.boot <- boot.rq(cbind(1,dat2),newdata$income,tau=0.5, R=10000)
boot.ci(mod2.boot, level = .95, type = "all")
Error in if (ncol(boot.out$t) < max(index)) { :
argument is of length zero
1) 为什么 boot.ci 不适用于分位数回归
2) 使用我从 stackexchange 获得的这个解决方案,我能够找到分位数 CI。
rq 的分位数(百分位数 CI)的解决方案
t(apply(mod2.boot$B, 2, quantile, c(0.025,0.975)))
我如何获得 bootstrap 的其他 CI(普通、基础、学生化、BCa)。
3) 此外,我的 boot.ci 线性回归命令会产生此警告
Warning message:
In sqrt(tv[, 2L]) : NaNs produced
这是什么意思?
使用summary.rq
您可以计算模型系数的 boostrap 标准误差。
有五种 boostrap 方法 (bsmethods
) 可用(参见 ?boot.rq
)。
summary(mod2, se = "boot", bsmethod= "xy")
# Call: rq(formula = income ~ education.c + prestige.c + women.c, data = newdata)
#
# tau: [1] 0.5
#
# Coefficients:
# Value Std. Error t value Pr(>|t|)
# (Intercept) 6542.83599 139.54002 46.88860 0.00000
# education.c 291.57468 117.03314 2.49139 0.01440
# prestige.c 89.68050 22.03406 4.07009 0.00010
# women.c -48.94856 5.79470 -8.44712 0.00000
要计算 bootstrap 个置信区间,您可以使用以下技巧:
mod1.boot <- Boot(mod1, R=999)
set.seed(1234)
boot.ci(mod1.boot, level = .95, type = "all")
dat2 <- newdata[5:7]
set.seed(1234)
mod2.boot <- boot.rq(cbind(1,dat2),newdata$income,tau=0.5, R=10000)
# Create an object with the same structure of mod1.boot
# but with boostrap replicates given by boot.rq
mod3.boot <- mod1.boot
mod3.boot$R <- 10000
mod3.boot$t0 <- coef(mod2)
mod3.boot$t <- mod2.boot$B
boot.ci(mod3.boot, level = .95, type = "all")
# BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
# Based on 10000 bootstrap replicates
#
# CALL :
# boot.ci(boot.out = mod3.boot, type = "all", level = 0.95)
#
# Intervals :
# Level Normal Basic Studentized
# 95% (6293, 6838 ) (6313, 6827 ) (6289, 6941 )
#
# Level Percentile BCa
# 95% (6258, 6772 ) (6275, 6801 )
感谢所有提供帮助的人。我能够自己找出解决方案。我 运行 循环计算分位数回归的系数,然后分别使用 boot 和 boot.ci。这是代码
仅引导命令,根据问题创建模型
mod3 <- formula(income ~ education.c + prestige.c + women.c)
coefsf <- function(data,ind){
rq(mod3, data=newdata[ind,])$coef
}
boot.mod <- boot(newdata,coefsf,R=10000)
myboot.ci <- list()
for (i in 1:ncol(boot.mod$t)){
myboot.ci[[i]] <- boot.ci(boot.mod, level = .95, type =
c("norm","basic","perc", "bca"),index = i)
}
我按照我的意愿做了这个 CI 所有变量而不仅仅是截距。