Commit e3ad1d63 authored by Cecilia Akerlund's avatar Cecilia Akerlund

Upload New File

parent 70841576
#1) Make new baseline/ms with new data from Peter
rm(list=ls())
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
nas <- c("NA","")
f <- "C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/Neurobot data/Core2.1/"
# Read in data: ---------------------------------
ms <- read.csv("C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/Fred Zeiler/CENTER_HR_Min-by-Min.csv", na.strings = nas, stringsAsFactors = FALSE)
ms <- ms %>% mutate(Date_Time = as.POSIXct(Date_Time))
ms <- ms %>% dplyr::rename(gupi = pid, icp = ICP, datetime = Date_Time) %>% select(-X) %>% filter(!is.na(icp))
length(unique(ms$gupi))
ms.new <- read.csv("C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/Peter_HRdata/ms.new.csv", stringsAsFactors = FALSE, na.strings = nas) #Created in HR_summaries.R
ms.new <- ms.new %>% rename(datetime = Time, icp=ICP, icp.evd = ICP.evd) %>% select(-DateTime)
ms.new <- ms.new %>% mutate(datetime = as.POSIXct(datetime))
length(unique(ms.new$gupi))
ms.new <- ms.new %>% filter(!is.na(icp) | !is.na(icp.evd))
length(unique(ms.new$gupi))
# dev.new()
# print(ggplot(ms.new %>% filter(!is.na(icp.evd)), aes(x=datetime, y=icp.evd)) + geom_line() + facet_wrap(~as.factor(gupi), scales = "free"))
#
# dev.new()
# print(ggplot(ms.new %>% filter(!is.na(icp), gupi %in% unique(filter(ms.new, !is.na(icp))$gupi)[1:20]), aes(datetime, icp)) + geom_line() + facet_wrap(~as.factor(gupi), scales="free"))
Baseline <- read.csv(paste0(f,"Baseline.csv"), stringsAsFactors = FALSE, na.strings = nas) # https://center-tbi.incf.org/_5edf245642f2945a46d94b66
colnames(Baseline) <- gsub(".*\\.","", colnames(Baseline))
Baseline <- Baseline %>% rename(ASA = MedHxPreInjASAPSClass, PupilsBaseline = PupilsBaselineDerived, EDHypotension = EDComplEventHypotension, EDHypoxia = EDComplEventHypoxia, GOSE6mo.derived = GOSE6monthEndpointDerived)
ms <- ms %>% mutate(icp = ifelse(gupi %in% filter(Baseline, SiteCode == "64e095")$gupi, icp/5,
ifelse(gupi %in% c("7ssY436", "9wBP293"), icp/4, icp)))
# ms <- ms %>% mutate(icp=ifelse(icp< -3|icp>70, NA, icp))
ms <- ms %>% filter(!is.na(icp))
length(unique(ms$gupi))
ms <- ms %>% group_by(gupi) %>% mutate(monitor.time = difftime(max(datetime), min(datetime), units="days"))
ms.new <- ms.new %>% group_by(gupi) %>% mutate(monitor.time = difftime(max(datetime), min(datetime), units="days"))
DailyTIL <- read.csv(paste0(f,"DailyTIL.csv"), na.strings = nas, stringsAsFactors = FALSE) %>% # https://center-tbi.incf.org/_5edf26e442f2945a46d94b67
rename(gupi = ï..gupi, Date = DailyTIL.TILDate, CSFVolume = DailyTIL.TILCCSFDrainageVolume)
colnames(DailyTIL) <- gsub("DailyTIL.", "", colnames(DailyTIL))
EEG.monitoring <- read.csv(paste0(f,"EEG_monitoring.csv"), na.strings = nas, stringsAsFactors = FALSE)
colnames(EEG.monitoring) <- c("gupi", "EEG")
EEG.monitoring <- EEG.monitoring %>% mutate(EEG = ifelse(is.na(EEG),0,EEG))
DailyTIL <- left_join(DailyTIL, EEG.monitoring, by="gupi")
rm(EEG.monitoring)
DailyTIL <- DailyTIL %>% mutate(TIL.Tier = ifelse(TILICPSurgeryDecomCranectomy==1 | TILSedationMetabolic == 1 | TILFeverMildHypothermia==1, 3, # https://link.springer.com/article/10.1007/s00134-019-05805-9/figures/2
ifelse(TILHyperventilationModerate ==1 | TILSedationNeuromuscular == 1 |
TILHyperosomolarTherapyHigher == 1 | TILHyperosomolarTherapyMannitolGreater2g == 1 |
TILFluidLoading == 1 | TILFluidLoadingVasopressor == 1, 2,
ifelse(TILSedationHigher == 1 | TILHyperventilation == 1 | TILHyperosmolarThearpy == 1 |
TILHyperosomolarTherapyHypertonicLow == 1 | TILHyperosmolarThearpy == 1 | TILCSFDrainage == 1 |
EEG == 1, 1, 0))))
DailyTIL <- DailyTIL %>% group_by(gupi) %>% summarise(TIL.mean = mean(TotalTIL, na.rm=TRUE),
TIL.max = max(TotalTIL, na.rm=TRUE),
TIL.Tier = max(TIL.Tier, na.rm=TRUE))
DailyTIL <- DailyTIL %>% mutate(TIL.mean = round(ifelse(is.nan(TIL.mean), NA, TIL.mean), 2),
TIL.max = ifelse(TIL.max == -Inf, NA, TIL.max),
TIL.Tier = ifelse(TIL.Tier == -Inf, NA, TIL.Tier)) %>%
filter(!is.na(TIL.max))
Baseline <- left_join(Baseline, DailyTIL, by="gupi")
GOSE.score <- read.csv(paste0(f,"GOSE_all_timepoints.csv"), stringsAsFactors = FALSE) # https://center-tbi.incf.org/_5edf274c42f2945a46d94b68
colnames(GOSE.score) <- c("gupi", "Timepoint", "GOSEScore")
Baseline <- left_join(Baseline, GOSE.score %>% filter(Timepoint=="6mo") %>% select(gupi, GOSEScore), by="gupi")
Baseline <- Baseline %>% mutate(GOSE6mo = ifelse(!is.na(GOSEScore), GOSEScore, GOSE6mo.derived))
Baseline <- Baseline %>% mutate(GOS = ifelse(GOSE6mo == 4,3,
ifelse(GOSE6mo == 5 | GOSE6mo == 6, 4,
ifelse(GOSE6mo == 7 | GOSE6mo == 8, 5, GOSE6mo))))
Baseline <- Baseline %>% select(-GOSEScore)
ms <- left_join(ms, Baseline %>% select(gupi, GOSE6mo, GOS, GOSE6mo.derived), by="gupi")
length(unique((ms %>% filter(!is.na(GOSE6mo)))$gupi)) #225
ms <- left_join(ms, DailyTIL, by="gupi")
ms <- ms %>% filter(monitor.time >=1)
ms <- ms %>% filter(datetime >= "1970-01-01")
ms <- ms %>% filter(datetime < "1970-01-08")
length(unique(ms$gupi)) #244
ms <- ms %>% filter(!is.na(GOSE6mo))
length(unique(ms$gupi)) #216
# ms.new:
ms.new <- left_join(ms.new, Baseline %>% select(gupi, GOSE6mo, GOS, GOSE6mo.derived), by="gupi")
length(unique(ms.new$gupi)) #273
length(unique((ms.new %>% filter(!is.na(GOSE6mo)))$gupi)) #242
ms.new <- left_join(ms.new, DailyTIL, by="gupi")
ms.new <- ms.new %>% filter(monitor.time >=1)
ms.new <- ms.new %>% filter(datetime >= "1970-01-01")
ms.new <- ms.new %>% filter(datetime < "1970-01-08")
length(unique(ms.new$gupi)) #265
ms.new <- ms.new %>% filter(!is.na(GOSE6mo))
length(unique(ms.new$gupi)) #235
# Baseline: ------------------------------------------------------------------------------------
Baseline <- Baseline %>% filter(Age >= 18)
Baseline <- Baseline %>% mutate(ICUAdmTime = ifelse(nchar(ICUAdmTime)==7, paste0("0",ICUAdmTime), ICUAdmTime),
ICUDischTime = ifelse(nchar(ICUDischTime)==7, paste0("0", as.character(ICUDischTime)), ICUDischTime))
Baseline <- Baseline %>% mutate(ASA = ifelse(ASA == 88, NA, ASA),
EDHypotension = ifelse(EDHypotension == 88, NA, EDHypotension),
EDHypoxia = ifelse(EDHypoxia == 88, NA, EDHypoxia),
GCSMotorBaselineDerived = ifelse(GCSMotorBaselineDerived == "UN", NA, GCSMotorBaselineDerived),
GCSScoreBaselineDerived = ifelse(GCSScoreBaselineDerived == "No Sum", NA, GCSScoreBaselineDerived),
DeathDate = as.POSIXct(DeathDate),
ICUAdmDateTime = ifelse(is.na(ICUAdmDate) | is.na(ICUAdmTime), NA, paste0(ICUAdmDate, " ", ICUAdmTime)),
ICUDischDateTime = ifelse(is.na(ICUDischDate) | is.na(ICUDischTime), NA, paste0(ICUDischDate, " ", ICUDischTime)))
Baseline <- Baseline %>% mutate(ICULOS = as.numeric(difftime(ICUDischDateTime, ICUAdmDateTime, units = "days")))
Baseline <- Baseline %>% group_by(gupi) %>% mutate(ExtracranISSHighest = max(BestOfFaceISS, BestOfAbdomenPelvicLumbarISS, BestOfChestSpineISS, BestOfExternaISS, BestOfExtremitiesISS))
temp2 <- c()
for (i in 1:nrow(Baseline))
{
temp <- sort(as.numeric(Baseline[i,] %>% select(BestOfChestSpineISS:BestOfExtremitiesISS)), decreasing = TRUE)
temp <- sum(temp[1:3])
temp2 <- c(temp2, temp)
}
Baseline$TotalISS.Extracran <- temp2
Baseline <- Baseline %>% select(-DateOfAdmission, -ICUAdmDateTime, -ICUDischDateTime, -ICUAdmDate, -ICUAdmTime, -ICUDischDate, -ICUDischTime,
- BestOfChestSpineISS, -BestOfFaceISS, -BestOfExternaISS, -BestOfExtremitiesISS, -BestOfAbdomenPelvicLumbarISS)
# Hypoxia, Hypotension: -------------------------
Hypo <- read.csv(paste0(f,"HypoxiahypotensionTemp.csv"), stringsAsFactors = FALSE, na.strings = nas) # https://center-tbi.incf.org/_5edf28e942f2945a46d94b69
colnames(Hypo) <- gsub(".*\\.","", colnames(Hypo))
colnames(Hypo)[4:5] <- c("TempLow", "TempHigh")
Hypo[Hypo==88] <- NA
Hypo <- Hypo %>% mutate(Temp.mean = (TempLow + TempHigh)/2)
Hypo <- Hypo %>% group_by(gupi) %>% summarise(HosComplEventHypoxia = max(HosComplEventHypoxia, na.rm=TRUE),
HosComplEventHypotension = max(HosComplEventHypotension, na.rm=TRUE),
DVTemp.mean = mean(Temp.mean, na.rm=TRUE))
Baseline <- left_join(Baseline, Hypo, by="gupi")
# CT:-------------------------------------------
CT <- read.csv(paste0(f,"CT_central.csv"), stringsAsFactors = FALSE, na.strings = nas) # https://center-tbi.incf.org/_5edf29e942f2945a46d94b6a
colnames(CT) <- gsub(".*\\.","", colnames(CT))
CT <- CT %>% mutate(tSAH = ifelse(TraumaticSubarachnoidHemorrhage == "present", 1, 0),
EDH = ifelse(EpiduralHematoma == "present", 1, 0),
aSDH = ifelse(SubduralHematomaAcute == "present", 1, 0),
cSDH = ifelse(SubduralHematomaSubacuteChronic == "present", 1, 0),
CisternalCompression = ifelse(CisternalCompression == "present", 1, 0),
SkullFracture = ifelse(SkullFracture == "present", 1, 0),
MidlineShift = ifelse(MidlineShift == "present", 1, 0),
MassLesion = ifelse(MassLesion == "present", 1, 0),
Contusion = ifelse(Contusion == "present", 1, 0),
IVH = ifelse(IntraventricularHemorrhage == "present", 1, 0)) %>%
select(gupi, Contusion, CisternalCompression:MassLesion, RotterdamCTScore:IVH) %>%
group_by(gupi) %>% summarise_all(max, na.rm=TRUE)
Baseline <- left_join(Baseline, CT, by="gupi")
Baseline[Baseline == -Inf] <- NA
# Craniectomy:---------------------------------------------------------------------------------------------
Craniectomy <- read.csv(paste0(f,"DecompressiveCran.csv"), na.strings = nas, stringsAsFactors = FALSE) # https://center-tbi.incf.org/_5edf2a5542f2945a46d94b6b
colnames(Craniectomy) <- gsub(".*\\.", "", colnames(Craniectomy))
Craniectomy <- Craniectomy %>% mutate(SurgeryDateTime = as.POSIXct(paste0(SurgeryStartDate, " ", SurgeryStartTime), format="%Y-%m-%d %H:%M:%S"),
InjuryDateTime = as.POSIXct(paste0(DateInj, " ", TimeInj), format="%Y-%m-%d %H:%M:%S"))
Craniectomy <- Craniectomy %>% mutate(SurgeryDateTime.minutes = SurgeryDateTime-InjuryDateTime)
Craniectomy <- Craniectomy %>% mutate(SurgeryDateTime.HR = as.POSIXct("1970-01-01 00:00:00") + SurgeryDateTime.minutes)
Craniectomy <- Craniectomy %>% filter(DecompressiveCran == 1)
Craniectomy <- Craniectomy %>% group_by(gupi, DecompressiveCran) %>% summarise(DecompressiveCran.HRtime = min(SurgeryDateTime.HR, na.rm=TRUE))
Baseline <- left_join(Baseline, Craniectomy, by="gupi")
Baseline <- Baseline %>% mutate(DecompressiveCran = ifelse(is.na(DecompressiveCran), 0, DecompressiveCran))
#---------------------------------------------
ms.summaries <- ms %>% group_by(gupi, monitor.time) %>% summarise(icp.mean = mean(icp, na.rm=TRUE),
icp.median = median(icp, na.rm = TRUE),
cpp.mean = mean(CPP, na.rm=TRUE),
cpp.median = median(CPP, na.rm = TRUE))
ms.new.summaries <- ms.new %>% group_by(gupi, monitor.time) %>% summarise(icp.mean = mean(icp, na.rm=TRUE),
icp.median = median(icp, na.rm = TRUE),
icp.evd.mean = mean(icp.evd, na.rm=TRUE),
icp.evd.median = median(icp.evd, na.rm=TRUE),
cpp.mean = mean(CPP, na.rm=TRUE),
cpp.median = median(CPP, na.rm = TRUE),
cpp.evd.mean = mean(CPP.evd, na.rm=TRUE),
cpp.evd.median = median(CPP.evd, na.rm = TRUE)
)
# Sodium day 2: ----------------------------------------
Labs <- read.csv(paste0(f,"Labs.csv"), na.strings = nas, stringsAsFactors = FALSE) # https://center-tbi.incf.org/_5edf2acd42f2945a46d94b6c
colnames(Labs) <- gsub(".*\\.", "", colnames(Labs))
Labs <- Labs %>% group_by(gupi, DLDate) %>% summarise(Sodium = max(DLSodiummmolL, na.rm=TRUE)) %>% filter(DLDate == "1970-01-02")
Labs[Labs == -Inf] <- NA
Baseline <- left_join(Baseline, Labs, by="gupi")
Baseline <- Baseline %>% select(-DLDate)
ms <- ms %>% select(gupi, datetime, HR, icp, MAP, CPP, PRx:RAP, GOSE6mo:TIL.Tier)
Baseline.old <- left_join(Baseline, ms.summaries, by="gupi")
Baseline.new <- left_join(Baseline, ms.new.summaries, by="gupi")
ms <- ms %>% filter(gupi %in% Baseline.old$gupi)
ms <- left_join(ms, select(Baseline.old, gupi, DecompressiveCran, DecompressiveCran.HRtime, ICPDevice), by="gupi")
length(unique(ms$gupi))
length(unique(ms.new$gupi))
ms.new <- ms.new %>% filter(gupi %in% Baseline.new$gupi)
length(unique(ms.new$gupi))
ms.new <- left_join(ms.new, select(Baseline.new, gupi, DecompressiveCran, DecompressiveCran.HRtime, ICPDevice), by="gupi")
# Additional filtering of ms, missings and implausible ICP curve appearances: -------------------------------------------
ms <- ms %>% mutate(datetime = as.POSIXct(datetime),
gupi = as.factor(gupi),
GOSE6mo = as.factor(GOSE6mo))
Badpt <- ms %>% filter(gupi == "5efh895", datetime > "1970-01-05")
ms <- anti_join(ms, Badpt)
# write.csv(ms, "C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/ms_200625.csv", row.names = FALSE)
Baseline.old <- Baseline.old %>% filter(gupi %in% unique(ms$gupi))
# write.csv(Baseline, "C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/Baseline_200625.csv", row.names = FALSE) # red zone time added in ICP_burden_200612.R
# write.csv(ms.new, "C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/ms.new_200625.csv", row.names = FALSE)
Baseline.new <- Baseline.new %>% filter(gupi %in% unique(ms.new$gupi))
# write.csv(Baseline.new, "C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/Baseline.new_200625.csv", row.names = FALSE) # red zone time added in ICP_burden_200612.R
# ------------------------------
HVICP <- read.csv(paste0(f,"ICPtemp.csv"), stringsAsFactors = FALSE, na.strings =nas) #https://center-tbi.incf.org/_5edf2b3f42f2945a46d94b6d
colnames(HVICP) <- c("gupi", "ICP", "datetime")
HVICP$datetime <- as.POSIXct(HVICP$datetime)
temp <- full_join(ms, HVICP, by=c("gupi", "datetime"))
temp$gupi <- as.factor(temp$gupi)
dev.new()
print(ggplot(temp %>% filter(gupi %in% c("9iyn249", "9wCW796","7ssY436", "9wBP293", "9Ydj395")), aes(datetime, icp, color=GOSE6mo.derived)) + geom_line() + geom_line(aes(datetime, ICP)) + facet_wrap(~gupi, scales="free_x"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment