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

Technical Source
2 min readJun 14, 2022

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?

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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 inputsMonthTestcat = categorical(MonthTest,1:12);
LeapTestcat = categorical(LeapYearTest,0:1);

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.