How Calculate R squared from a linear regress

6 Ansichten (letzte 30 Tage)
Nicholas Deosaran
Nicholas Deosaran am 29 Sep. 2020
Beantwortet: Star Strider am 29 Sep. 2020
Hey all I have this equation below and trying to figure out how to get the R^2.
I can't seem to understand what I am doing wrong.
x = 0:0.1:10;
n = 0;
noise = n*rand(1,length(x));
y = 2*x+1+noise; % y function
b = regress(y(:),[ones(size(x(:))),x(:)]); % get the intercept and slope
figure();
plot(x,y,'d') %plot as diamonds
hline = refline(b(2),b(1)); % plot the liner regresson line
hline.Color = 'r'; % change linear regresson line to red
I have looked at different ways in MATLAB but can't seem to understand.
Thank you.

Akzeptierte Antwort

Star Strider
Star Strider am 29 Sep. 2020
They are hidden in the ‘stats’ output:
[b,~,~,~,stats] = regress(y(:),[ones(size(x(:))),x(:)]); % get the intercept and slope
Rsq = stats(1)
Rsq_p = stats(3)
Fstat = stats(2)
ErrVar = stats(4)
Even the documentation is not straightforward with disclosing which outputs are those it mentions. This is my best effort as to deciphering them. (I am certain that the first two are correct, based on my reading of the documentation.)

Weitere Antworten (0)

Kategorien

Mehr zu Historical Contests 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