Calculating per year slope using daily time series data
4 次查看(过去 30 天)
显示 更早的评论
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
2 个评论
Peter Perkins
2020-11-19
There are a couple things not clear in your question that you are going to have to explain:
Not at all clear what you are trying to achieve with retime.
Does annual slope mean "slope for each year", or does it mean "slope for the one year period following each time point"? It's especially unclear given that your sample data runs for only 84 days, has duration timestamps that are not even regular, and has repeated time points.
What does "actual time-points that are at least 1-month apart compared to first timepoint" mean.
To use polyval, you'll need to convert your timestamps into numeric values, so you are on the right track there. I recommend not using datenum, for one thing, such large numbers don't help with accuracy. I would say use the day function on your datetimes to get day of year, but you don't seem to have datetimes. And using hard-coded 365 is a bug about 25% of the time.
回答(1 个)
Mohith Kulkarni
2020-11-25
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})
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Events 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!