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 CAL_YR

# --- CAL_YR (Variable 18) ---
if ("CAL_YR" %in% names(datos)) {
  cal_yr_vals <- suppressWarnings(as.numeric(datos$CAL_YR))
} else if ("cal_yr" %in% names(datos)) {
  cal_yr_vals <- suppressWarnings(as.numeric(datos$cal_yr))
} else {
  stop("No se encontró 'CAL_YR' ni 'cal_yr' en 'datos'.")
}
cal_yr_vals <- na.omit(cal_yr_vals)

cortes_yr <- c(2018.5, 2019.5, 2020.5)
etiquetas_yr <- c("2019", "2020")
yr_cat <- cut(cal_yr_vals, breaks = cortes_yr, labels = etiquetas_yr, include.lowest = TRUE)

ni_yr <- table(yr_cat)
TDF_18 <- data.frame(categoria = names(ni_yr), ni = as.numeric(ni_yr), stringsAsFactors = FALSE)
TDF_18 <- TDF_18[TDF_18$ni > 0, ]

total_yr <- sum(TDF_18$ni)
hi_yr <- round(TDF_18$ni / total_yr * 100, 2)
hi_yr[which.max(hi_yr)] <- round(hi_yr[which.max(hi_yr)] + (100 - sum(hi_yr)), 2)

Ni_Asc_yr <- cumsum(TDF_18$ni)
Hi_Asc_yr <- round(cumsum(hi_yr), 2)
Hi_Asc_yr[length(Hi_Asc_yr)] <- 100.00
Ni_Desc_yr <- rev(cumsum(rev(TDF_18$ni)))
Hi_Desc_yr <- round(rev(cumsum(rev(hi_yr))), 2)

tabla_yr <- data.frame(
  Anio_Calendario = TDF_18$categoria,
  ni = TDF_18$ni,
  hi = hi_yr,
  Ni_Asc = as.numeric(Ni_Asc_yr),
  Hi_Asc = Hi_Asc_yr,
  Ni_Desc = as.numeric(Ni_Desc_yr),
  Hi_Desc = Hi_Desc_yr,
  stringsAsFactors = FALSE
)

fila_total_yr <- data.frame(
  Anio_Calendario = "TOTAL",
  ni = sum(tabla_yr$ni),
  hi = round(sum(tabla_yr$hi), 2),
  Ni_Asc = "-",
  Hi_Asc = "-",
  Ni_Desc = "-",
  Hi_Desc = "-",
  stringsAsFactors = FALSE
)

tabla_yr_final <- rbind(tabla_yr, fila_total_yr)

datos_graf_yr <- tabla_yr_final[tabla_yr_final$Anio_Calendario != "TOTAL", ]
colores_yr <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_yr))
hi_global_yr <- round(as.numeric(datos_graf_yr$ni) / nrow(datos) * 100, 2)

Tabla_Yr <- tabla_yr_final %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°. 18*"),
    subtitle = md("**Tabla de distribución de frecuencias del año calendario del reporte (CAL_YR)**")
  ) %>%
  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 = Anio_Calendario == "TOTAL")
  )

Tabla_Yr
Tabla N°. 18
Tabla de distribución de frecuencias del año calendario del reporte (CAL_YR)
Anio_Calendario ni hi Ni_Asc Hi_Asc Ni_Desc Hi_Desc
2019 171040 51.76 171040 51.76 330466 100
2020 159426 48.24 330466 100 159426 48.24
TOTAL 330466 100.00 - - - -
Fuente: Dataset Mining_model_input_data

GRÁFICOS CAL_YR

Frecuencia absoluta (ni)

if (!exists("datos_graf_yr")) {
  datos_graf_yr <- tabla_yr_final[tabla_yr_final$Anio_Calendario != "TOTAL", ]
  colores_yr <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_yr))
  hi_global_yr <- round(as.numeric(datos_graf_yr$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 6.5, 4, 2), mgp = c(4.8, 1, 0), las = 2)
