ANÁLISIS ESTADÍSTICO

CARGA DE DATOS Y LIBRERÍAS

rm(list = ls())

if (!require("gt")) install.packages("gt")
if (!require("dplyr")) install.packages("dplyr")
if (!require("knitr")) install.packages("knitr")

library(gt)
library(dplyr)
library(knitr)

ruta_zip <- "Mining_model_input_data.zip"
ruta_csv_local <- "Mining_model_input_data.csv"
ruta_csv_pc <- "C:\\Users\\vlady\\Documents\\debers alexa no borrar\\Mining_model_input_data.csv"

if (file.exists(ruta_zip)) {
  archivos_zip <- unzip(ruta_zip, list = TRUE)$Name
  archivo_csv <- if ("Mining_model_input_data.csv" %in% archivos_zip) {
    "Mining_model_input_data.csv"
  } else {
    archivos_zip[grepl("\\.csv$", archivos_zip, ignore.case = TRUE)][1]
  }
  datos <- read.csv(unz(ruta_zip, archivo_csv))
} else if (file.exists(ruta_csv_local)) {
  datos <- read.csv(ruta_csv_local)
} else if (file.exists(ruta_csv_pc)) {
  datos <- read.csv(ruta_csv_pc)
} else {
  stop("No se encontró Mining_model_input_data.zip ni Mining_model_input_data.csv.")
}

TABLA DE DISTRIBUCIÓN EIA_COOLING_DEGREE_DAYS_US_AVERAGE

# --- EIA_cooling_degree_days_us_average ---
cdd <- suppressWarnings(as.numeric(datos$EIA_cooling_degree_days_us_average))
cdd <- na.omit(cdd)

cortes_cdd <- c(0, 50, 120, 220, 320, 410)
etiquetas_cdd <- c(
  "0 - 50",
  "51 - 120",
  "121 - 220",
  "221 - 320",
  "321 - 410"
)
cdd_cat <- cut(cdd, breaks = cortes_cdd, labels = etiquetas_cdd, include.lowest = TRUE)

ni_cdd <- table(cdd_cat)
TDF_28 <- data.frame(categoria = names(ni_cdd), ni = as.numeric(ni_cdd), stringsAsFactors = FALSE)
TDF_28 <- TDF_28[TDF_28$ni > 0, ]

total_cdd <- sum(TDF_28$ni)
hi_cdd <- round(TDF_28$ni / total_cdd * 100, 2)
hi_cdd[which.max(hi_cdd)] <- round(hi_cdd[which.max(hi_cdd)] + (100 - sum(hi_cdd)), 2)

Ni_Asc_cdd <- cumsum(TDF_28$ni)
Hi_Asc_cdd <- round(cumsum(hi_cdd), 2)
Hi_Asc_cdd[length(Hi_Asc_cdd)] <- 100.00
Ni_Desc_cdd <- rev(cumsum(rev(TDF_28$ni)))
Hi_Desc_cdd <- round(rev(cumsum(rev(hi_cdd))), 2)

tabla_cdd <- data.frame(
  Dias_Grado_Refrigeracion = TDF_28$categoria,
  ni = TDF_28$ni,
  hi = hi_cdd,
  Ni_Asc = as.numeric(Ni_Asc_cdd),
  Hi_Asc = Hi_Asc_cdd,
  Ni_Desc = as.numeric(Ni_Desc_cdd),
  Hi_Desc = Hi_Desc_cdd,
  stringsAsFactors = FALSE
)

fila_total_cdd <- data.frame(
  Dias_Grado_Refrigeracion = "TOTAL",
  ni = sum(tabla_cdd$ni),
  hi = round(sum(tabla_cdd$hi), 2),
  Ni_Asc = "-",
  Hi_Asc = "-",
  Ni_Desc = "-",
  Hi_Desc = "-",
  stringsAsFactors = FALSE
)

tabla_cdd_final <- rbind(tabla_cdd, fila_total_cdd)

datos_graf_cdd <- tabla_cdd_final[tabla_cdd_final$Dias_Grado_Refrigeracion != "TOTAL", ]
colores_cdd <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_cdd))
hi_global_cdd <- round(as.numeric(datos_graf_cdd$ni) / nrow(datos) * 100, 2)

# ## Tabla de distribución de frecuencias - EIA_cooling_degree_days_us_average
Tabla_CDD <- tabla_cdd_final %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°. 28*"),
    subtitle = md("**Tabla de distribución de frecuencias de los días grado de refrigeración promedio en EE. UU.**")
  ) %>%
  tab_source_note(
    source_note = md("__Fuente: Dataset Mining_model_input_data__")
  ) %>%
  fmt_number(columns = c(hi, Hi_Asc, Hi_Desc), decimals = 2) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    column_labels.border.top.color = "black",
    column_labels.border.bottom.color = "black",
    column_labels.border.bottom.width = px(2),
    row.striping.include_table_body = TRUE,
    heading.border.bottom.color = "black",
    heading.border.bottom.width = px(2),
    table_body.hlines.color = "gray",
    table_body.border.bottom.color = "black"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(rows = Dias_Grado_Refrigeracion == "TOTAL")
  )

