How to create a second order linear model in MATLAB if I have 5 predictors?

Basically, I have a dataset with 5 predictors and one target variable. I need to fit a second order linear model in MATLAB. So do I need to create a total of 20 predictor variables and then use fitlm or is there any other approach so that I donot need to create 20 variables.

Answers (1)

I am not certain what you are asking.
If by ‘second order linear model’ you are describing a quadratic, this may do what you want:
Predictors = rand(10,5); % Create Data
Target = rand(10,1); % Create Data
DM = [Predictors.^2, Predictors, ones(size(Target))]; % Design Matrix
B = DM \ Target; % Estimate Parameters
FitTarget = DM * B; % Predicted Values
You can easily adapt it to work with fitlm and regress.
If this is not what you intend, please describe what you want to do in more detail.

Tags

Asked:

on 7 Jan 2018

Answered:

on 7 Jan 2018

Community Treasure Hunt

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

Start Hunting!