library(readxl)
library(knitr)
library(fitdistrplus)
## Cargando paquete requerido: MASS
## Cargando paquete requerido: survival
# 1. Carga de datos
Datos <- read_xlsx("C:/Users/LEO/Documents/Antisana/weatherdataANTISANA.csv.xlsx")
# Trabajamos con Relative_Humidity
Hum_Total <- as.numeric(Datos$Relative_Humidity)
Hum_Total <- Hum_Total[!is.na(Hum_Total)]
# --- GRÁFICA: TODOS LOS DATOS ORIGINALES DE HUMEDAD ---
hist(Hum_Total, freq = FALSE, main="Distribución Total de Humedad Relativa",
xlab="%", ylab="Densidad", col="blue")

# --- FILTRO CRÍTICO ---
# Rango ajustado para mejorar el ajuste del modelo
Humedad <- Hum_Total[Hum_Total >= 0.55 & Hum_Total <= 0.8]
# ----------------------------------------------------------------
# GRÁFICA 107: Sin agrupación manual (Breaks automáticos de R)
histograma <- hist(Humedad, freq = FALSE,
main="Gráfica 107. Modelo de probabilidad WEIBULL (Humedad)",
xlab="%", ylab="Densidad de probabilidad", col="blue")

# GRÁFICA 108: Ajuste del modelo al histograma (WEIBULL)
histograma <- hist(Humedad, freq = FALSE,
main="Gráfica 108. Ajuste de Densidad WEIBULL (Humedad)",
xlab="%", ylab="Densidad de probabilidad", col="blue")
h <- length(histograma$counts)
# --- AJUSTE WEIBULL ---
fit_wei <- fitdist(Humedad, "weibull")
shape_h <- fit_wei$estimate["shape"]
scale_h <- fit_wei$estimate["scale"]
# Curva suave WEIBULL
x_seq <- seq(min(Humedad), max(Humedad), length.out = 500)
lines(x_seq, dweibull(x_seq, shape = shape_h, scale = scale_h), lwd=4, col="black")

# Frecuencia simple observada (Counts del histograma automático)
Fo <- histograma$counts
# Frecuencia simple esperada usando la función pweibull
P <- c(0)
for (i in 1:h) {
P[i] <- (pweibull(histograma$breaks[i+1], shape = shape_h, scale = scale_h) -
pweibull(histograma$breaks[i], shape = shape_h, scale = scale_h))
}
Fe <- P * length(Humedad)
# Comparar tamaño real y el modelo
sum(Fe)
## [1] 70.68412
n <- length(Humedad)
# 2. Test de Pearson (Correlación)
plot(Fo, Fe, main="Gráfica 109: Correlación de frecuencias (Humedad - Weibull)",
xlab="Frecuencia Observada", ylab="Frecuencia esperada", col="blue3", pch=19)
abline(lm(Fe ~ Fo), col="red", lwd=2)

Correlación <- cor(Fo, Fe) * 100
Correlación
## [1] 87.82833
# TEST DE CHI-CUADRADO
grados_libertad <- (length(histograma$counts)-1)
nivel_significancia <- 0.05
x2 <- sum((Fe - Fo)^2 / Fe)
umbral_aceptacion <- qchisq(1 - nivel_significancia, grados_libertad)
# ¿Se acepta el modelo?
x2 < umbral_aceptacion
## [1] TRUE
# 3. Tabla resumen de test
Variable <- c("Relative Humidity (Weibull 0.55-0.8)")
tabla_resumen <- data.frame(Variable, round(Correlación, 2), round(x2, 2), round(umbral_aceptacion, 2))
colnames(tabla_resumen) <- c("Variable", "Test Pearson (%)", "Chi Cuadrado", "Umbral de aceptación")
kable(tabla_resumen, format = "markdown", caption = "Tabla. Resumen de validación para Humedad (WEIBULL)")
Tabla. Resumen de validación para Humedad (WEIBULL)
| Relative Humidity (Weibull 0.55-0.8) |
87.83 |
6.88 |
9.49 |
# 4. Cálculo de probabilidad
# Rango sugerido dentro de los nuevos límites
limite_inf <- 0.60
limite_sup <- 0.75
prob_area <- pweibull(limite_sup, shape_h, scale_h) - pweibull(limite_inf, shape_h, scale_h)
x_grafica <- seq(min(Humedad), max(Humedad), length.out = 1000)
y_grafica <- dweibull(x_grafica, shape_h, scale_h)
# GRAFICAR MODELO FINAL
plot(x_grafica, y_grafica, type = "l", col = "skyblue3", lwd = 2,
main="Distribución WEIBULL Final (Humedad)",
ylab="Densidad de Probabilidad",
xlab="%")
# Sombreado de la probabilidad
x_sombra <- seq(limite_inf, limite_sup, length.out = 100)
y_sombra <- dweibull(x_sombra, shape_h, scale_h)
polygon(c(x_sombra, rev(x_sombra)), c(y_sombra, rep(0, length(y_sombra))),
col = rgb(1, 0, 0, 0.4), border = "red")
# Leyenda
legend("topleft",
legend = c("Modelo Weibull", paste("Prob 60-75%:", round(prob_area*100, 2), "%")),
fill = c(NA, rgb(1, 0, 0, 0.4)), border = c("skyblue3", "red"), bty = "n")
