Calculating 95% confidence intervals in quantile regression in R using rq function

守給你的承諾、 提交于 2019-12-03 14:05:16

You can use the boot.rq function directly to bootstrap the coefficients:

x<-1:50
y<-c(x[1:48]+rnorm(48,0,5),rnorm(2,150,5))

QR <- rq(y~x, tau=0.5)
summary(QR, se='boot')

LM<-lm(y~x)

QR.b <- boot.rq(cbind(1,x),y,tau=0.5, R=10000)

t(apply(QR.b$B, 2, quantile, c(0.025,0.975)))
confint(LM)


plot(x,y)
abline(coefficients(LM),col="green")
abline(coefficients(QR),col="blue")

for(i in seq_len(nrow(QR.b$B))) {
  abline(QR.b$B[i,1], QR.b$B[i,2], col='#0000ff01')
}

You may want to use the boot package to compute intervals other than the percentile interval.

You could also simply retrieve the vcov from the object, setting covariance=TRUE. This amounts to using boostrapped standard errors in your CI:

vcov.rq <- function(x, se = "iid") {
 vc <- summary.rq(x, se=se, cov=TRUE)$cov
 dimnames(vc) <- list(names(coef(x)), names(coef(x)))
 vc
}

confint(QR)

But yes, the better way to do this is to use a studentized bootstrap.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!