I want to fit a generalized linear model (logistic regression) to Titanic dataset. In EDA stage I transformed a variable (Pclass) to ordered factor. Before passing data to glm function should I:

a. leave it as ordered factor

b. transform it to unordered factor or

c. transform it to integer?

  • $\begingroup$ Don't transform to integer. The results will be equivalent for an ordered factor and an unordered factor - the only difference is what sort of contrasts are applied. Personally, I find unordered factors with the default contrasts (all levels are compared to the reference level) easier to understand almost all the time. $\endgroup$ Jul 1, 2022 at 13:15
  • 1
    $\begingroup$ If you are building the model for prediction, and not to learn about the parameters of the model, then the concept of "contrasts" is not particularly relevant. The advice to not convert categorical variables to numeric stands. And if you want to know what contrasts are, see 1 (more math) or 2 (less math). $\endgroup$
    – dipetkov
    Jul 1, 2022 at 13:34

1 Answer 1


In this particular case, with only 3 levels of the predictor, there won't be much practical difference between (a) and (b). Either way you only have 2 unique coefficients associated with it and the difference is just how the levels are represented in the model matrix.

Although coding to integer (option c) by itself isn't wise, with a larger number of ordered levels of a predictor then a hybrid combining aspects of (b) with (c) can make sense, depending on your goals in modeling. Sometimes in this situation the association of an ordinal predictor with outcome is close to linear. Then you can consider a combination of (c) for the linear trend and (b) for deviations from the linear trend to gauge the importance of the non-linear associations. For a k-level predictor, you have 1 coefficient for the linear trend and k-2 for the deviations.

Splines provide another approach. For example, Helwig discusses "Regression with Ordered Predictors via Ordinal Smoothing Splines" in Front. Appl. Math. Stat., 28 July 2017.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.