Question

The title says it all, I ordered a factor variable when I generated it, now I would like to remove the ordering and use it as an unordered factor variable. And another question, if I use my factor variable as a predictor in a regression does it make a difference to R if it is ordered (ordinal) or simple factor variable (categorical)?

Was it helpful?

Solution

All you need is

x <- factor( x , ordered = FALSE )

e.g.

x <- factor( c(1,2,"a") , ordered = TRUE )
x
#[1] 1 2 a
#Levels: 1 < 2 < a

x <- factor( x , ordered = FALSE )
x
#[1] 1 2 a
#Levels: 1 2 a

OTHER TIPS

If you created your variable via ordered, it's as simple as resetting its class to factor.

f <- ordered(letters)
class(f) <- "factor"
identical(f, factor(letters))

In a linear or additive model (including linear regression, logistic regression and anything fit with lm, glm and gam) a factor predictor is treated exactly the same as an ordered predictor in terms of the overall model fit. You'll get the same predicted values, residuals, lack-of-fit statistics, etc regardless of which one you use.

However, the contrasts are different for the two classes. A factor uses treatment contrasts, ie the usual dummy-variable coding with a given level treated as the baseline. An ordered factor uses polynomial contrasts, which are based on orthogonal polynomials (whatever that means; I've never had a reason to use ordered factors). Because of this, the t-stats and P-values for individual coefficients will be different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top