How can I tell R to use a specific reference level in a linear model?

59 views Asked by At

I am trying to change the reference level for a linear model. R thinks my distance variable (entered as a character string) is "ordered" based on alphabetical order as follows: 10m, 15m, 20m(post), 20m(pre), 3m, 5m. But I'm really just analyzing these distances as discrete categories with respect to the dependent variable, and I don't need them in order. I just need to be able to tell R which one to use as a reference distance (and ideally be able to change it easily).

I can't use relevel to change the reference distance because it says it only works on "unordered" factors. So I used this code to tell R to unorder my "distance" factor: factor(distance, ordered = FALSE ), which seemed to run just fine, but then when I try to use relevel it once again says it only works on unordered factors.

This is all of the code:

factor(distance, ordered = FALSE)

C_data$distance <- relevel(C_data$distance, ref = "20m(pre)")

C_lm = lm(seedrate ~ distance, data = C_data)
0

There are 0 answers