フィルターのクリア

How Calculate R squared from a linear regress

4 ビュー (過去 30 日間)
Nicholas Deosaran
Nicholas Deosaran 2020 年 9 月 29 日
回答済み: Star Strider 2020 年 9 月 29 日
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 Center および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by