1 Regresión Lineal

1.1 Carga de datos

setwd("/cloud/project")
datos <- read.csv("DERRAMES_GLOBALEST.csv", header = TRUE, sep = ";" , dec = ".")
str(datos)
## 'data.frame':    3550 obs. of  23 variables:
##  $ Id                              : int  6786 6250 8220 6241 6216 6620 6262 6229 6201 6221 ...
##  $ Dia                             : int  19 3 21 16 19 7 10 12 18 29 ...
##  $ Mes                             : int  1 6 4 3 12 10 2 5 3 1 ...
##  $ Año                             : chr  "A" "1979" "2010" "1978" ...
##  $ Nombre                          : chr  "Arabian Gulf Spills; Persian Gulf, Kuwait" "IXTOC I; Bahia de Campeche, Mexico" "Deepwater Horizon; Gulf of Mexico" "Amoco Cadiz; Brittany, France" ...
##  $ Ubicacion                       : chr  "Persian Gulf, Kuwait" "Bahia de Campeche, Mexico" "Gulf of Mexico" "Brittany, France" ...
##  $ Latitud                         : chr  "29,5" "19,4083" "28,7367" "48,5833" ...
##  $ Longuitud                       : chr  "48" "-92,325" "-88,3872" "-4,71667" ...
##  $ Amenaza                         : chr  "Oil" "Oil" "Oil" "Oil" ...
##  $ Etiquetas                       : chr  "" "Collision" "" "Grounding" ...
##  $ Tipo_de_crudo                   : chr  "Kuwait crude oil" "IXTOC I crude oil" "Diesel, crude oil" "Arabian light crude, Iranian light crude, Bunker C" ...
##  $ Cantidad_recuperada_superficie  : int  NA NA 1 NA NA NA NA NA NA NA ...
##  $ Cantidad_recuperada_costas      : int  NA NA 1 NA NA NA NA NA NA NA ...
##  $ Cantidad_tratada_biologicamente : int  1 NA 1 1 NA NA NA NA NA NA ...
##  $ Cantidad_dispersada_quimicamente: int  NA 1 1 1 NA NA NA 1 1 1 ...
##  $ Cantidad_quemada                : int  NA 1 1 NA NA NA NA 1 1 1 ...
##  $ Maximo_liberacion_galones       : int  336000009 NA 205000000 68000017 NA NA NA 9240000 36100000 NA ...
##  $ Barreras_de_contencion_flotantes: int  35 12 182 17 3 3 7 8 5 6 ...
##  $ Causa_principal                 : chr  "Daño del tanque  " "Incendio y explosion " "Incendio y explosion " "Daño del tanque " ...
##  $ Volumen_derramados_galones      : chr  "336.000.000" "365.000.000" "600.000.000" "68.000.000" ...
##  $ Respuesta_actual_galones        : chr  "336000000" "252000000" "168000000" "68700000" ...
##  $ Fuente_respuesta                : chr  "description and posts" "posts" "description" "posts" ...
##  $ etiqueta_actualizacion          : chr  "RA updated" "RA newly acquired" "RA updated" "RA updated" ...

1.2 Carga de librería

library(dplyr)   # Para agrupar y resumir datos
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

1.3 Selección y transformación de variables

x <- as.numeric(datos$Respuesta_actual_galones)
## Warning: NAs introduced by coercion
y <- as.numeric(datos$Maximo_liberacion_galones)
plot(x, y)
grid(col = "gray50", lty = "dotted")

datos <- data.frame(x = x, y = y)

1.4 Agrupar por x y obtener promedio de y

promedios_xy <- datos %>%
  group_by(x) %>%
  summarise(y_prom = mean(y, na.rm = TRUE))

1.5 Ajustar modelo con los puntos promediados

modelo_prom <- lm(y_prom ~ x, data = promedios_xy)
summary(modelo_prom)
## 
## Call:
## lm(formula = y_prom ~ x, data = promedios_xy)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -22829406   -148897   -143164   -142049  30633345 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.429e+05  1.416e+05   1.009    0.313    
## x           1.037e+00  7.200e-03 144.039   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2764000 on 384 degrees of freedom
##   (70 observations deleted due to missingness)
## Multiple R-squared:  0.9818, Adjusted R-squared:  0.9818 
## F-statistic: 2.075e+04 on 1 and 384 DF,  p-value: < 2.2e-16

1.6 Gráfica con puntos promediados y línea de regresión

plot(x, y, col = rgb(0, 0, 1, alpha = 0.1), pch = 16,
     main = "Regresión Lineal: Respuesta actual vs Maxima liberación",
     xlab = "Respuesta actual (galones)",
     ylab = "Máxima liberación (galones)")

points(promedios_xy$x, promedios_xy$y_prom, col = "aquamarine3", pch = 19)
abline(modelo_prom, col = "darkgreen", lwd = 2)

grid(col = "gray50", lty = "dotted")

1.7 Test de bondad

Rm <- cor(promedios_xy$x, promedios_xy$y_prom, use = "complete.obs")
Rm
## [1] 0.9908723
Rm2 <- Rm^2 * 100
Rm2
## [1] 98.18279