FECHA: 21/11/2025
#Estadística Descriptiva
#Ariana Viteri
#20/11/2025
library(gt)
library(dplyr)
library(RColorBrewer)
#Cargar los datos
datos <- read.csv("~/ariana tercer semestre/Estadistica/city_day.csv",
header = TRUE, dec = ".", sep = ",")
# ================================
# ORDENAR AQI_Bucket COMO VARIABLE ORDINARIA
# ================================
# ORDEN de jerarquía (del mejor al peor)
datos$AQI_Bucket <- factor(
datos$AQI_Bucket,
levels = c("Good", "Satisfactory", "Moderate",
"Poor", "Very Poor", "Severe","-"),
ordered = TRUE
)
# TABLAS CUALITATIVAS
# ================================
AQI_Bucket <- datos$AQI_Bucket
#Tabla de distribución de frecuencia
TDF_AQI_Bucket <- data.frame(table(AQI_Bucket))
ni <- TDF_AQI_Bucket$Freq
hi <- round((ni / sum(ni)) * 100, 2)
AQI_Bucket <- TDF_AQI_Bucket$AQI_Bucket
TDF_AQI_Bucket <- data.frame(AQI_Bucket, ni, hi)
Summary <- data.frame(AQI_Bucket = "TOTAL", ni = sum(ni), hi = 100)
TDF_AQI_Bucket_suma <- rbind(TDF_AQI_Bucket, Summary)
colnames(TDF_AQI_Bucket_suma) <- c("AQI_Nivel", "ni", "hi(%)")
# ================================
# TABLA GT
# ================================
TDF_AQI_Bucket_suma %>%
gt() %>%
tab_header(
title = md("Tabla Nro. 1"),
subtitle = md("Tabla de frecuencias de AQI_Bucket en el estudio calidad del aire en India de 2015-2020")
) %>%
tab_source_note(
source_note = md("Fuente: Datos procesados por el autor a partir de archivo city.day.csv")
) %>%
tab_style(
style = cell_borders(sides = "left", color = "black", weight = px(2)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = "right", color = "black", weight = px(2)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = "left", color = "black", weight = px(2)),
locations = cells_column_labels()
) %>%
tab_style(
style = cell_borders(sides = "right", color = "black", weight = px(2)),
locations = cells_column_labels()
) %>%
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"
)
| Tabla Nro. 1 |
| Tabla de frecuencias de AQI_Bucket en el estudio calidad del aire en India de 2015-2020 |
| AQI_Nivel |
ni |
hi(%) |
| Good |
1341 |
4.54 |
| Satisfactory |
8224 |
27.85 |
| Moderate |
8829 |
29.90 |
| Poor |
2781 |
9.42 |
| Very Poor |
2337 |
7.91 |
| Severe |
1338 |
4.53 |
| - |
4681 |
15.85 |
| TOTAL |
29531 |
100.00 |
| Fuente: Datos procesados por el autor a partir de archivo city.day.csv |
#DIAGRAMAS
# GDF 1
# Diagrama de barrras local
TDF_AQI_Bucket$Zona <- iconv(TDF_AQI_Bucket$AQI_Bucket, from = "latin1", to = "UTF-8", sub = "")
library(RColorBrewer)
n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)
barplot(
height = TDF_AQI_Bucket$ni,
names.arg = TDF_AQI_Bucket$AQI_Bucket,
main = "Grafica Nro 1: Distribución de AQI_Bucket del
estudio calidad del aire en India, entre 2015-2020",
xlab = "",
ylab = "Cantidad",
col = heat.colors(length(TDF_AQI_Bucket$ni)),
las = 2,
cex.names = 0.7,
cex.main = 0.8,
)
mtext("AQI_Bucket", side = 1, line = 4, cex = 1)

#Histograma porcentual local
library(RColorBrewer)
n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)
barplot(
TDF_AQI_Bucket$hi,
main = "Gráfica N°2: Distribución porcentual de AQI_NIVEL estudio
calidad del aire en India, entre 2015-2020",
xlab = "",
ylab = "Porcentaje (%)",
col = colores,
names.arg = TDF_AQI_Bucket$AQI_Bucket,
ylim = c(0, 30),
las = 2,
cex.names = 0.7,
cex.main = 0.8,
)
mtext("AQI_Bucket", side = 1, line = 4, cex = 1)

#Histograma de Barras de Cantidad Global
library(RColorBrewer)
n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
max_y_global <- Summary$ni[1]
# Generar el gráfico de barras
barplot(
height = TDF_AQI_Bucket$ni,
names.arg = TDF_AQI_Bucket$AQI_Bucket,
main = "Gráfica Nro 3: Distribución de AQI_Bucket del estudio
calidad del aire en India, entre 2015-2020",
xlab = "",
ylab = "Cantidad",
col = colores,
ylim = c(0, max_y_global),
las = 2,
cex.names = 0.7,
cex.main = 0.8,
mgp = c(3.3, 1, 0) # El '5' hace que el título 'Cantidad' se aleje
)
mtext("AQI_Bucket", side = 1, line = 4, cex = 1)

