# Load necessary libraries
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
# Define the range of x uniformly distributed between -1 and 1
x <- seq(-1, 1, length.out = 400)

# Compute logit(mu(x)) and its derivative for each error distribution
logit_mu_normal <- log(pnorm(x) / (1 - pnorm(x)))
logit_mu_normal_deriv <- dnorm(x) / (pnorm(x) * (1 - pnorm(x)))

logit_mu_logistic <- (pi * x / sqrt(3))
logit_mu_logistic_deriv <- rep(pi / sqrt(3), length(x))

logit_mu_uniform <- log((x + 2 * sqrt(3)) / (2 * sqrt(3) - x))
logit_mu_uniform_deriv <- (4 * sqrt(3)) / ((4 * 3) - x^2)

# Find the range of the true log odds ratio
range_normal <- range(logit_mu_normal_deriv)
range_logistic <- range(logit_mu_logistic_deriv)
range_uniform <- range(logit_mu_uniform_deriv)

# Print the results
cat("Range of true log odds ratio for Normal distribution:\n")
## Range of true log odds ratio for Normal distribution:
print(range_normal)
## [1] 1.595770 1.812735
cat("Range of true log odds ratio for Logistic distribution:\n")
## Range of true log odds ratio for Logistic distribution:
print(range_logistic)
## [1] 1.813799 1.813799
cat("Range of true log odds ratio for Uniform distribution:\n")
## Range of true log odds ratio for Uniform distribution:
print(range_uniform)
## [1] 0.5773506 0.6298367
# Create a data frame for plotting
df <- data.frame(
  x = rep(x, 3),
  logit_mu_deriv = c(logit_mu_normal_deriv, logit_mu_logistic_deriv, logit_mu_uniform_deriv),
  distribution = factor(rep(c("Normal", "Logistic", "Uniform"), each = length(x)))
)

# Plotting the derivatives
ggplot(df, aes(x = x, y = logit_mu_deriv, color = distribution)) +
  geom_line() +
  labs(title = expression("Log Odds Ratio (Derivative of " * logit(mu(x)) * ")"),
       y = expression("Log Odds Ratio"), x = "x") +
  theme_minimal() +
  scale_color_manual(values = c("blue", "green", "red")) +
  theme(legend.title = element_blank())