Fan plot: Prediction of Oncology Drug Sales

Shota Hasui

2023-05-16

For this project, I created a fan chart that displays the historical sales data along with predicted sales data of a computer-generated fictional oncology drug product. The “fan” with the colored background is where the prediction lies. The blurred area shows the uncertainty of the prediction over time.

Code can be found below.

# Install and load necessary libraries
library(fanplot)

# Generate existing oncology drug sales data
set.seed(123)  # Ensure reproducibility
years <- 2000:2022
sales <- round(50 * exp((years - 2000) / 10) + rnorm(length(years), sd = 10))

# Display the generated sales data
existing_data <- data.frame(Year = years, Sales = sales)
print(existing_data)

# Generate the prediction data using a split-normal distribution
years_pred <- 2022:2040
n_years_pred <- length(years_pred)
p <- seq(0.05, 0.95, 0.05)
p <- c(0.01, p, 0.99)
predictions <- matrix(NA, nrow = length(p), ncol = n_years_pred)
for (i in 1:n_years_pred) {
  predictions[, i] <- qsplitnorm(p, mode = sales[length(sales)] * (1 + i / 100), sd = i * 10, skew = 1 - i / n_years_pred)
}

# Plot existing data and the fan plot
plot(existing_data$Year, existing_data$Sales, type = "l", col = "blue", lwd = 2,
     xlim = c(2000, 2040), ylim = c(0, max(predictions)),  # Start the x axis 2000 and End the x axis at 2040
     xaxt = "n", yaxt = "n", ylab="Sales (in thousands)", xlab = "")  # Label the y axis as sales
rect(2022, par("usr")[3], 2040, par("usr")[4], border = "gray90", col = "gray90")
fan(data = predictions, data.type = "values", probs = p,
    start = 2022, frequency = 1,
    anchor = sales[length(sales)],
    fan.col = colorRampPalette(c("red", "gray90")),  # Make the color of the fan pink
    ln = NULL, rlab = NULL)
axis(2, las = 2, tcl = 0.5)
axis(4, las = 2, tcl = 0.5, labels = FALSE)
axis(1, at = seq(2000, 2040, 2), tcl = 0.5)
axis(1, at = 2000:2040, labels = FALSE, tcl = 0.2)