0.- Carga de Librerías

library(readxl)
library(gt)
library(dplyr)
library(e1071)
library(RColorBrewer)

1.- Carga de Datos

datos <- read_excel("AQI.xlsx", sheet = "Hoja1")

2.- Selección de variable

AQI <- as.numeric(datos$AQI)
AQI <- AQI[!is.na(AQI)]

3.- Conteo de frecuencias

Li <- c(0, 100, 200, 300, 400)
Ls <- c(100, 200, 300, 400, 500)
MC <- round((Li + Ls) / 2, 0)

ni <- numeric(length(Li))
for(i in 1:length(Li)){
  if(i < length(Li)){
    ni[i] <- sum(AQI >= Li[i] & AQI < Ls[i])
  } else {
    ni[i] <- sum(AQI >= Li[i] & AQI <= Ls[i])
  }
}

N <- sum(ni)
hi <- (ni / N) * 100
Ni_asc <- cumsum(ni)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_asc <- cumsum(hi)
Hi_desc <- rev(cumsum(rev(hi)))

Intervalo <- c(
  "[0 - 100)",
  "[100 - 200)",
  "[200 - 300)",
  "[300 - 400)",
  "[400 - 500]"
)

4.- Tabla de distribución de frecuencias

TDF_AQI <- data.frame(
  Intervalo, MC, ni, hi = round(hi, 2),
  Ni_asc, Ni_desc, Hi_asc = round(Hi_asc, 2), Hi_desc = round(Hi_desc, 2)
)

totales <- data.frame(
  Intervalo = "Totales", MC = "-", ni = sum(ni), hi = round(sum(hi), 2),
  Ni_asc = "-", Ni_desc = "-", Hi_asc = "-", Hi_desc = "-"
)

TDF_AQI <- rbind(TDF_AQI, totales)

TDF_AQI %>%
  gt() %>%
  tab_header(
    title = md("## **Tabla Nro. 1**"),
    subtitle = md("*Distribución del Índice de Calidad del Aire (AQI) en China (2015-2025)*")
  ) %>%
  tab_source_note(
    source_note = md("**Autor:** Mateo Tapia **Fuente:** AQI.xlsx (China, 2015-2025)")
  ) %>%
  opt_table_font(
    font = list(google_font("Poppins"), default_fonts())
  ) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.width = px(3),
    table.border.bottom.width = px(3),
    heading.align = "center",
    heading.title.font.size = px(22),
    heading.subtitle.font.size = px(14),
    heading.background.color = "#F4F6F7",
    column_labels.font.weight = "bold",
    column_labels.font.size = px(14),
    column_labels.background.color = "#D6EAF8",
    column_labels.border.top.color = "black",
    column_labels.border.bottom.color = "black",
    column_labels.border.bottom.width = px(2),
    table_body.hlines.color = "#B3B6B7",
    table_body.vlines.color = "#B3B6B7",
    row.striping.include_table_body = TRUE,
    row.striping.background_color = "#F8F9F9",
    table.font.size = px(13),
    source_notes.font.size = px(11),
    data_row.padding = px(8)
  ) %>%
  tab_style(
    style = cell_fill(color = "#F4F6F7"),
    locations = cells_source_notes()
  ) %>%
  tab_style(
    style = cell_borders(sides = c("left", "right"), color = "#A6ACAF", weight = px(1)),
    locations = cells_body()
  ) %>%
  tab_style(
    style = cell_borders(sides = c("left", "right"), color = "black", weight = px(1.5)),
    locations = cells_column_labels()
  ) %>%
  cols_align(
    align = "center",
    columns = everything()
  )

Tabla Nro. 1

