How to calculate average inter-arrival time from data set?
12 views (last 30 days)
Show older comments
Hi all,
I am attempting to calculate the average inter-arrival time of hospital admissions for each hour during the afternoon shift (14:00-22:00) for each of the 45 days that I have data (note: the days are not consecutive, they are only weekdays). Ultimately, I would like to save the output in a table for easier viewing if possible. I have attached both a schematic of what I am trying to do and the txt file of data.
I have attempted to use loops to do this but my intution says there is an easier way to do this so I figured I would ask the community. Help would be greatly appreciated as I have never computed anything like this using matlab before.
Matt
0 Comments
Answers (1)
cdawg
on 28 Apr 2023
I took a shot at this even though I had for loops :-)
Enjoyed this (even if it might not be correct- haha) thanks!!
%% IMPORT TEXT FILE
opts = delimitedTextImportOptions("NumVariables", 4);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["Diagnosis", "Date", "Time", "Region"];
opts.VariableTypes = ["categorical", "datetime", "datetime", "categorical"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Diagnosis", "Region"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "Date", "InputFormat", "yyyy-MM-dd");
opts = setvaropts(opts, "Time", "InputFormat", "HH:mm");
% Import the data
data = readtable("pts_admission_Feb_March.txt", opts);
clear opts
data = removevars(data,["Diagnosis","Region"]);
%% START DATA ANALYSIS
% Remove irrelevant data (not within afternoon shift)
data = data(isbetween(data.Time, datetime('14:00','InputFormat','HH:mm'),datetime('22:00','InputFormat','HH:mm')),:);
% Start sorting the data based on date/time
data.Date = categorical(data.Date,unique(data.Date),cellstr(strcat('Day ',string(1:length(unique(data.Date))))));
times = split(string(data.Time),':');
data.Hours = categorical(times(:,1), unique(times(:,1)), cellstr(unique(times(:,1))));
data.mins = str2double(times(:,2));
% Initialize tables
avs = zeros(length(categories(data.Date)), length(categories(data.Hours))-1);
ci = zeros(length(categories(data.Date)), length(categories(data.Hours))-1);
cat1 = categories(data.Date);
cat2 = categories(data.Hours);
z = 1.96; % for 95% CI
for ii = 1:length(cat1)
datii = data(data.Date == cat1{ii},:);
for jj = 1:length(cat2)-1
datjj = datii(datii.Hours == cat2{jj},:);
n = length(datjj.mins);
avs(ii,jj) = mean(diff(datjj.mins));
s = std(diff(datjj.mins));
ci(ii,jj) = z*s/sqrt(n);
end
end
meanTimes = array2table(avs, "VariableNames", strcat('Hour',cat2(1:end-1)), "RowNames",cat1)
CI = array2table(ci, "VariableNames", strcat('Hour',cat2(1:end-1)), "RowNames",cat1)
So the confidence intervals are meanTimes +/- CI?
See Also
Categories
Find more on Data Distribution Plots 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!