Modelo Log-normal

Librerías necesarias

library(fitdistrplus)
## Loading required package: MASS
## Loading required package: survival
library(gt)

Cargar datos

datos <- read.delim("LatitudAccidente.csv", header = TRUE)

Asegurar que Latitud sea numérica (limpiando caracteres raros)

Latitud <- gsub(",", ".", datos$Latitud)   # reemplaza comas por puntos decimales
Latitud <- gsub("[^0-9\\.]", "", Latitud)  # elimina cualquier símbolo extraño
Latitud <- as.numeric(Latitud)
Latitud <- Latitud[!is.na(Latitud)]        # eliminar NA

Usar Latitud completa (sin filtro de outliers)

LatitudVC <- Latitud

Estimación de parámetros lognormales

log_lat <- log(LatitudVC)
ulog <- mean(log_lat, na.rm = TRUE)
sigmalog <- sd(log_lat, na.rm = TRUE)

Histograma con curva lognormal

par(mar = c(4, 6, 4, 2))
HistLati <- hist(LatitudVC,
                 freq = FALSE,
                 breaks = 7,
                 main = "Histograma de Latitud con curva Log-Normal",
                 xlab = "Latitud (°)",
                 ylab = "Densidad de probabilidad",
                 col = "darkseagreen3",
                 las = 1)
# Secuencia desde el mínimo al máximo de latitud
x <- seq(min(LatitudVC), max(LatitudVC), 0.01)
# Curva lognormal

lines(x, dlnorm(x, meanlog = ulog, sdlog = sigmalog),
      col = "red", lwd = 3)

legend("topright", legend = "Modelo Log-Normal",
       col = "red", lwd = 2, lty = 1, box.lty = 1)

# Bondad de ajuste (Chi-cuadrado)

h <- length(HistLati$counts)
Fo <- HistLati$counts
P <- numeric(h)
for (i in 1:h) {
  P[i] <- plnorm(HistLati$breaks[i+1], ulog, sigmalog) -
    plnorm(HistLati$breaks[i], ulog, sigmalog)
}
Fe <- P * length(LatitudVC)
Fe[Fe == 0] <- 1e-6  # evitar divisiones por cero

x2_log <- sum((Fe - Fo)^2 / Fe)
gl_log <- (h - 1) - 2
umbral_aceptacion <- qchisq(0.95, gl_log)

Tabla resumen

Tabla_resumen <- data.frame(
  Variable = "Latitud",
  Modelo = "Log-Normal",
  Pearson = round(cor(Fo, Fe) * 100, 2),
  Chi_Cuadrado = round(x2_log, 2),
  Umbral = round(umbral_aceptacion, 2),
  Test = ifelse(x2_log < umbral_aceptacion, "Aprobado", "Rechazado")
)

Tabla resumen

Tabla_resumen %>%
  gt() %>%
  tab_header(
    title = md("**Tabla N°1**"),
    subtitle = md("**Resumen del Test de Bondad de Ajuste Log-Normal**")
  )
Tabla N°1
Resumen del Test de Bondad de Ajuste Log-Normal
Variable Modelo Pearson Chi_Cuadrado Umbral Test
Latitud Log-Normal 96.72 3914.64 15.51 Rechazado