How to regress with the simple model like y=kx or y=k/x

12 Ansichten (letzte 30 Tage)
Jiazeng Shan
Jiazeng Shan am 20 Sep. 2011
Usually, the regression is done in Matlab with "regress", but it recommends the input X with a column of ones. And I find that if without the constant coefficient setup, the R2 in the states would be negative. My question is: How to regress the k in the simple model y=kx or y=k/x? Note that no constant factor in the model. By the way, can the definition of R^2=sum((y_p-y_mean).^2)/sum((y-y2).^2) still be used to calculate the correlation factor when facing the y=kx or y=k/x model? Thank you.

Antworten (1)

Grzegorz Knor
Grzegorz Knor am 20 Sep. 2011
When you are sure that he model without constant is appropriate for the data you can use it withous column of ones.
% first case y = kx
x = 0.1:.1:10;
x = x(:);
y = 2*x+rand(size(x));
plot(x,y)
k = x\y % just for compare
[b,bint,r,rint,stats] = regress(y,x);
hold on
plot(x,k*x,'r')
% second case y = k/x
x = 0.1:.1:10;
x = x(:);
y = 2./x+rand(size(x));
figure
plot(x,y);
k = (1./x)\y %just for compare
[b,bint,r,rint,stats] = regress(y,(1./x));
hold on
plot(x,k./x,'r')
Alternatively, you can catch the situation when the model is inadequate:
% first case y = kx+c
x = 0.1:.1:10;
x = x(:);
y = 3+2*x+rand(size(x));
plot(x,y)
[b,bint,r,rint,stats] = regress(y,x);
[msgstr, msgid] = lastwarn;
if strcmpi(msgid,'stats:regress:NoConst')
[b,bint,r,rint,stats] = regress(y,[x ones(size(x))]);
end
hold on
if length(b)==1
plot(x,b*x,'r')
else
plot(x,b(1)*x+b(2),'r')
end
If you still want to fit model like y = kx use left matrix division operator (see example 1 & 2).
  1 Kommentar
Jiazeng Shan
Jiazeng Shan am 27 Sep. 2011
Thank you Grzegorz. It helps me to solve me question. But do you know how to correctly calculate the correlation factor between the measurement and the prediction data?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by