Multiple linear regression with constraint

22 views (last 30 days)
I need some help with a code. I need to run a multiple linear regression such that the sum of the coefficients = 1 and I would like to drop the intercept. I have 7 variables, so i need 7 seven coefficients.
Thanks !

Accepted Answer

John D'Errico
John D'Errico on 25 Jan 2014
Edited: John D'Errico on 25 Jan 2014
Use lsqlin, IF you have the optimization toolbox.
Thus, if X is your nx7 design matrix, and Y an nx1 column vector, then the call to lsqlin would look like this:
lb = zeros(1,7);
ub = ones(1,7);
Aeq = ones(1,7);
beq = 1;
coef = lsqlin(X,Y,[],[],Aeq,beq,lb,ub);
There is no need to use a tool like fmincon as Amit has suggested. That tool is designed for nonlinear problems, which this is not. See that fmincon needs a starting value, and it will be considerably less efficient.
If you lack the optimization toolbox, this can still be solved using lsqnonneg, although that would require a transformation of the problem. Anyway, I'd strongly recommend a copy of that TB if you will do any work of this sort in the future.
  1 Comment
abdoulaye thiam
abdoulaye thiam on 7 Feb 2014
Hello, John, How can I make sure that all my coefficient stay positive while using the "lsqlin"?

Sign in to comment.

More Answers (3)

Amit
Amit on 25 Jan 2014
Edited: Amit on 25 Jan 2014
If you have 7 coefficients and there is a constrain that sum of coefficients are 1. Then, isn't technically you need to find only 6 coefficients while the 7th will be 1 - sum(all 6 coefficients)?
You can rearrange your linear regression model to incorporate this constrain.
  5 Comments
Amit
Amit on 25 Jan 2014
Wow .. so many NOs. I agree LSQLIN would be better and easier.
I suggested FMINCON, because thats something I use more often than any other optimization. I never use linear regression. However, one thing I'd say that FMINCON can solve it too. Longer process but it will solve it. But thank you, I learnt something new today. Thats what I love about MATLAB Answers.
John D'Errico
John D'Errico on 25 Jan 2014
Ok, I just saw No, No, Nanette. :)
The point is, fmincon is a nonlinear solver. It uses an essentially iterative scheme to search for the solution, part of which requires it to differentiate the function.
If you KNOW the problem is essentially a linear one, then use a tool designed to solve a linear problem, so lsqlin here. It has no need to do any differentiation, no need to perform searches of the type that fmincon uses.

Sign in to comment.


andrea capone
andrea capone on 23 Nov 2016
I havee the same truble:
the code that i have wrote contain in input Y vector 36*1, and X matrix 36*8. I want to use this script:
%multivariate regression Y =data_2(:,1); X =data_3(:,1:8); lb = zeros(1,8); ub = ones(1,8); Aeq = ones(1,8); beq = 1; coef = lsqlin(X,Y,[],[],Aeq,beq,lb,ub);
The variables are eight.
I have this error:Warning: The trust-region-reflective algorithm can handle bound constraints only; using active-set algorithm instead. > In lsqlin (line 300)
  2 Comments
Torsten
Torsten on 23 Nov 2016
The message you receive is not an error, but an information for you that the solution method has automatically been switched to active-set algorithm.
Best wishes
Torsten.

Sign in to comment.


ARUN BORGOHAIN
ARUN BORGOHAIN on 26 Jun 2017
Can someone help me with regARIMA; as I donot have it; but I've both regression(say polyfit: I may use) & ARIMA tools; how to get the same output as with regARIMA! The same example as given in the matlab website can be taken! Beta=2.5 & -0.6; yt=2.5x2-0.6x1+ut; & in ut I would like to apply arima modelling & also get the same output! (Presently I m doing these in excel with lots of lots of consecutive steps & in R just in 3 steps!) But how to do in matlab!
https://in.mathworks.com/help/econ/regarima-class.html
Mdl = regARIMA('Intercept',0,'Beta',[2.5; -0.6],... 'AR',{0.7, -0.3, 0.1},'MA',{0.5, 0.2},... 'Variance',1,'D',1)

Community Treasure Hunt

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

Start Hunting!