MATLAB Answers


How can I calculate r-square from basic fitting parameters?

Asked by Mary
on 11 Aug 2012

hello there! i have a short question about how to calculate r-square with the help of the basic fitting parameters. i have two vectors x and y, which i plot against each other in a scatter plot and fit a linear regression line through that cluster with the basic fitting tool matlab provides. i get the residuals and coefficients from that, but i don't know how to get to the r-square value from there. help anyone? thanks a lot in advance!



No products are associated with this question.

3 Answers

Answer by the cyclist
on 11 Aug 2012

Which "basic fitting tool" are you using?

The "regress" command in the Statistics Toolbox reports the value of r-square in the stats output structure.


Answer by Image Analyst
on 11 Aug 2012

You mean like where you got an array of estimated values and you have an array of actual values? Like if you used the actual values in polyfit() to get an equation, and then used polyval() to get the fitted (estimated) values? From the help for corrcoeff():

R = corrcoef(X) returns a matrix R of correlation coefficients calculated from ....
R = corrcoef(x,y) where x and y are column vectors is the same as corrcoef([x y]). If x and y are not column vectors, corrcoef converts them to column vectors. For example, in this case R=corrcoef(x,y) is equivalent to R=corrcoef([x(:) y(:)]).

Here x would be the actual values and y would be your estimated (fitted) values.


Answer by Star Strider
on 11 Aug 2012
Edited by Star Strider
on 11 Aug 2012

I refer you to the core MATLAB function corrcoef. It should do what you want.

It will even calculate the P-value and confidence limits for the R-statistic if you want it to.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!