How Calculate R squared from a linear regress

2 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2020-9-29
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 个)

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by