Univercidad Central del Ecuador

FIGEMPA-Ingeniería Ambiental

#Estadística descriptiva
#5/12/2025
#Lorien Arcentales
#Carga de paquetes
library(gt)
library(dplyr)
datos<-read.csv("city_day.csv", header = TRUE, dec = ".",
                sep = ",")
#Extracción variable
latitud<-datos$Latitud
length(latitud)
## [1] 29531
latitud <- as.numeric(latitud)
min<-min(latitud)
max<-max(latitud)
R=max-min
k=1+(3.3)*log(length(latitud))
k<-floor(k)
A<-R/k
#Generación de intervalos
Li <- seq(from = min, to = max - A, by = A)
Ls <- c (seq(from = min + A, to = max - A, by = A), max)  # último límite = max
MC<-(Li+Ls)/2
#Creación de ni
latitud <- round(latitud, 3)  # redondear los datos de ozono
Li <- round(Li, 3)        # redondear límites inferiores
Ls <- round(Ls, 3)  
ni <- numeric(length(Li))
for (i in 1:length(Li)) {
  if (i < length(Li)) {
    ni[i] <- sum(latitud >= Li[i] & latitud < Ls[i])
  } else {
    ni[i] <- sum(latitud >= Li[i] & latitud<= Ls[i])  # Último intervalo cerrado
  }
}
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 <- paste0("[", round(Li,2), " - ", round(Ls,2), ")")
Intervalo[length(Intervalo)] <- paste0("[", round(Li[length(Li)],2), " - ", 
                                       round(Ls[length(Ls)],2), "]")
TDF_latitud <- data.frame(
  Intervalo = Intervalo,
  MC = round(MC, 2),
  ni = ni,
  hi = round(hi, 2),
  Ni_ascendente = Ni_asc,
  Ni_descendente = Ni_desc,
  Hi_ascendente = round(Hi_asc, 2),
  Hi_descendente = round(Hi_desc, 2)
)
# Crear fila de totales
totales <- data.frame(
  Intervalo = "Totales",
  MC = "-",
  ni = sum(ni),
  hi = sum(hi),
  Ni_ascendente = "-",
  Ni_descendente = "-",
  Hi_ascendente = "-",
  Hi_descendente = "-"
)
# Agregar al final del data.frame
TDF_latitud <- rbind(TDF_latitud, totales)
library(gt)
library(dplyr)
TDF_latitud %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nro. 1*"),
    subtitle = md("**Distribucion de frecuencia de Latitud ,estudio calidad del aire en India entre 2015-2020 **")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo 2\n Fuente:https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india")
  ) %>%
  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