# DIAGRAMA DE BARRAS porcentaje global
colores <- c("yellow", "orange")
library(RColorBrewer)
n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)
barplot(
TDF_AQI_Bucket$hi,
main = "Gráfica N°4:Distribución AQI_NIVEL en el estudio
calidad del aire en India entre 2015-2020",
xlab = "",
ylab = "Porcentaje (%)",
col = colores,
names.arg = TDF_AQI_Bucket$AQI_Bucket,
ylim = c(0, 100),
las = 2,
cex.names = 0.7,
cex.main = 0.8,
)
mtext("AQI_Bucket", side = 1, line = 3, cex = 1)

# DIAGRAMA CIRCULAR
# ================================
n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)
etiqueta <- paste(hi, "%")
library(RColorBrewer)
n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)
pie(
hi,
labels = etiqueta,
radius = 0.8,
col = color,
main = "Gráfica No.5:
Distribución de AQI_Bucket en el estudio calidad del aire en India, entre 2015-2020",
cex.main = 0.8
)
legend(
x = 1.1, y = 1.07,
legend = TDF_AQI_Bucket$AQI_Bucket,
title = "Leyenda",
fill = color,
cex = 0.65
)

# =========================================================
# INDICADORES ESTADÍSTICOS para la variable AQI_Bucket (Cualitativa Ordinal)
# 1. CÁLCULO DE LA MODA (Mo)
# Es el nivel con la mayor frecuencia (ni)
Moda_row <- TDF_AQI_Bucket[which.max(TDF_AQI_Bucket$ni), ]
Mo_calc <- as.character(Moda_row$AQI_Bucket[1])
# 2. CÁLCULO DE LA MEDIANA (Me)
# Frecuencias Acumuladas
Ni_calc <- cumsum(TDF_AQI_Bucket$ni)
N_total <- sum(TDF_AQI_Bucket$ni)
Posicion_Me <- N_total / 2
# Categoría que contiene la posición N/2
Mediana_row_index <- which(Ni_calc >= Posicion_Me)[1]
Me_calc <- as.character(TDF_AQI_Bucket$AQI_Bucket[Mediana_row_index])
# 3. RANGO
# Usamos los niveles extremos de la variable ordinal
Rango_calc <- paste0(
as.character(TDF_AQI_Bucket$AQI_Bucket[1]),
" a ",
as.character(TDF_AQI_Bucket$AQI_Bucket[length(TDF_AQI_Bucket$AQI_Bucket)-1])
)
# =========================================================
# CÓDIGO DE LA TABLA (Mantenido lo más fiel posible a tu original)
# =========================================================
Variable <- "AQI_Bucket (Nivel de Calidad del Aire)"
Rango <- Rango_calc # Rango calculado
# Mediana
Me <- Me_calc # Mediana calculada
# Media
X <- "-"
# Moda
Mo <- Mo_calc # Moda calculada
# Indicadores de Dispersión
# Varianza
var<-"-"
# Desviación estandar
desv<-"-"
# Coeficiente de variación
CV <- "-"
# Indicadores de Forma
# Coeficiente de Asimetría
As <-"-"
# Curtosis
K <- "-"
# Crear el Data Frame
Tabla_indicadores <- data.frame(Variable, Rango, X, Me, Mo, var, desv, CV, As, K)
colnames(Tabla_indicadores) <- c("Variable","Rango","X", "Me", "Mo","Var",
"sd","CV","As","K")
library(gt)
library(dplyr)
# Generar la Tabla GT
Tabla_indicadores %>%
gt() %>%
tab_header(
title = md("*Tabla Nro. 3*"),
subtitle = md("**Indicadores Estadísticos del Nivel AQI_Bucket (Calidad del Aire) en India entre 2015-2020**")
) %>%
tab_source_note(
source_note = md("Autor: Grupo 2\n Fuente: Datos procesados por el autor a partir de archivo city.day.csv")
) %>%
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"
)
| Tabla Nro. 3 |
| Indicadores Estadísticos del Nivel AQI_Bucket (Calidad del Aire) en India entre 2015-2020 |
| Variable |
Rango |
X |
Me |
Mo |
Var |
sd |
CV |
As |
K |
| AQI_Bucket (Nivel de Calidad del Aire) |
Good a Severe |
- |
Moderate |
Moderate |
- |
- |
- |
- |
- |
| Autor: Grupo 2
Fuente: Datos procesados por el autor a partir de archivo city.day.csv |