#Variable Cuantitativa Discreta
#AQI
#Autor: Ariel chiluisa
#Fecha:31/05/2026
0.- Carga de Librerias
#Carga de Librerias
library(gt)
## Warning: package 'gt' was built under R version 4.5.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.3
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(e1071)
## Warning: package 'e1071' was built under R version 4.5.3
1.- Carga de Datos
# ===== CARGA DE DATOS =====
datos <- read.csv("~/ariana tercer semestre/Estadistica/city_day.csv",
header = TRUE, dec = ".", sep = ";")
2. SELECCIÓN DE LA VARIABLE
AQI <- datos$AQI
AQI <- AQI[AQI != "-"]
AQI <- as.numeric(AQI)
AQI <- AQI[!is.na(AQI)]
3.CONTEO FRECUENCIAS
Li <- c(0,100,200,300,400,500)
Ls <- c(100,200,300,400,500,max(AQI))
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)",
paste0("[500 - ", max(AQI), "]")
)
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 de frecuencias del Índice de Calidad del Aire (AQI), estudio de calidad del aire en India (2015-2020)*"
)
) %>%
tab_source_note(
source_note = md(
"**Autor:** Grupo 1
**Fuente:** https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india"
)
) %>%
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),
source_notes.background.color = "#F4F6F7",
data_row.padding = px(8)
) %>%
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 de frecuencias del Índice de Calidad del Aire (AQI), estudio de calidad del aire en India (2015-2020) |
| Intervalo |
MC |
ni |
hi |
Ni_asc |
Ni_desc |
Hi_asc |
Hi_desc |
| [0 - 100) |
50 |
9343 |
37.60 |
9343 |
24850 |
37.6 |
100 |
| [100 - 200) |
150 |
9009 |
36.25 |
18352 |
15507 |
73.85 |
62.4 |
| [200 - 300) |
250 |
2790 |
11.23 |
21142 |
6498 |
85.08 |
26.15 |
| [300 - 400) |
350 |
2359 |
9.49 |
23501 |
3708 |
94.57 |
14.92 |
| [400 - 500) |
450 |
804 |
3.24 |
24305 |
1349 |
97.81 |
5.43 |
| [500 - 2049] |
1274 |
545 |
2.19 |
24850 |
545 |
100 |
2.19 |
| Totales |
- |
24850 |
100.00 |
- |
- |
- |
- |
Autor: Grupo 1
Fuente: https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india |
5.GRÁFICAS DE DISTRIBUCIÓN DE FRECUENCIA
5.1 GIAGRAMA DE BARRAS LOCAL
# ===========================
# GRÁFICA Nro. 1: HISTOGRAMA LOCAL AQI
# ===========================
library(RColorBrewer)
colores <- brewer.pal(6, "Set3")
barplot(
height = ni,
names.arg = c(
"[0-100)",
"[100-200)",
"[200-300)",
"[300-400)",
"[400-500)",
paste0("[500-", max(AQI), "]")
),
main = "Gráfica Nro. 1\nDistribución del Índice de Calidad del Aire (AQI)\nEstudio de calidad del aire en India (2015-2020)",
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
)

5.2 DIAGRAMA DE BARRAS GLOBAL
# ===========================
# GRÁFICA Nro. 2: HISTOGRAMA GLOBAL AQI
# ===========================
library(RColorBrewer)
colores <- brewer.pal(6, "Set3")
barplot(
height = ni,
names.arg = c(
"[0-100)",
"[100-200)",
"[200-300)",
"[300-400)",
"[400-500)",
paste0("[500-", max(AQI), "]")
),
main = "Gráfica Nro. 2\nDistribución Global del Índice de Calidad del Aire (AQI)\nEstudio de calidad del aire en India (2015-2020)",
xlab = "Índice de Calidad del Aire (AQI)",
ylab = "Cantidad",
col = colores,
border = "white",
space = 0,
ylim = c(0, length(AQI)), # techo = tamaño de muestra
yaxt = "n",
las = 1,
cex.names = 0.9,
cex.lab = 1,
cex.main = 1
)
# Eje Y global
marcas_y <- seq(0, length(AQI), by = 5000)
axis(
2,
at = marcas_y,
labels = marcas_y,
las = 1,
cex.axis = 0.9
)
grid(
nx = NA,
ny = NULL,
col = "gray85",
lty = 1
)

