# Weights in quantile regression for complex survey in R

I want to include sample weights to my quantile regression model, but I'm not sure how to do this.

I've already define my weight, which are replicated weights already given in survey dataset (computed in survey package):

w<-svrepdesign(variables=data[,1:10],repweights=data[,11:30],type="BRR",
combined.weights=TRUE, weights=r.weights, rho=0.5,dbname="")


and my rq model is:

rq(y~x,tau=c(.1,.2,.3,.4,.5,.6,.7,.8,.9),data=my.data))


I tried to use withReplicates function, but with no success. Any suggestions?

• What software are you using? In Stata 13 you can add survey weights to quantile regression. Jul 12 '13 at 11:23
• Make sure that what you are weighting on is something you want to marginalize, and realize that you pay a high variance price for doing this. If the survey over-sampled males and you want to develop a non-sex-specific estimate then weighting is for you. If on the other hand you want conditional estimates, i.e., you want to adjust for sex as an ordinary covariate, then weighting may be counterproductive. Nov 10 '13 at 14:03
• @FrankHarrell, if in your example the weights are 3 for males and 10 for (undersampled) females, then the estimates within each gender group are as good as i.i.d., and their standard errors do not suffer from unequal weights: for males, we have $\sum_i w_i y_i/\sum_i w_i = 3 \sum_i y_i / \sum_i 1 = \bar y$ where summation is over males only. Mar 16 '14 at 16:27
• @Alicja, make sure that you have theoretical justification for what you are about to do. That is, which variance estimation method gives you consistent standard errors for your method. For rq with i.i.d. data, the standard errors involve a kernel density estimate of the errors density at a chosen quantile point. This may or may not be a meaningful quantity with complex survey data. As such, rq is based on non-smooth estimation equations that involve jump functions, and BRR theory is generally established only for smooth statistics. Mar 16 '14 at 16:31
• @StasK I don't think that is the relevant calculation. Gender-specific estimates (e.g., conditional on gender) are what they are and no weighted is needed or appropriate. If one wants to "un-over-sample" one gender group, the resulting weighted estimate (which unconditions on gender) has low precision, effectively lowering the sample size for the over-sampled group. Mar 16 '14 at 17:19

i am not sure @Metrics answer will give the correct standard errors for a survey-weighted quantreg call. here's an example of what you're trying to do. you are certainly hitting a bug because the qr function nested within the withReplicates function at this point cannot handle multiple tau parameters at once (even though the qr function on its own might). just call one at a time, perhaps like this :)

library(survey)
library(quantreg)

data(scd)
repweights <-
cbind(c(4,0,3,0,4,0), c(3,0,0,4,0,3),c(0,3,4,0,0,2),c(0,1,0,4,3,0))

# tack on the fake replicate weights
x <- cbind( scd , repweights )

# tack on some fake main weights
x[,9] <- c( 3 , 2 , 3 , 4 , 1 , 4 )

names( x )[ 5:9 ] <- c( paste0( 'rep' , 1:4 ) , "wgt" )

# create a replicate-weighted survey design object
scdrep <-
svrepdesign(
data = x ,
type = "BRR" ,
repweights = "rep" ,
weights = ~wgt ,
combined.weights = TRUE
)

# loop through each desired value of tau
for ( i in seq( 0.1 , 0.9 , by = 0.1 ) ){

print( i )

# follow the call described here:
print(
withReplicates(
scdrep ,
quote(
coef(
rq( arrests ~ alive , tau = i , weights = .weights )
)
)
)
)

}


The usage of rq in quantreg package

rq(formula, tau=.5, data, subset, weights, na.action,
method="br", model = TRUE, contrasts, ...)


where, weights=vector of observation weights; if supplied, the algorithm fits to minimize the sum of the weights multiplied into the absolute residuals. The length of weights must be the same as the number of observations. The weights must be nonnegative and it is strongly recommended that they be strictly positive, since zero weights are ambiguous.