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_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.")
}

TABLA DE DISTRIBUCIÓN LONGITUDE

# --- LONGITUDE (Variable 11) ---
if ("LONGITUDE" %in% names(datos)) {
  lon_vals <- suppressWarnings(as.numeric(datos$LONGITUDE))
} else if ("longitude" %in% names(datos)) {
  lon_vals <- suppressWarnings(as.numeric(datos$longitude))
} else {
  stop("No se encontró 'LONGITUDE' ni 'longitude' en 'datos'.")
}
lon_vals <- na.omit(lon_vals)

cortes_lon <- c(-170, -150, -130, -110, -90, -75, -65)
etiquetas_lon <- c(
  "-170,00 - -150,00", "-150,01 - -130,00", "-130,01 - -110,00",
  "-110,01 - -90,00", "-90,01 - -75,00", "-75,01 - -65,00"
)
lon_cat <- cut(lon_vals, breaks = cortes_lon, labels = etiquetas_lon, include.lowest = TRUE)

ni_lon <- table(lon_cat)
TDF_11 <- data.frame(categoria = names(ni_lon), ni = as.numeric(ni_lon), stringsAsFactors = FALSE)
TDF_11 <- TDF_11[TDF_11$ni > 0, ]

total_lon <- sum(TDF_11$ni)
hi_lon <- round(TDF_11$ni / total_lon * 100, 2)
hi_lon[which.max(hi_lon)] <- round(hi_lon[which.max(hi_lon)] + (100 - sum(hi_lon)), 2)

Ni_Asc_lon <- cumsum(TDF_11$ni)
Hi_Asc_lon <- round(cumsum(hi_lon), 2)
Hi_Asc_lon[length(Hi_Asc_lon)] <- 100.00
Ni_Desc_lon <- rev(cumsum(rev(TDF_11$ni)))
Hi_Desc_lon <- round(rev(cumsum(rev(hi_lon))), 2)

tabla_lon <- data.frame(
  Longitud_WGS84 = TDF_11$categoria,
  ni = TDF_11$ni,
  hi = hi_lon,
  Ni_Asc = as.numeric(Ni_Asc_lon),
  Hi_Asc = Hi_Asc_lon,
  Ni_Desc = as.numeric(Ni_Desc_lon),
  Hi_Desc = Hi_Desc_lon,
  stringsAsFactors = FALSE
)

fila_total_lon <- data.frame(
  Longitud_WGS84 = "TOTAL",
  ni = sum(tabla_lon$ni),
  hi = round(sum(tabla_lon$hi), 2),
  Ni_Asc = "-",
  Hi_Asc = "-",
  Ni_Desc = "-",
  Hi_Desc = "-",
  stringsAsFactors = FALSE
)

tabla_lon_final <- rbind(tabla_lon, fila_total_lon)

datos_graf_lon <- tabla_lon_final[tabla_lon_final$Longitud_WGS84 != "TOTAL", ]
colores_lon <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lon))
hi_global_lon <- round(as.numeric(datos_graf_lon$ni) / nrow(datos) * 100, 2)

Tabla_Lon <- tabla_lon_final %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°. 11*"),
    subtitle = md("**Tabla de distribución de frecuencias de la longitud geográfica del yacimiento (GCS WGS84)**")
  ) %>%
  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 = Longitud_WGS84 == "TOTAL")
  )

Tabla_Lon
Tabla N°. 11
Tabla de distribución de frecuencias de la longitud geográfica del yacimiento (GCS WGS84)
Longitud_WGS84 ni hi Ni_Asc Hi_Asc Ni_Desc Hi_Desc
-170,00 - -150,00 2037 0.62 2037 0.62 330466 100
-150,01 - -130,00 2652 0.80 4689 1.42 328429 99.38
-130,01 - -110,00 53067 16.06 57756 17.48 325777 98.58
-110,01 - -90,00 123477 37.36 181233 54.84 272710 82.52
-90,01 - -75,00 120556 36.48 301789 91.32 149233 45.16
-75,01 - -65,00 28677 8.68 330466 100 28677 8.68
TOTAL 330466 100.00 - - - -
Fuente: Dataset Mining_model_input_data

GRÁFICOS LONGITUDE

Frecuencia absoluta (ni)

if (!exists("datos_graf_lon")) {
  datos_graf_lon <- tabla_lon_final[tabla_lon_final$Longitud_WGS84 != "TOTAL", ]
  colores_lon <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lon))
  hi_global_lon <- round(as.numeric(datos_graf_lon$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 6.5, 4, 2), mgp = c(4.8, 1, 0), las = 2)