5.3 DIAGRAMA LOCAL PORCENTUAL
# ===========================
# GRÁFICA Nro. 3: HISTOGRAMA PORCENTUAL LOCAL AQI
# ===========================
library(RColorBrewer)
colores <- brewer.pal(6, "Set3")
barplot(
height = hi,
names.arg = c(
"[0-100)",
"[100-200)",
"[200-300)",
"[300-400)",
"[400-500)",
paste0("[500-", max(AQI), "]")
),
main = "Gráfica Nro. 3\nDistribución Porcentual del Índice de Calidad del Aire (AQI)\nEstudio de calidad del aire en India (2015-2020)",
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"
)
# Eje Y porcentual automático
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
)

5.4 DIAGRAMA GLOBAL PORCENTUAL
# ===========================
# GRÁFICA Nro. 4: HISTOGRAMA PORCENTUAL GLOBAL AQI
# ===========================
library(RColorBrewer)
colores <- brewer.pal(6, "Set3")
barplot(
height = hi,
names.arg = c(
"[0-100)",
"[100-200)",
"[200-300)",
"[300-400)",
"[400-500)",
paste0("[500-", max(AQI), "]")
),
main = "Gráfica Nro. 4\nDistribución Porcentual Global del Índice de Calidad del Aire (AQI)\nEstudio de calidad del aire en India (2015-2020)",
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
)
# Eje Y fijo de 0% a 100%
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
)

6. 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 India entre 2015-2020",
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
)

7. OJIVAS
7.1 OJIVAS (NI)
# ====================================================================
# ===== GRÁFICA NRO. 6: OJIVAS LOCALES AQI ============================
# ====================================================================
par(mar = c(5, 6, 4, 2) + 0.1)
# Límites superiores de los intervalos
x <- Ls
# Ojiva Ascendente
plot(
x, Ni_asc,
type = "b",
col = "orange",
pch = 19,
lwd = 2,
main = "Gráfica Nro. 6: Distribución Acumulada (Ojivas) del AQI\nEstudio de calidad del aire en India (2015-2020)",
xlab = "Índice de Calidad del Aire (AQI)",
ylab = "Frecuencia Acumulada (Días)",
ylim = c(0, N),
xaxt = "n",
yaxt = "n",
las = 1
)
# Ojiva Descendente
lines(
x, Ni_desc,
type = "b",
col = "blue",
pch = 19,
lwd = 2
)
# Eje X con los límites reales
axis(
1,
at = x,
labels = x,
las = 1,
cex.axis = 0.9
)
# Eje Y automático
marcas_y <- pretty(c(0, N))
axis(
2,
at = marcas_y,
labels = marcas_y,
las = 1,
cex.axis = 0.9
)
# Cuadrícula
grid(
nx = NULL,
ny = NA,
col = "gray85",
lty = 1
)
# Leyenda
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
)

7.2 OJIVAS(HI)
# ====================================================================
# ===== GRÁFICA NRO. 7: OJIVAS PORCENTUALES AQI =======================
# ====================================================================
# Ajuste de márgenes
par(mar = c(6, 5, 4, 2) + 0.1)
# Límites superiores reales de los intervalos AQI
x <- Ls
# Ojiva Ascendente Porcentual
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)\nEstudio de calidad del aire en India (2015-2020)",
xlab = "",
ylab = "Porcentaje Acumulado (%)",
ylim = c(0, 100),
xaxt = "n",
yaxt = "n",
panel.first = grid(
col = "lightgray",
lty = 2
)
)
# Ojiva Descendente Porcentual
lines(
x, Hi_desc,
type = "b",
col = "blue",
pch = 19,
lwd = 2
)
# Eje X
axis(
1,
at = x,
labels = x,
las = 2,
cex.axis = 0.9
)
# Título del eje X
mtext(
"Índice de Calidad del Aire (AQI)",
side = 1,
line = 4.5
)
# Eje Y porcentual
marcas_y <- seq(0, 100, by = 20)
axis(
2,
at = marcas_y,
labels = paste0(marcas_y, "%"),
las = 1,
cex.axis = 0.9
)
# Leyenda
legend(
"right",
legend = c("Ascendente", "Descendente"),
col = c("orange", "blue"),
pch = 19,
lty = 1,
lwd = 2,
bty = "n",
cex = 0.9
)

