hospital.sim <- function(NUMSIM) {
sim.num.patients <- rep(0, NUMSIM)
sim.n.waited <- rep(0, NUMSIM)
sim.average.wait.time <- rep(0, NUMSIM)
sim.office.close <- rep(0, NUMSIM)
lambda = 1/10
minutes.in.workday = 7 * 60 # 7 hours times 60min/hr
for(k in 1:NUMSIM) {
inter.times = rexp(n=1000, rate=lambda)
arrival.times = cumsum(inter.times) #minutes after 4pm
arrival.times = arrival.times[arrival.times <= minutes.in.workday]
#i.) Number of patients came to office
number.patients = length(arrival.times)
n.waited <- 0 #number of patients that needed to wait at some point
wait.patient <- rep(0, number.patients)
doctor.available.at <- c(0,0,0) #all doctors start with availability at start time of clinic
for(i in 1:number.patients) {
#select doctor index
doctor.j = which.min(doctor.available.at)
if(min(doctor.available.at) - arrival.times[i] > 0) {
wait.patient[i] = min(doctor.available.at) - arrival.times[i]
n.waited = n.waited + 1
} else {
wait.patient[i] = 0
}
appt.start <- max(min(doctor.available.at), arrival.times[i])
appt.end <- appt.start + runif(1, min=5, max=20)
#update doctors next available time. the one that worked on patient i
doctor.available.at[doctor.j] <- appt.end
} #end inner for loop
if(n.waited > 0) {
average.wait.time = sum(wait.patient)/n.waited
} else {
average.wait.time = 0
}
office.close <- appt.end
sim.num.patients[k] <- number.patients
sim.n.waited[k] <- n.waited
sim.average.wait.time[k] <- average.wait.time
sim.office.close[k] <- office.close - 420
}
return.list = list(num.patients = sim.num.patients, n.waited=sim.n.waited, average.wait = sim.average.wait.time, office.closed = sim.office.close)
return(return.list)
}
set.seed(100)
lambda <- 1/10
NUMSIM <- 100
answer = hospital.sim(NUMSIM)
Our 50% median results can be seen below in the following order:
Number of Patients
Number of Patients that Waited
Average Wait Time
Minutes after 4pm the clinic closed
quantile(answer$num.patients, 0.50)
## 50%
## 42
quantile(answer$n.waited, 0.50)
## 50%
## 6
quantile(answer$average.wait, 0.50)
## 50%
## 3.629005
quantile(answer$office.closed, 0.50)
## 50%
## 4.838912