Distribución del Índice de Calidad del Aire (AQI) en China (2015-2025)
Intervalo MC ni hi Ni_asc Ni_desc Hi_asc Hi_desc
[0 - 100) 50 596 19.87 596 3000 19.87 100
[100 - 200) 150 578 19.27 1174 2404 39.13 80.13
[200 - 300) 250 611 20.37 1785 1826 59.5 60.87
[300 - 400) 350 627 20.90 2412 1215 80.4 40.5
[400 - 500] 450 588 19.60 3000 588 100 19.6
Totales - 3000 100.00 - - - -
Autor: Mateo Tapia Fuente: AQI.xlsx (China, 2015-2025)

5.- Gráfica de distribución de frecuencias

5.1 Diagrama de cantidad

# ===========================
# GRÁFICA Nro. 1: DIAGRAMA DE CANTIDAD QUE GENERA R STUDIO
# ===========================
colores <- brewer.pal(5, "Set3")

barplot(
  height = ni,
  names.arg = Intervalo,
  main = "Gráfica Nro. 1\nDistribución del Índice de Calidad del Aire (AQI)\nen China (2015-2025) - Escala Local",
  xlab = "Índice de Calidad del Aire (AQI)",
  ylab = "Cantidad",
  col = colores,
  border = "white",
  space = 0,
  las = 1,
  cex.names = 0.9,
  cex.lab = 1,
  cex.main = 1
)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

# ===========================
# GRÁFICA Nro. 2: DIAGRAMA DE CANTIDAD CON RESPECTO AL TOTAL
# ===========================
barplot(
  height = ni,
  names.arg = Intervalo,
  main = "Gráfica Nro. 2\nDistribución del Índice de Calidad del Aire (AQI)\nen China (2015-2025) - Con respecto al total (N = 3000)",
  xlab = "Índice de Calidad del Aire (AQI)",
  ylab = "Cantidad",
  col = colores,
  border = "white",
  space = 0,
  ylim = c(0, length(AQI)),
  yaxt = "n",
  las = 1,
  cex.names = 0.9,
  cex.lab = 1,
  cex.main = 1
)

marcas_y <- seq(0, length(AQI), by = 500)
axis(2, at = marcas_y, labels = marcas_y, las = 1, cex.axis = 0.9)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

5.2 Diagrama porcentual

# ===========================
# GRÁFICA Nro. 3: DIAGRAMA PORCENTUAL QUE GENERA R STUDIO
# ===========================
barplot(
  height = hi,
  names.arg = Intervalo,
  main = "Gráfica Nro. 3\nDistribución Porcentual del Índice de Calidad del Aire (AQI)\nen China (2015-2025) - Escala Local",
  xlab = "Índice de Calidad del Aire (AQI)",
  ylab = "Porcentaje",
  col = colores,
  border = "white",
  space = 0,
  las = 1,
  cex.names = 0.9,
  cex.lab = 1,
  cex.main = 1,
  yaxt = "n"
)

marcas_y <- pretty(c(0, max(hi)))
axis(2, at = marcas_y, labels = paste0(marcas_y, "%"), las = 1, cex.axis = 0.9)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

# ===========================
# GRÁFICA Nro. 4: DIAGRAMA PORCENTUAL CON RESPECTO AL TODO
# ===========================
barplot(
  height = hi,
  names.arg = Intervalo,
  main = "Gráfica Nro. 4\nDistribución Porcentual del Índice de Calidad del Aire (AQI)\nen China (2015-2025) - Con respecto al total (100%)",
  xlab = "Índice de Calidad del Aire (AQI)",
  ylab = "Porcentaje",
  col = colores,
  border = "white",
  space = 0,
  ylim = c(0, 100),
  yaxt = "n",
  las = 1,
  cex.names = 0.9,
  cex.lab = 1,
  cex.main = 1
)

marcas_y <- seq(0, 100, by = 20)
axis(2, at = marcas_y, labels = paste0(marcas_y, "%"), las = 1, cex.axis = 0.9)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

5.3 Diagrama de cajas

