# Load necessary packages
library(ggplot2)

# Input the data
data <- data.frame(
  d = c(rep(0, 20), rep(1, 20)),
  y = c(0.22, -0.87, -2.39, -1.79, 0.37, -1.54, 1.28, -0.31, -0.74, 1.72,
        0.38, -0.17, -0.62, -1.10, 0.30, 0.15, 2.30, 0.19, -0.50, -0.09,
        -5.13, -2.19, -2.43, -3.83, 0.50, -3.25, 4.32, 1.63, 5.18, -0.43,
        7.11, 4.87, -3.10, -5.81, 3.76, 6.31, 2.58, 0.07, 5.76, 3.50)
)

# Calculate the eCDFs for each group
data <- data[order(data$d, data$y), ]
data <- data %>% 
  group_by(d) %>%
  mutate(eCDF = rank(y) / n())

# Plot the eCDFs
ggplot(data, aes(x = y, y = eCDF, color = factor(d))) +
  geom_step(size = 1) +
  scale_color_manual(values = c("blue", "red"), labels = c("Control", "Treatment")) +
  labs(title = "Kolmogorov-Smirnov Test: eCDF",
       x = "y", y = "eCDF", color = "") +
  theme_minimal()

# Conduct the Kolmogorov-Smirnov test
ks_test <- ks.test(data$y[data$d == 0], data$y[data$d == 1], exact = TRUE)
print(ks_test)

