Specifying which category is the reference level for glmfit

조회 수: 10 (최근 30일)
the cyclist
the cyclist 2014년 7월 28일
댓글: the cyclist 2014년 7월 30일
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.

채택된 답변

Tom Lane
Tom Lane 2014년 7월 29일
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
the cyclist
the cyclist 2014년 7월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Gaussian Process Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by