**Distribucion de frecuencia de Latitud ,estudio calidad del aire en India entre 2015-2020 **
Intervalo MC ni hi Ni_ascendente Ni_descendente Hi_ascendente Hi_descendente
[8.55 - 9.22) 8.88 162 0.55 162 29531 0.55 100
[9.22 - 9.9) 9.56 0 0.00 162 29369 0.55 99.45
[9.9 - 10.58) 10.24 0 0.00 162 29369 0.55 99.45
[10.58 - 11.26) 10.92 0 0.00 162 29369 0.55 99.45
[11.26 - 11.94) 11.6 162 0.55 324 29369 1.1 99.45
[11.94 - 12.62) 12.28 0 0.00 324 29207 1.1 98.9
[12.62 - 13.3) 12.96 2009 6.80 2333 29207 7.9 98.9
[13.3 - 13.97) 13.64 0 0.00 2333 27198 7.9 92.1
[13.97 - 14.65) 14.31 0 0.00 2333 27198 7.9 92.1
[14.65 - 15.33) 14.99 0 0.00 2333 27198 7.9 92.1
[15.33 - 16.01) 15.67 0 0.00 2333 27198 7.9 92.1
[16.01 - 16.69) 16.35 1169 3.96 3502 27198 11.86 92.1
[16.69 - 17.37) 17.03 951 3.22 4453 26029 15.08 88.14
[17.37 - 18.05) 17.71 1462 4.95 5915 25078 20.03 84.92
[18.05 - 18.73) 18.39 0 0.00 5915 23616 20.03 79.97
[18.73 - 19.4) 19.06 0 0.00 5915 23616 20.03 79.97
[19.4 - 20.08) 19.74 0 0.00 5915 23616 20.03 79.97
[20.08 - 20.76) 20.42 0 0.00 5915 23616 20.03 79.97
[20.76 - 21.44) 21.1 2122 7.19 8037 23616 27.22 79.97
[21.44 - 22.12) 21.78 2319 7.85 10356 21494 35.07 72.78
[22.12 - 22.8) 22.46 0 0.00 10356 19175 35.07 64.93
[22.8 - 23.48) 23.14 0 0.00 10356 19175 35.07 64.93
[23.48 - 24.16) 23.82 1112 3.77 11468 19175 38.83 64.93
[24.16 - 24.83) 24.49 0 0.00 11468 18063 38.83 61.17
[24.83 - 25.51) 25.17 0 0.00 11468 18063 38.83 61.17
[25.51 - 26.19) 25.85 2360 7.99 13828 18063 46.83 61.17
[26.19 - 26.87) 26.53 2009 6.80 15837 15703 53.63 53.17
[26.87 - 27.55) 27.21 289 0.98 16126 13694 54.61 46.37
[27.55 - 28.23) 27.89 0 0.00 16126 13405 54.61 45.39
[28.23 - 28.91) 28.57 9207 31.18 25333 13405 85.78 45.39
[28.91 - 29.58) 29.24 0 0.00 25333 4198 85.78 14.22
[29.58 - 30.26) 29.92 925 3.13 26258 4198 88.92 14.22
[30.26 - 30.94) 30.6 1114 3.77 27372 3273 92.69 11.08
[30.94 - 31.62] 31.28 2159 7.31 29531 2159 100 7.31
Totales - 29531 100.00 - - - -
Autor: Grupo 2 Fuente:https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india
#PROCESO DE SIMPLIFICACIÓN
#Hist(latitud)
#Elemnetos simplificados 
breaks <- Histograma_latitud$breaks

Lis <- breaks[1:(length(breaks)-1)]
Lss <- breaks[2:length(breaks)]
MCs<-(Lis+Lss)/2
nis<-Histograma_latitud$counts
N<-length(latitud)
his <- (nis / N) * 100
Nis_asc <- cumsum(nis) 
His_asc <- cumsum(his) 
Nis_desc <- rev(cumsum(rev(nis)))
His_desc <- rev(cumsum(rev(his)))
Intervalos <- paste0("[", round(Lis,2), " - ", round(Lss,2), ")")
Intervalos[length(Intervalos)] <- paste0("[", round(Lis[length(Lis)],2), 
                                         " - ", round(Lss[length(Lss)],2), "]")
TDF_latitudsimplificado <- data.frame(
  Intervalo = Intervalos,
  MC = round(MCs, 2),
  ni = nis,
  hi= round(his, 2),
  Ni_ascendente = Nis_asc,
  Hi_ascendente = round(His_asc, 2),
  Ni_descendente = Nis_desc,
  Hi_descendente = round(His_desc, 2)
)
colnames(TDF_latitudsimplificado) <- c(
  "Intervalo",
  "MC",
  "ni",
  "hi(%)",
  "Ni_asc",
  "Hi_asc (%)",
  "Ni_desc",
  "Hi_desc (%)"
)
totaless <- data.frame(
  Intervalo = "Totales",
  MC = "-",
  ni = sum(nis),           # suma total de ni
  hi = sum(his),            # suma total de hi (%)
  Ni_ascendente = "-",
  Ni_descendente = "-",
  Hi_ascendente = "-",
  Hi_descendente = "-"
)
colnames(totaless) <- c(
  "Intervalo",
  "MC",
  "ni",
  "hi(%)",
  "Ni_asc",
  "Hi_asc (%)",
  "Ni_desc",
  "Hi_desc (%)"
)
# Agregar al final de la tabla
TDF_latitudsimplificado <- rbind(TDF_latitudsimplificado, totaless)
#Tabla 2 
TDF_latitudsimplificado %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nro. 2*"),
    subtitle = md("**Distribucion de frecuencia simplificado de Latitud ,estudio calidad del aire en India entre 2015-2020 **")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo 2\n Fuente:https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india")
  ) %>%
  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. 2
