Specifying which category is the reference level for glmfit

8 views (last 30 days)
When using glmfit() with categorical predictor variables, how is reference level determined? Is there a way to specify which category should be the reference level instead of the default?
The equivalent in R is the relevel() command.

Accepted Answer

Tom Lane
Tom Lane on 29 Jul 2014
There is not currently a way to specify the reference category for categorical predictors in functions like fitglm. The glmfit function doesn't directly support categorical predictors, so you have some control there in the sense that you could use dummyvar and omit any category you want. Of course you could do the same thing with fitglm, and bypass the categorical support.
But you can accomplish this by making your categorical predictor ordinal, and changing the order. First start with the default order:
>> load fisheriris
>> t = array2table(meas);
>> t.species = ordinal(species);
>> fitlm(t,'meas1~meas2+species')
...
Estimate SE tStat pValue
________ _______ ______ __________
(Intercept) 2.2514 0.36975 6.0889 9.5681e-09
meas2 0.80356 0.10634 7.5566 4.1873e-12
species_versicolor 1.4587 0.11211 13.012 3.4782e-26
species_virginica 1.9468 0.10001 19.465 2.0945e-42
Now change the order to make 'virginica' first and fit again:
>> t.species = reordercats(t.species,{'virginica' 'versicolor' 'setosa'});
>> fitlm(t,'meas1~meas2+species')
...
Estimate SE tStat pValue
________ ________ _______ __________
(Intercept) 4.1982 0.32226 13.027 3.1675e-26
meas2 0.80356 0.10634 7.5566 4.1873e-12
species_versicolor -0.48807 0.090238 -5.4088 2.5354e-07
species_setosa -1.9468 0.10001 -19.465 2.0945e-42
You can see that the omitted category changed. As a check, let's verify that the fitted value for the versicolor category at meas2=0 is the same in both fits:
>> 2.2514+1.4587
ans =
3.7101
>> 4.1982-.48807
ans =
3.7101
  1 Comment
the cyclist
the cyclist on 30 Jul 2014
Thanks for the answer, Tom. I ended up just reordering my cell array manually (using ismember and then sort), such that the first element was the one I wanted as the reference. It was a bit kludgy, but perhaps no more so than your solution.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!