MATLAB Answers

Is it possible to extract also R^2 value from linear fit between 2 vectors ?

3 views (last 30 days)
Galina Machavariani
Galina Machavariani on 14 Jan 2020
Commented: Star Strider on 14 Jan 2020
Hello,
I know that it is possible to find fit parameters using polyfit command.
F.e., linearCoefficients = polyfit(x, y, 1)
Is it possible to extract also R^2 value from linear fit between 2 vectors ?
Thank you !

  0 Comments

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Jan 2020
It is, however polyfit wil not do it for you.
Try this:
x = 1:10; % Create ‘x’
y = randn(size(x)) + 0.2*x; % Create ‘y’
linearCoefficients = polyfit(x, y, 1); % Coefficients
yfit = polyval(linearCoefficients, x); % Estimated Regression Line
SStot = sum((y-mean(y)).^2); % Total Sum-Of-Squares
SSres = sum((y-yfit).^2); % Residual Sum-Of-Squares
Rsq = 1-SSres/SStot; % R^2

  4 Comments

Show 1 older comment
Star Strider
Star Strider on 14 Jan 2020
My pleasure.
Neither polyfit nor polyval return the value.
The polyfit function optionally returns a covariance matrix (in the ‘S’ output in this example) that can be used to calculate the confidence intervals for the parameters and the regression, although it does not calculate them directly. The polyval function can use these to return a ‘delta’ output that is similar to the confidence intervals on the regression, although not exactly.

Sign in to comment.

Sign in to answer this question.

Tags