Multiple regression with categorical variables

23 次查看(过去 30 天)
Hi, I'm new to Matlab sorry if my question is silly. I have dataset consists of 100 rows and 10 column which are Age, Diastolic, Gender, Height, systolic, LastName, Weight, Smoker, Location, SelfAssessedHealthStatus. I need to create a linear regression to predict systolic based on Age, Gender, Height, Weight, Smoker, Location, SelfAssessedHealthStatus. the problem for me is I have 3 categorical variables I'm not sure about how to deal with them in right way. belew is my try. can you please suggest to me how to deal with them..
if true
load ('patients');
patients= table(Age, Gender, Height, Location, SelfAssessedHealthStatus, Smoker, Weight);
patients.Gender = nominal(patients.Gender);
dv = dummyvar(patients.Gender);
patients.Location = nominal(patients.Location);
dv1 = dummyvar(patients.Location);
patients.SelfAssessedHealthStatus = nominal(patients.SelfAssessedHealthStatus);
dv2= dummyvar(patients.Location);
x=[Age dv Height Weight Smoker dv1 dv2];
y= Systolic;
ml=fitlm(x,y)
end

采纳的回答

Sid Jhaveri
Sid Jhaveri 2017-6-7
编辑:Sid Jhaveri 2017-6-7
In "fitlm" function you can specify which variables are categorical. For more information on how to achieve this, I would suggest to refer the documentation example given in the link below:
  1 个评论
Tanvir Kaisar
Tanvir Kaisar 2019-2-22
Hello, I have followed the suggested link. I have 2 questions:
1. Will it work when there are more than 2 levels of category? (Say for sex - male, female and transgender)
2.Will this approach also work for multinomial logistic regression mnrfit function? If it won't then how should I approach to deal with the categorical variables in case of mnrfit?

请先登录,再进行评论。

更多回答(1 个)

Ebby Thomas
Ebby Thomas 2017-12-7
what i believe is that the following code should work for you as per the documentation.
linearmodel = fitlm(patients,'ResponseVar','Weight','PredictorVars',{'Age', 'Gender', 'Height', 'Location', 'SelfAssessedHealthStatus', 'Smoker'},'CategoricalVar',{'Gender','Location','SelfAssessedHealthStatus'})
However, I am interested to know about your interpretation s of the result. Please share your findings as well..
Ebby

标签

Community Treasure Hunt

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

Start Hunting!

Translated by