bp_lon_ni <- barplot(
  datos_graf_lon$ni,
  names.arg = datos_graf_lon$Longitud_WGS84,
  col = colores_lon,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, max(datos_graf_lon$ni) * 1.18),
  main = "Distribución de Longitud Geográfica del Yacimiento (WGS84)",
  ylab = "Frecuencia Absoluta (ni)"
)
text(
  x = bp_lon_ni,
  y = datos_graf_lon$ni + (max(datos_graf_lon$ni) * 0.015),
  labels = datos_graf_lon$ni,
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

4.2- Diagrama porcentual (escala local)

if (!exists("datos_graf_lon")) {
  datos_graf_lon <- tabla_lon_final[tabla_lon_final$Longitud_WGS84 != "TOTAL", ]
  colores_lon <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lon))
  hi_global_lon <- round(as.numeric(datos_graf_lon$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_lon_local <- barplot(
  datos_graf_lon$hi,
  names.arg = datos_graf_lon$Longitud_WGS84,
  col = colores_lon,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, max(datos_graf_lon$hi) * 1.18),
  main = "Diagrama Porcentual (Escala Local) - Longitud (WGS84)",
  ylab = "Porcentaje Local (%)"
)
text(
  x = bp_lon_local,
  y = datos_graf_lon$hi + (max(datos_graf_lon$hi) * 0.015),
  labels = paste0(datos_graf_lon$hi, "%"),
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

4.3- Diagrama de cantidad (escala global)

if (!exists("datos_graf_lon")) {
  datos_graf_lon <- tabla_lon_final[tabla_lon_final$Longitud_WGS84 != "TOTAL", ]
  colores_lon <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lon))
  hi_global_lon <- round(as.numeric(datos_graf_lon$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 7.5, 4, 2), mgp = c(5.2, 1, 0), las = 2)
bp_lon_cant_glob <- barplot(
  datos_graf_lon$ni,
  names.arg = datos_graf_lon$Longitud_WGS84,
  col = colores_lon,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, nrow(datos) * 1.12),
  main = "Diagrama de Cantidad (Escala Global) - Longitud (WGS84)",
  ylab = "Frecuencia Absoluta (ni)"
)
text(
  x = bp_lon_cant_glob,
  y = datos_graf_lon$ni + (nrow(datos) * 0.015),
  labels = datos_graf_lon$ni,
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

4.4- Diagrama porcentual (escala global)

if (!exists("datos_graf_lon")) {
  datos_graf_lon <- tabla_lon_final[tabla_lon_final$Longitud_WGS84 != "TOTAL", ]
  colores_lon <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lon))
  hi_global_lon <- round(as.numeric(datos_graf_lon$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_lon_porc_glob <- barplot(
  hi_global_lon,
  names.arg = datos_graf_lon$Longitud_WGS84,
  col = colores_lon,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, 100),
  main = "Diagrama Porcentual (Escala Global) - Longitud (WGS84)",
  ylab = "Porcentaje Global (%)"
)
text(
  x = bp_lon_porc_glob,
  y = hi_global_lon + 1.5,
  labels = paste0(hi_global_lon, "%"),
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

Gráfico circular

if (!exists("datos_graf_lon")) {
  datos_graf_lon <- tabla_lon_final[tabla_lon_final$Longitud_WGS84 != "TOTAL", ]
  colores_lon <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lon))
  hi_global_lon <- round(as.numeric(datos_graf_lon$ni) / nrow(datos) * 100, 2)
}

valores_lon <- as.numeric(datos_graf_lon$hi)
etiquetas_pie_lon <- ifelse(valores_lon >= 3, paste0(valores_lon, "%"), "")

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_lon,
  labels = etiquetas_pie_lon,
  col = colores_lon,
  border = "white",
  radius = 0.78,
  main = "Distribución Porcentual - Longitud (WGS84)",
  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_lon$Longitud_WGS84, " (", valores_lon, "%)"),
  fill = colores_lon,
  border = "black",
  cex = 0.78,
  bty = "o",
  title = "Longitud (WGS84)"
)

layout(1)

CONCLUSIÓN

El análisis de LONGITUDE permite observar cómo se distribuyen los registros válidos de la longitud geográfica de los yacimientos (en grados decimales, GCS WGS84) entre los distintos rangos este-oeste, contrastando su proporción local frente a la escala global respecto al total de registros del dataset.