# Simulate data from heteroskedastic regression
n <- 200
X <- runif(n=n,min=0,max=10)
X <- cbind(1,X)
y <- 1 + 2*X[,2] + rnorm(n=n, mean=0, sd=.6*X[,2])
# Initiate plot
## Plot datapoints
plot(X[,2], y, main="", cex=.6, xlab="X")
# Initialize the inputs for QRc
Data = list(y=y, X=X, p=.05)
Prior = list(beta0=rep(0,ncol(X)),V0=100*diag(ncol(X)), shape0=0.01, scale0=0.01)
Mcmc = list(R=5000, keep=1)
# Write loop to analyze 5 quantiles
for (i in 1:5) {
if (i==1) p = .05
if (i==2) p = .25
if (i==3) p = .50
if (i==4) p = .75
if (i==5) p = .95
Data$p = p
out = QRc(Data=Data, Prior=Prior, Mcmc=Mcmc)
## Add quantile regression lines to the plot (exclude first 500 burn-in draws)
abline(a=mean(out$betadraw[500:Mcmc$R,1]),b=mean(out$betadraw[500:Mcmc$R,2]),lty=i,col=i)
}
# Estimate and plot OLS model
outOLS = lm(y~X[,2])
abline(outOLS,lty=1,lwd=2,col=6)
# Add legend to plot
legend(x=0,y=max(y),legend=c(.05,.25,.50,.75,.95,"OLS"),lty=c(1,2,3,4,5,1),lwd=c(1,1,1,1,1,2),col=c(1:6),title="Quantile")Run the code above in your browser using DataLab