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 LATITUDE

# --- LATITUDE (Variable 12) ---
if ("LATITUDE" %in% names(datos)) {
  lat_vals <- suppressWarnings(as.numeric(datos$LATITUDE))
} else if ("latitude" %in% names(datos)) {
  lat_vals <- suppressWarnings(as.numeric(datos$latitude))
} else {
  stop("No se encontró 'LATITUDE' ni 'latitude' en 'datos'.")
}
lat_vals <- na.omit(lat_vals)

cortes_lat <- c(15, 25, 35, 45, 55, 65, 75)
etiquetas_lat <- c(
  "15,00 - 25,00", "25,01 - 35,00", "35,01 - 45,00",
  "45,01 - 55,00", "55,01 - 65,00", "65,01 - 75,00"
)
lat_cat <- cut(lat_vals, breaks = cortes_lat, labels = etiquetas_lat, include.lowest = TRUE)

ni_lat <- table(lat_cat)
TDF_12 <- data.frame(categoria = names(ni_lat), ni = as.numeric(ni_lat), stringsAsFactors = FALSE)
TDF_12 <- TDF_12[TDF_12$ni > 0, ]

total_lat <- sum(TDF_12$ni)
hi_lat <- round(TDF_12$ni / total_lat * 100, 2)
hi_lat[which.max(hi_lat)] <- round(hi_lat[which.max(hi_lat)] + (100 - sum(hi_lat)), 2)

Ni_Asc_lat <- cumsum(TDF_12$ni)
Hi_Asc_lat <- round(cumsum(hi_lat), 2)
Hi_Asc_lat[length(Hi_Asc_lat)] <- 100.00
Ni_Desc_lat <- rev(cumsum(rev(TDF_12$ni)))
Hi_Desc_lat <- round(rev(cumsum(rev(hi_lat))), 2)

tabla_lat <- data.frame(
  Latitud_WGS84 = TDF_12$categoria,
  ni = TDF_12$ni,
  hi = hi_lat,
  Ni_Asc = as.numeric(Ni_Asc_lat),
  Hi_Asc = Hi_Asc_lat,
  Ni_Desc = as.numeric(Ni_Desc_lat),
  Hi_Desc = Hi_Desc_lat,
  stringsAsFactors = FALSE
)

fila_total_lat <- data.frame(
  Latitud_WGS84 = "TOTAL",
  ni = sum(tabla_lat$ni),
  hi = round(sum(tabla_lat$hi), 2),
  Ni_Asc = "-",
  Hi_Asc = "-",
  Ni_Desc = "-",
  Hi_Desc = "-",
  stringsAsFactors = FALSE
)

tabla_lat_final <- rbind(tabla_lat, fila_total_lat)

datos_graf_lat <- tabla_lat_final[tabla_lat_final$Latitud_WGS84 != "TOTAL", ]
colores_lat <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lat))
hi_global_lat <- round(as.numeric(datos_graf_lat$ni) / nrow(datos) * 100, 2)

Tabla_Lat <- tabla_lat_final %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°. 12*"),
    subtitle = md("**Tabla de distribución de frecuencias de la latitud 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 = Latitud_WGS84 == "TOTAL")
  )

Tabla_Lat
Tabla N°. 12
Tabla de distribución de frecuencias de la latitud geográfica del yacimiento (GCS WGS84)
Latitud_WGS84 ni hi Ni_Asc Hi_Asc Ni_Desc Hi_Desc
15,00 - 25,00 774 0.23 774 0.23 330466 100
25,01 - 35,00 70396 21.30 71170 21.53 329692 99.77
35,01 - 45,00 217600 65.85 288770 87.38 259296 78.47
45,01 - 55,00 37817 11.44 326587 98.82 41696 12.62
55,01 - 65,00 3588 1.09 330175 99.91 3879 1.18
65,01 - 75,00 291 0.09 330466 100 291 0.09
TOTAL 330466 100.00 - - - -
Fuente: Dataset Mining_model_input_data

GRÁFICOS LATITUDE

Frecuencia absoluta (ni)

if (!exists("datos_graf_lat")) {
  datos_graf_lat <- tabla_lat_final[tabla_lat_final$Latitud_WGS84 != "TOTAL", ]
  colores_lat <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lat))
  hi_global_lat <- round(as.numeric(datos_graf_lat$ni) / nrow(datos) * 100, 2)
}

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

4.2- Diagrama porcentual (escala local)

if (!exists("datos_graf_lat")) {
  datos_graf_lat <- tabla_lat_final[tabla_lat_final$Latitud_WGS84 != "TOTAL", ]
  colores_lat <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lat))
  hi_global_lat <- round(as.numeric(datos_graf_lat$ni) / nrow(datos) * 100, 2)
}

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

4.3- Diagrama de cantidad (escala global)

if (!exists("datos_graf_lat")) {
  datos_graf_lat <- tabla_lat_final[tabla_lat_final$Latitud_WGS84 != "TOTAL", ]
  colores_lat <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lat))
  hi_global_lat <- round(as.numeric(datos_graf_lat$ni) / nrow(datos) * 100, 2)
}

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

4.4- Diagrama porcentual (escala global)

if (!exists("datos_graf_lat")) {
  datos_graf_lat <- tabla_lat_final[tabla_lat_final$Latitud_WGS84 != "TOTAL", ]
  colores_lat <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lat))
  hi_global_lat <- round(as.numeric(datos_graf_lat$ni) / nrow(datos) * 100, 2)
}

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

Gráfico circular

if (!exists("datos_graf_lat")) {
  datos_graf_lat <- tabla_lat_final[tabla_lat_final$Latitud_WGS84 != "TOTAL", ]
  colores_lat <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_lat))
  hi_global_lat <- round(as.numeric(datos_graf_lat$ni) / nrow(datos) * 100, 2)
}

valores_lat <- as.numeric(datos_graf_lat$hi)
etiquetas_pie_lat <- ifelse(valores_lat >= 3, paste0(valores_lat, "%"), "")

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_lat,
  labels = etiquetas_pie_lat,
  col = colores_lat,
  border = "white",
  radius = 0.78,
  main = "Distribución Porcentual - Latitud (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_lat$Latitud_WGS84, " (", valores_lat, "%)"),
  fill = colores_lat,
  border = "black",
  cex = 0.78,
  bty = "o",
  title = "Latitud (WGS84)"
)

layout(1)

CONCLUSIÓN

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