. DATOS

library(readr)
datasetf <- read_csv("estadistica /cosasr/datasetf.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 2795 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (18): Accident Date/Time, Operator Name, Pipeline/Facility Name, Pipelin...
## dbl (18): Report Number, Supplemental Number, Accident Year, Operator ID, Ac...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

1. SELECCIÓN DE VARIABLES

# X = Unintentional Release, Y = Liquid Recovery
data_log <- data.frame(
  Liberacion = datasetf$`Unintentional Release (Barrels)`,
  Recuperacion = datasetf$`Liquid Recovery (Barrels)`
)

2. LIMPIEZA (X debe ser > 0 para el logaritmo)

data_log <- na.omit(data_log)
data_log <- data_log[data_log$Liberacion > 0, ]

3. TABLA DE VALORES

data_log

4. GRÁFICA DE NUBES (Exploración)

plot(data_log$Liberacion, data_log$Recuperacion, col="steelblue", pch=16,
     main="Nube de Puntos: Liberación vs Recuperación")

5. CONJETURA Y MODELO LOGARÍTMICO

# Aplicamos logaritmo a la variable independiente X
modelo_log <- lm(Recuperacion ~ log(Liberacion), data = data_log)

6. CÁLCULO DE PARÁMETROS

# Recuperacion = b0 + b1 * log(Liberacion)
params <- coef(modelo_log)
cat("Intercepto (b0):", params[1], "\nPendiente logarítmica (b1):", params[2], "\n")
## Intercepto (b0): -10.67203 
## Pendiente logarítmica (b1): 65.5821

7. GRÁFICA CON EL MODELO

plot(data_log$Liberacion, data_log$Recuperacion, 
     main="Ajuste Logarítmico: Recuperación vs Liberación",
     xlab="Barriles Liberados", ylab="Barriles Recuperados", pch=16, col="gray")

# Dibujar la curva logarítmica
x_seq <- seq(min(data_log$Liberacion), max(data_log$Liberacion), length.out=500)
y_pred <- predict(modelo_log, newdata = data.frame(Liberacion = x_seq))
lines(x_seq, y_pred, col="darkgreen", lwd=3)

8. TESTS DE PEARSON (Indicadores)

# A. Test de Pearson Normal
cor_normal <- cor.test(data_log$Liberacion, data_log$Recuperacion)
cat("\n--- TEST DE PEARSON (COMPLETO) ---\n")
## 
## --- TEST DE PEARSON (COMPLETO) ---
print(cor_normal$estimate) # Coeficiente
##       cor 
## 0.5131019
print(cor_normal$p.value)  # Significancia
## [1] 1.426059e-185
# B. Test de Pearson Omitiendo Outliers
# Definimos outliers como el 5% superior en liberación
limite_outlier <- quantile(data_log$Liberacion, 0.95)
data_sin_outliers <- subset(data_log, Liberacion < limite_outlier)

cor_filtrado <- cor.test(data_sin_outliers$Liberacion, data_sin_outliers$Recuperacion)
cat("\n--- TEST DE PEARSON (SIN OUTLIERS) ---\n")
## 
## --- TEST DE PEARSON (SIN OUTLIERS) ---
print(cor_filtrado$estimate)
##       cor 
## 0.8586589

9. ESTIMACIÓN

#Para la estimación, supongamos un derrame de 1000 barriles:
pred_1000 <- predict(modelo_log, newdata = data.frame(Liberacion = 1000))
cat("Para 1000 bbl liberados, se estima recuperar:", round(pred_1000, 2), "bbl")
## Para 1000 bbl liberados, se estima recuperar: 442.35 bbl

10. CONCLUCIÓN

El modelo logarítmico indica que la tasa de recuperación disminuye conforme el desastre aumenta. El Test de Pearson sin outliers suele mostrar una correlación más alta, lo que demuestra que los accidentes extremos (mega-derrames) no siguen la lógica normal de contención y “ensucian” la estadística.