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
set.seed(42)
service_data <- tibble(
WaitTime = c(
rexp(28, rate = 1 / 7) + 3,
25.2,
30.0
)
)
service_data %>%
summarise(
N = n(),
Minimum = min(WaitTime),
Maximum = max(WaitTime),
Range = max(WaitTime) - min(WaitTime),
Variance = var(WaitTime),
SD = sd(WaitTime),
IQR = IQR(WaitTime),
MAD = mad(WaitTime)
)
## # A tibble: 1 × 8
## N Minimum Maximum Range Variance SD IQR MAD
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 30 3.27 38.0 34.7 89.8 9.48 6.89 5.15
The IQR and MAD are more resistant to extreme values than the variance, standard deviation, and range. Therefore, IQR and MAD are useful measures of variability when extreme observations may affect the results.
quantile(
service_data$WaitTime,
probs = c(0.05, 0.25, 0.50, 0.75, 0.95)
)
## 5% 25% 50% 75% 95%
## 3.522856 5.176006 7.643546 12.064015 33.871799
ggplot(service_data, aes(y = WaitTime)) +
geom_boxplot(
fill = "#ffb703",
alpha = 0.8
) +
labs(
title = "Boxplot of Waiting Times",
y = "Waiting time (minutes)"
) +
theme_minimal()
The median waiting time is 7.643546 minutes, and the interquartile range
(IQR) is 6.888009 minutes. The boxplot indicates potential high-value
outliers above the upper whisker, with the maximum waiting time reaching
37.97178 minutes.
service_data %>%
mutate(
WaitBin = cut_width(WaitTime, width = 5, boundary = 0)
) %>%
count(WaitBin, name = "Frequency")
## # A tibble: 6 × 2
## WaitBin Frequency
## <fct> <int>
## 1 [0,5] 7
## 2 (5,10] 11
## 3 (10,15] 7
## 4 (15,20] 1
## 5 (25,30] 2
## 6 (35,40] 2
ggplot(service_data, aes(x = WaitTime)) +
geom_histogram(
binwidth = 5,
boundary = 0,
fill = "#8ecae6",
color = "white"
) +
labs(
title = "Distribution of Waiting Times",
x = "Waiting time (minutes)",
y = "Count"
) +
theme_minimal()
ggplot(service_data, aes(x = WaitTime)) +
geom_histogram(
binwidth = 2,
boundary = 0,
fill = "#8ecae6",
color = "white"
) +
labs(
title = "Distribution of Waiting Times — Smaller Bins",
x = "Waiting time (minutes)",
y = "Count"
) +
theme_minimal()
Changing the bin width changes how observations are grouped, which can
reveal or hide features of the distribution. Smaller bins show more
detail but can make the distribution appear more irregular.
ggplot(service_data, aes(x = WaitTime)) +
geom_density(
fill = "#219ebc",
alpha = 0.35,
color = "#023047",
linewidth = 1
) +
labs(
title = "Density of Waiting Times",
x = "Waiting time (minutes)",
y = "Density"
) +
theme_minimal()
The waiting times are concentrated around approximately 8 minutes, with an IQR of 6.888009 minutes, indicating the spread of the middle 50% of observations. The distribution is strongly right-skewed, with a long upper tail extending to approximately 37.97 minutes. The percentiles, boxplot, histogram, and density plot all support the presence of unusually high waiting times.