why am i getting zero slope?

I calculate linear regression (temperature change (y) over 30 years (x)) using a large dataset (producing >55000 slopes). 62% of the slopes are exactly zero, which makes no sense.
Also, although no error comes up, I am getting a warning 'x is rank deficient to machine precision', presumably this is because of the zeros. The regress function, I understood, manages nan values by excluding them. Should I tell it to treat zeros as nan, or is that too much of an assumption?
% Read in the necessary variables
lat = ncread('file.nc', 'lat');
lon = ncread('file.nc', 'lon');
time = ncread('file.nc', 'time');
TXx = double(ncread('file.nc', 'TXx'));
% Create storage for regression
M = numel(lon);
N = numel(lat);
slope = zeros(M, N);
intercept = zeros(M, N);
T = numel(time);
% Regress each lat/lon location
for i = 1 : M
for j = 1 : N
% Get all time instances of each lat/lon location
y = squeeze(TXx(i, j, :));
% Create regression problem and solve
x = [ones(T, 1) time];
c = regress(y, x);
intercept(i, j) = c(1);
slope(i, j) = c(2);
end
end

3 Comments

NaN will propogate as NaN.
Would need to see the data to be able to tell anything; rank-defiicent for a linear regression would imply there simply aren't any data left for the case that are different between observations excepting by a constant ratio.
Have you done the obvious of plotting the data to ensure it looks at all like you expect it does?
But, attach a .mat file with a representative sub-sample of the data that will exhibit the problem you encounter...a single sample would suffice in the limit, of course, but seeing if there's something going on in the coding with some of the actual data would be better, probably
hi, thanks for your message, sorry -kind of a newbie here - but how would i do a representative sub-sample?
"but how would i do a representative sub-sample?"
You pick enough of your data so that it shows the behavior that you describe. Start by selecting one single set of x, y, and c arrays where this situation occurs, and upload them here in one .mat file.

Sign in to comment.

Answers (0)

Products

Release

R2018b

Asked:

on 6 Dec 2018

Commented:

on 6 Dec 2018

Community Treasure Hunt

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

Start Hunting!