# ===========================
# GRÁFICA Nro. 5: BOXPLOT AQI
# ===========================
par(mar = c(4, 6, 4, 2))
boxplot(
  AQI,
  horizontal = TRUE,
  main = "Gráfica Nro. 5\nDistribución del Índice de Calidad del Aire (AQI)\nen China (2015-2025)",
  xlab = "Índice de Calidad del Aire (AQI)",
  col = "#76D7C4",
  border = "black",
  pch = 1,
  cex.main = 1,
  cex.lab = 1,
  las = 1
)
grid(nx = NULL, ny = NA, col = "gray85", lty = 1)

5.4 Diagrama de Ojivas

5.4.1 Ojivas (Ni)

# ====================================================================
# ===== GRÁFICA NRO. 6: OJIVAS LOCALES AQI ============================
# ====================================================================
par(mar = c(5, 6, 4, 2) + 0.1)

x <- Ls

plot(
  x, Ni_asc,
  type = "b",
  col = "orange",
  pch = 19,
  lwd = 2,
  main = "Gráfica Nro. 6: Distribución Acumulada (Ojivas) del AQI\nen China (2015-2025)",
  xlab = "Índice de Calidad del Aire (AQI)",
  ylab = "Cantidad",
  ylim = c(0, N),
  xaxt = "n",
  yaxt = "n",
  las = 1
)

lines(
  x, Ni_desc,
  type = "b",
  col = "blue",
  pch = 19,
  lwd = 2
)

axis(1, at = x, labels = x, las = 1, cex.axis = 0.9)
marcas_y <- pretty(c(0, N))
axis(2, at = marcas_y, labels = marcas_y, las = 1, cex.axis = 0.9)
grid(nx = NULL, ny = NA, col = "gray85", lty = 1)

legend(
  "right",
  legend = c(
    "Ascendente (Ojiva Menor que)",
    "Descendente (Ojiva Mayor que)"
  ),
  col = c("orange", "blue"),
  pch = 19,
  lty = 1,
  lwd = 2,
  bty = "n",
  cex = 0.9
)

5.4.2 Ojivas (Hi)

# ====================================================================
# ===== GRÁFICA NRO. 7: OJIVAS PORCENTUALES AQI =======================
# ====================================================================
par(mar = c(6, 5, 4, 2) + 0.1)

x <- Ls

plot(
  x, Hi_asc,
  type = "b",
  col = "orange",
  pch = 19,
  lwd = 2,
  main = "Gráfica Nro. 7: Ojiva Porcentual del Índice de Calidad del Aire (AQI)\nen China (2015-2025)",
  xlab = "",
  ylab = "Porcentaje",
  ylim = c(0, 100),
  xaxt = "n",
  yaxt = "n",
  panel.first = grid(col = "lightgray", lty = 2)
)

lines(
  x, Hi_desc,
  type = "b",
  col = "blue",
  pch = 19,
  lwd = 2
)

axis(1, at = x, labels = x, las = 1, cex.axis = 0.9)
mtext("Índice de Calidad del Aire (AQI)", side = 1, line = 3.5)

marcas_y <- seq(0, 100, by = 20)
axis(2, at = marcas_y, labels = paste0(marcas_y, "%"), las = 1, cex.axis = 0.9)

legend(
  "right",
  legend = c("Ascendente", "Descendente"),
  col = c("orange", "blue"),
  pch = 19,
  lty = 1,
  lwd = 2,
  bty = "n",
  cex = 0.9
)

6.- Indicadores

# ====================================================================
# ===== INDICADORES ESTADÍSTICOS Y TABLA RESUMEN (AQI) ===============
# ====================================================================

X <- mean(AQI, na.rm = TRUE)
Me <- median(AQI, na.rm = TRUE)
Mo <- Intervalo[which.max(ni)]

desv <- sd(AQI, na.rm = TRUE)
CV <- (desv / X) * 100

As <- skewness(AQI, na.rm = TRUE)
K <- kurtosis(AQI, na.rm = TRUE)

