MATLAB Answers

0

excel linear regression trouble vs excel linear regression

Asked by Kolton Jones on 12 Apr 2019
Latest activity Edited by John D'Errico
on 13 Apr 2019
I am trying to find the equation for the linear regression line of x and y.
I was able to get the linear regression from excel, but I am trying to find it with matlab.
Excel says the linear regression equation is y = -0.003x + 1.7919.
x = 5.92 22.75 73.26 227.56 308.74 589.54 613.66
y = 2.550865 1.869146 1.1623 0.567358 0.459001 0.248734 0.225807
beta = regress(y',x')
% I had to rotate the arrays otherwise the function would not give an answer
the function spits out beta = 7.9666e-04.

  0 Comments

Sign in to comment.

1 Answer

Answer by John D'Errico
on 13 Apr 2019
Edited by John D'Errico
on 13 Apr 2019

Simple enough.
polyfit(x,y,1)
ans =
-0.002965 1.7919
Or:
regress(y',[x',ones(length(x),1)])
ans =
-0.002965
1.7919
Or:
[x',ones(length(x),1)]\y'
ans =
-0.002965
1.7919
I could go on for at least a half dozen others. Don't dare me. ;-) With the curve fitting toolbox...
fit(x',y','poly1')
ans =
Linear model Poly1:
ans(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = -0.002965 (-0.005117, -0.0008127)
p2 = 1.792 (1.03, 2.554)
>> lsqr([x',ones(length(x),1)],y')
lsqr converged at iteration 2 to a solution with relative residual 0.34.
ans =
-0.002965
1.7919
>> lsqlin([x',ones(length(x),1)],y')
ans =
-0.002965
1.7919
>> lsqminnorm([x',ones(length(x),1)],y')
ans =
-0.002965
1.7919
>> pinv([x',ones(length(x),1)])*y'
ans =
-0.002965
1.7919
>> lscov([x',ones(length(x),1)],y')
ans =
-0.002965
1.7919
Wanna bet I can't come up with a half dozen more? Save your money. :)

  0 Comments

Sign in to comment.