11 views (last 30 days)

Dear all,

I have a daily time series data and I want to calculate per year slope from this. While calculating the slope, I wanted to select only those time-points which were at least 1-month apart compared to first timepoint. I tried to use "retime" function that would give me possible time-points, but the problem is Var1 data are duplicated in TT2 and placed in new time-points, but I want to choose the next available time-point from TT1.mat. For example, if my 30th day from 1st time point (-10 days) is 20th day and there is no data for 20th day in TT1.mat, I will choose next available timepoint from TT1.mat i.e., 26th day. So my questions are:

- How can I choose actual time-points from TT1.mat that are at 1, 2, 3, 4, 5......month distant from first timepoint?
- How can I select 12-month timepoint (from first timepoint) as the cut-off timepoint?
- Is converting "days" to "years" and using them as X variable the correct way to calculate slope per year? Or, do I have to do divide it by the difference of last time point and first time point?

dt = days(30);

TT2 = retime(TT1,'regular','next','TimeStep',dt); %please see TT1.mat in attachment

T = timetable2table(TT2); %converting to table

[C,IA,IC] = unique(T.Var1,'first'); %take first instance for duplicate data

B = sortrows(T(IA,:)); %sort ascending

B=B(~any(ismissing(B),2),:); %remove missing or NaN values

daynumbers = floor(datenum(B.Time)); %convert to day numbers

non_zero_X=[daynumbers/365]; %converting daynumbers to yearnumber

non_zero_Y=[B.Var1]; %eGFR values

M=polyfit(non_zero_X,non_zero_Y,1);

y1 = polyval(M,non_zero_X);

cc(m,1)=M(1); %raw slope

cc(m,2)=M(2); %raw intercept

Mohith Kulkarni
on 25 Nov 2020

Here is the workaround to get your desired B matrix. This code runs a loop through the TT1 to find the next time stamp of the 30 day interval.

dur = -10:30:1660;

rows = zeros(length(dur),1);

currDurIdx = 1;

for idx = 1:size(TT1,1)

if TT1.Time(idx) >= days(dur(currDurIdx))

rows(currDurIdx) = idx;

currDurIdx = currDurIdx +1;

end

if currDurIdx > length(dur)

break

end

end

B = timetable(TT1.Time(rows),TT1{rows,1})

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/652543-calculating-per-year-slope-using-daily-time-series-data#comment_1145163

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/652543-calculating-per-year-slope-using-daily-time-series-data#comment_1145163

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/652543-calculating-per-year-slope-using-daily-time-series-data#comment_1147828

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/652543-calculating-per-year-slope-using-daily-time-series-data#comment_1147828

Sign in to comment.