FECHA: 05/12/2025
#Estadística Descriptiva
#Variable Continua (NH3)
#Llumitasig Daniela
#Cargar librerias
library(gt)
library(dplyr)
##
## 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)
#Cargar datos
city_day_2_ <- read.csv("city_day (2).csv")
datos<-read.csv("~/Documentos R/TABLAS A/city_day (2).csv")
#Extraccion de los "-" de la variable NH3 debido a que son valores inexistentes,
# para un mejor analisis, cambia tamaño muestral de 29531 a 19203
city_day_2_$NH3[city_day_2_$NH3 == "-"] <- NA
city_day_2_$NH3 <- as.numeric(city_day_2_$NH3)
NH3 <- na.omit(city_day_2_$NH3)
#Filtrar NH3
NH3 <- subset(city_day_2_$NH3, city_day_2_$NH3 >= 0)
length(NH3)
## [1] 19203
# Calcular el mínimo y máximo de NH3
min_NH3 <- min(NH3)
max_NH3 <- max(NH3)
min_NH3
## [1] 0.01
max_NH3
## [1] 352.89
#Calcular rango
R <- max_NH3 - min_NH3
#Calcular intervalos
K <- floor(1 + 3.33 * log10(length(NH3)))
K
## [1] 15
#Calcular amplitud
A <-R/K
#Limite inferior
Li <-round(seq(from=min_NH3,to=max_NH3-A,by=A),2)
#Limite superior
Ls <-round(seq(from=min_NH3+A,to=max_NH3,by=A),2)
#Marca de clase
Mc <- (Li+Ls)/2
Mc
## [1] 11.775 35.300 58.825 82.350 105.875 129.400 152.925 176.450 199.975
## [10] 223.500 247.025 270.550 294.075 317.600 341.125
# Vector vacío para guardar las frecuencias de cada clase
ni <- c()
for (i in 1:K) {
if (i < K) {
# Para las primeras clases: [Li , Ls)
ni[i] <- length(subset(NH3, NH3 >= Li[i] & NH3 < Ls[i]))
} else {
# Para la última clase: [Li , Ls]
ni[i] <- length(subset(NH3, NH3 >= Li[i] & NH3 <= 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 <- paste0("[", round(Li,2), " - ", round(Ls,2), ")")
Intervalo[length(Intervalo)] <- paste0("[", round(Li[length(Li)],2), " - ",
round(Ls[length(Ls)],2), "]")
TDF_NH3 <- data.frame(
Intervalo = Intervalo,
Mc = round(Mc, 2),
ni = ni,
hi = round(hi, 2),
Ni_asc = Ni_asc,
Ni_desc = Ni_desc,
Hi_asc = round(Hi_asc, 2),
Hi_desc = round(Hi_desc, 2)
)
# Crear fila de totales para Benzene
totales <- data.frame(
Intervalo = "Totales",
Mc = "-",
ni = sum(ni),
hi = round(sum(hi), 2),
Ni_asc = "-",
Ni_desc = "-",
Hi_asc= "-",
Hi_desc = "-"
)
TDF_NH3 <- rbind(TDF_NH3, totales)
# Tabla 1
TDF_NH3 %>%
gt() %>%
tab_header(
title = md("*Tabla Nro. 1*"),
subtitle = md("*Distribución de frecuencias de la concentración de NH3 en el estudio de calidad del aire en la India*")
) %>%
tab_source_note(
source_note = md("Fuente:Datos procesados por el autor a partir del archivo *city_day_(2).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. 1 |
| Distribución de frecuencias de la concentración de NH3 en el estudio de calidad del aire en la India |
| Intervalo |
Mc |
ni |
hi |
Ni_asc |
Ni_desc |
Hi_asc |
Hi_desc |
| [0.01 - 23.54) |
11.78 |
12403 |
64.59 |
12403 |
19203 |
64.59 |
100 |
| [23.54 - 47.06) |
35.3 |
4775 |
24.87 |
17178 |
6800 |
89.45 |
35.41 |
| [47.06 - 70.59) |
58.83 |
1306 |
6.80 |
18484 |
2025 |
96.26 |
10.55 |
| [70.59 - 94.11) |
82.35 |
305 |
1.59 |
18789 |
719 |
97.84 |
3.74 |
| [94.11 - 117.64) |
105.88 |
176 |
0.92 |
18965 |
414 |
98.76 |
2.16 |
| [117.64 - 141.16) |
129.4 |
85 |
0.44 |
19050 |
238 |
99.2 |
1.24 |
| [141.16 - 164.69) |
152.93 |
43 |
0.22 |
19093 |
153 |
99.43 |
0.8 |
| [164.69 - 188.21) |
176.45 |
43 |
0.22 |
19136 |
110 |
99.65 |
0.57 |
| [188.21 - 211.74) |
199.98 |
22 |
0.11 |
19158 |
67 |
99.77 |
0.35 |
| [211.74 - 235.26) |
223.5 |
10 |
0.05 |
19168 |
45 |
99.82 |
0.23 |
| [235.26 - 258.79) |
247.02 |
11 |
0.06 |
19179 |
35 |
99.88 |
0.18 |
| [258.79 - 282.31) |
270.55 |
5 |
0.03 |
19184 |
24 |
99.9 |
0.12 |
| [282.31 - 305.84) |
294.08 |
15 |
0.08 |
19199 |
19 |
99.98 |
0.1 |
| [305.84 - 329.36) |
317.6 |
3 |
0.02 |
19202 |
4 |
99.99 |
0.02 |
| [329.36 - 352.89] |
341.12 |
1 |
0.01 |
19203 |
1 |
100 |
0.01 |
| Totales |
- |
19203 |
100.00 |
- |
- |
- |
- |
| Fuente:Datos procesados por el autor a partir del archivo city_day_(2).csv |
# PROCESO DE SIMPLIFICACIÓN PARA NH3
#Histograma NH3
histo_NH3 <- hist(NH3, plot = FALSE)
#Elementos simplificados
Lis<-histo_NH3$breaks [1:18]
Lss<-histo_NH3$breaks [2:19]
MCs<-(Lis+Lis)/2
nis<-histo_NH3$counts
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_NH3simplificado <- 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_NH3simplificado) <- c(
"Intervalo",
"MC",
"ni",
"hi(%)",
"Ni_asc",
"Hi_asc (%)",
"Ni_desc",
"Hi_desc (%)"
)
totaless <- data.frame(
Intervalo = "Totales",
MC = "-",
ni = sum(nis),
hi = sum(his),
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_NH3simplificado <- rbind(TDF_NH3simplificado, totaless)
#Tabla 2
TDF_NH3simplificado %>%
gt() %>%
tab_header(
title = md("*Tabla Nro. 2*"),
subtitle = md("*Distribución de frecuencia simplificada de concentración de NH3
en el Estudio de calidad de aire en la India*")
) %>%
tab_source_note(
source_note = md("Fuente: Datos procesados por el autor a partir del archivo *city_day (2)*")
) %>%
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 |
| Distribución de frecuencia simplificada de concentración de NH3
en el Estudio de calidad de aire en la India |
| Intervalo |
MC |
ni |
hi(%) |
Ni_asc |
Hi_asc (%) |
Ni_desc |
Hi_desc (%) |
| [0 - 20) |
0 |
11259 |
58.63 |
11259 |
58.63 |
19203 |
100 |
| [20 - 40) |
20 |
5023 |
26.16 |
16282 |
84.79 |
7944 |
41.37 |
| [40 - 60) |
40 |
1824 |
9.50 |
18106 |
94.29 |
2921 |
15.21 |
| [60 - 80) |
60 |
531 |
2.77 |
18637 |
97.05 |
1097 |
5.71 |
| [80 - 100) |
80 |
206 |
1.07 |
18843 |
98.13 |
566 |
2.95 |
| [100 - 120) |
100 |
131 |
0.68 |
18974 |
98.81 |
360 |
1.87 |
| [120 - 140) |
120 |
73 |
0.38 |
19047 |
99.19 |
229 |
1.19 |
| [140 - 160) |
140 |
38 |
0.20 |
19085 |
99.39 |
156 |
0.81 |
| [160 - 180) |
160 |
37 |
0.19 |
19122 |
99.58 |
118 |
0.61 |
| [180 - 200) |
180 |
26 |
0.14 |
19148 |
99.71 |
81 |
0.42 |
| [200 - 220) |
200 |
13 |
0.07 |
19161 |
99.78 |
55 |
0.29 |
| [220 - 240) |
220 |
9 |
0.05 |
19170 |
99.83 |
42 |
0.22 |
| [240 - 260) |
240 |
9 |
0.05 |
19179 |
99.88 |
33 |
0.17 |
| [260 - 280) |
260 |
4 |
0.02 |
19183 |
99.9 |
24 |
0.12 |
| [280 - 300) |
280 |
12 |
0.06 |
19195 |
99.96 |
20 |
0.1 |
| [300 - 320) |
300 |
5 |
0.03 |
19200 |
99.98 |
8 |
0.04 |
| [320 - 340) |
320 |
2 |
0.01 |
19202 |
99.99 |
3 |
0.02 |
| [340 - 360] |
340 |
1 |
0.01 |
19203 |
100 |
1 |
0.01 |
| Totales |
- |
19203 |
100.00 |
- |
- |
- |
- |
| Fuente: Datos procesados por el autor a partir del archivo city_day (2) |
#GRAFICAS
#Histogramas local
hist(NH3, breaks = 19,
main = "Gráfica N°1: Distribución de la Concentración de NH3
presente en el estudio de la calidad del aire en India ",
xlab = " NH3 (µg/m3)",
ylab = "Cantidad",
ylim = c(0, max(nis)),
col = "orange",
cex.main = 0.9,
cex.lab = 1,
cex.axis = 0.9,
xaxt = "n")
axis(1, at = histo_NH3$breaks,
labels = histo_NH3$breaks, las = 1,
cex.axis = 0.9)

#Histograma global
hist(NH3, breaks = 19,
main = "Gráfica N°2:Distribución de la Concentración de NH3
presente en el estudio de la calidad del aire en India",
xlab = "NH3 (µg/m3)",
ylab = "Cantidad",
ylim = c(0, length(NH3)),
col = "orange",
cex.main = 1,
cex.lab = 1,
cex.axis = 0.9,
xaxt = "n")
axis(1, at = histo_NH3$breaks,
labels = histo_NH3$breaks, las = 1,
cex.axis = 0.9)

#Histograma porcentual local
# hi(%) local desde tu tabla simplificada (sin la fila Totales)
hi_loc <- TDF_NH3simplificado$`hi(%)`[1:(nrow(TDF_NH3simplificado) - 1)]
# MISMO sistema de breaks locales
breaks_NH3_simplificado <- c(Lis[1], Lss)
histo_NH3_simplificado <- hist(NH3, breaks = breaks_NH3_simplificado, plot = FALSE)
# LIENZO vacío con eje Y en PORCENTAJE, SIN MARCO
plot(NA,
xlim = range(breaks_NH3_simplificado),
ylim = c(0, max(hi_loc) * 1.1),
xlab = "NH3 (µg/m³)",
ylab = "Porcentaje (%)",
main = "Gráfica N°3: Histograma porcentual local de NH3",
xaxt = "n",
bty = "n") # <<--- QUITA EL MARCO COMPLETO
# DIBUJAR LAS BARRAS (como histograma) usando hi(%)
for (i in seq_along(hi_loc)) {
rect(xleft = breaks_NH3_simplificado[i],
ybottom = 0,
xright = breaks_NH3_simplificado[i + 1],
ytop = hi_loc[i],
col = "orange",
border = "black")
}
# EJE X igual que en el histograma global/local absoluto (con etiquetas salteadas)
idx <- seq(1, length(histo_NH3_simplificado$breaks), by = 2)
axis(1,
at = histo_NH3_simplificado$breaks[idx],
labels = round(histo_NH3_simplificado$breaks[idx], 0),
las = 1,
cex.axis = 0.9)

#HISTOGRAMA PORCENTUAL GLOBAL PARA NH3
# 1. Histograma global para obtener breaks y counts
histo_NH3_global <- hist(NH3, breaks = 19, plot = FALSE)
# 2. Frecuencias absolutas
ni_global <- histo_NH3_global$counts
# 3. Calcular porcentajes globales
hi_global <- ni_global / sum(ni_global) * 100
# 4. Crear lienzo vacío con eje Y de 0 a 100%, SIN MARCO
plot(NA,
xlim = range(histo_NH3_global$breaks),
ylim = c(0, 100),
xlab = "NH3 (µg/m³)",
ylab = "Porcentaje (%)",
main = "Gráfica N°4: Histograma porcentual global de NH3",
xaxt = "n",
bty = "n" #QUITA EL MARCO
)
# 5. Dibujar barras del histograma porcentual GLOBAL
for (i in seq_along(hi_global)) {
rect(
xleft = histo_NH3_global$breaks[i],
ybottom = 0,
xright = histo_NH3_global$breaks[i + 1],
ytop = hi_global[i],
col = "orange",
border = "black"
)
}
# 6. Eje X limpio (saltando un break)
idx <- seq(1, length(histo_NH3_global$breaks), by = 2)
axis(1,
at = histo_NH3_global$breaks[idx],
labels = round(histo_NH3_global$breaks[idx], 0),
las = 1,
cex.axis = 0.9)

#Diagrama de caja
CajaNH3<-boxplot(NH3, horizontal = T,col = "red", border = "black",
main= "Gráfica No.6: Distribución de la concentración de NH3
Estudio de calidad del aire en la India",
xlab="NH3 (µg/m3)")

#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°7:Distribución de Frecuencias Ascendente y Descendente
de la Concentración de NH3",
xlab = "NH3 (µg/m3)",
ylab = "Cantidad",
ylim = c(0, max(c(Nis_asc, Nis_desc)))
)
# 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°8:Distribución de Frecuencias Ascendente y Descendente
de la Concentración de NH3",
xlab = "NH3 (µg/m3)",
ylab = "Porcentaje (%)",
col = "blue",
pch = 19,
ylim = c(0, 100)
)
# Ojiva descendente porcentual
lines(
x, His_desc,
type = "b",
col = "red",
pch = 19
)

#INDICADORES DE POSICION
#MEDIA ARITMETICA
X <- sum(NH3) / length(NH3)
X
## [1] 23.48348
#MEDIANA
Me <- median(NH3)
Me
## [1] 15.85
#MODA
Mo <- "[0,20]"
Mo
## [1] "[0,20]"
#Porque la tabla simplificada, la clase número 1 en el intervalo [0,20] tiene la frecuencia más alta.
#La mayor frecuencia es 11259, que está en la posición 1 del vector
#INDICADORES DE DISPERSION
#VARIANZA
var<- var(NH3)
var
## [1] 659.682
#DESVIACION ESTANDAR
sd <- sd(NH3)
sd
## [1] 25.68427
# COEFICIENTE DE VARIACIÓN (%)
CV <- (sd / X) * 100
CV
## [1] 109.3717
#La dispersión de NH3 es muy alta respecto al promedio
#INDICADORES DE FORMA
#COEFICIENTE DE ASIMETRIA
install.packages("e1071")
## Warning: package 'e1071' is in use and will not be installed
library(e1071)
As <- skewness(NH3)
As
## [1] 4.083355
#CUORTOSIS
library(e1071)
Cu <- kurtosis(NH3)
Cu
## [1] 27.95379
#outliers
cajaBigotes <- boxplot(NH3, plot = FALSE)
outliers <- cajaBigotes$out
min(outliers)
## [1] 62.2
max(outliers)
## [1] 352.89
length(outliers)
## [1] 1015
Variable<-"NH3"
Rango<-Rango <- "[0,352.88]"
VA <- paste0(
length(outliers),
" (",
round(min(outliers), 2),
" – ",
round(max(outliers), 2),
")"
)
Tabla_indicadores <- data.frame(
Variable = Variable,
Rango = Rango,
X = round(X, 3),
Me = Me,
Mo = Mo,
sd = round(sd, 2),
CV = round(CV, 2),
As = round(As, 2),
Cu = round(Cu, 2),
VA =VA
)
#Tabla indicadores
library(gt)
library(dplyr)
Tabla_indicadores %>%
gt() %>%
tab_header(
title = md("Tabla Nro. 3"),
subtitle = md("*Indicadores Estadísticos de concentración de NH3*")
) %>%
tab_source_note(
source_note = md("Fuente: Datos procesados por el autor a partir del archivo *city_day (2)*")
) %>%
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 NH3 |
| Variable |
Rango |
X |
Me |
Mo |
sd |
CV |
As |
Cu |
VA |
| NH3 |
[0,352.88] |
23.483 |
15.85 |
[0,20] |
25.68 |
109.37 |
4.08 |
27.95 |
1015 (62.2 – 352.89) |
| Fuente: Datos procesados por el autor a partir del archivo city_day (2) |