**Distribucion de frecuencia simplificado de Latitud ,estudio calidad del aire en India entre 2015-2020 **
Intervalo MC ni hi(%) Ni_asc Hi_asc (%) Ni_desc Hi_desc (%)
[8 - 10) 9 162 0.55 162 0.55 29531 100
[10 - 12) 11 162 0.55 324 1.1 29369 99.45
[12 - 14) 13 2009 6.80 2333 7.9 29207 98.9
[14 - 16) 15 0 0.00 2333 7.9 27198 92.1
[16 - 18) 17 3582 12.13 5915 20.03 27198 92.1
[18 - 20) 19 0 0.00 5915 20.03 23616 79.97
[20 - 22) 21 4441 15.04 10356 35.07 23616 79.97
[22 - 24) 23 1112 3.77 11468 38.83 19175 64.93
[24 - 26) 25 1858 6.29 13326 45.13 18063 61.17
[26 - 28) 27 2800 9.48 16126 54.61 16205 54.87
[28 - 30) 29 9207 31.18 25333 85.78 13405 45.39
[30 - 32] 31 4198 14.22 29531 100 4198 14.22
Totales - 29531 100.00 - - - -
Autor: Grupo 2 Fuente:https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india
#GRAFICAS

#Histogramas local
hist(latitud, breaks = 13,
     main = "Gráfica N°1: Distribución de la Latitud
     en el estudio sobre calidad del aire en India entre 2015-2020 ",
     xlab = " Latitud (°)",
     ylab = "Cantidad",
     ylim = c(0, max(nis)),
     col = "blue",
     cex.main = 0.9,
     cex.lab = 1,
     cex.axis = 0.9,
     xaxt = "n")
axis(1, at = Histograma_latitud$breaks,
     labels = Histograma_latitud$breaks, las = 1,
     cex.axis = 0.9)

#Histograma global
hist(latitud, breaks = 13,
     main = "Gráfica N°2:Distribución de la cantidad de Latitud en el estudio
     sobre calidad del aire en India entre 2015-2020",
     xlab = "Latitud (°)",
     ylab = "Cantidad",
     col = "blue",
     cex.main = 1,
     cex.lab = 1,
     cex.axis = 0.9,
     xaxt = "n",
     ylim = c(0,length(latitud)))
axis(1, at = Histograma_latitud$breaks,
     labels = Histograma_latitud$breaks, las = 1,
     cex.axis = 0.9)

#Histograma porcentual global
TDF_latitudsimplificado$`hi (%)` <- as.numeric(TDF_latitudsimplificado$`hi(%)`)

post<-barplot(TDF_latitudsimplificado$`hi(%)`[1:(nrow(TDF_latitudsimplificado)-1)],
              space = 0,
              col = "blue",
              main = "Gráfica N°3:Distribución de la cantidad Latitud, estudio
        calidad del aire en India, 2015-2020",
              xlab = "Latitud (°)",
              ylab = "Porcentaje (%)",
              names.arg = TDF_latitudsimplificado$MC[1:(nrow(TDF_latitudsimplificado)-1)],
              ylim = c(0,100), xaxt = "n"
)
axis(side = 1,
     at = post,
     labels = TDF_latitudsimplificado$MC[1:(nrow(TDF_latitudsimplificado)-1)],
     tck = -0.02)

