How Calculate R squared from a linear regress
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
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.)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Linear and Nonlinear Regression 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!