Loop through date time heart rate data

1 view (last 30 days)
Ross Thompson
Ross Thompson on 23 Apr 2021
Answered: Eric Sofen on 7 May 2021
I have a large csv with lots of days worth of continuous heartrate data which can be seen below. I have been splitting the data up into 2 groups, each an hour in length, to calculate the p value between the 2 groups. I want to create a loop which will continuously move the split point (30th point in this case) of the groups forward by 1 datapoint and then calculte the new p value for the new split. I want to then plot these p-values over the whole timeframe. How would i go about doing this?
Timestamp Heart rate
2021-02-01 12:00:00 76.0
2021-02-01 12:02:00 89.0
2021-02-01 12:04:00 86.0
2021-02-01 12:06:00 89.0
2021-02-01 12:08:00 82.0
2021-02-01 12:10:00 87.0
2021-02-01 12:12:00 87.0
2021-02-01 12:14:00 84.0
2021-02-01 12:16:00 81.0
2021-02-01 12:18:00 81.0
data = readtable('heartrate.csv');
A = data.HeartRate(1:30);
B = data.HeartRate(31:60);
[h, p, ci] = ttest2(A,B)
C = data.HeartRate(2:31);
D = data.HeartRate(32:61);
[h, p, ci] = ttest2(C,D)

Answers (1)

Eric Sofen
Eric Sofen on 7 May 2021
Can you assume that the data is always sampled uniformly every 2 minutes? That can simplify things, but the code below should be general.
Also, I'd recommend using a timetable for this, as it will make time subscripting easier.
The timerange function will help with selecting the data chunks.
% Cook up some synthetic data
HeartRate = 60+randi(20,[30*24,1]);
data = timetable(HeartRate,'TimeStep',minutes(2),'StartTime',datetime(2021,05,07));
data.Properties.DimensionNames{1} = 'Timestamp';
windowStart = data.Timestamp(1);
% Need to figure out where to stop so there's enough data for the last set
% of moving windows.
windowEnd = data.Timestamp(end)-hours(2);
numWindows = height(data(timerange(windowStart,windowEnd),:));
% Preallocate the p-value timetable. Split the confidence interval into 2
% vars. It could also be one 2-element wide var.
tp = array2timetable(nan(numWindows,4),'RowTimes',data.Timestamp(1:numWindows),'VariableNames',{'h','p','ciLow','ciHigh'});
for ii = 1:numWindows
start = data.Timestamp(ii);
A = data.HeartRate(timerange(start, start+hours(1)));
B = data.HeartRate(timerange(start+hours(1),start+hours(2)));
[h, p, ci] = ttest2(A,B);
tp.h(ii) = h;
tp.p(ii) = p;
tp{ii,["ciLow","ciHigh"]} = ci';
end

Categories

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