Telling R to change the reference level of a categorical variable

2,944 views
Skip to first unread message

eloiz

unread,
Nov 9, 2012, 9:56:17 PM11/9/12
to statforli...@googlegroups.com
Dear all,

Does anyone know how to change the reference level of a dependent variable?

I have computed a multinomial regression on the basis of a dataset including 8 categorical variables. 

- Assuming a four-level dependent variable and its individual levels: L1, L2, L3 and L4;
- Assuming L2 as the reference level;
- Assuming that I want to plot the predicted values for L4 in decreasing order.

The output I keep getting is a graph showing predicted values for L4 but in the decreasing order of the predicted values for L2.

Does anyone know how to fix this?

Thanks!!

Eloiz

Stefan Th. Gries

unread,
Nov 9, 2012, 9:58:07 PM11/9/12
to statforli...@googlegroups.com
?relevel

# from the second edition:
x <- factor(rep(c("long", "intmed", "short"), 1:3)); x # create a factor x
x <- relevel(x, "short"); x # change order of levels: choose a
different 1st reference level (you can also use the number of the new
reference level)

Also see:

?reorder

STG
--
Stefan Th. Gries
-----------------------------------------------
University of California, Santa Barbara
http://www.linguistics.ucsb.edu/faculty/stgries
-----------------------------------------------

eloiz

unread,
Nov 9, 2012, 10:18:25 PM11/9/12
to statforli...@googlegroups.com
Wow! Thanks for the quick response!

So, am I right to understand that in order to be able to plot the predicted values for each of the four levels of the depend variables, I need to 'relevel' 3 times (not counting the default level) and then run the regression three times and also get predicted values (with 'fitted') 3 times?

Thanks for your help, it's greatly appreciated!

Eloiz

Stefan Th. Gries

unread,
Nov 9, 2012, 10:27:45 PM11/9/12
to statforli...@googlegroups.com
> So, am I right to understand that in order to be able to plot the predicted values for each of the four levels of the depend variables
depend variableS?? Without a replicable example I am not sure I
understand what you're trying to do.

rm(list=ls(all=TRUE))
set.seed(1); library(effects); library(nnet)
A <- factor(rep(c("o", "l", "t"), each=100))
S <- factor(c(sample(c("f", "m"), 150, replace=TRUE, prob=c(0.7,
0.3)), sample(c("f", "m"), 150, replace=TRUE, prob=c(0.3, 0.7))))
model.01 <- multinom(A ~ S)
allEffects(model.01); plot(allEffects(model.01))
Reply all
Reply to author
Forward
0 new messages