Tasks
arrivals<-read.csv("C:/Users/Home/Downloads/Week 5-Homework 3 attached files Oct 7, 2026 1021 AM/arrivals.csv")
treatment<-read.csv("C:/Users/Home/Downloads/Week 5-Homework 3 attached files Oct 7, 2026 1021 AM/treatment_times.csv")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Part A: Patient Arrivals
Compute descriptive statistics.
mean(arrivals$Patients)
## [1] 24.1
median(arrivals$Patients)
## [1] 23.5
sd(arrivals$Patients)
## [1] 5.609203
var(arrivals$Patients)
## [1] 31.46316
min(arrivals$Patients)
## [1] 16
max(arrivals$Patients)
## [1] 35
quantile(arrivals$Patients)
## 0% 25% 50% 75% 100%
## 16.00 19.75 23.50 28.25 35.00
The hourly patient arrivals vary from 16 to 35 patients, with an average of approximately 24 patients per hour.
Determine whether a Poisson distribution is appropriate.
# Compare the sample mean and variance
arrival_mean <- mean(arrivals$Patients)
arrival_variance <- var(arrivals$Patients)
arrival_mean
## [1] 24.1
arrival_variance
## [1] 31.46316
# Dispersion ratio
dispersion_ratio <- arrival_variance / arrival_mean
dispersion_ratio
## [1] 1.305525
A Poisson distribution is a reasonable first approximation for the patient-arrival data because the variable represents counts of patients occurring during fixed one-hour intervals. For a Poisson distribution, the theoretical mean and variance should be approximately equal. In this case, the mean is 24.10 and the variance is 31.46, giving a variance-to-mean ratio of 1.31. The variance is somewhat higher than the mean, indicating some overdispersion. However, the difference is not extreme, and there are only 20 observations. Therefore, a Poisson distribution is reasonable for this assignment, although a larger dataset would be needed to determine whether the Poisson assumption is appropriate for actual hospital staffing decisions.
Estimate the arrival rate parameter.
# Estimate lambda
lambda <- mean(arrivals$Patients)
lambda
## [1] 24.1
For a Poisson distribution, the estimated arrival rate parameter, lambda, is equal to the sample mean. In this case, lambda is 24.10 patients per hour, meaning that the hospital can expect approximately 24 patients to arrive per hour on average.
Calculate the probability of more than 30 arrivals in an hour.
# P(X > 30) = 1 - P(X <= 30)
prob_more_than_30 <- 1 - ppois(30, lambda)
prob_more_than_30
## [1] 0.09952132The probability of having more than 30 patient arrivals in one hour is 0.0995, or approximately 9.95%. Therefore, based on the fitted Poisson model, there is approximately a 10% chance that the emergency department will receive more than 30 patients in a given hour. This is important for staffing because high-volume periods can create additional pressure on emergency department staff.
Part B: Treatment Times
Visualize the distribution.
# Histogram
hist(treatment$TreatmentMinutes,
main = "Distribution of Treatment Times",
xlab = "Treatment Time (minutes)",
ylab = "Frequency",
col = "lightblue",
border = "black")
# Boxplot
boxplot(treatment$TreatmentMinutes,
main = "Treatment Time Boxplot",
ylab = "Treatment Time (minutes)",
col = "lightgreen")
The histogram shows that treatment times are distributed around the middle of the data, with most treatments taking approximately 20 to 50 minutes. The boxplot does not show any obvious extreme outliers. The distribution appears reasonably balanced, although the sample is relatively small. Overall, the treatment-time data appear suitable for considering a Normal distribution as an approximation.
Estimate mean and standard deviation.
# Mean and standard deviation
treatment_mean <- mean(treatment$TreatmentMinutes)
treatment_sd <- sd(treatment$TreatmentMinutes)
treatment_mean
## [1] 36.06667
treatment_sd
## [1] 11.02897
The estimated mean treatment time is 36.07 minutes, and the standard deviation is 11.03 minutes. Therefore, the average patient spends approximately 36 minutes receiving treatment, with treatment times typically varying by about 11 minutes around the mean.
Determine whether a Normal distribution is reasonable.
# Shapiro-Wilk normality test
shapiro.test(treatment$TreatmentMinutes)
##
## Shapiro-Wilk normality test
##
## data: treatment$TreatmentMinutes
## W = 0.97087, p-value = 0.8707
The Shapiro-Wilk test produces a p-value of approximately 0.8707. Because the p-value is greater than 0.05, we do not reject the null hypothesis of normality. The histogram and boxplot also do not show any obvious violations of normality. Therefore, a Normal distribution is a reasonable approximation for the treatment-time data. However, there are only 15 patients in the sample, so this conclusion should be interpreted cautiously. A larger sample of treatment times would provide stronger evidence.
Calculate the probability that treatment exceeds 45 minutes.
# Calculate P(T > 45)
prob_treatment_over_45 <- 1 - pnorm(
45,
mean = treatment_mean,
sd = treatment_sd
)
prob_treatment_over_45
## [1] 0.2089735The probability that a patient’s treatment will exceed 45 minutes is 0.2090, or approximately 20.90%. Therefore, under the fitted Normal model, approximately 21% of patients are expected to require more than 45 minutes of treatment. This suggests that the hospital should account for longer treatment times when determining staffing levels.
Part C: Simulation
Simulate one month of arrivals.
# Set seed so the simulation can be reproduced
set.seed(624)
# Define the number of days and hours
days <- 30
hours_per_day <- 24
# Simulate hourly arrivals
simulated_arrivals <- rpois(
days * hours_per_day,
lambda = lambda
)
# Put simulated arrivals into a 30 x 24 matrix
monthly_arrivals <- matrix(
simulated_arrivals,
nrow = days,
ncol = hours_per_day
)
# Calculate total arrivals for each day
daily_demand <- rowSums(monthly_arrivals)
# Display daily demand
daily_demand
## [1] 579 548 575 601 583 560 614 532 587 537 562 586 543 565 570 571 614 578 606
## [20] 588 569 590 607 607 613 595 560 559 558 599
A 30-day month was simulated using a Poisson distribution with an arrival rate of 24.10 patients per hour. The simulation represents 30 days multiplied by 24 hours, resulting in 720 simulated hours. Because the simulation is random, the exact daily totals may vary slightly if a different random seed is used.
Estimate expected daily demand.
# Theoretical expected daily demand
expected_daily_demand <- lambda * 24
expected_daily_demand
## [1] 578.4
# Simulated average daily demand
mean(daily_demand)
## [1] 578.5333
# Standard deviation of simulated daily demand
sd(daily_demand)
## [1] 23.42079
# Minimum and maximum simulated daily demand
min(daily_demand)
## [1] 532
max(daily_demand)
## [1] 614
# Plot simulated daily demand
plot(
1:30,
daily_demand,
type = "o",
main = "Simulated Emergency Department Demand",
xlab = "Day",
ylab = "Total Patient Arrivals"
)
abline(
h = mean(daily_demand),
lty = 2
)
The theoretical expected daily demand is 24.10 multiplied by 24, which equals 578.40 patients per day. The simulation produced an average daily demand of approximately 573 patients per day. The small difference between the theoretical and simulated averages is expected because the simulation is random. Therefore, the hospital should expect approximately 578 patient arrivals per day based on the estimated arrival rate.
Recommend staffing improvements.
# Summary information useful for staffing
cat("Average hourly arrivals:", round(lambda, 2), "\n")
## Average hourly arrivals: 24.1
cat("Probability of more than 30 arrivals:",
round(prob_more_than_30 * 100, 2), "%\n")
## Probability of more than 30 arrivals: 9.95 %
cat("Average daily demand:",
round(expected_daily_demand, 2), "\n")
## Average daily demand: 578.4
cat("Probability treatment exceeds 45 minutes:",
round(prob_treatment_over_45 * 100, 2), "%\n")
## Probability treatment exceeds 45 minutes: 20.9 %
The hospital should maintain baseline staffing for approximately 24 patient arrivals per hour, which represents the estimated average demand. However, staffing should not be based entirely on the average because patient arrivals can vary considerably between hours. There is approximately a 9.95% probability of more than 30 arrivals in an hour, so the hospital should have additional staffing capacity available during unusually busy periods.
Flexible staffing could help the hospital respond to changes in demand. Staff schedules could be adjusted so that more employees are available during historically busy periods rather than maintaining the same staffing level throughout the day. The hospital should also maintain surge capacity by having procedures for bringing in additional staff or reallocating existing staff when patient arrivals become unusually high.
Treatment times should also be considered when determining staffing needs. The average treatment time is approximately 36 minutes, but about 20.90% of treatments are expected to exceed 45 minutes under the Normal model. Longer treatment times can increase the amount of time staff must spend with individual patients and may affect the department’s overall capacity.
Finally, the hospital should collect more data before making major staffing decisions. Only 20 hours of arrival data and 15 treatment times were provided for this analysis. Collecting several months of hourly arrival and treatment data would provide more reliable estimates and help identify specific times of day when staffing needs are highest. Overall, staffing should account for both average demand and the possibility of unusually high patient arrivals and longer treatment times.
Overall:
cat("Overall Conclusion\n")
## Overall Conclusion
cat("-------------------\n")
## -------------------
cat("The emergency department averages approximately",
round(lambda, 2),
"patient arrivals per hour.\n")
## The emergency department averages approximately 24.1 patient arrivals per hour.
cat("The fitted Poisson model estimates a",
round(prob_more_than_30 * 100, 2),
"% probability of more than 30 arrivals in an hour.\n")
## The fitted Poisson model estimates a 9.95 % probability of more than 30 arrivals in an hour.
cat("Average treatment time is approximately",
round(treatment_mean, 2),
"minutes, with a standard deviation of",
round(treatment_sd, 2),
"minutes.\n")
## Average treatment time is approximately 36.07 minutes, with a standard deviation of 11.03 minutes.
cat("The estimated probability of treatment exceeding 45 minutes is",
round(prob_treatment_over_45 * 100, 2),
"%.\n")
## The estimated probability of treatment exceeding 45 minutes is 20.9 %.
cat("Expected daily demand is approximately",
round(expected_daily_demand, 2),
"patients.\n")
## Expected daily demand is approximately 578.4 patients.
Overall, the emergency department experiences an average of approximately 24.10 patient arrivals per hour, resulting in an expected daily demand of about 578 patients. The Poisson model provides a reasonable first approximation for patient arrivals, although the variance is somewhat higher than the mean. There is approximately a 9.95% chance of receiving more than 30 patients in a given hour.
Treatment times average approximately 36.07 minutes, with a standard deviation of 11.03 minutes. The Normal distribution provides a reasonable approximation for treatment times, with approximately 20.90% of patients expected to require more than 45 minutes of treatment.
Based on these findings, the hospital should use flexible staffing and maintain additional surge capacity rather than staffing only for average demand. Collecting more historical data would improve the reliability of these estimates and allow staffing levels to be adjusted more accurately based on hourly patient demand.