Clear Filters
Clear Filters

how to create a linear regression in least squares method and Least absolute value method, and find the correlation coefficients.

3 views (last 30 days)
I would like to create a linear regression in a scatter plot which are created by following code in two methods, least squares method and Least absolute value method, and I find the correlation coefficients. I want to create a linear regression from all of the plot, not for each data set. If you have good idea, please give me advice.
following my code;
for i = 1:12
hold on
if i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12
for j = 1:31
xfilename = sprintf('TA2004%02d%02d.txt',i,j);
yfilename = sprintf('TS2004%02d%02d.txt',i,j);
x = load(xfilename);
y = load(yfilename);
if i==1 && j == 1
scatter(x,y);
else
scatter(x,y);
end
end
elseif i==2
for j = 1:29
xfilename = sprintf('TA2004%02d%02d.txt',i,j);
yfilename = sprintf('TS2004%02d%02d.txt',i,j);
x = load(xfilename);
y = load(yfilename);
scatter(x,y);
end
else
for j = 1:30
xfilename = sprintf('TA2004%02d%02d.txt',i,j);
yfilename = sprintf('TS2004%02d%02d.txt',i,j);
x = load(xfilename);
y = load(yfilename);
scatter(x,y);
end
end
hold off
end
xlabel('surface temperature');
ylabel('near-surface air temperature');

Answers (1)

michio
michio on 10 Oct 2016
As far as I know, MATLAB does not have least absolute value method implemented. Related answers are
  1. Is there a routine to perform quantile regression or LAD (Least Absolute Deviations) in MATLAB?
  2. Least Absolute Value based regression
It seems that you have two text files for each day in 2004 (366x2=732 files). If you are looking at getting some statistics from data, instead of just visualizing them (eg. scatter, histogram), I'd suggest you import all of the data (if RAM allows, it seems it does) then use functions like polyfit and corrcoef.
There are multiple ways to achieve this, but if you are using R2014b or later, I strongly recommend using datastore functionality. See Getting started with datastore.
If the data format allows, your data import becomes as simple as the following four lines.
dsX = datastore('TA2004*.txt');
x = readall(dsX);
dsY = datastore('TS2004*.txt');
y = readall(dsY);

Categories

Find more on Descriptive Statistics 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!