필터 지우기
필터 지우기

Dummy Variables in non linear regression models

조회 수: 14 (최근 30일)
Dominik Gautschy
Dominik Gautschy 2020년 6월 16일
댓글: Dominik Gautschy 2020년 6월 21일
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

답변 (1개)

Apurvi Mansinghka
Apurvi Mansinghka 2020년 6월 19일
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
Dominik Gautschy
Dominik Gautschy 2020년 6월 21일
Alright, it helped!I can work with this!Thanks for helping!
Greetings

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by