Tabla_CDD
Tabla N°. 28
Tabla de distribución de frecuencias de los días grado de refrigeración promedio en EE. UU.
Dias_Grado_Refrigeracion ni hi Ni_Asc Hi_Asc Ni_Desc Hi_Desc
0 - 50 164074 49.65 164074 49.65 330466 100
51 - 120 40772 12.34 204846 61.99 166392 50.35
121 - 220 27998 8.47 232844 70.46 125620 38.01
221 - 320 41967 12.70 274811 83.16 97622 29.54
321 - 410 55655 16.84 330466 100 55655 16.84
TOTAL 330466 100.00 - - - -
Fuente: Dataset Mining_model_input_data

GRÁFICOS EIA_COOLING_DEGREE_DAYS_US_AVERAGE

4.1- Diagrama porcentual (escala local)

if (!exists("datos_graf_cdd")) {
  datos_graf_cdd <- tabla_cdd_final[tabla_cdd_final$Dias_Grado_Refrigeracion != "TOTAL", ]
  colores_cdd <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_cdd))
  hi_global_cdd <- round(as.numeric(datos_graf_cdd$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_cdd_local <- barplot(
  datos_graf_cdd$hi,
  names.arg = datos_graf_cdd$Dias_Grado_Refrigeracion,
  col = colores_cdd,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, max(datos_graf_cdd$hi) * 1.15),
  main = "Diagrama Porcentual (Escala Local) - Días Grado de Refrigeración",
  ylab = "Porcentaje Local (%)"
)
text(x = bp_cdd_local, y = datos_graf_cdd$hi, labels = paste0(datos_graf_cdd$hi, "%"), pos = 3, cex = 0.75, font = 2)

4.2- Diagrama de cantidad (escala global)

if (!exists("datos_graf_cdd")) {
  datos_graf_cdd <- tabla_cdd_final[tabla_cdd_final$Dias_Grado_Refrigeracion != "TOTAL", ]
  colores_cdd <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_cdd))
  hi_global_cdd <- round(as.numeric(datos_graf_cdd$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_cdd_cant_glob <- barplot(
  datos_graf_cdd$ni,
  names.arg = datos_graf_cdd$Dias_Grado_Refrigeracion,
  col = colores_cdd,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, nrow(datos)),
  main = "Diagrama de Cantidad (Escala Global) - Días Grado de Refrigeración",
  ylab = "Frecuencia Absoluta (ni)"
)
text(x = bp_cdd_cant_glob, y = datos_graf_cdd$ni, labels = datos_graf_cdd$ni, pos = 3, cex = 0.75, font = 2)

4.3- Diagrama porcentual (escala global)

if (!exists("datos_graf_cdd")) {
  datos_graf_cdd <- tabla_cdd_final[tabla_cdd_final$Dias_Grado_Refrigeracion != "TOTAL", ]
  colores_cdd <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_cdd))
  hi_global_cdd <- round(as.numeric(datos_graf_cdd$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_cdd_porc_glob <- barplot(
  hi_global_cdd,
  names.arg = datos_graf_cdd$Dias_Grado_Refrigeracion,
  col = colores_cdd,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, 100),
  main = "Diagrama Porcentual (Escala Global) - Días Grado de Refrigeración",
  ylab = "Porcentaje Global (%)"
)
text(x = bp_cdd_porc_glob, y = hi_global_cdd, labels = paste0(hi_global_cdd, "%"), pos = 3, cex = 0.75, font = 2)

Gráfico circular

if (!exists("datos_graf_cdd")) {
  datos_graf_cdd <- tabla_cdd_final[tabla_cdd_final$Dias_Grado_Refrigeracion != "TOTAL", ]
  colores_cdd <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_cdd))
  hi_global_cdd <- round(as.numeric(datos_graf_cdd$ni) / nrow(datos) * 100, 2)
}

par(mar = c(2, 2, 4, 13), xpd = TRUE)
pie(
  datos_graf_cdd$hi,
  labels = paste0(datos_graf_cdd$hi, "%"),
  col = colores_cdd,
  border = "white",
  radius = 0.75,
  main = "Distribución Porcentual - Días Grado de Refrigeración",
  cex = 0.8
)
legend(
  "topright",
  inset = c(-0.42, 0.05),
  legend = datos_graf_cdd$Dias_Grado_Refrigeracion,
  fill = colores_cdd,
  cex = 0.72,
  title = "°F-día"
)

CONCLUSIÓN

Los resultados muestran que el rango de 0 a 50 días grado de refrigeración presenta la mayor frecuencia relativa, con un 49,65 % de los registros, seguido del rango de 321 a 410, con un 16,84 %. Esto evidencia que, en el proyecto Modelo de uso de agua en la minería de Estados Unidos, 2019–2020, predominan los registros correspondientes a zonas con menores necesidades de refrigeración, según los días grado registrados.