# Example with synthetic data
tau <- 0.75
m <- 4 # number of schools
n <- 25 # number of students
N <- m*n
p <- 1 # number of covariates
betaT <- 0.5
alphaT <- seq(-10,10, length=m)
# Generate from the asymmetric Laplace
# using a mixture of a Normal and an Exponential
lambdaT <- 0.1;
xi <- rexp(N, 1/lambdaT)
epsilon <- (sqrt((lambdaT*2*xi)/(tau*(1-tau)))*rnorm(N,0,1) +
(1-2*tau)/(tau*(1-tau))*xi)
epsilon <- rnorm(N,0,1)
alphavec <- rep(alphaT, each=n)
x <- rnorm(N,250,1)
y <- x*betaT + alphavec + epsilon
X <- cbind(x)
school <- rep(1:m, each=n)
# \donttest{
fitQ3 <- qVA(y=y, xmat=X, school=school, tau=0.75, verbose=FALSE)
# quantile value-added estimates with 95% credible intervals for each school
qVA.est <- apply(fitQ3$qVA,2,mean)
qVA.int <- apply(fitQ3$qVA,2,function(x) quantile(x, c(0.025, 0.975)))
beta <- fitQ3$beta
alpha <- fitQ3$alpha
mVA <- fitQ3$mVA
cVA <- fitQ3$cVA
Q <- fitQ3$Q
# Plot results.
plot(x,y, col=rep(c("red","blue","green","orange"), each=n), pch=19)
# Plot Q3 quantile regression line for each school
lines(X[school==1,],
(X[school==1,])*mean(beta) + apply(alpha,2,mean)[1], col='red', lwd=3)
lines(X[school==2,],
(X[school==2,])*mean(beta) + apply(alpha,2,mean)[2], col='blue', lwd=3)
lines(X[school==3,],
(X[school==3,])*mean(beta) + apply(alpha,2,mean)[3], col='green', lwd=3)
lines(X[school==4,],
(X[school==4,])*mean(beta) + apply(alpha,2,mean)[4], col='orange', lwd=3)
# Plot the marginal VA for each school
points(tapply(X, school,mean), apply(mVA,2,mean),
col=c("red","blue","green","orange"), pch=4, cex=2, lwd=2)
# Plot the conditional VA for each school
points(tapply(X, school,mean), apply(cVA,2,mean),
col=c("red","blue","green","orange"),pch=10,cex=2, lwd=2)
# Plot the "global" Q3 quantile regression line.
points(X, apply(Q,2,mean), type='l', lwd=2)
# }
Run the code above in your browser using DataLab