bp_yr_ni <- barplot(
  datos_graf_yr$ni,
  names.arg = datos_graf_yr$Anio_Calendario,
  col = colores_yr,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, max(datos_graf_yr$ni) * 1.18),
  main = "Distribución por Año Calendario del Reporte (CAL_YR)",
  ylab = "Frecuencia Absoluta (ni)"
)
text(
  x = bp_yr_ni,
  y = datos_graf_yr$ni + (max(datos_graf_yr$ni) * 0.015),
  labels = datos_graf_yr$ni,
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

4.2- Diagrama porcentual (escala local)

if (!exists("datos_graf_yr")) {
  datos_graf_yr <- tabla_yr_final[tabla_yr_final$Anio_Calendario != "TOTAL", ]
  colores_yr <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_yr))
  hi_global_yr <- round(as.numeric(datos_graf_yr$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_yr_local <- barplot(
  datos_graf_yr$hi,
  names.arg = datos_graf_yr$Anio_Calendario,
  col = colores_yr,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, max(datos_graf_yr$hi) * 1.18),
  main = "Diagrama Porcentual (Escala Local) - Año Calendario (CAL_YR)",
  ylab = "Porcentaje Local (%)"
)
text(
  x = bp_yr_local,
  y = datos_graf_yr$hi + (max(datos_graf_yr$hi) * 0.015),
  labels = paste0(datos_graf_yr$hi, "%"),
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

4.3- Diagrama de cantidad (escala global)

if (!exists("datos_graf_yr")) {
  datos_graf_yr <- tabla_yr_final[tabla_yr_final$Anio_Calendario != "TOTAL", ]
  colores_yr <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_yr))
  hi_global_yr <- round(as.numeric(datos_graf_yr$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 7.5, 4, 2), mgp = c(5.2, 1, 0), las = 2)
bp_yr_cant_glob <- barplot(
  datos_graf_yr$ni,
  names.arg = datos_graf_yr$Anio_Calendario,
  col = colores_yr,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, nrow(datos) * 1.12),
  main = "Diagrama de Cantidad (Escala Global) - Año Calendario (CAL_YR)",
  ylab = "Frecuencia Absoluta (ni)"
)
text(
  x = bp_yr_cant_glob,
  y = datos_graf_yr$ni + (nrow(datos) * 0.015),
  labels = datos_graf_yr$ni,
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

4.4- Diagrama porcentual (escala global)

if (!exists("datos_graf_yr")) {
  datos_graf_yr <- tabla_yr_final[tabla_yr_final$Anio_Calendario != "TOTAL", ]
  colores_yr <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_yr))
  hi_global_yr <- round(as.numeric(datos_graf_yr$ni) / nrow(datos) * 100, 2)
}

par(mar = c(9, 5.5, 4, 2), mgp = c(3.8, 1, 0), las = 2)
bp_yr_porc_glob <- barplot(
  hi_global_yr,
  names.arg = datos_graf_yr$Anio_Calendario,
  col = colores_yr,
  border = "black",
  cex.names = 0.85,
  cex.axis = 0.85,
  ylim = c(0, 100),
  main = "Diagrama Porcentual (Escala Global) - Año Calendario (CAL_YR)",
  ylab = "Porcentaje Global (%)"
)
text(
  x = bp_yr_porc_glob,
  y = hi_global_yr + 1.5,
  labels = paste0(hi_global_yr, "%"),
  srt = 90,
  adj = c(0, 0.5),
  cex = 0.75,
  font = 2
)

Gráfico circular

if (!exists("datos_graf_yr")) {
  datos_graf_yr <- tabla_yr_final[tabla_yr_final$Anio_Calendario != "TOTAL", ]
  colores_yr <- colorRampPalette(c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"))(nrow(datos_graf_yr))
  hi_global_yr <- round(as.numeric(datos_graf_yr$ni) / nrow(datos) * 100, 2)
}

valores_yr <- as.numeric(datos_graf_yr$hi)
etiquetas_pie_yr <- ifelse(valores_yr >= 3, paste0(valores_yr, "%"), "")

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_yr,
  labels = etiquetas_pie_yr,
  col = colores_yr,
  border = "white",
  radius = 0.78,
  main = "Distribución Porcentual - Año Calendario (CAL_YR)",
  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_yr$Anio_Calendario, " (", valores_yr, "%)"),
  fill = colores_yr,
  border = "black",
  cex = 0.78,
  bty = "o",
  title = "Año (AAAA)"
)

layout(1)

CONCLUSIÓN

El análisis de CAL_YR permite observar cómo se distribuyen los registros válidos del año calendario de reporte entre 2019 y 2020, contrastando su proporción local frente a la escala global respecto al total de registros del dataset[cite: 1].