Variable <- "AQI"
min_aqi <- min(AQI, na.rm = TRUE)
max_aqi <- max(AQI, na.rm = TRUE)
Rango <- paste0("[", round(min_aqi, 0), " - ", round(max_aqi, 0), "]")

atipicos_reales <- boxplot.stats(AQI)$out
valoresatipicos <- ifelse(
  length(atipicos_reales) > 0,
  paste0(length(atipicos_reales), " atípicos (> ", round(min(atipicos_reales), 0), ")"),
  "Sin valores atípicos"
)

Tabla_indicadores_AQI <- data.frame(
  Variable = Variable,
  Rango = Rango,
  X = round(X, 2),
  Me = round(Me, 2),
  Mo = Mo,
  sd = round(desv, 2),
  CV = round(CV, 2),
  As = round(As, 2),
  K = round(K, 2),
  valoresatipicos = valoresatipicos
)

colnames(Tabla_indicadores_AQI) <- c(
  "Variable", "Rango", "X", "Me", "Mo", "sd", "CV", "As", "K", "Valores atípicos"
)

Tabla_indicadores_AQI %>%
  gt() %>%
  tab_header(
    title = md("## **Tabla Nro. 2**"),
    subtitle = md("*Indicadores Estadísticos de la Distribución del Índice de Calidad del Aire (AQI) en China (2015-2025)*")
  ) %>%
  tab_source_note(
    source_note = md("**Autor:** Mateo Tapia **Fuente:** AQI.xlsx (China, 2015-2025)")
  ) %>%
  opt_table_font(
    font = list(google_font("Poppins"), default_fonts())
  ) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.width = px(3),
    table.border.bottom.width = px(3),
    heading.align = "center",
    heading.background.color = "#F4F6F7",
    column_labels.font.weight = "bold",
    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,
    row.striping.background_color = "#F8F9F9",
    table_body.hlines.color = "#B3B6B7",
    table_body.border.bottom.color = "black"
  ) %>%
  tab_style(
    style = cell_fill(color = "#D6EAF8"),
    locations = cells_column_labels()
  ) %>%
  cols_align(
    align = "center",
    columns = everything()
  )

Tabla Nro. 2

Indicadores Estadísticos de la Distribución del Índice de Calidad del Aire (AQI) en China (2015-2025)
Variable Rango X Me Mo sd CV As K Valores atípicos
AQI [0 - 499] 249.64 248 [300 - 400) 143.4 57.44 -0.02 -1.19 Sin valores atípicos
Autor: Mateo Tapia Fuente: AQI.xlsx (China, 2015-2025)
## 7.- Conclusión
atipicos_aqi <- boxplot.stats(AQI)$out

texto_atipicos <- ifelse(
  length(atipicos_aqi) > 0,
  paste0("Se evidencian ", length(atipicos_aqi), " valores atípicos superiores a ", round(min(atipicos_aqi), 0), "."),
  "No se evidencian valores atípicos en la distribución."
)

df_conclusion_aqi <- data.frame(
  Conclusión = paste(
    "En el estudio de la distribución del Índice de Calidad del Aire (AQI) en China (2015-2025), la variable fluctúa entre",
    round(min(AQI, na.rm = TRUE), 0), "y", round(max(AQI, na.rm = TRUE), 0),
    ", y sus valores giran en torno a", round(mean(AQI, na.rm = TRUE), 2), ".",
    texto_atipicos
  )
)

df_conclusion_aqi %>%
  gt() %>%
  tab_header(
    title = md("## **Conclusión**")
  ) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.width = px(3),
    table.border.bottom.width = px(3),
    heading.align = "center"
  ) %>%
  tab_style(
    style = cell_text(align = "justify", v_align = "middle"),
    locations = cells_body(columns = Conclusión)
  )

Conclusión

Conclusión
En el estudio de la distribución del Índice de Calidad del Aire (AQI) en China (2015-2025), la variable fluctúa entre 0 y 499 , y sus valores giran en torno a 249.64 . No se evidencian valores atípicos en la distribución.