MATLAB Answers

How can I turn new dummy variables into the same dummy variables I used to create a model?

2 views (last 30 days)
I have created a linear regression model which takes in 4 categorical variables (dayofweek, month, isholiday, and period). It uses the dummyvar() function to successfully create a matrix of dummy variables. This is my problem: I went to try and predict a future point using my model but I'm unclear as to how the dummy variable aspect works now. For example, dummyvar() picked up that there are 12 different months in the year and thus it created 12 columns (one for each month). But now that I'm predicting, I don't have 12 months in the samples I'd like to predict. For example, if I want to predict what the dependent variable will be on Halloween, the input vector looks something like this:
[6 10 1 4].
If I use dummyvar() on this vector, I'm obviously not going to get what I want which is a 1 in the "October" column (a 0 in every other month column), and so on.
Is there a way to tell dummyvar to use the dummyvar matrix made earlier as a reference or does some other function do this?

Accepted Answer

Siddharth Sundar
Siddharth Sundar on 28 Oct 2014
There is no direct way to tell dummyvar to use a previously created dummyvar predictor matrix as a reference. So, in this case you would need to dummy-code each categorical variable separately and then do the same for the test vector. The following example should explain the workflow you would need to follow:
Say you have two categorical variables: Month, with categories, 1 to 12 and LeapYear with categories 0 and 1.
In this case, you would need to use the following code:
Monthcat = categorical(Month);
LeapYearcat = categorical(LeapYear);
dumMonth = dummyvar(Monthcat);
dumLeap = dummyvar(LeapYearcat);
dumPredictorMat = [dumMonth dumLeap]; % This would be the input to train your model
% Test inputs
MonthTest = 4;
LeapYearTest = 1;
% Separately dummy coding these inputs
MonthTestcat = categorical(MonthTest,1:12);
LeapTestcat = categorical(LeapYearTest,0:1);
%%Dummy coding them individually before using the model to predict
dumMonthTest = dummyvar(MonthTestcat);
dumLeapTest = dummyvar(LeapTestcat);
dummyTestMat = [dumMonthTest dumLeapTest];
Now you should be able to use the dummyTestMat as an input to the predict function of the model to generate the corresponding predicted responses.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!