Effacer les filtres
Effacer les filtres

How to force the intercept of a regression line to zero?

216 vues (au cours des 30 derniers jours)
Ali Y.
Ali Y. le 16 Juil 2015
Commenté : John D'Errico le 12 Août 2022
Hi; How to set the intercept of a regression line,, resulted from fitlm, to zero?
clc
X = 1:10
y = [1, 2, 2.9, 4, 5.1, 6, 7, 7.8, 8.6, 9.5]
dlm = fitlm(X,y)
Thank you, in advance, for your help.
  1 commentaire
Brendan Hamm
Brendan Hamm le 16 Juil 2015
I would highly suggest learning the Wilkinson notation, as this allows you to fit models and specify the form of the equation you would like to fit.

Connectez-vous pour commenter.

Réponse acceptée

Brendan Hamm
Brendan Hamm le 16 Juil 2015
There are 2 main ways you can do this:
dlm = fitlm(X,y,'Intercept',false);
or using Wilkinson notation:
dlm = fitlm(X,y,'y~x1-1');
I would highly suggest learning the Wilkinson notation, as this allows you to fit models and specify the form of the equation you would like to fit.
  2 commentaires
DEWDROP
DEWDROP le 10 Mai 2020
could you please tell me what is the difference between mdl=fitlm and dlm=fitlm while fitting the regression line?
John D'Errico
John D'Errico le 12 Août 2022
You can name an output variable to be anything you want. There is NO relevance.

Connectez-vous pour commenter.

Plus de réponses (1)

George Tzintzarov
George Tzintzarov le 6 Oct 2018
I would use the 'fittype' function:
ft1 = fittype({'x'}) %This creates a linear 'fittype' variable that is of the form f(a,x)=ax.
ft2 = fittype({'x','1'}) %This creates a linear 'fittype' variable that is of the form f(a,x)=ax+b.
Then fit and evaluate to values you want: (Note that in the fit function x and y must be column vectors)
x = [1 2 3 4]; y = [2 3 4 5];
p1 = fit(x',y',ft1); %This creates a 'cfit' variable p that is your fitted function
p2 = fit(x',y',ft2); %This creates a 'cfit' variable p that is your fitted function
x_fit = linspace(0,6,10); %x-values to evaluate
y1_fitted = feval(p1, x_fit); %y-values for the evaluated x-values
y2_fitted = feval(p2, x_fit); %y-values for the evaluated x-values
Here is what you should get:
plot(x,y,'ro'); hold on;
plot(x_fit,y1_fitted,'b-')
plot(x_fit,y2_fitted,'b--')
legend({'Raw Data','Fitted with y-int','Fitted through (0,0)'})
  1 commentaire
Vladimir Jara
Vladimir Jara le 12 Août 2022
any solution withoug additional toolboxs?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by