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_total_marketed_production_x (Variable 34 / gas_prod) ---
if ("EIA_natural_gas_total_marketed_production_x" %in% names(datos)) {
prod_gas <- suppressWarnings(as.numeric(datos$EIA_natural_gas_total_marketed_production_x))
} else if ("gas_prod" %in% names(datos)) {
prod_gas <- suppressWarnings(as.numeric(datos$gas_prod))
} else {
stop("No se encontró 'EIA_natural_gas_total_marketed_production_x' ni 'gas_prod' en 'datos'.")
}
prod_gas <- na.omit(prod_gas)
cortes_prod <- c(94, 96, 98, 100, 102, 104, 106)
etiquetas_prod <- c(
"94,0000 - 96,0000", "96,0001 - 98,0000", "98,0001 - 100,0000",
"100,0001 - 102,0000", "102,0001 - 104,0000", "104,0001 - 106,0000"
)
prod_cat <- cut(prod_gas, breaks = cortes_prod, labels = etiquetas_prod, include.lowest = TRUE)
ni_prod <- table(prod_cat)
TDF_34 <- data.frame(categoria = names(ni_prod), ni = as.numeric(ni_prod), stringsAsFactors = FALSE)
TDF_34 <- TDF_34[TDF_34$ni > 0, ]
total_prod <- sum(TDF_34$ni)
hi_prod <- round(TDF_34$ni / total_prod * 100, 2)
hi_prod[which.max(hi_prod)] <- round(hi_prod[which.max(hi_prod)] + (100 - sum(hi_prod)), 2)
Ni_Asc_prod <- cumsum(TDF_34$ni)
Hi_Asc_prod <- round(cumsum(hi_prod), 2)
Hi_Asc_prod[length(Hi_Asc_prod)] <- 100.00
Ni_Desc_prod <- rev(cumsum(rev(TDF_34$ni)))
Hi_Desc_prod <- round(rev(cumsum(rev(hi_prod))), 2)
tabla_prod <- data.frame(
Produccion_Gas_EIA = TDF_34$categoria,
ni = TDF_34$ni,
hi = hi_prod,
Ni_Asc = as.numeric(Ni_Asc_prod),
Hi_Asc = Hi_Asc_prod,
Ni_Desc = as.numeric(Ni_Desc_prod),
Hi_Desc = Hi_Desc_prod,
stringsAsFactors = FALSE
)
fila_total_prod <- data.frame(
Produccion_Gas_EIA = "TOTAL",
ni = sum(tabla_prod$ni),
hi = round(sum(tabla_prod$hi), 2),
Ni_Asc = "-",
Hi_Asc = "-",
Ni_Desc = "-",
Hi_Desc = "-",
stringsAsFactors = FALSE
)
tabla_prod_final <- rbind(tabla_prod, fila_total_prod)
datos_graf_prod <- tabla_prod_final[tabla_prod_final$Produccion_Gas_EIA != "TOTAL", ]
colores_prod <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_prod))
hi_global_prod <- round(as.numeric(datos_graf_prod$ni) / nrow(datos) * 100, 2)
Tabla_Prod <- tabla_prod_final %>%
gt() %>%
tab_header(
title = md("*Tabla N°. 34*"),
subtitle = md("**Tabla de distribución de frecuencias de la producción comercializada total de gas natural (EIA)**")
) %>%
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 = Produccion_Gas_EIA == "TOTAL")
)
Tabla_Prod
| Tabla N°. 34 | ||||||
| Tabla de distribución de frecuencias de la producción comercializada total de gas natural (EIA) | ||||||
| Produccion_Gas_EIA | ni | hi | Ni_Asc | Hi_Asc | Ni_Desc | Hi_Desc |
|---|---|---|---|---|---|---|
| 94,0000 - 96,0000 | 26683 | 8.07 | 26683 | 8.07 | 330466 | 100 |
| 96,0001 - 98,0000 | 94978 | 28.75 | 121661 | 36.82 | 303783 | 91.93 |
| 98,0001 - 100,0000 | 70564 | 21.35 | 192225 | 58.17 | 208805 | 63.18 |
| 100,0001 - 102,0000 | 42418 | 12.84 | 234643 | 71.01 | 138241 | 41.83 |
| 102,0001 - 104,0000 | 54345 | 16.44 | 288988 | 87.45 | 95823 | 28.99 |
| 104,0001 - 106,0000 | 41478 | 12.55 | 330466 | 100 | 41478 | 12.55 |
| TOTAL | 330466 | 100.00 | - | - | - | - |
| Fuente: Dataset Mining_model_input_data | ||||||
if (!exists("datos_graf_prod")) {
datos_graf_prod <- tabla_prod_final[tabla_prod_final$Produccion_Gas_EIA != "TOTAL", ]
colores_prod <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_prod))
hi_global_prod <- round(as.numeric(datos_graf_prod$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 6.5, 4, 2), mgp = c(4.8, 1, 0), las = 2)
bp_prod_ni <- barplot(
datos_graf_prod$ni,
names.arg = datos_graf_prod$Produccion_Gas_EIA,
col = colores_prod,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, max(datos_graf_prod$ni) * 1.18),
main = "Distribución de Producción Comercializada de Gas Natural (EIA)",
ylab = "Frecuencia Absoluta (ni)"
)
text(
x = bp_prod_ni,
y = datos_graf_prod$ni + (max(datos_graf_prod$ni) * 0.015),
labels = datos_graf_prod$ni,
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_prod")) {
datos_graf_prod <- tabla_prod_final[tabla_prod_final$Produccion_Gas_EIA != "TOTAL", ]
colores_prod <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_prod))
hi_global_prod <- round(as.numeric(datos_graf_prod$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_prod_local <- barplot(
datos_graf_prod$hi,
names.arg = datos_graf_prod$Produccion_Gas_EIA,
col = colores_prod,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, max(datos_graf_prod$hi) * 1.18),
main = "Diagrama Porcentual (Escala Local) - Producción de Gas Natural (EIA)",
ylab = "Porcentaje Local (%)"
)
text(
x = bp_prod_local,
y = datos_graf_prod$hi + (max(datos_graf_prod$hi) * 0.015),
labels = paste0(datos_graf_prod$hi, "%"),
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_prod")) {
datos_graf_prod <- tabla_prod_final[tabla_prod_final$Produccion_Gas_EIA != "TOTAL", ]
colores_prod <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_prod))
hi_global_prod <- round(as.numeric(datos_graf_prod$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 7.5, 4, 2), mgp = c(5.2, 1, 0), las = 2)
bp_prod_cant_glob <- barplot(
datos_graf_prod$ni,
names.arg = datos_graf_prod$Produccion_Gas_EIA,
col = colores_prod,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, nrow(datos) * 1.12),
main = "Diagrama de Cantidad (Escala Global) - Producción de Gas Natural (EIA)",
ylab = "Frecuencia Absoluta (ni)"
)
text(
x = bp_prod_cant_glob,
y = datos_graf_prod$ni + (nrow(datos) * 0.015),
labels = datos_graf_prod$ni,
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_prod")) {
datos_graf_prod <- tabla_prod_final[tabla_prod_final$Produccion_Gas_EIA != "TOTAL", ]
colores_prod <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_prod))
hi_global_prod <- round(as.numeric(datos_graf_prod$ni) / nrow(datos) * 100, 2)
}
par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_prod_porc_glob <- barplot(
hi_global_prod,
names.arg = datos_graf_prod$Produccion_Gas_EIA,
col = colores_prod,
border = "black",
cex.names = 0.85,
cex.axis = 0.85,
ylim = c(0, 100),
main = "Diagrama Porcentual (Escala Global) - Producción de Gas Natural (EIA)",
ylab = "Porcentaje Global (%)"
)
text(
x = bp_prod_porc_glob,
y = hi_global_prod + 1.5,
labels = paste0(hi_global_prod, "%"),
srt = 90,
adj = c(0, 0.5),
cex = 0.75,
font = 2
)
if (!exists("datos_graf_prod")) {
datos_graf_prod <- tabla_prod_final[tabla_prod_final$Produccion_Gas_EIA != "TOTAL", ]
colores_prod <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_prod))
hi_global_prod <- round(as.numeric(datos_graf_prod$ni) / nrow(datos) * 100, 2)
}
valores_prod <- as.numeric(datos_graf_prod$hi)
etiquetas_pie_prod <- ifelse(valores_prod >= 3, paste0(valores_prod, "%"), "")
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_prod,
labels = etiquetas_pie_prod,
col = colores_prod,
border = "white",
radius = 0.78,
main = "Distribución Porcentual - Producción de Gas Natural (EIA)",
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_prod$Produccion_Gas_EIA, " (", valores_prod, "%)"),
fill = colores_prod,
border = "black",
cex = 0.78,
bty = "o",
title = "Producción Gas (EIA)"
)
layout(1)
El análisis de
EIA_natural_gas_total_marketed_production_x
(gas_prod) permite observar cómo se distribuyen los
registros válidos de la producción comercializada total de gas natural
(EIA) entre los distintos intervalos, contrastando su proporción local
frente a la escala global respecto al total de registros del
dataset.