rregressionquantilequantreg

In R, how do you get the Beta coefficient and 95% CI of a dropped level of a categorical variable when using deviation coding in quantile regression?


Sample Code


library(quantreg)
df <- data.frame(
  outcome = c(10, 12, 14, 11, 13, 15, 9, 8, 10),
  group = factor(c("A", "A", "A", "B", "B", "B", "C", "C", "C"))
)

model <- rq(outcome ~ group, data = df,tau=0.5,contrasts=list(group="contr.sum"))

summary.rq(model)

coefs <- coef(model)
print(coefs)
coef_C <- -sum(coefs[2:3])
print(coef_C)

Output

> summary.rq(model)

Call: rq(formula = outcome ~ group, tau = 0.5, data = df, contrasts = list(group = "contr.sum"))

tau: [1] 0.5

Coefficients:
            coefficients lower bd upper bd
(Intercept) 11.33333     10.01894 12.64773
group1       0.66667     -1.58269  2.91602
group2       1.66667     -0.49871  3.91602
Warning message:
In rq.fit.br(x, y, tau = tau, ci = TRUE, ...) : Solution may be nonunique
> 
> coefs <- coef(model)
> print(coefs)
(Intercept)      group1      group2 
 11.3333333   0.6666667   1.6666667 
> coef_C <- -sum(coefs[2:3])
> print(coef_C)
[1] -2.333333

Calculate 95% CI for Group C's Beta Coefficient?

I used this as reference: https://stats.oarc.ucla.edu/r/library/r-library-contrast-coding-systems-for-categorical-variables/#DEVIATION

If I understand correctly, group C is coded as "-1" in deviation coding, so the beta coefficient for group C = 0 - beta coefficient for group A - beta coefficient for group B. I think I calculated this manually using the coef function correctly.

However, how do I obtain the 95% CI?


Solution

  • Not optimal in some ways, but you could simply fit the model with a different reference category, that will estimate the C group coefficient:

    library(quantreg)
    #> Loading required package: SparseM
    df <- data.frame(
      outcome = c(10, 12, 14, 11, 13, 15, 9, 8, 10),
      grp = factor(c("A", "A", "A", "B", "B", "B", "C", "C", "C"))
    )
    
    df$grp1 <- df$grp
    contrasts(df$grp1) <- contr.sum(3)
    colnames(contrasts(df$grp1)) <- c("A","B")
    model <- rq(outcome ~ grp1, data = df,tau=0.5)
    summary.rq(model)
    #> Warning in rq.fit.br(x, y, tau = tau, ci = TRUE, ...): Solution may be
    #> nonunique
    #> 
    #> Call: rq(formula = outcome ~ grp1, tau = 0.5, data = df)
    #> 
    #> tau: [1] 0.5
    #> 
    #> Coefficients:
    #>             coefficients lower bd upper bd
    #> (Intercept) 11.33333     10.01894 12.64773
    #> grp1A        0.66667     -1.58269  2.91602
    #> grp1B        1.66667     -0.49871  3.91602
    
    df$grp2 <- relevel(df$grp, ref = "C")
    contrasts(df$grp2) <- contr.sum(3)
    colnames(contrasts(df$grp2)) <- c("C", "A")
    model2 <- rq(outcome ~ grp2, data = df,tau=0.5)
    summary(model2)
    #> Warning in rq.fit.br(x, y, tau = tau, ci = TRUE, ...): Solution may be
    #> nonunique
    #> 
    #> Call: rq(formula = outcome ~ grp2, tau = 0.5, data = df)
    #> 
    #> tau: [1] 0.5
    #> 
    #> Coefficients:
    #>             coefficients lower bd upper bd
    #> (Intercept) 11.33333      9.84280 12.64773
    #> grp2C       -2.33333     -4.16538 -0.50129
    #> grp2A        0.66667     -1.49871  2.91602
    
    df$grp3 <- factor(as.character(df$grp), levels=c("B", "C", "A"))
    contrasts(df$grp3) <- contr.sum(3)
    colnames(contrasts(df$grp3)) <- c("B", "C")
    model3 <- rq(outcome ~ grp3, data = df,tau=0.5)
    summary(model3)
    #> Warning in rq.fit.br(x, y, tau = tau, ci = TRUE, ...): Solution may be
    #> nonunique
    #> 
    #> Call: rq(formula = outcome ~ grp3, tau = 0.5, data = df)
    #> 
    #> tau: [1] 0.5
    #> 
    #> Coefficients:
    #>             coefficients lower bd upper bd
    #> (Intercept) 11.33333      9.84280 12.64773
    #> grp3B        1.66667     -0.49871  3.91602
    #> grp3C       -2.33333     -4.16538 -0.50129
    

    Created on 2025-07-11 with reprex v2.1.1