#Histograma porcentual local
n <- as.numeric(nrow(TDF_latitudsimplificado))
pos<-barplot(
  TDF_latitudsimplificado$`hi(%)`[1:(n-1)],
  space = 0,
  main = "Gráfica No. 4:Distribución concentración de Latitud en el estudio 
  calidad del aire en India, 2015-2020",
  ylab = "Porcentaje (%)",
  xlab = "Latitud (°)",
  names.arg = TDF_latitudsimplificado$MC[1:(n-1)],
  col = "blue"
)
axis(side = 1,
     at = pos,
     labels = TDF_latitudsimplificado$MC[1:(nrow(TDF_latitudsimplificado)-1)],
     tck = -0.04,   # controla la longitud de la rayita
     las = 1)       # rota las etiquetas si quieres verticales

#Box plot
Cajalatitud<-boxplot(latitud, horizontal = T,col = "brown", border = "black",
                    main= "Gráfica No. 5: Distribución de la Latitud,
                   estudio calidad del aire en India desde 2015-2020",
                    xlab="Latitud (°)")

#Ojivas locales

# Usar un único eje X para ambas ojivas
x <- Lss

plot(
  x, Nis_asc,
  type = "b",
  col = "red",
  pch = 19,
  main = "Gráfica N°6:Distribución de cantidad de Latitud en el 
  estudio calidad del aire en India 2015-2020",
  xlab = "Latitud (°)",
  ylab = "Cantidad",
  ylim = c(0, max(c(Nis_asc, Nis_desc)))   # 👈 Eje Y iniciando en 0
)

# Ojiva descendente
lines(
  x, Nis_desc,
  type = "b",
  col = "blue",
  pch = 19
)

#Ojiva porcentual 

# Usar un único eje X para ambas ojivas
x <- Lss   

# Ojiva ascendente porcentual
plot(
  x, His_asc,
  type = "b",
  main ="Gráfica N°7:Distribución de cantidad de Latitud en el 
  estudio calidad del aire en India 2015-2020",
  xlab = "Latitud (°)",
  ylab = "Porcentaje (%)",
  col = "orange",
  pch = 19,
  ylim = c(0, 100)   
)
# Ojiva descendente porcentual
lines(
  x, His_desc,
  type = "b",
  col = "black",
  pch = 19
)

#INDICADORES

#Indicadores de Tendencia Central

# Mediana
Me <- median(latitud)
Me
## [1] 26.853
# Media
X <- mean(latitud)
X
## [1] 24.64169
# Moda
Mo <- "[28,30]"
Mo
## [1] "[28,30]"
#Indicadores de Dispersión

# Varianza
var(latitud)
## [1] 31.44975
# Desviación estandar
desv<-round(sd(latitud), 2)

# Coeficiente de variación 
CV <- (sd(latitud)/X)*100
CV
## [1] 22.75821
#Indicadores de Forma

# Coeficiente de Asimetría
library(e1071)
As <- skewness(latitud)
As
## [1] -0.8224199
# Curtosis
K <- kurtosis(latitud)
K
## [1] -0.4371487
veloresatipicos<-"162,[18871,19032]"
Variable <- "Latitud"
Rango <- "[8.54,31.72]"

Tabla_indicadores <- data.frame(Variable,Rango,round(X,3),Me,Mo,round(desv,2),round(CV,2),round(As,2),round(K,2),
                                veloresatipicos)
colnames(Tabla_indicadores) <- c("Variable","Rango","X", "Me", "Mo","sd","CV","As","K","Valores atípicos")

library(gt)
library(dplyr)
Tabla_indicadores %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nro. 3*"),
    subtitle = md("**Indicadores Estadísticos de concentración de Latitud,estudio calidad del aire en India entre 2015-2020 **")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo 2\n Fuente:https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india")
  ) %>%
  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 de concentración de Latitud,estudio calidad del aire en India entre 2015-2020 **
Variable Rango X Me Mo sd CV As K Valores atípicos
Latitud [8.54,31.72] 24.642 26.853 [28,30] 5.61 22.76 -0.82 -0.44 162,[18871,19032]
Autor: Grupo 2 Fuente:https://www.kaggle.com/datasets/rohanrao/air-quality-data-in-india