0

I am working with a data set of 10,000s of variables which have been repeatedly measured since the 1980s. The first meassurements for each variable are not on the same date and the variables are irregularly measured - sometimes measurements are only a month apart, in a small number of cases they are decades apart.

I want to get the change in each variable per month.

So far I have a cell of dates of measurements,and interpolated rates of change between measurements (each cell represents a single variable in either, and I've only posted the first 5 cells in each array)

DateNumss= {[736614;736641;736669]  [736636;736666] 736672  [736631;736659;736685]  736686}

LinearInterpss={[17.7777777777778;20.7142857142857;0]   [0.200000000000000;0]   0   [2.57142857142857;2.80769230769231;0]}

How do I get monthly sums of the interpolated change in variable?

i.e.

If the first measurement for a variable is made on the January 1st, and the linearly interpolated change between that an the next measurement is 1 per day; and the next measurement is on Febuary the 5th and the corresponding linearly interpolated change is 2; then January has a total change of 1*31 (31 days at 1) and febuary has a total change of 1*5+2*23 (5 days at 1, 23 days at 2).

Abijah
  • 512
  • 4
  • 17

1 Answers1

0

You would need the points in the serial dates that correspond with the change of a month.

mat(:,1)=sort(repmat(1980:1989,[1,12]));
mat(:,2)=repmat(1:12,[1,size(mat,1)/12]);
mat(:,3)=1;
monthseps=datenum(mat);

This gives you a list of all 120 changes of months in the eighties.

Now you want, for each month, the change per day, and sum it. If you take the original data it is easier, since you can just interpolate each day's value using matlab. If you only have the "LinearInterpss" you need to map it on the days using interp1 with the method 'previous'.

for ct = 2:length(monthseps)
    days = monthseps(ct-1):(monthseps(ct)-1); %days in the month
    %now we need each day assigned a certain change. This value depends on your "LinearInterpss". interp1 with method 'previous' searches LineairInterpss for the last value.
    vals = interp1(DateNumss,LinearInterpss,days,'previous'); 
    sum(vals); %the sum over the change in each day is the total change in a month
end
Gelliant
  • 1,835
  • 1
  • 11
  • 23
  • That doesn't quite work: `mat(:,1)=sort(repmat(2010:2019,[1,12])); mat(:,2)=repmat(1:12,[1,10]); mat(:,3)=1; monthseps=datenum(mat); values=LinearInterpss{1}; datnums=DateNumss{1}; for ct = 2:length(monthseps) sum(values(datnums>monthseps(ct-1)&datnums – Abijah Apr 25 '17 at 16:51
  • I updated the answer to interpolate over all the days in the month. If you type "help interp1" you'll see there are different methods of interpolation you can use instead of 'nearest'. – Gelliant Apr 26 '17 at 12:29
  • Your new method sums the interpolated values; not the change between them. What i am trying to obtain is ***change per month*** for each variable. – Abijah Apr 27 '17 at 09:32
  • I changed the method to 'previous'. Now it takes the last value in LinearInterpss as the change value for the day. – Gelliant Apr 27 '17 at 17:51