statistics, visualization and more using r - r-package...
TRANSCRIPT
statistics, visualization and more using r
R-package simmer
Nina Biller, Fabian Köhnke, Sebastian Mayr20. Mai 2019
Universität Salzburg
Table of contents
Introduction
Basic example (hospital)
Extension of the basic example
Extension: simmer.plot
1
introduction
simulation problems
Example Problems
∙ queue of customers arriving at a bank
∙ interruption in a machine shop process
∙ consultation in a hospital
⇒ simmer: for discrete-event simulation (DES) in R
3
simulation problems
Example Problems
∙ queue of customers arriving at a bank
∙ interruption in a machine shop process
∙ consultation in a hospital
⇒ simmer: for discrete-event simulation (DES) in R
3
The discrete-event simulation (DES)
∙ modeling, simulating, and analyzing systems
∙ models a system as a discrete sequence of events
∙ stochastic: some variables are random
∙ advantage: involves coincidence and probability
4
Terminology
∙ Entitiy: is an object of interest in the system (e.g. patient)
∙ Attribute: is a property of an entity
∙ Resource: e.g. doctor for a patient
∙ Queue: list that an entitiy enters if a resource is occupied
∙ Event: any change in the state of the system
5
Pipe Operator: %>%
∙ it takes the output of one statement and forwards it as an input ofthe next statement
∙ you can think of it as a ’ THEN ’
∙ function(argument)⇒ argument % > % function()
∙ advantage for complexe code (more readable)
∙ shortcut for mac: shift, command, m
∙ shortcut for windows: string, command, m
6
Pipe Operator example
without the pipe operator:
round ( exp ( d i f f ( log ( x ) ) ) , 1 )
with the pipe operator:
x %>% log ( ) %>%
d i f f ( ) %>%
exp ( ) %>%
round ( 1 )
7
basic example (hospital)
example- important functions
∙ simmer(): this method initialises a simulation environment∙ trajectory(): comprises a chain of activities that can be attached toa generator
∙ add_resource(): defines a new resource in a simulationenvironment
∙ add_generator(): attaches a new source of arrivals to a trajectoryfrom a generator function.
∙ run(): executes steps until a given criterion∙ now(): gets the current simulation time∙ log_(): activities for displaying messages preceded by thesimulation time and the name of the arrival.
∙ leave(): activity for leaving the trajectory with some probability9
log() and leave()
log(.trj, message)leave(.trj, prob)
. . .pa t ient <− t r a j e c t o r y ( ” pat ients path ” ) %>%## add an intake a c t i v i t y ( ” nurse ” )log_ ( ” I am here ” ) %>%leave ( 1 / 5 ) %>%se i ze ( ” nurse ” , 1 ) %>%timeout ( funct ion ( ) 1 5 ) %>%release ( ” nurse ” , 1 ) %>%. . .
10
extension of the basic example
Extension of the basic example
∙ Extend the trajectory∙ Use random number generators
∙ Define attributes (like health status)
∙ Include Sub- trajectories
∙ Extend the simulation environment∙ Generate patients with higher priority
∙ Run the simulation multiple times
∙ Monitor the simulations
12
Trajectory manipulation
timeout(.trj, task)
dynamical approach:
%>% timeout ( funct ion ( ) rnorm ( 1 , 1 5 ) )
static approach: initialized at the start of the simulation and treatedas a constant
%>% timeout ( rnorm ( 1 , 1 5 ) )
add_generator(.env, name_prefix, trajectory, distribution, ...)
%>% add_generator ( ” pat ient ” , pat ient , funct ion ( )rnorm ( 1 , 10 , 2 ) , mon = 2 )
13
Resource usage
14
Trajectory manipulation - Attributes
Assigning properties to system entities:
Se t _a t t r i bu te ( . t r j , keys , values , . . . )
keys - vector or single variable with the attribute namevalues - integer or function that return an integer (or integer- vector)
pat ient <− t r a j e c t o r y ( ” pat ients ’ path ” ) %>%. . .s e t _a t t r i bu t e ( keys = ” health ” , values = funct ion ( )sample ( 1 : 1 0 0 , 1 ) ) %>%se t _a t t r i bu t e ( ” mark fo r surgery ” , funct ion ( )i f e l s e ( ge t _a t t r i bu te ( env , ” health ” ) <= 10 , 2 , 0 ) ) %>%
. . .15
Trajectory manipulation - branch
branch(.trj, option, continue, ...)
option - Integer or function that returns an integer (0: skip branch, >1 :follow subtrajectory)continue - Boolean (TRUE: system entity resumes main path afterfinishing the subtrajectory
. . .branch ( option = funct ion ( ) i f e l s e ( ge t _a t t r i bu te( env , ” mark fo r surgery ” ) > 1 , 1 , 0 ) , continue = FALSE ,t r a j e c t o r y ( ” surgery ” ) %>%log_ ( ” surgery s ta r ted ” ) %>%se i ze ( ” surgeon ” , 1 ) %>%timeout ( funct ion ( ) rnorm ( 1 , 1 2 0 , 1 0 ) ) %>%release ( ” surgeon ” , 1 )
) %>%. . .
16
Exercise - Trajectory manipulation
Use the provided “bank” example
1. Exercise1.1 The time at the counter should be drawn from the normal distribution
around a mean of 12.1.2 Add an attribute called “mood” and assign random values from 1 to 10
to it (hint use function() sample() )
2. Exercise2.1 Change the parameters in branch in that way, that the sub trajectory
“Talking to the manager” will never be used.2.2 run the simulation for 100 minutes
17
Extension of the basic example
∙ Extend the trajectory∙ Use random number generators
∙ Define attributes (like health status)
∙ Include Sub- trajectories
∙ Extend the simulation environment∙ Generate patients with higher priority
∙ Run the simulation multiple times
∙ Monitor the simulations
18
Setting priority
add_generator(... , distribution, mon = 1, priority = 0, ...)
add_resource(... , preemptive = TRUE , ...)
preemptive - Boolean (TRUE: system entity with higher priority willinterrupt running operations on the specific resource and use itimmediately)
19
Setting priority
. . .env %>%add_resource ( ” nurse ” , preemptive = TRUE , 1 ) %>%add_resource ( ” doctor ” , 2 ) %>%add_resource ( ” surgeon ” , 1 ) %>%add_resource ( ” admin is t ra t ion ” , 1 ) %>%add_generator ( ” pat ient ” , pat ient , funct ion ( )rnorm ( 1 , 10 , 2 ) , mon = 2 ) %>%## add pat ients with higher p r i o r i t y e . g . be t te rinsuranceadd_generator ( ” important_pat ient ” ,pat ient , p r i o r i t y = 1 , at ( c ( 4 0 , 6 7 , 8 9 , 1 3 0 ) ) )
. . .
20
Output
21
Extension of the basic example
∙ Extend the trajectory∙ Use random number generators
∙ Define attributes (like health status)
∙ Include Sub- trajectories
∙ Extend the simulation environment∙ Generate patients with higher priority
∙ Run the simulation multiple times
∙ Monitor the simulations
22
Multiple runs of the simulation
lapply(X, FUN, ...)
X - vectorFUN - function to be applied to each element of X
We have to put the whole simulation (trajectory, definition ofresources ...) into FUN
23
Multiple runs of the simulation
##START REPLICATION LOOP − Recp l i ca te the simulat ion 10times with lapply ( )envs <− lapply ( 1 : 1 0 , funct ion ( i ) {env <− simmer ( ” Hospi ta l ” ). . .
# def ine T ra j e c to r y. . .# def ine resources and generators. . .# run the simulat ion} ) #END rep l i c a t i on> s t r ( envs )L i s t of 10$ : C lasses ’ simmer ’ , ’ R6 ’ <simmer>. . . . 24
Extension of the basic example
∙ Extend the trajectory∙ Use random number generators
∙ Define attributes (like health status)
∙ Include Sub- trajectories
∙ Extend the simulation environment∙ Generate patients with higher priority
∙ Run the simulation multiple times
∙ Monitor the simulations
25
Monitoring:
get_mon_arrivals(.envs, per_resource = FALSE,...)
Returns a data frame with informations about the arrivals of systementitiesper_resource - adds the variable resource to the data frame that isreturned by the function
26
Monitoring:
get_mon_attributes(.envs)Returns a data frame with informations about the attributes ofsystem entities (e.g. ’health’)
27
Monitoring:
get_mon_resources(.envs)Returns a data frame with informations about the resourses of thesimulation
28
Summary
29
Summary
30
Examples
Use the provided example ’bank’
1. Exercise1.1 Add a generator for bank robbers. They should just appear 40 minutes
after starting the simulation1.2 Set the priority level of the bank robber generator to priority = 2. Which
consequences arise from this?
2. Exercise2.1 Replicate the simulation 50 times2.2 Use the monitoring- functions to find out how many people were at the
bank the whole day (180 minutes)
31
extension: simmer.plot
Extension: simmer.plot
∙ by Iñaki Ucar & Bart Smeets (2017)
∙ Extension provides a set of methods to plot simmer statistics andsimmer trajectories
∙ Helps to decide on recommendations for resources and clarifiesthe simulation’s trajectory
Statistics
∙ Resources: Utilization and Usage
∙ Arrivals: Activity, Flow and Waiting Time
∙ Attributes
Trajectory33
simmer.plot - statistics resources
Utilization = the average use of a resource, total time in use dividedby the total simulation time
resources <− get_mon_resources ( envs )p lo t ( resources , metr ic = ’ u t i l i z a t i o n ’ )
get_mon_resources = monitored data of resourcesmetric = set to ’utilization’
34
resource utilization
35
resource utilization
36
simmer.plot - statistics resources
Usage = how many units of the resource are is use in relation to time
resources <− get_mon_resources ( envs )p lo t ( resources , metr ic = ” usage ” , ”names ” ,items= c ( ” server ” , ” queue ” ) )
metric = set to ’usage’
names = can be set to certain resources e.g. ’nurse’ or c(’nurse’,’surgeon’)
items = can be set to ’server’, ’queue’, ’system’ (=server + queue),separately or combined
37
Resource usage
38
Resource usage
39
simmer.plot - statistics arrivals
a r r i v a l s <− get_mon_arr ivals ( envs )p lo t ( a r r i v a l s , metr ic = ” . . . time ” )
’metric’ has to be set to...
∙ activity_time = timeout time, e.g. time spent at nurse, doctor etc.∙ flow_time = total time spent in system∙ waiting_time = time between activity times, flow time - activitytime
40
Waiting time evolution
41
Waiting time evolution
42
simmer.plot - statistics attributes
If attributes are declared, it’s also possible to plot those.
a t t r i bu t e s <− get_mon_attr ibutes ( envs )p lo t ( a t t r i bu t e s )
43
Attribute evolution
44
Change aesthetics with ggplot2
∙ ggplot2 functions as backend to simmer.plot and is automaticallyinstalled with simmer.plot
∙ all simmer.plot statistic plots are ggplot2 objects
∙ ⇒ ggplot2 functions can also be applied to plots created withsimmer.plot
∙ funtions can simply be added with a ’+’ to your defaultsimmer.plot plot
∙ if you’re not familiar with ggplot2 and are interested in modifyingplots, take a look here: Link
45
Change aesthetics with ggplot2
Some useful functions for simmer plots:
∙ resources - utilization: change color and outline of bars:
. . . +geom_bar ( s t a t = ” i d en t i t y ” , f i l l = ” red ” ,co lor = ” blue ” )
∙ arrivals - all: change color of average line:
. . . +geom_smooth ( co lor =” red ” )
∙ attributes: add dots to indicate units
. . . +geom_point ( )
46
Change aesthetics with ggplot2
∙ all: change label of axis:
. . . +y lab ( ” time ” ) +xlab ( ” evolut ion ” )
∙ all: change title of your plot:
. . . +g g t i t l e ( ” t i t l e ” )
47
Change aesthetics with ggplot2
Example: Change color of average line in waiting time plot to green!
a r r i v a l s <− get_mon_arr ivals ( envs )p lo t ( a r r i v a l s , metr ic = ” wai t ing_t ime ” ) +geom_smooth ( co lor = ” green ” )
Attributes plot with no replication. Points added to mark eachpatient.
48
simmer.optim
∙ parameter optimization method∙ example: maximization of number of patients before 4h mark∙ objective: a variable we want to minimize or maximize∙ constraints: upper and lower boundaries on other variables∙ inputs: the simulation parameters that we want to manipulate
⇒ optimization method computes optimal amount of parameters
∙ not officially released, development dormant
⇒ not in the CRAN-repository, installation possible with devtools viagithub
49
simmer.plot - trajectories
∙ plot generic to visualize diagrams of trajectory objects usingDiagrammeR as backend package
get_pa le t te <− sca les : : brewer_pal ( type = ” type ” , pa le t te = 1 )p lo t ( t r a j e c to r y , f i l l = get_palet te , verbose =FALSE )
scales :: brewer_pal = get brewer_pal function from scales packagesbrewer_pal = color brewer palettetype = must be ’seq’, ’div’ or ’qual’palette = number for color palettetrajectory = in our case ’patient’verbose = can be set to TRUE if additional info is needed
50
Trajectory
51
Trajectory
52
Exercises
1. Exercises: install and load simmer.plot1.1 Take a look at the usage of the resource “counter” in the bank example
and display server and queue lines. What can you observe?1.2 Take a look at the waiting time and change the color of the average line
to red. What can you observe?
2. Change the number of counters to 2 and plot the same two graphsas above. What can you observe?
3. Create a trajectory plot for the bank example with a color paletteof your choice. Also display additional information to thetrajectory. Explain the trajectory in your own words.
53
References
∙ Parameter Optimization Functions for ’simmer’.https://github.com/r-simmer/simmer.optim (15.05.2019).
∙ Ucar, I. & Smeets, B. Simmer.https://r-simmer.org (15.05.2019).
∙ Ucar I., Smeets B. & Azcorra A. (2018). simmer: Discrete-EventSimulation for R. Journal of Statistical Software, *accepted forpublication*.https://arxiv.org/abs/1705.09746>.
54
Thanks for your attention!
55