En este informe se construye un modelo de regresión múltiple para predecir el Índice de Calidad del Aire (ICA) en Barranquilla con base en variables contaminantes como PM2.5, PM10, NO2, SO2, CO y O3. Todas las variables son cuantitativas, lo que permite aplicar este tipo de análisis.
# Cargar librerías necesarias
library(tidyverse)
library(car)
library(GGally)
library(broom)
library(olsrr)
library(performance)
# Leer datos
datos <- read.csv("Contaminacion_Barranquilla_Limpia.csv")
# Vista previa de los datos
glimpse(datos)
## Rows: 200
## Columns: 10
## $ Fecha <chr> "2018-01-18", "2018-02-01", "2018-03-03", "2018-03-25", …
## $ Estacion <chr> "Estación Centro", "Estación Norte", "Estación Norte", "…
## $ PM2_5 <dbl> 65.81, 17.76, 35.78, 43.28, 37.23, 88.23, 15.13, 28.66, …
## $ PM10 <dbl> 38.67, 93.33, 49.27, 108.45, 116.39, 128.41, 89.50, 105.…
## $ NO2 <dbl> 41.94, 61.68, 6.75, 91.41, 32.47, 83.05, 98.32, 67.96, 8…
## $ SO2 <dbl> 52.21, 36.23, 15.74, 45.05, 26.78, 38.05, 5.98, 3.87, 43…
## $ CO <dbl> 7.18, 6.10, 7.93, 7.20, 1.85, 4.76, 1.17, 9.75, 2.97, 8.…
## $ O3 <dbl> 93.78, 65.30, 104.38, 110.86, 87.29, 67.35, 91.95, 118.6…
## $ ICA <int> 131, 35, 71, 86, 74, 176, 30, 57, 146, 33, 91, 74, 99, 1…
## $ Categoria_ICA <chr> "Dañina para grupos sensibles", "Buena", "Moderada", "Mo…
summary(datos)
## Fecha Estacion PM2_5 PM10
## Length:200 Length:200 Min. : 5.08 Min. : 10.45
## Class :character Class :character 1st Qu.:25.27 1st Qu.: 40.47
## Mode :character Mode :character Median :44.67 Median : 72.75
## Mean :47.12 Mean : 75.51
## 3rd Qu.:68.55 3rd Qu.:107.22
## Max. :89.72 Max. :149.51
## NO2 SO2 CO O3
## Min. : 5.76 Min. : 2.30 Min. :0.150 Min. : 10.02
## 1st Qu.:31.08 1st Qu.:15.11 1st Qu.:2.765 1st Qu.: 39.92
## Median :54.56 Median :28.48 Median :5.905 Median : 71.92
## Mean :54.95 Mean :28.87 Mean :5.376 Mean : 68.25
## 3rd Qu.:79.49 3rd Qu.:42.51 3rd Qu.:7.732 3rd Qu.: 97.78
## Max. :99.72 Max. :59.92 Max. :9.960 Max. :119.97
## ICA Categoria_ICA
## Min. : 10.00 Length:200
## 1st Qu.: 49.75 Class :character
## Median : 89.00 Mode :character
## Mean : 93.75
## 3rd Qu.:136.25
## Max. :179.00
# Matriz de correlaciones
GGally::ggpairs(datos[,3:9])
# Boxplots
datos %>%
pivot_longer(cols = PM2_5:O3) %>%
ggplot(aes(x = name, y = value)) +
geom_boxplot(fill = "skyblue") +
theme_minimal()
# Modelo completo
modelo <- lm(ICA ~ PM2_5 + PM10 + NO2 + SO2 + CO + O3, data = datos)
summary(modelo)
##
## Call:
## lm(formula = ICA ~ PM2_5 + PM10 + NO2 + SO2 + CO + O3, data = datos)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.5126 -0.2602 -0.0135 0.2348 0.5204
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.5912689 0.0933949 -6.331 1.67e-09 ***
## PM2_5 2.0010055 0.0008359 2393.911 < 2e-16 ***
## PM10 -0.0002903 0.0005215 -0.557 0.578
## NO2 0.0012266 0.0007484 1.639 0.103
## SO2 -0.0004193 0.0012324 -0.340 0.734
## CO 0.0046761 0.0070630 0.662 0.509
## O3 -0.0001044 0.0006391 -0.163 0.870
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2868 on 193 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 9.765e+05 on 6 and 193 DF, p-value: < 2.2e-16
# Selección hacia atrás
modelo_opt <- step(modelo, direction = "backward")
## Start: AIC=-492.67
## ICA ~ PM2_5 + PM10 + NO2 + SO2 + CO + O3
##
## Df Sum of Sq RSS AIC
## - O3 1 0 16 -494.64
## - SO2 1 0 16 -494.55
## - PM10 1 0 16 -494.35
## - CO 1 0 16 -494.22
## <none> 16 -492.67
## - NO2 1 0 16 -491.91
## - PM2_5 1 471482 471498 1565.07
##
## Step: AIC=-494.64
## ICA ~ PM2_5 + PM10 + NO2 + SO2 + CO
##
## Df Sum of Sq RSS AIC
## - SO2 1 0 16 -496.53
## - PM10 1 0 16 -496.29
## - CO 1 0 16 -496.21
## <none> 16 -494.64
## - NO2 1 0 16 -493.83
## - PM2_5 1 481108 481123 1567.11
##
## Step: AIC=-496.53
## ICA ~ PM2_5 + PM10 + NO2 + CO
##
## Df Sum of Sq RSS AIC
## - PM10 1 0 16 -498.17
## - CO 1 0 16 -498.06
## <none> 16 -496.53
## - NO2 1 0 16 -495.72
## - PM2_5 1 481522 481538 1565.28
##
## Step: AIC=-498.17
## ICA ~ PM2_5 + NO2 + CO
##
## Df Sum of Sq RSS AIC
## - CO 1 0 16 -499.76
## <none> 16 -498.17
## - NO2 1 0 16 -497.41
## - PM2_5 1 481719 481735 1563.37
##
## Step: AIC=-499.76
## ICA ~ PM2_5 + NO2
##
## Df Sum of Sq RSS AIC
## <none> 16 -499.76
## - NO2 1 0 16 -498.94
## - PM2_5 1 481853 481869 1561.42
summary(modelo_opt)
##
## Call:
## lm(formula = ICA ~ PM2_5 + NO2, data = datos)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.53206 -0.25701 0.00145 0.23317 0.50525
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.6074718 0.0601271 -10.103 <2e-16 ***
## PM2_5 2.0009966 0.0008203 2439.459 <2e-16 ***
## NO2 0.0012376 0.0007403 1.672 0.0962 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2846 on 197 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 2.977e+06 on 2 and 197 DF, p-value: < 2.2e-16
a función lm() estima los valores de 𝛽 0 , 𝛽 1 , 𝛽 2 β 0 ,β 1 ,β 2 usando el método de mínimos cuadrados ordinarios (OLS), es decir, minimizando la suma de los errores cuadrados: i=1 ∑ n (ICA i −(β 0 +β 1 ⋅PM2.5 i +β 2 ⋅NO2 i )) 2
# ANOVA del modelo final
anova(modelo_opt)
## Analysis of Variance Table
##
## Response: ICA
## Df Sum Sq Mean Sq F value Pr(>F)
## PM2_5 1 482023 482023 5.9531e+06 < 2e-16 ***
## NO2 1 0 0 2.7944e+00 0.09618 .
## Residuals 197 16 0
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(modelo_opt, which = 2) # Q-Q plot
shapiro.test(resid(modelo_opt))
##
## Shapiro-Wilk normality test
##
## data: resid(modelo_opt)
## W = 0.95792, p-value = 1.185e-05
plot(modelo_opt, which = 3) # Scale-Location
vif(modelo_opt)
## PM2_5 NO2
## 1.000378 1.000378
plot(modelo_opt, which = 1)
Este análisis permite comprender mejor qué contaminantes afectan más la calidad del aire en Barranquilla. Las autoridades ambientales pueden usar este modelo como guía para priorizar acciones y políticas públicas.