How to calculate a line-of-best-fit equation (y=mx+b) from a simple x-y dataset, and then to use this equation to calculate r-square?

2 views (last 30 days)
Hi,
As stated in the title, I am trying to calculate a line-of-best-fit equation (y=mx+b) from a simple x-y dataset, and then to use this equation to calculate r-square.
At the moment I have the following syntax defining the x & y variables:
x1=dat(:,8); y1=dat(:,14);
But I am unsure of where to go from here. I have been searching these forums & MATLAB Help but I have been unable to find a workable solution.
Therefore my 2 questions are: 1. How do I use MATLAB to get a line-of-best-fit equation for this x-y dataset? 2. How do I use this equation (in conjuction with the x-y dataset) to calculate r-square?
Also, I am new to MATLAB so please go easy on me!
Thanks,
Alan

Accepted Answer

Sean de Wolski
Sean de Wolski on 16 Jun 2011
doc polyfit
and then
doc polyval
doc corrcoef
like magic!
Welcome to MATLAB Answers!

More Answers (2)

Matt Tearle
Matt Tearle on 16 Jun 2011
Approach 1: what Sean said. (Note corrcoef gives the correlation coefficient r, not the coefficient of determination r^2)
Approach 2: use regress, if you have Statistics Toolbox. This allows all sorts of fancy stuff beyond just a fit, as well as post-fit diagnostics.
Approach 3: DIY:
F = [x1.^0 x1]; % make design matrix [1,x]
c = F\y1 % get least-squares fit
res = y1 - F*c; % calculate residuals
r2 = 1 - var(res)/var(y) % calculate R^2

Alan Mason
Alan Mason on 17 Jun 2011
Thank you both for replying. I actually went with Matt's DIY approach (as this showed the logical steps) and it worked great. The rest of my code I'm not so sure about, but that's another story.....
Here's what I ended up with (practically a copy of Matt's DIY code):
%curve fitting model #1 vpd&LE
x1=dat(:,8);
y1=dat(:,14);
% rsquare_vpd
% make design matrix [1,x]
F1 = [x1.^0 x1];
% get least-squares fit
c1 = F1\y1;
% calculate residuals
res1 = y1 - F1*c1;
% calculate R^2
rsquare_vpd = 1 - var(res1)/var(y1);

Community Treasure Hunt

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

Start Hunting!