Clear Filters
Clear Filters

Dummy Variables in non linear regression models

14 views (last 30 days)
Hello everybody!
i am quite a beginner in matlab and also in general in analyzing data and creating models.
Anyway I want to create a non-linear model that best fits my data.
Approximately:
ln Y= B0+B1X1+B2X2+ B3X3+Errorterm
It is suggested to work with the fitnlm function but I also have categorical variables and the documentation under Non linear Regression says :
"You cannot use categorical predictors for nonlinear regression. A categorical predictor is one that takes values from a fixed set of possibilities."
So can I create dummy variables so that I can use my categorical variables in my non linear regression model? Or what are other ways to create such a model?
Thanks for the answer and help
Dominik

Answers (1)

Apurvi Mansinghka
Apurvi Mansinghka on 19 Jun 2020
Answer:
Hi Dominik,
I understand you want to handlecategorical variable with nonlinear regression function fitnlm.
You can use dummyvar(group) function to get numeric representation for any categorical variable.
Example:
Suppose there is a dataset with a categorical variable 'Colours' that can take any of 3 values {'Red','Blue','Green'}
The dataset has 6 rows with following value for 'Colours':
Colours = {'Red';'Blue';'Green';'Red';'Green';'Blue'};
1.Create a dummy variable 'D' for the categorical variable
D = dummyvar(Colours)
This gives the following result :
D = 6×3
0 0 1
1 0 0
0 1 0
0 0 1
0 1 0
1 0 0
The columns in D correspond to the levels in Colours. For example, the first column of dummyvar corresponds to the first level, 'Blue', in Colours.
2. Now each column of D is a variable in your regression.
Create a non-linear model function with the additional variables
modelfun= @(k,x)(k(1)*x(:,1)+k(2)*x(:,2)+k(3)*x(:,3))...
*(k(4)*x(:,4))*(k(5).x(:,5));
3. Set the parameter beta0 and create the model using fitnlm
beta0 = [-50 500 -1 500 -1];
mdl = fitnlm(tbl, modelfun, beta0)
Prefer to go through the tips section of the below link to understand best practices to handle categorical predicators with dummyvar:
  1 Comment
Dominik Gautschy
Dominik Gautschy on 21 Jun 2020
Alright, it helped!I can work with this!Thanks for helping!
Greetings

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!