Multiple linear regression with constraint

조회 수: 14 (최근 30일)
Cam_cat
Cam_cat 2014년 1월 25일
답변: ARUN BORGOHAIN 2017년 6월 26일
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 !

채택된 답변

John D'Errico
John D'Errico 2014년 1월 25일
편집: John D'Errico 2014년 1월 25일
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
abdoulaye thiam
abdoulaye thiam 2014년 2월 7일
Hello, John, How can I make sure that all my coefficient stay positive while using the "lsqlin"?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Amit
Amit 2014년 1월 25일
편집: Amit 2014년 1월 25일
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
Amit
Amit 2014년 1월 25일
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 2014년 1월 25일
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.

댓글을 달려면 로그인하십시오.


andrea capone
andrea capone 2016년 11월 23일
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
Torsten
Torsten 2016년 11월 23일
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.
andrea capone
andrea capone 2016년 11월 23일
But I don't recive any solution!

댓글을 달려면 로그인하십시오.


ARUN BORGOHAIN
ARUN BORGOHAIN 2017년 6월 26일
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)

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by