필터 지우기
필터 지우기

Loop through date time heart rate data

조회 수: 6 (최근 30일)
Ross Thompson
Ross Thompson 2021년 4월 23일
답변: Eric Sofen 2021년 5월 7일
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)

답변 (1개)

Eric Sofen
Eric Sofen 2021년 5월 7일
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

카테고리

Help CenterFile Exchange에서 Timetables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by