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_rds <- "datos_proyecto.rds"
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_rds)) {
datos <- readRDS(ruta_rds)
} else 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ó datos_proyecto.rds, Mining_model_input_data.zip ni Mining_model_input_data.csv.")
}
# --- EIA_natural_gas_working_inventory_us_total (Variable 33 / gas_inv) ---
if ("EIA_natural_gas_working_inventory_us_total" %in% names(datos)) {
inv_gas <- suppressWarnings(as.numeric(datos$EIA_natural_gas_working_inventory_us_total))
} else if ("gas_inv" %in% names(datos)) {
inv_gas <- suppressWarnings(as.numeric(datos$gas_inv))
} else {
stop("No se encontró 'EIA_natural_gas_working_inventory_us_total' ni 'gas_inv' en 'datos'.")
}
inv_gas <- na.omit(inv_gas)
cortes_gas <- c(1100, 1500, 2000, 2500, 3000, 3500, 4000)
etiquetas_gas <- c(
"1.100,00 - 1.500,00", "1.500,01 - 2.000,00", "2.000,01 - 2.500,00",
"2.500,01 - 3.000,00", "3.000,01 - 3.500,00", "3.500,01 - 4.000,00"
)
gas_cat <- cut(inv_gas, breaks = cortes_gas, labels = etiquetas_gas, include.lowest = TRUE)
ni_gas <- table(gas_cat)
TDF_33 <- data.frame(categoria = names(ni_gas), ni = as.numeric(ni_gas), stringsAsFactors = FALSE)
TDF_33 <- TDF_33[TDF_33$ni > 0, ]
total_gas <- sum(TDF_33$ni)
hi_gas <- round(TDF_33$ni / total_gas * 100, 2)
hi_gas[which.max(hi_gas)] <- round(hi_gas[which.max(hi_gas)] + (100 - sum(hi_gas)), 2)
Ni_Asc_gas <- cumsum(TDF_33$ni)
Hi_Asc_gas <- round(cumsum(hi_gas), 2)
Hi_Asc_gas[length(Hi_Asc_gas)] <- 100.00
Ni_Desc_gas <- rev(cumsum(rev(TDF_33$ni)))
Hi_Desc_gas <- round(rev(cumsum(rev(hi_gas))), 2)
tabla_gas <- data.frame(
Inventario_Gas_Bcf = TDF_33$categoria,
ni = TDF_33$ni,
hi = hi_gas,
Ni_Asc = as.numeric(Ni_Asc_gas),
Hi_Asc = Hi_Asc_gas,
Ni_Desc = as.numeric(Ni_Desc_gas),
Hi_Desc = Hi_Desc_gas,
stringsAsFactors = FALSE
)
fila_total_gas <- data.frame(
Inventario_Gas_Bcf = "TOTAL",
ni = sum(tabla_gas$ni),
hi = round(sum(tabla_gas$hi), 2),
Ni_Asc = "-",
Hi_Asc = "-",
Ni_Desc = "-",
Hi_Desc = "-",
stringsAsFactors = FALSE
)
tabla_gas_final <- rbind(tabla_gas, fila_total_gas)
datos_graf_gas <- tabla_gas_final[tabla_gas_final$Inventario_Gas_Bcf != "TOTAL", ]
colores_gas <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_gas))
hi_global_gas <- round(as.numeric(datos_graf_gas$ni) / nrow(datos) * 100, 2)
Tabla_Gas <- tabla_gas_final %>%
gt() %>%
tab_header(
title = md("*Tabla N°. 33*"),
subtitle = md("**Tabla de distribución de frecuencias del inventario total de gas natural activo en EE. UU. (Bcf)**")
) %>%
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 = Inventario_Gas_Bcf == "TOTAL")
)
Tabla_Gas
| Tabla N°. 33 | ||||||
| Tabla de distribución de frecuencias del inventario total de gas natural activo en EE. UU. (Bcf) | ||||||
| Inventario_Gas_Bcf | ni | hi | Ni_Asc | Hi_Asc | Ni_Desc | Hi_Desc |
|---|---|---|---|---|---|---|
| 1.100,00 - 1.500,00 | 27527 | 8.33 | 27527 | 8.33 | 330466 | 100 |
| 1.500,01 - 2.000,00 | 28219 | 8.54 | 55746 | 16.87 | 302939 | 91.67 |
| 2.000,01 - 2.500,00 | 69226 | 20.95 | 124972 | 37.82 | 274720 | 83.13 |
| 2.500,01 - 3.000,00 | 55547 | 16.81 | 180519 | 54.63 | 205494 | 62.18 |
| 3.000,01 - 3.500,00 | 68007 | 20.58 | 248526 | 75.21 | 149947 | 45.37 |
| 3.500,01 - 4.000,00 | 81940 | 24.79 | 330466 | 100 | 81940 | 24.79 |
| TOTAL | 330466 | 100.00 | - | - | - | - |
| Fuente: Dataset Mining_model_input_data | ||||||
if (!exists("datos_graf_gas")) {
datos_graf_gas <- tabla_gas_final[tabla_gas_final$Inventario_Gas_Bcf != "TOTAL", ]
colores_gas <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_gas))
hi_global_gas <- round(as.numeric(datos_graf_gas$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 6.5, 4, 2), mgp = c(4.8, 1, 0), las = 2)
bp_gas_ni <- barplot(
datos_graf_gas$ni,
names.arg = datos_graf_gas$Inventario_Gas_Bcf,
col = colores_gas,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, max(datos_graf_gas$ni) * 1.18),
main = "Distribución del Inventario de Gas Natural Activo (Bcf)",
ylab = "Frecuencia Absoluta (ni)"
)
text(
x = bp_gas_ni,
y = datos_graf_gas$ni + (max(datos_graf_gas$ni) * 0.015),
labels = datos_graf_gas$ni,
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_gas")) {
datos_graf_gas <- tabla_gas_final[tabla_gas_final$Inventario_Gas_Bcf != "TOTAL", ]
colores_gas <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_gas))
hi_global_gas <- round(as.numeric(datos_graf_gas$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_gas_local <- barplot(
datos_graf_gas$hi,
names.arg = datos_graf_gas$Inventario_Gas_Bcf,
col = colores_gas,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, max(datos_graf_gas$hi) * 1.18),
main = "Diagrama Porcentual (Escala Local) - Inventario de Gas Natural (Bcf)",
ylab = "Porcentaje Local (%)"
)
text(
x = bp_gas_local,
y = datos_graf_gas$hi + (max(datos_graf_gas$hi) * 0.015),
labels = paste0(datos_graf_gas$hi, "%"),
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_gas")) {
datos_graf_gas <- tabla_gas_final[tabla_gas_final$Inventario_Gas_Bcf != "TOTAL", ]
colores_gas <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_gas))
hi_global_gas <- round(as.numeric(datos_graf_gas$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 7.5, 4, 2), mgp = c(5.2, 1, 0), las = 2)
bp_gas_cant_glob <- barplot(
datos_graf_gas$ni,
names.arg = datos_graf_gas$Inventario_Gas_Bcf,
col = colores_gas,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, nrow(datos) * 1.12),
main = "Diagrama de Cantidad (Escala Global) - Inventario de Gas Natural (Bcf)",
ylab = "Frecuencia Absoluta (ni)"
)
text(
x = bp_gas_cant_glob,
y = datos_graf_gas$ni + (nrow(datos) * 0.015),
labels = datos_graf_gas$ni,
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_gas")) {
datos_graf_gas <- tabla_gas_final[tabla_gas_final$Inventario_Gas_Bcf != "TOTAL", ]
colores_gas <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_gas))
hi_global_gas <- round(as.numeric(datos_graf_gas$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_gas_porc_glob <- barplot(
hi_global_gas,
names.arg = datos_graf_gas$Inventario_Gas_Bcf,
col = colores_gas,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, 100),
main = "Diagrama Porcentual (Escala Global) - Inventario de Gas Natural (Bcf)",
ylab = "Porcentaje Global (%)"
)
text(
x = bp_gas_porc_glob,
y = hi_global_gas + 1.5,
labels = paste0(hi_global_gas, "%"),
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_gas")) {
datos_graf_gas <- tabla_gas_final[tabla_gas_final$Inventario_Gas_Bcf != "TOTAL", ]
colores_gas <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_gas))
hi_global_gas <- round(as.numeric(datos_graf_gas$ni) / nrow(datos) * 100, 2)
}
valores_gas <- as.numeric(datos_graf_gas$hi)
etiquetas_pie_gas <- ifelse(valores_gas >= 3, paste0(valores_gas, "%"), "")
layout(matrix(c(1, 2), nrow = 1), widths = c(1.35, 1))
par(mar = c(2, 2, 4, 1), xpd = TRUE, las = 1)
pie(
valores_gas,
labels = etiquetas_pie_gas,
col = colores_gas,
border = "white",
radius = 0.78,
main = "Distribución Porcentual - Inventario de Gas Natural (Bcf)",
cex = 0.85,
font = 2
)
par(mar = c(2, 0.5, 4, 2), xpd = TRUE, las = 1)
plot.new()
legend(
"left",
legend = paste0(datos_graf_gas$Inventario_Gas_Bcf, " (", valores_gas, "%)"),
fill = colores_gas,
border = "black",
cex = 0.78,
bty = "o",
title = "Gas Natural (Bcf)"
)
layout(1)
El análisis de
EIA_natural_gas_working_inventory_us_total
(gas_inv) permite observar cómo se distribuyen los
registros válidos del inventario total de gas natural activo en EE. UU.
(en Bcf) entre los distintos intervalos, contrastando su proporción
local frente a la escala global respecto al total de registros del
dataset.