knitr::opts_chunk$set( collapse = TRUE, comment = "%>" )
install.packages("devtools") devtools::install_github("r-lib/devtools")
url <- "http://owos.gm.fh-koeln.de:8055/bartz/spot.git" devtools::install_git(url = url)
url <- "http://owos.gm.fh-koeln.de:8055/bartz/babsim.hospital" devtools::install_git(url = url, subdir = "babsim.hospital")
rm(list = ls()) suppressPackageStartupMessages({ library("SPOT") library("babsim.hospital") library("simmer") library("simmer.plot") library("plotly") })
SPOT must be larger than 2.2.4:packageVersion("SPOT")
simData: simulation data, i.e., input data for the simulation. Here,
we will use data from UK.fieldData: real data, i.e., data from the DIVI-Intensivregister. The
field data will be used for validating the simulation output.
Statistically speaking, the babsim.hospital simulator models
resources usage in hospitals, e.g., number of ICU beds ($y$), as a function
of the number of infected individuals ($x$).
In addition to the number of infections, information about age and gender can be used as simulation input.
You will take a closer look at these data in the following sections. Therefore, we have provided som real data from the UK.
library(readxl) X20201111UKdata <- read_excel("../../inst/extdata/CovidDataGroup1.xlsx") ukdataRawFull1 <- X20201111UKdata
str(ukdataRawFull1)
Infected: New cases = new cases either admitted to hospital or diagnosed in the hospitalbed: Total COVID inpatient = total in hospital (includes ICU)ìntensiveBed: COVID-19 Non-invasive = patients on non-invasive ventilators (CPAP). In theory these should be on ICU but we don’t have space for them all. The model should probably consider them to be ICU but not intubated (level 2)intensiveBedVentilated: COVID-19 Ventilated = patients ventilated and intubated on ICUNewCasesUK: New cases in UK = new cases diagnosed across the city. In March/April the government testing was very poor so there were a lot of undiagnosed cases in the UK. Currently we think we’re diagnosing 30-50% of cases.ukdataRaw <- ukdataRawFull1[as.Date(ukdataRawFull1$Date) > "2020-09-01",] str(ukdataRaw)
simData <- data.frame(Day = as.Date(ukdataRaw$Date), Infected = ukdataRaw$NewCases)
plot(simData$Infected ~ simData$Day, type="b")
Note: non ICU patients can be calculated as
TotalCOVID19Inpatient - COVID19NonInvasiveCPAP - COVID19VentilatedICU
We convert the data into a data.frame with
Day <- as.Date(ukdataRaw$Date) bed <- ukdataRaw$TotalCOVID19Inpatient - ukdataRaw$COVID19NonInvasiveCPAP - ukdataRaw$COVID19VentilatedICU intensiveBed <- ukdataRaw$COVID19NonInvasiveCPAP intensiveBedVentilation <- ukdataRaw$COVID19VentilatedICU fieldData <- data.frame(Day = Day, bed = bed, intensiveBed = intensiveBed, intensiveBedVentilation = intensiveBedVentilation) str(fieldData)
max(fieldData$intensiveBed)
bed: non ICU patients in hospitalintensiveBed: ICU bed without ventilationintensiveBedVentilation: ICU bed with ventilationseed = 123 simrepeats = 2 parallel = FALSE percCores = 0.8 resourceNames = c("bed", "intensiveBed", "intensiveBedVentilation") resourceEval = c("bed", "intensiveBed", "intensiveBedVentilation")
ukdataRaw for the simulation as
follows:FieldStartDate = as.Date(min(fieldData$Day)) rownames(fieldData) <- NULL icu = FALSE icuWeights = c(1,2,10)
SimStartDate = FieldStartDate
data:data <- list(simData = simData, fieldData = fieldData)
visualizeUK <- function(data, region = 05315){ dataCov <- data$fieldData par(mfrow=c(3,1)) plot(dataCov$Day, dataCov$bed, type = "b", xlab = "Day", ylab="Patients", main = paste0("bed: non ICU patients in hospital") ) dataCov$weekly <- slide_dbl(dataCov$bed, ~mean(.x), .before = (7 -1)) lines(dataCov$Day, dataCov$weekly, col = "red") ## plot(dataCov$Day, dataCov$intensiveBed, type = "b", xlab = "Tag", ylab="Patients", main = paste0("intensiveBed: ICU bed without ventilation") ) dataCov$weekly <- slide_dbl(dataCov$intensiveBed, ~mean(.x), .before = (7 -1)) lines(dataCov$Day, dataCov$weekly, col = "red") ## plot(dataCov$Day, dataCov$intensiveBedVentilation, type = "b", xlab = "Tag", ylab="Patients", main = paste0("intensiveBedVentilation: ICU bed with ventilation") ) dataCov$weekly <- slide_dbl(dataCov$intensiveBedVentilation, ~mean(.x), .before = (7 -1)) lines(dataCov$Day, dataCov$weekly, col = "red") }
visualizeUK(data=data)
UKdata <- data ### usethis::use_data(UKdata, overwrite = TRUE)
conf list, i.e.,
conf refers to the simulation configuration, e.g.,
sequential or parallel evaluation,
number of cores,
resource names,
log level, etc.conf <- babsimToolsConf() conf$ResourceEval <- conf$ResourceNames conf <- getConfFromData(conf = conf, simData = data$simData, fieldData = data$fieldData) conf$parallel = parallel conf$simRepeats = simrepeats conf$ICU = FALSE conf$ResourceNames = resourceNames conf$ResourceEval = resourceEval conf$percCores = percCores conf$logLevel = 0 conf$w2 = icuWeights set.seed(conf$seed)
babsim.hospital simulations is based on the simmer package. para.para <- babsimHospitalPara() str(para)
babsim.hospital simulator requires the specification ofarrivalTimesconfparameter list para
for the simulation.
In addition to the arrivalTimes, a risk can be specified, i.e.,
a data.frame with the following entries can be passed to the main simulation
function babsimHospital:
time: arrival time Risk: risk (based on age and gender)Risk values is optional.envs.arrivalTimes <- data.frame(time = getArrivalTimes(data$simData$Infected))
envs <- babsimHospital(arrivalTimes = arrivalTimes, conf = conf, para = para)
First, we illustrate how to generate plots using the simmer.plot package.
In the following graph, the individual lines are all separate replications. The smoothing performed is a cumulative average.
Besides intensiveBed and intensiveBedVentilation, babsim.hospital
also provides information about the number of non-ICU beds. The non-ICU beds are
labeled as bed.
Summarizing, babsim.hospital generates output for three bed categories:
bedintensiveBedintensiveBedVentilation
To plot resource usage for three resources side-by-side, we can proceed as follows:
resources <- get_mon_resources(envs) resources$capacity <- resources$capacity/1e5 plot(resources, metric = "usage", c("bed", "intensiveBed", "intensiveBedVentilation"), items = "server",steps= TRUE)
Each resource can be plotted separately.
The following command generates a plot of non icu beds:
plot(resources, metric = "usage", "bed", items = "server", steps = TRUE)
plot(resources, metric = "usage", "intensiveBed", items = "server", steps = TRUE)
plot(resources, metric = "usage", "intensiveBedVentilation", items = "server", steps = TRUE)
babsim.hospital provides functions for evaluating the quality of the
simulation results.babsim.hospital provides a default parameter set, that is based on
knowledge from domain experts (doctors, members of COVID-19 crises teams,
mathematicians, and many more).fieldEvents <- getRealBeds(data = data$fieldData, resource = conf$ResourceNames) res <- getDailyMaxResults(envs = envs, fieldEvents = fieldEvents, conf=conf) resDefault <- getError(res, conf=conf)
The error is r resDefault.
babsim plots can be generated.p <- plotDailyMaxResults(res, showBeds = TRUE) plot(p)
ggplot and plotlycan be used to generate interactive plots.ggplotly(p)
babsim.hospital provides a default parameter set, which
can be used for simulations.babsimHospitalPara() provides a convenient way to access the
default parameter set:para <- babsimHospitalPara() print(para)
babsim provides an interface to optimize the parameter values of
the simulation model. conf$simulationDates$StartDate conf$simulationDates$EndDate conf$fieldDates$StartDate conf$fieldDates$EndDate
# modified 17.2.2021: factorUK added # modified 22.2.2021: factorIcuUK added library("babsim.hospital") library("SPOT") library("simmer") studyDate <- as.Date( min(conf$simulationDates$EndDate, conf$fieldDates$EndDate )) resUK <- runoptUK( expName = paste0("UK-", format(Sys.time(), "%Y-%b.%d-%H.%M-V"), utils::packageVersion("babsim.hospital")), simData = data$simData, fieldData = data$fieldData, TrainSimStartDate = studyDate - 10*7, # "2020-09-02", TrainFieldStartDate = studyDate - 8*7, # "2020-09-15", TestSimStartDate = studyDate - 6*7 , #"2020-09-30", TestFieldStartDate = studyDate - 4*7, #"2020-10-23", Overlap = 0, seed = 101170, repeats = 2, funEvals = 50, size = 30, simrepeats = 2, parallel = TRUE, percCores = 0.9, icu = FALSE, icuWeights = c(1,2,10), verbosity = 0, resourceNames = c("bed", "intensiveBed", "intensiveBedVentilation"), resourceEval = c("bed", "intensiveBed", "intensiveBedVentilation"), factorUK = 0.1, factorIcuUK = 1.0 )
runoptUK() returns a list with two elements:result.df, which is a data.framereslist
result.df contains the best (optimized) results from the SPOT runs.
ukpara as follows:ukpara01 <- resUK[[1]] usethis::use_data(ukpara01, overwrite = TRUE)
# modified 17.2.2021: factorUK added # modified 22.2.2021: factorIcuUK added library("babsim.hospital") library("SPOT") library("simmer") studyDate <- as.Date( min(conf$simulationDates$EndDate, conf$fieldDates$EndDate )) resUK <- runoptUK( expName = paste0("UK-", format(Sys.time(), "%Y-%b.%d-%H.%M-V"), utils::packageVersion("babsim.hospital")), simData = data$simData, fieldData = data$fieldData, TrainSimStartDate = studyDate - 10*7, # "2020-09-02", TrainFieldStartDate = studyDate - 8*7, # "2020-09-15", TestSimStartDate = studyDate - 6*7 , #"2020-09-30", TestFieldStartDate = studyDate - 4*7, #"2020-10-23", Overlap = 0, seed = 101170, repeats = 2, funEvals = 50, size = 30, simrepeats = 2, parallel = TRUE, percCores = 0.9, icu = FALSE, icuWeights = c(1,2,10), verbosity = 0, resourceNames = c("bed", "intensiveBed", "intensiveBedVentilation"), resourceEval = c("bed", "intensiveBed", "intensiveBedVentilation"), factorUK = 0.1, factorIcuUK = 2 )
ukpara02 <- resUK[[1]] usethis::use_data(ukpara02, overwrite = TRUE)
getBestParameter picks out the best and maps it to the variables that are
used by the BaBSim.Hospital simulator.ukpara <- ukpara01 print(ukpara)
ukpara02 :para <- getBestParameter(resUK[[1]])
ukpara and can be converted into a babsimhospitalparameter set
as follows:para01 <- getBestParameter(ukpara01) str(para01)
conf$simRepeats = 10 res01 <- modelResultHospital(para=para01, conf=conf, data = data) resOpt01 <- getError(res01, conf=conf)
r resDefault to r resOpt01.p01 <- plotDailyMaxResults(res01, showBeds = TRUE) print(p01)
ggplot and plotlycan be used to generate interactive plots.ggplotly(p01)
getBestParameter picks out the best and maps it to the variables that are
used by the BaBSim.Hospital simulator.ukpara <- ukpara02 print(ukpara)
ukpara02 :para <- getBestParameter(resUK[[1]])
ukpara and can be converted into a babsimhospitalparameter set
as follows:para02 <- getBestParameter(ukpara02) str(para02)
conf$simRepeats = 100 res02 <- modelResultHospital(para=para02, conf=conf, data = data) resOpt02 <- getError(res02, conf=conf)
r resDefault to r resOpt02.p02 <- plotDailyMaxResults(res02, showBeds = TRUE) print(p02)
ggplot and plotlycan be used to generate interactive plots.ggplotly(p02)
babsim.hospital includes tools to analyse parameter settings.infec: infectedout: transfer out, no hospital requiredhosp: hospitalnormal: normal station, no ICUintens: ICU (without ventilation)vent: ICU ventilatedintafter: intensive aftercare (from ICU with ventilation, on ICU)aftercare: aftercare (from ICU, on normal station)death: patient dieshealthy: recoveredvisualizeGraph(para=para, option = "P")
getMatrixP(para = para )
visualizeGraph(para = para, option = "D")
getMatrixD(para = para)
babsim.hospital::babsimHospitalPara that is
used by the babsimHospital simulator.help function, e.g. `help(babsim.hospital::babsimHospitalPara).babsim.hospital::babsimHospital function:help function, e.g. `help(babsim.hospital::babsimHospital).set.seed?visualizeGraph() command.babsim.hospital::babsimHospital function:## install.packages("devtools") ## devtools::install_github("r-lib/devtools") ## url <- "http://owos.gm.fh-koeln.de:8055/bartz/spot.git" ## devtools::install_git(url = url) ## url <- "http://owos.gm.fh-koeln.de:8055/bartz/babsim.hospital" ## devtools::install_git(url = url, subdir = "babsim.hospital") rm(list = ls()) suppressPackageStartupMessages({ library("SPOT") library("babsim.hospital") library("simmer") library("simmer.plot") library("readxl") }) packageVersion("SPOT")
### X20201111UKdata <- read_excel("/Users/bartz/workspace/Lehre.d/IDEA-Master-AIT-WS2020-2021/Numerical-Methods/Datasets/CovidDataGroup1.xlsx") X20201111UKdata <- read_excel("~bartz/workspace/Lehre.d/IDEA-Master-AIT-WS2020-2021/Numerical-Methods/Datasets/CovidDataGroup1.xlsx") ukdataRaw <- X20201111UKdata[as.Date(X20201111UKdata$Date) > "2020-09-01",]
simData <- data.frame(Day = as.Date(ukdataRaw$Date), Infected = ukdataRaw$NewCases)
Day <- as.Date(ukdataRaw$Date) bed <- ukdataRaw$TotalCOVID19Inpatient - ukdataRaw$COVID19NonInvasiveCPAP - ukdataRaw$COVID19VentilatedICU intensiveBed <- ukdataRaw$COVID19NonInvasiveCPAP intensiveBedVentilation <- ukdataRaw$COVID19VentilatedICU fieldData <- data.frame(Day = Day, bed = bed, intensiveBed = intensiveBed, intensiveBedVentilation = intensiveBedVentilation) rownames(fieldData) <- NULL
data <- list(simData = simData, fieldData = fieldData)
conf <- babsimToolsConf() conf$ResourceEval <- conf$ResourceNames conf <- getConfFromData(conf = conf, simData = data$simData, fieldData = data$fieldData) conf$parallel = TRUE conf$simRepeats = 2 conf$ICU = FALSE conf$ResourceNames = c("bed", "intensiveBed", "intensiveBedVentilation") conf$ResourceEval = c("bed", "intensiveBed", "intensiveBedVentilation") conf$percCores = 0.8 conf$logLevel = 0 conf$w2 = c(1,2,10) conf$seed = 123 set.seed(conf$seed)
para <- babsimHospitalPara()
arrivalTimes <- data.frame(time = getArrivalTimes(data$simData$Infected))
envs <- babsimHospital(arrivalTimes = arrivalTimes, conf = conf, para = para)
resources <- get_mon_resources(envs) resources$capacity <- resources$capacity /1e5
plot(resources, metric = "usage", c("bed", "intensiveBed", "intensiveBedVentilation"), items = "server",steps= TRUE)
# plot(resources, metric = "usage", "bed", items = "server", steps = TRUE) # plot(resources, metric = "usage", "intensiveBed", items = "server", steps = TRUE) # plot(resources, metric = "usage", "intensiveBedVentilation", items = "server", steps = TRUE)
fieldEvents <- getRealBeds(data = data$fieldData, resource = conf$ResourceNames)
res <- getDailyMaxResults(envs = envs, fieldEvents = fieldEvents, conf=conf)
errDefault <- getError(res, conf=conf) print(errDefault)
p <- plotDailyMaxResults(res, showBeds = TRUE) plot(p) ggplotly(p)
library("babsim.hospital") library("SPOT") library("simmer") studyDate <- as.Date( min(conf$simulationDates$EndDate, conf$fieldDates$EndDate )) resUK <- runoptUK( expName = paste0("UK-", format(Sys.time(), "%Y-%b.%d-%H.%M-V"), utils::packageVersion("babsim.hospital")), simData = data$simData, fieldData = data$fieldData, TrainSimStartDate = studyDate - 10*7, # "2020-09-02", TrainFieldStartDate = studyDate - 8*7, # "2020-09-15", TestSimStartDate = studyDate - 6*7 , #"2020-09-30", TestFieldStartDate = studyDate - 4*7, #"2020-10-23", Overlap = 0, seed = 101170, repeats = 2, funEvals = 50, size = 35, simrepeats = 2, parallel = TRUE, percCores = 0.9, icu = FALSE, icuWeights = c(1,2,10), verbosity = 0, resourceNames = c("bed", "intensiveBed", "intensiveBedVentilation"), resourceEval = c("bed", "intensiveBed", "intensiveBedVentilation") )
ukpara <- resUK[[1]] print(ukpara)
paraOpt <- getBestParameter(resUK[[1]]) str(paraOpt)
# Best para as provided by SPOT. # Can be used if optimization is not possible: # paraOpt <- getBestParameter(ukpara) # print(paraOpt)
conf$simRepeats = 10 resOpt <- modelResultHospital(para=paraOpt, conf=conf, data = data)
errOpt <- getError(resOpt, conf=conf) print(errOpt)
p <- plotDailyMaxResults(resOpt, showBeds = TRUE) print(p) # ggplotly(p)
getMatrixP(para = para )
visualizeGraph(para=paraOpt, option = "P")
getMatrixD(para = paraOpt)
visualizeGraph(para = paraOpt, option = "D")
SPOT.SPOT for automated and interactive tuning were illustrated and the underling concepts of the SPOT approach were explained. SPOT approach are techniques such as exploratory fitness landscape analysis and response surface methodology.runoptUK function:bounds <- getBounds() a <- bounds$lower b <- bounds$upper length(a)
getParameterNameList(1:29)
x20: "FactorPatientsIntensiveToVentilation"
The function getParameterDataFrame() show the values and gives a nice summary:
getParameterDataFrame()
factorUK <- 0.1 bounds <- getBounds() a <- bounds$lower b <- bounds$upper a[16] <- a[16] * factorUK a[18] <- a[18] * factorUK a[20] <- a[20] * factorUK b[16] <- b[16] * factorUK b[18] <- b[18] * factorUK b[20] <- b[20] * factorUK
factorUK <- 0.1 factorIcuUK <- 2 bounds <- getBounds() a <- bounds$lower b <- bounds$upper a[16] <- a[16] * factorUK a[18] <- a[18] * factorUK a[20] <- a[20] * factorUK b[16] <- b[16] * factorUK b[18] <- b[18] * factorUK b[20] <- b[20] * factorUK a[6] <- a[6] * factorIcuUK a[7] <- a[7] * factorIcuUK a[8] <- a[8] * factorIcuUK b[6] <- b[6] * factorIcuUK b[7] <- b[7] * factorIcuUK b[8] <- b[8] * factorIcuUK a
para.df <- getParameterDataFrame() para.df[,] para.table <- xtable::xtable(para.df)
para.df <- getParameterDataFrame() para.df[,"obk"] <- a para.df[,"koeln"] <- b colnames(para.df) <- c("default", "minUK", "maxUK", "minDE", "MaxDE") para.table <- xtable::xtable(para.df) para.table
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.