I have an unordered categorical variable (event_time) with 5 different options ("future", "past", "prebirth", "never", "uncertain")
as a predictor variable, and I want to specify somehow to make "never"
the reference category (ideally without transforming the variable). I'm just using lm and then texreg::screenreg(list(m1, m2, m3)
to compare output for models with different outcome variables but this same predictor.
If there's a way to to rearrange the order that the categories show up in the model (perhaps within screenreg
?) that'd be wonderful.
And an added bonus if this can all be done without dealing with transforming and factor variables (I know how to do this with relevel if the variable was a factor already)...thanks much.
Some data:
structure(list(yvar = c(4.43024525984776, -3.01051231657988,
4.70993862460106, -2.03636967067474, -1.09802960848352, -1.16527740798651,
5.6002805983151, -7.03524067599639, 1.02474010023752, 0.647438645180132
), event_time = c(NA, "Pre", "Future", "Time unknown", "Future", "Future", NA,
"Never", NA, "Never"), race = c("Black", "Black", "White", "Black",
"Black", "Black", "Black", "White", "Black", "White"), log_parent_income = c(4.0073333,
NA, 3.8066626, 2.1972246, 0.69314718, 4.2484951, 3.9120231, 1.9459101,
2.3025851, 3.8066626)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
And then just doing a simple lm(yvar ~ event_time + log_parent_income + race ...
model.
I don't know if this will make you happy or not, but here goes.
A helper function that will be useful for reordering:
Fit the model. Here I'm using the
forcats
package becausefct_relevel
is less fussy about accepting a character vector (i.e. I don't needrelevel(factor(event_time), "Never")
.If you like the tidyverse you can make it slightly more compact:
Now
texreg::screenreg(m1)
will actually output the coefficients in your preferred order ("Future", "Pre", "Time unknown") because it happens to be alphabetical. If you wanted to change the order to something else you could:While it would theoretically be possible to do what you want without touching the data set (by setting up a custom contrast), I think it would be considerably harder.In the long run trying to work in a language without adopting its idioms can be tough — you might try figuring out what you don't like about factors and trying to address it (theforcats
package can be helpful for some tasks).