How does fitlm set reference level with categorical variables?
Show older comments
I am running linear regression using fitlm with categorical datasets:
model = fitlm(DataTable ,'Score ~ Industry + Rating + Liquid')

The regressor set the Industry and Rating reference level to the 1st row cells, but for "Liquid" variable, it sets "Q1" as the reference level. I am a little confused on this select? I thought the regressor will always set the 1st row as reference for all 3 variables. Could you please explain why it choose a different reference level for the "Liquid" variable.
Answers (1)
Cris LaPierre
on 11 Oct 2024
1 vote
See this example: Linear Regression with Categorical Predictor
fitlm treats a categorical predictor as follows:
- A model with a categorical predictor that has L levels (categories) includes L – 1 indicator variables. The model uses the first category as a reference level, so it does not include the indicator variable for the reference level. If the data type of the categorical predictor is categorical, then you can check the order of categories by using categories and reorder the categories by using reordercats to customize the reference level. For more details about creating indicator variables, see Automatic Creation of Dummy Variables.
- fitlm treats the group of L – 1 indicator variables as a single variable. If you want to treat the indicator variables as distinct predictor variables, create indicator variables manually by using dummyvar. Then use the indicator variables, except the one corresponding to the reference level of the categorical variable, when you fit a model. For the categorical predictor X, if you specify all columns of dummyvar(X) and an intercept term as predictors, then the design matrix becomes rank deficient.
- Interaction terms between a continuous predictor and a categorical predictor with L levels consist of the element-wise product of the L – 1 indicator variables with the continuous predictor.
- Interaction terms between two categorical predictors with L and M levels consist of the (L – 1)*(M – 1) indicator variables to include all possible combinations of the two categorical predictor levels.
- You cannot specify higher-order terms for a categorical predictor because the square of an indicator is equal to itself.
9 Comments
Guohua
on 11 Oct 2024
Cris LaPierre
on 11 Oct 2024
Can you attach your data table file to your post using the paperclip icon?
Walter Roberson
on 11 Oct 2024
"the first category" does not refer to the entry that is encountered first:
"the first category" refers to the category that sorts first.
The attached data does not contain the Liquid column, so i can't reproduce the issue, but as Walter stated, categories tend to be ordered alphabetically, so the first one listed is not necessarily the first category.
Liquid = categorical(["Q2";"Q2";"Q1";"Q1";"Q2";"Q4";"Q1";"Q5";"Q2";"Q1";"Q2";"Q3"])
C = categories(Liquid)
C{1}
If you want to change the order of your categories, use reordercats
Liquid_V2 = reordercats(Liquid,["Q2";"Q1";"Q4";"Q5";"Q3"]);
C2 = categories(Liquid_V2)
C2{1}
Guohua
on 14 Oct 2024
The reason for the behavior you are seeing is because Industry and Rating are not categorical variables.
load matlab_datasample2.mat
varfun(@class,DataSample)
If you want row 1 to be the reference values, then either don't use categorical data types, or use reordercats to ensure the row 1 categorical values are the first category.
Here, I'm converting Liquid to string.
DataSample = convertvars(DataSample, "Liquid","string")
varfun(@class,DataSample)
model = fitlm(DataSample,'Score ~ Industry + Rating + Liquid')
Guohua
on 15 Oct 2024
Categories
Find more on Regularization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!