1

I am currently working on an ordinal logistic regression model with R. The output coefficients are using the same reference level. I am wondering how can I change the reference level? Be more specific, see the example below. I do not want to use real data, so I simulated one. Both a and T are from 1 to 5

polr(formula = T ~ a, data = d, Hess = TRUE)

Coefficients:
      Value Std. Error  t value
a2  0.18823     0.5734  0.32825
a3  0.14747     0.5287  0.27895
a4 -0.50157     0.5766 -0.86985
a5  0.02843     0.5448  0.05219

The coefficient of "a" use reference level 1, a2, a3, a4, and a5 basically compare level 2,3,4,5 with reference level 1. My question is how can I relevel it so that the output will give a3|2, a4|3, a5|4(i.e. the beta3-beta2,beta4-beta3) automatically? I have search around and didn't find similar question. Thanks a lot.

hehehe
  • 11
  • 4
  • A very very quick and dirty is to rename your variables. a1 is your baseline because alphabetically, a1 < a2, a3, a4, a5. – Philippe Remy Jul 12 '17 at 01:16
  • i suggest you improve your searching skills: https://stackoverflow.com/questions/3872070/how-to-force-r-to-use-a-specified-factor-level-as-reference-in-a-regression – MichaelChirico Jul 12 '17 at 01:21
  • Possible duplicate of [How to force R to use a specified factor level as reference in a regression?](https://stackoverflow.com/questions/3872070/how-to-force-r-to-use-a-specified-factor-level-as-reference-in-a-regression) – MichaelChirico Jul 12 '17 at 01:21
  • 1
    I think `MASS::contr.sdif()` does the kind of contrasts you want. So `contrasts(d$a) = MASS::contr.sdif(5)` before running your `polr` command. – Marius Jul 12 '17 at 01:22
  • 1
    @MichaelChirico: the contrasts outlined in the question don't just involve setting a different reference level for treatment contrasts, that's not a dupe. – Marius Jul 12 '17 at 01:23
  • @Marius Thank you so much, your code works perfectly! – hehehe Jul 12 '17 at 02:34

1 Answers1

2

I think you want the contr.sdif function from the MASS package: from ?contr.sdif,

The contrast coefficients are chosen so that the coded coefficients in a one-way layout are the differences between the means of the second and first levels, the third and second levels, and so on. This makes most sense for ordered factors, but does not assume that the levels are equally spaced.

So:

library(MASS)
contrasts(d$a) <- contr.sdif(5) # set contrasts: from @Marius in comments
polr(formula = T ~ a, data = d, Hess = TRUE)

According to the documentation for ?polr, it should be possible to add the argument contrasts=list(a=contr.sdif) instead of setting the contrasts on the variable itself; however, this seems to give an error (an analogous setup works fine for lm). This looks like a bug in polr to me, but that's somewhat hard to believe since this a venerable and well-tested part of R ... Where possible (maybe not in this case) I prefer the contrasts argument in the formula because it's a bit more explicit/visible when reading the code, but tastes differ.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I tried with the argument inside the formula, but it gives me the following error. invalid type (list) for variable '(contrasts)' – hehehe Jul 12 '17 at 02:35