# How Calculate R squared from a linear regress

38 views (last 30 days)
Nicholas Deosaran on 29 Sep 2020
Answered: Star Strider on 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 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 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 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Gaussian Process Regression in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by