Force constant slope in linear regression

조회 수: 14 (최근 30일)
Abed Alnaif
Abed Alnaif 2021년 3월 30일
댓글: Matt J 2021년 4월 5일
Hello,
I am trying to perform a linear regression with a fixed slope term (i.e., only fitting the intercept). Is there a way to do this using the 'fitlm' function?
I came across this post asking a similar question: https://www.mathworks.com/matlabcentral/answers/67434-how-can-i-do-a-linear-fit-with-forced-slope. However, the solution in this post isn't ideal for my application. I'd like to stay within the fitlm framework so that I can use it's functionality (e.g., the 'predict' function and confidence interval calculation), and also because I'd like to give the user the flexibility to fit the slope or constrain it.
Thank you,
Abed
  댓글 수: 1
dpb
dpb 2021년 3월 30일
I don't see any way to do so, directly, no.
AFAICT, none of the ways to specify the model allow you to enter a constant for a given term, nor can you build a LinearModel object other than through fitlm for an OLS fit; you can't try subterfuge of fitting the ordinary model and then replacing the coefficients, either; they're read-only.
The only way I see would be by the other subterfuge of creating a set of data that will produce the desired coefficents and fitting that. That would be relatively easy to have code to do since the intercept is easy-enough to calculate, one can generate a set of points that fit the proposed line and fit those. However, of course, the statistics other than the coefficients themselves will be bogus because the fit will be exact.
So the net result for which reason you propose wanting to do so will not produce the desired results; you'd still have to compute those intervals, etc., directly.
Of course, when one sets the value of the slope, none of the assumptions for those statistics are valid any longer, anyways...

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

채택된 답변

Matt J
Matt J 2021년 3월 30일
편집: Matt J 2021년 3월 30일
Since you know the slope, m, it should be the same as fitting a constant term to y-m*x.
mdl = fitlm(x,y-m*x,'constant')
  댓글 수: 7
Abed Alnaif
Abed Alnaif 2021년 4월 5일
This is similar to what I'm doing currently, except I haven't nicely packaged it in a way like this. In the predict function, I think 'yci' also needs to be corrected in a similar manner as 'ypred', no?
Matt J
Matt J 2021년 4월 5일
I don't think so. Additing or removing the known slope term doesn't change how much stochastic uncertainty you have.

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

추가 답변 (1개)

Bruno Luong
Bruno Luong 2021년 4월 5일
% find intercept such that
% y ~= givenslope*x + intercept
% in the least squares sense
intercept = mean(y - givenslope*x)

카테고리

Help CenterFile Exchange에서 Multiple Linear Regression에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by