How to regress with the simple model like y=kx or y=k/x
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Grzegorz Knor
el 20 de Sept. de 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).
Ver también
Categorías
Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!