Force constant slope in linear regression

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 Comment

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...

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 30 Mar 2021
Edited: Matt J on 30 Mar 2021
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 Comments

It computes the intercept term, but the way the linear model object is built and packaged, there's not much useful that can be done with the result.
Since there's no predictor variable, all plot(mdl) returns is a histogram of the residuals. Similar limitations with the other functions for the various diagnostics plots.
The added terms stuff would let you add the independent variable to the model, but then it automagically recomputes with the effect of the variable the coefficients and one is left with the OLS results, not with the specific slope.
It's easy-enough to solve the problem OP is after, but to be able to package the results in a useful form to retain the builtin functionality of the linear model object seems to be out of reach.
It's easy to modify the plots:
h=plot(mdl);
for i=1:numel(h)
h(i).YData=h(i).YData + m*h(i).XData;
end
Thank you for these comments. This was more of a programming question than a mathematical/statistical question. I was looking for a solution that works for both the free exponent and fixed exponent cases, so that I can accomodate both cases with as few lines of code as possible. But, it seems like I'll need to provide write different code for each case, which is unfortunate because, besides being less elegant, it's also more error-prone, and doesn't make sole use of built-in functionality.
Matt, your first comment is close to what I was trying to do. I had considered this approach earlier. The problem with this solution is that, if you then use mdl.predict, it will just spit out the intercept, rather than the prediction at a particular value of x.
The problem with this solution is that, if you then use mdl.predict, it will just spit out the intercept, rather than the prediction at a particular value of x.
Why is that a problem? Can't you just do
mdl.predict + m*x
One thing that might help is to write your own mdl class for the known-slope case, one which provides the same interface to the fit as what regular LinearModel objects give you.
classdef KnownSlope
properties
slope,
mdlConst
end
methods
function mdl=KnownSlope(m,x,y)
mdl.slope=m;
mdl.mdlConst=fitlm(x,y-m*x,'constant');
end
function [ypred,yci] = predict(mdl,Xnew,varargin)
[ypred,yci] = predict(mdl.mdlConst,Xnew,varargin{:});
ypred=ypred+mdl.slope*Xnew;
end
function h=plot(mdl)
h=plot(mdl.mdlConst);
for i=1:numel(h)
h(i).YData=h(i).YData + mdl.slope*h(i).XData;
end
end
end
end
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?
I don't think so. Additing or removing the known slope term doesn't change how much stochastic uncertainty you have.

Sign in to comment.

More Answers (1)

% find intercept such that
% y ~= givenslope*x + intercept
% in the least squares sense
intercept = mean(y - givenslope*x)

Products

Release

R2021a

Asked:

on 30 Mar 2021

Commented:

on 5 Apr 2021

Community Treasure Hunt

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

Start Hunting!