8. INDICADORES
# ====================================================================
# ===== INDICADORES ESTADÍSTICOS Y TABLA RESUMEN (AQI) ===============
# ====================================================================
library(e1071)
library(gt)
library(dplyr)
# INDICADORES DE TENDENCIA CENTRAL
X <- mean(AQI, na.rm = TRUE)
Me <- median(AQI, na.rm = TRUE)
Mo <- Intervalo[which.max(ni)]
# INDICADORES DE DISPERSIÓN
var_aqi <- var(AQI, na.rm = TRUE)
desv <- sd(AQI, na.rm = TRUE)
CV <- (desv / X) * 100
# INDICADORES DE FORMA
As <- skewness(AQI, na.rm = TRUE)
K <- kurtosis(AQI, na.rm = TRUE)
# DATOS PARA LA TABLA
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),
"]"
)
# VALORES ATÍPICOS
atipicos_reales <- boxplot.stats(AQI)$out
valoresatipicos <- paste0(
length(atipicos_reales),
" atípicos (> ",
round(min(atipicos_reales),0),
")"
)
# TABLA DE INDICADORES
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 NRO. 2
Tabla_indicadores_AQI %>%
gt() %>%
tab_header(
title = md("## **Tabla Nro. 2**"),
subtitle = md("*Indicadores Estadísticos del Índice de Calidad del Aire (AQI), estudio de calidad del aire en India entre 2015-2020*")
) %>%
tab_source_note(
source_note = md(
"**Autor:** Grupo 1
**Fuente:** https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india"
)
) %>%
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.background.color = "#D6EAF8",
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"
) %>%
cols_align(
align = "center",
columns = everything()
)
Tabla Nro. 2
|
| Indicadores Estadísticos del Índice de Calidad del Aire (AQI), estudio de calidad del aire en India entre 2015-2020 |
| Variable |
Rango |
X |
Me |
Mo |
sd |
CV |
As |
K |
Valores atípicos |
| AQI |
[13 - 2049] |
166.46 |
118 |
[0 - 100) |
140.7 |
84.52 |
3.4 |
21.42 |
1358 atípicos (> 399) |
Autor: Grupo 1
Fuente: https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india |
9. CONCLUSIÓN
# ===== CREACIÓN DEL DATA FRAME DE CONCLUSIÓN =====
df_conclusion_aqi <- data.frame(
Conclusión = paste(
"Tras analizar el Índice de Calidad del Aire (AQI) en India entre 2015 y 2020,",
"se observa que la mayoría de los registros (73.85%) se concentran en niveles",
"inferiores a 200, clasificados dentro de los rangos de menor contaminación.",
"No obstante, el promedio de", mean(AQI, na.rm = TRUE) %>% round(2),
"y la existencia de", length(boxplot.stats(AQI)$out),
"valores atípicos superiores a 399, indican la presencia de episodios críticos",
"de contaminación atmosférica que superan significativamente los niveles base.",
"La alta variabilidad (CV =", (sd(AQI, na.rm = TRUE)/mean(AQI, na.rm = TRUE)*100) %>% round(2),
"%) sugiere que la calidad del aire es inestable y altamente dependiente de factores externos."
)
)
# ===== GENERACIÓN DE LA TABLA DE CONCLUSIÓN (gt) =====
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 |
| Tras analizar el Índice de Calidad del Aire (AQI) en India entre 2015 y 2020, se observa que la mayoría de los registros (73.85%) se concentran en niveles inferiores a 200, clasificados dentro de los rangos de menor contaminación. No obstante, el promedio de 166.46 y la existencia de 1358 valores atípicos superiores a 399, indican la presencia de episodios críticos de contaminación atmosférica que superan significativamente los niveles base. La alta variabilidad (CV = 84.52 %) sugiere que la calidad del aire es inestable y altamente dependiente de factores externos. |