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_BRENT_CRUDE_OIL_SPOT_PRICE

# --- EIA_brent_crude_oil_spot_price ---
brent <- suppressWarnings(as.numeric(datos$EIA_brent_crude_oil_spot_price))
brent <- na.omit(brent)

cortes_brent <- c(15, 30, 40, 50, 60, 75)
etiquetas_brent <- c(
  "15,00 - 30,00",
  "30,01 - 40,00",
  "40,01 - 50,00",
  "50,01 - 60,00",
  "60,01 - 75,00"
)
brent_cat <- cut(brent, breaks = cortes_brent, labels = etiquetas_brent, include.lowest = TRUE)

ni_brent <- table(brent_cat)
total_brent <- sum(ni_brent)
hi_brent <- round(as.numeric(ni_brent) / total_brent * 100, 2)
hi_brent[which.max(hi_brent)] <- round(hi_brent[which.max(hi_brent)] + (100 - sum(hi_brent)), 2)

Ni_Asc_brent <- cumsum(ni_brent)
Hi_Asc_brent <- round(cumsum(hi_brent), 2)
Ni_Desc_brent <- rev(cumsum(rev(ni_brent)))
Hi_Desc_brent <- round(rev(cumsum(rev(hi_brent))), 2)

tabla_brent <- data.frame(
  Precio_Brent = names(ni_brent),
  ni = as.numeric(ni_brent),
  hi = hi_brent,
  Ni_Asc = as.numeric(Ni_Asc_brent),
  Hi_Asc = Hi_Asc_brent,
  Ni_Desc = as.numeric(Ni_Desc_brent),
  Hi_Desc = Hi_Desc_brent
)

fila_total_brent <- data.frame(
  Precio_Brent = "TOTAL",
  ni = sum(tabla_brent$ni),
  hi = round(sum(tabla_brent$hi), 2),
  Ni_Asc = "-",
  Hi_Asc = "-",
  Ni_Desc = "-",
  Hi_Desc = "-"
)

tabla_brent_final <- rbind(tabla_brent, fila_total_brent)

datos_graf_brent <- tabla_brent_final[tabla_brent_final$Precio_Brent != "TOTAL", ]
colores_brent <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_brent))
hi_global_brent <- round(as.numeric(datos_graf_brent$ni) / nrow(datos) * 100, 2)

# ## Tabla de distribución de frecuencias - EIA_brent_crude_oil_spot_price
Tabla_Brent <- tabla_brent_final %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°. 27*"),
    subtitle = md("**Tabla de distribución de frecuencias del precio al contado del petróleo crudo Brent (USD/barril)**")
  ) %>%
  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 = Precio_Brent == "TOTAL")
  )

Tabla_Brent
Tabla N°. 27
Tabla de distribución de frecuencias del precio al contado del petróleo crudo Brent (USD/barril)
Precio_Brent ni hi Ni_Asc Hi_Asc Ni_Desc Hi_Desc
15,00 - 30,00 26393 7.99 26393 7.99 330466 100
30,01 - 40,00 13324 4.03 39717 12.02 304073 92.01
40,01 - 50,00 93066 28.16 132783 40.18 290749 87.98
50,01 - 60,00 55931 16.92 188714 57.1 197683 59.82
60,01 - 75,00 141752 42.90 330466 100 141752 42.9
TOTAL 330466 100.00 - - - -
Fuente: Dataset Mining_model_input_data

GRÁFICOS EIA_BRENT_CRUDE_OIL_SPOT_PRICE

Frecuencia absoluta (ni)

