Multiple regression with categorical variables

31 vues (au cours des 30 derniers jours)
SAMIA ALGHAMDI
SAMIA ALGHAMDI le 5 Juin 2017
Commenté : Tanvir Kaisar le 22 Fév 2019
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

Réponse acceptée

Sid Jhaveri
Sid Jhaveri le 7 Juin 2017
Modifié(e) : Sid Jhaveri le 7 Juin 2017
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 commentaire
Tanvir Kaisar
Tanvir Kaisar le 22 Fév 2019
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?

Connectez-vous pour commenter.

Plus de réponses (1)

Ebby Thomas
Ebby Thomas le 7 Déc 2017
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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by