ANÁLISIS ESTADÍSTICO

CARGA DE DATOS Y LIBRERÍAS

CARGA DE DATOS

library(dplyr)
library(gt)

datos <- read.csv("D:/dataset_CMIO_geologico.csv")

ASIGNACION DE VARIABLES

ASIGNACION DE VARIABLES

df_ley <- data.frame(
  nivel_ley = trimws(toupper(datos$Ley_Categoria))
)
orden_ley <- c("BAJA", "MEDIA", "ALTA", "MUY ALTA")

df_ley$nivel_ley <- factor(
  df_ley$nivel_ley,
  levels = orden_ley,
  ordered = TRUE
)

TABLA DE DISTRIBUCIÓN DE CANTIDAD

TABLA DE DISTRIBUCION DE CANTIDAD

TDF_ley <- df_ley %>%
  count(nivel_ley, name = "ni") %>%
  arrange(nivel_ley) %>%
  mutate(hi = round(ni / sum(ni) * 100, 0))
tabla_ley <- TDF_ley %>%
  gt() %>%
  tab_header(
    title = "Tabla N° 1",
    subtitle = "Distribución de la Ley Mineral (Variable Ordinal)"
  )

tabla_ley
Tabla N° 1
Distribución de la Ley Mineral (Variable Ordinal)
nivel_ley ni hi
BAJA 1024 41
MEDIA 975 39
ALTA 501 20
# Agregamos fila TOTAL
tabla_final_ley <- TDF_ley %>%
  mutate(
    nivel_ley = as.character(nivel_ley)
  )

tabla_final_ley <- bind_rows(
  tabla_final_ley,
  data.frame(
    nivel_ley = "TOTAL",
    ni = sum(tabla_final_ley$ni),
    hi = sum(tabla_final_ley$hi)
  )
)

# TABLA ESQUELETO
tabla_ley_gt <- tabla_final_ley %>%

  gt() %>%

  tab_header(
    title = md("**Tabla Nº2**"),
    subtitle = md("Distribución ordinal de la ley mineral")
  ) %>%

  cols_label(
    nivel_ley = "Nivel de Ley",
    ni = "Frecuencia",
    hi = "Porcentaje (%)"
  ) %>%

  cols_align(
    align = "center",
    columns = everything()
  ) %>%

  fmt_number(
    columns = c(ni, hi),
    decimals = 0
  ) %>%

  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(
      rows = nivel_ley == "TOTAL"
    )
  ) %>%

  tab_source_note(
    source_note = md("Autor: Grupo 2")
  )

tabla_ley_gt
Tabla Nº2
Distribución ordinal de la ley mineral
Nivel de Ley Frecuencia Porcentaje (%)
BAJA 1,024 41
MEDIA 975 39
ALTA 501 20
TOTAL 2,500 100
Autor: Grupo 2

GRÁFICAS DE DISTRIBUCIÓN DE CANTIDAD

barplot(TDF_ley$ni,
        main = "Gráfica Nº1: Frecuencia de la Ley Mineral",
        xlab = "Nivel de Ley",
        ylab = "Cantidad (ni)",
        col = "steelblue",
        names.arg = TDF_ley$nivel_ley,
        cex.names = 0.7,
        las = 2)

barplot(TDF_ley$ni,
        main = "Gráfica Nº2: Frecuencia de la Ley Mineral (Escala Ajustada)",
        xlab = "Nivel de Ley",
        ylab = "Cantidad (ni)",
        col = "steelblue",
        names.arg = TDF_ley$nivel_ley,
        cex.names = 0.7,
        las = 2,
        ylim = c(0, max(TDF_ley$ni)*1.2))

barplot(TDF_ley$hi,
        main = "Gráfica Nº3: Porcentaje de la Ley Mineral",
        xlab = "Nivel de Ley",
        ylab = "Porcentaje (%)",
        col = "steelblue",
        names.arg = TDF_ley$nivel_ley,
        cex.names = 0.7,
        las = 2)

barplot(TDF_ley$hi,
        main = "Gráfica Nº4: Porcentaje del Nivel de Ley (Escala Completa)",
        xlab = "Nivel de Ley",
        ylab = "Porcentaje (%)",
        col = "steelblue",
        names.arg = TDF_ley$nivel_ley,
        cex.names = 0.7,
        las = 2,
        ylim = c(0, 100))

par(mar = c(4,4,4,8))

colores <- rainbow(length(TDF_ley$hi))

pie(TDF_ley$hi,
    col = colores,
    main = "Distribución de la Ley Mineral",
    labels = NA)

legend("right",
       legend = paste(TDF_ley$nivel_ley, TDF_ley$hi, "%"),
       fill = colores,
       title = "NIVELES",
       bty = "o",
       xpd = TRUE,
       inset = c(-0.15,0))

INDICADORES ESTADISTICOS

moda_ley <- TDF_ley[TDF_ley$ni == max(TDF_ley$ni), ]
moda_ley
##   nivel_ley   ni hi
## 1      BAJA 1024 41
TDF_ley <- TDF_ley %>%
  mutate(Ni = cumsum(ni))

N <- sum(TDF_ley$ni)

mediana_ley <- TDF_ley %>%
  filter(Ni >= N/2) %>%
  slice(1)

mediana_ley
##   nivel_ley  ni hi   Ni
## 1     MEDIA 975 39 1999
#conclucion