# Load necessary libraries
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
# Function to simulate data and fit logistic regression
simulate_and_fit <- function(n, beta0, beta1, error_dist) {
  x <- runif(n, -1, 1)
  if (error_dist == "normal") {
    epsilon <- rnorm(n, 0, 1)
  } else if (error_dist == "logistic") {
    epsilon <- rlogis(n, location = 0, scale = sqrt(3) / pi)
  } else if (error_dist == "uniform") {
    epsilon <- runif(n, -sqrt(3), sqrt(3))
  }
  y_star <- beta0 + beta1 * x + epsilon
  y <- ifelse(y_star > 0, 1, 0)
  
  model <- glm(y ~ x, family = binomial(link = "logit"))
  return(coef(model))
}

# Simulate data with large n and fit the model for each error distribution
set.seed(123) # For reproducibility
n <- 10000
beta0_true <- 0
beta1_true <- 1

# Fit models
coef_normal <- simulate_and_fit(n, beta0_true, beta1_true, "normal")
coef_logistic <- simulate_and_fit(n, beta0_true, beta1_true, "logistic")
coef_uniform <- simulate_and_fit(n, beta0_true, beta1_true, "uniform")

# Print the results
print("Estimated coefficients for Normal distribution:")
## [1] "Estimated coefficients for Normal distribution:"
print(coef_normal)
##  (Intercept)            x 
## -0.009718758  1.582983871
print("Estimated coefficients for Logistic distribution:")
## [1] "Estimated coefficients for Logistic distribution:"
print(coef_logistic)
##  (Intercept)            x 
## -0.001003309  1.839102262
print("Estimated coefficients for Uniform distribution:")
## [1] "Estimated coefficients for Uniform distribution:"
print(coef_uniform)
## (Intercept)           x 
## -0.00712363  1.26282323