MTE Project: Task 1
MTE Project: Task 1
Nishant Kumar
21/01/2021
Task 1:-
# Load packages
library(data.table)
Task 2:-
# Sort the data by id, antibiotic type, day
setorder(antibioticDT, patient_id, antibiotic_type, day_given)
antibioticDT[1:40]
# Use shift to calculate the last day a particular drug was administered
antibioticDT[ , last_administration_day := shift(day_given, 1),
by = .(patient_id, antibiotic_type)]
# Calculate the number of days since the drug was last administered
antibioticDT[ , days_since_last_admin := day_given – last_administration_day]
# Create antibiotic_new with an initial value of one, then reset it to zero as needed
antibioticDT[ , antibiotic_new := 1]
antibioticDT[days_since_last_admin <= 2, antibiotic_new := 0]
Task 3 :-
# Read in blood_cultureDT.csv
blood_cultureDT <- fread(“blood_cultureDT.csv”)
## patient_id blood_culture_day
## 1: 1 3
## 2: 1 13
## 3: 8 2
## 4: 8 13
## 5: 23 3
## 6: 39 10
## 7: 45 4
## 8: 45 9
## 9: 45 11
## 10: 51 3
## 11: 51 6
## 12: 59 2
## 13: 64 1
## 14: 66 9
## 15: 66 10
## 16: 69 2
## 17: 69 6
## 18: 69 7
## 19: 69 11
## 20: 69 16
## 21: 76 1
## 22: 77 3
## 23: 79 5
## 24: 79 11
## 25: 79 12
## 26: 80 3
## 27: 80 12
## 28: 81 2
## 29: 112 6
## 30: 115 2
## patient_id blood_culture_day
Task 4:-
# Merge antibioticDT with blood_cultureDT
combinedDT <- merge(blood_cultureDT,antibioticDT,all = FALSE,by = ‘patient_id’)
Task 5:-
# Make a new variable called drug_in_bcx_window
combinedDT[ , drug_in_bcx_window :=
as.numeric(day_given - blood_culture_day <= 2 &
day_given - blood_culture_day >= -2)]
Task 6:-
# Create a variable indicating if there was at least one I.V. drug given in the window
combinedDT[ ,
any_iv_in_bcx_window := as.numeric(any(route == 'IV' & drug_in_bcx_window ==
1)),
by = .(patient_id, blood_culture_day)]
# Exclude rows in which the blood_culture_day does not have any I.V. drugs in window
combinedDT <- combinedDT[any_iv_in_bcx_window == 1]
Task 7:-
# Create a new variable called day_of_first_new_abx_in_window
combinedDT[ ,
day_of_first_new_abx_in_window :=
day_given[antibiotic_new == 1 & drug_in_bcx_window == 1][1],
by = .(patient_id, blood_culture_day)]
# Remove rows where the day is before this first qualifying day
combinedDT <- combinedDT[day_given >= day_of_first_new_abx_in_window]
Task 8
# Create a new data.table containing only patient_id, blood_culture_day, and day_given
simplified_data <- combinedDT[ , .(patient_id, blood_culture_day, day_given)]
Task 9
# Count the antibiotic days within each patient/blood culture day combination
simplified_data[ , num_antibiotic_days := .N, by = .(patient_id, blood_culture_day)]
# Remove blood culture days with less than four rows
simplified_data <- simplified_data[num_antibiotic_days >= 4]
Task 10:-
# Make the indicator for consecutive sequence
first_four_days[ , four_in_seq := as.numeric(max(diff(day_given)) < 3), by = .
(patient_id, blood_culture_day)]
Task 11:-
# Select the rows which have four_in_seq equal to 1
suspected_infection <- first_four_days[four_in_seq == 1]
# Remove duplicates
suspected_infection <- unique(suspected_infection)
Task 12:-
# Read in "all_patients.csv"
all_patientsDT <- fread("all_patients.csv")
# Calculate the percentage of patients who met the criteria for presumed infection
ans <- all_patientsDT[ , 100*mean(infection == 1)]