if (!exists("datos_graf_brent")) {
  datos_graf_brent <- tabla_brent_final[tabla_brent_final$Precio_Brent != "TOTAL", ]
  colores_brent <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_brent))
  hi_global_brent <- round(as.numeric(datos_graf_brent$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_brent_ni <- barplot(
  datos_graf_brent$ni,
  names.arg = datos_graf_brent$Precio_Brent,
  col = colores_brent,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, max(datos_graf_brent$ni) * 1.15),
  main = "Distribución del Precio al Contado del Crudo Brent",
  ylab = "Frecuencia Absoluta (ni)"
)
text(x = bp_brent_ni, y = datos_graf_brent$ni, labels = datos_graf_brent$ni, pos = 3, cex = 0.75, font = 2)

4.2- Diagrama porcentual (escala local)

if (!exists("datos_graf_brent")) {
  datos_graf_brent <- tabla_brent_final[tabla_brent_final$Precio_Brent != "TOTAL", ]
  colores_brent <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_brent))
  hi_global_brent <- round(as.numeric(datos_graf_brent$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_brent_local <- barplot(
  datos_graf_brent$hi,
  names.arg = datos_graf_brent$Precio_Brent,
  col = colores_brent,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, max(datos_graf_brent$hi) * 1.15),
  main = "Diagrama Porcentual (Escala Local) - Precio Brent",
  ylab = "Porcentaje Local (%)"
)
text(x = bp_brent_local, y = datos_graf_brent$hi, labels = paste0(datos_graf_brent$hi, "%"), pos = 3, cex = 0.75, font = 2)

4.3- Diagrama de cantidad (escala global)

if (!exists("datos_graf_brent")) {
  datos_graf_brent <- tabla_brent_final[tabla_brent_final$Precio_Brent != "TOTAL", ]
  colores_brent <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_brent))
  hi_global_brent <- round(as.numeric(datos_graf_brent$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_brent_cant_glob <- barplot(
  datos_graf_brent$ni,
  names.arg = datos_graf_brent$Precio_Brent,
  col = colores_brent,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, nrow(datos)),
  main = "Diagrama de Cantidad (Escala Global) - Precio Brent",
  ylab = "Frecuencia Absoluta (ni)"
)
text(x = bp_brent_cant_glob, y = datos_graf_brent$ni, labels = datos_graf_brent$ni, pos = 3, cex = 0.75, font = 2)

4.4- Diagrama porcentual (escala global)

if (!exists("datos_graf_brent")) {
  datos_graf_brent <- tabla_brent_final[tabla_brent_final$Precio_Brent != "TOTAL", ]
  colores_brent <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_brent))
  hi_global_brent <- round(as.numeric(datos_graf_brent$ni) / nrow(datos) * 100, 2)
}

par(mar = c(8, 5, 4, 2))
bp_brent_porc_glob <- barplot(
  hi_global_brent,
  names.arg = datos_graf_brent$Precio_Brent,
  col = colores_brent,
  border = "black",
  las = 2,
  cex.names = 0.85,
  ylim = c(0, 100),
  main = "Diagrama Porcentual (Escala Global) - Precio Brent",
  ylab = "Porcentaje Global (%)"
)
text(x = bp_brent_porc_glob, y = hi_global_brent, labels = paste0(hi_global_brent, "%"), pos = 3, cex = 0.75, font = 2)

Gráfico circular

if (!exists("datos_graf_brent")) {
  datos_graf_brent <- tabla_brent_final[tabla_brent_final$Precio_Brent != "TOTAL", ]
  colores_brent <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_brent))
  hi_global_brent <- round(as.numeric(datos_graf_brent$ni) / nrow(datos) * 100, 2)
}

par(mar = c(2, 2, 4, 13), xpd = TRUE)
pie(
  datos_graf_brent$hi,
  labels = paste0(datos_graf_brent$hi, "%"),
  col = colores_brent,
  border = "white",
  radius = 0.75,
  main = "Distribución Porcentual del Precio Brent",
  cex = 0.8
)
legend(
  "topright",
  inset = c(-0.42, 0.05),
  legend = datos_graf_brent$Precio_Brent,
  fill = colores_brent,
  cex = 0.72,
  title = "USD / barril"
)

CONCLUSIÓN

Los resultados muestran que el rango de 60,01 a 75,00 USD por barril presenta la mayor frecuencia relativa, con un 42,90 %, seguido del rango de 40,01 a 50,00 USD, con un 28,16 %. Esto evidencia que, en el proyecto Modelo de uso de agua en la minería de Estados Unidos, 2019–2020, la mayoría de los registros se concentra en períodos en los que el precio del petróleo Brent se ubicó entre 60,01 y 75,00 USD por barril.