MATLAB: Do I need to add a column of ones on the predictor matrix for use in LinearModel.fit(X,y)

linearmodel.fitregression analysis

I have a predictor matrix X and I want to create a model using mdl=LinearModel.fit(X,y,'interactions'). Is it necessary to add a column of ones in the predictor matrix.
Also can I use the function "x2fx" with LinearModel.fit(X,y)

Best Answer

By default, LinearModel.fit also includes a column of ones.
For example
X = rand(10,2);
y = 3*X(:,1) + 4*X(:,2) + 5*X(:,1).*X(:,2) + 6;
LinearModel.fit(X,y,'interactions')
You will see that the value of (intercept) is correctly found as 6.
You can specify to not use it by setting the 'intercept' argument to false.
LinearModel.fit(X,y,'interactions','intercept',false)
I am not sure what you mean by using x2fx with LinearModel.fit. You certainly could, but LinearModel.fit gives you a great number of options such as 'interactions', 'purequadratic', 'quadratic', and you can also specify custom interactions, so that you don't really have to worry about x2fx.
Related Question