This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

#Estadistica Descriptiva

#Alexander Sailema

#20/11/2025
library(gt)
library(dplyr)
#Cargar Datos

datos <- read.csv("C:/Users/Alexander/Downloads/soil_pollution_diseases (2).csv", sep=",", dec=".")

#Extracción Variable Cuantitativa Continua


Precipitacion <- datos$Rainfall_mm

#Manualmente
k<-1+(3.3*log10(3000))
k<-floor(k)
min<-min(Precipitacion)
max<-max(Precipitacion)
R<- max-min
A<-R/k
Li<-round(seq(from=min, to=max-A, by=A),4)
Ls<-round(seq(from=min+A, to=max, by=A), 4)
MC<-round((Li+Ls)/2,2)
ni <- numeric(length(Li))
for (i in 1:length(Li)) {
  ni[i] <- sum(Precipitacion>= Li[i] & Precipitacion < Ls[i]) 
}
ni[length(Li)] <- sum(Precipitacion >= Li[length(Li)] & Precipitacion <= max)
sum(ni)
## [1] 3000
## [1] 3000
hi <- ni/sum(ni)*100

sum(hi)
## [1] 100
## [1] 100
Niasc<-cumsum(ni)
Nidsc<-rev(cumsum(rev(ni)))
Hiasc<-round(cumsum(hi))
Hidsc<-round(rev(cumsum(rev(hi))))

TDFPrecipitacion<-data.frame(Li, Ls, MC, ni, hi, Niasc, Nidsc, Hiasc, Hidsc)

total_ni<-sum(ni)
total_hi<-100

TDFPrecipitacionCompleto<-rbind(
  TDFPrecipitacion,
  data.frame(Li=" Total", Ls=" ", MC=" ",
             ni=total_ni, hi=total_hi, Niasc=" ", Nidsc=" ", 
             Hiasc=" ", Hidsc=" ")
)

#Formato tabla

#Tabla

library(gt)
library(dplyr)

# Redondeo opcional
TDFPrecipitacion$hi <- round(TDFPrecipitacion$hi, 2)

tabla_Precipitacion<- TDFPrecipitacionCompleto %>%
  gt() %>%
  fmt_number(
    columns = hi,
    decimals = 2
  ) %>%
  
  tab_header(
    title = md("*Tabla Nº5*"),
    subtitle = md("**Tabla de Precipitación (mm)**")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo3")
  ) %>%
  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"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(
      rows = Li == "Total"
    )
  )

tabla_Precipitacion
Tabla Nº5
Tabla de Precipitación (mm)
Li Ls MC ni hi Niasc Nidsc Hiasc Hidsc
0 33.3083 16.65 238 7.93 238 3000 8 100
33.3083 66.6167 49.96 251 8.37 489 2762 16 92
66.6167 99.925 83.27 256 8.53 745 2511 25 84
99.925 133.2333 116.58 250 8.33 995 2255 33 75
133.2333 166.5417 149.89 241 8.03 1236 2005 41 67
166.5417 199.85 183.2 268 8.93 1504 1764 50 59
199.85 233.1583 216.5 263 8.77 1767 1496 59 50
233.1583 266.4667 249.81 244 8.13 2011 1233 67 41
266.4667 299.775 283.12 239 7.97 2250 989 75 33
299.775 333.0833 316.43 238 7.93 2488 750 83 25
333.0833 366.3917 349.74 245 8.17 2733 512 91 17
366.3917 399.7 383.05 267 8.90 3000 267 100 9
Total 3000 100.00
Autor: Grupo3
#Histograma
histoP<-hist(
  Precipitacion,
  main= "Distribución de la Precipitacion (mm)",
  xlab= "Precipitacion(mm)",
  ylab= "Cantidad", col="blue",
)

#Tabla simplificada en base al histograma
Limites <- histoP$breaks
LimInf <- Limites[1:(length(Limites)-1)]
LimSup <- Limites[2:length(Limites)]
Mc <- histoP$mids
ni <- histoP$counts
sum(ni)
## [1] 3000
## [1] 3000
hi <- round(ni/sum(ni)*100, 2)
sum(hi)
## [1] 99.99
## [1] 100
Ni_asc <- cumsum(ni)
Ni_dsc <- rev(cumsum(rev(ni)))
Hi_asc <- round(cumsum(hi), 2)
Hi_dsc <- round(rev(cumsum(rev(hi))), 2)

TDFP<-data.frame(LimInf, LimSup, Mc, ni, hi, Ni_asc, Ni_dsc, Hi_asc, Hi_dsc)


totalni <- sum(ni)
totalhi <- 100  

TDFPCompleto<-rbind(
  TDFP,
  data.frame(LimInf="Total",
             LimSup=" ", Mc=" ", ni=totalni,
             hi=totalhi, Ni_asc=" ", Ni_dsc=" ", 
             Hi_asc=" ", Hi_dsc=" ")
)

tablaPre<-TDFPCompleto %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nº6*"),
    subtitle = md("**Tabla simplificada de Precipitación (mm)**")
  ) %>%
  tab_source_note(
    source_note = md("Autor:Grupo 3")
  ) %>%
  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"
  )  %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(
      rows = LimInf == "Total"
    )
  )

tablaPre
Tabla Nº6
Tabla simplificada de Precipitación (mm)
LimInf LimSup Mc ni hi Ni_asc Ni_dsc Hi_asc Hi_dsc
0 50 25 365 12.17 365 3000 12.17 99.99
50 100 75 382 12.73 747 2635 24.9 87.82
100 150 125 367 12.23 1114 2253 37.13 75.09
150 200 175 392 13.07 1506 1886 50.2 62.86
200 250 225 385 12.83 1891 1494 63.03 49.79
250 300 275 360 12.00 2251 1109 75.03 36.96
300 350 325 349 11.63 2600 749 86.66 24.96
350 400 375 400 13.33 3000 400 99.99 13.33
Total 3000 100.00
Autor:Grupo 3
#Histograma

#Graficas
hist(
  Precipitacion,
  breaks = seq(min, max, A),
  main = "Gráfica Nº23: Frecuencia de la Precipitación (mm) (Local)",
  xlab = "Precipitación (mm)",
  ylab = "Frecuencia",
  col = "#4A90E2",
  cex.main = 1.1,
  cex.lab = 1.1
)

#Gráficas
#Global
hist(
  Precipitacion,
  breaks = seq(min, max, A),
  main = "Gráfica Nº24: Frecuencia de la Precipitación (mm) (Global)",
  xlab = "Precipitación (mm)",
  ylab = "Frecuencia",
  col = "green",
  ylim = c(0, 3000),
  cex.main = 1.1,
  cex.lab = 1.1
)

# Gráfica Local hi
barplot(
  TDFPrecipitacion$hi,
  space = 0,
  col = "skyblue",
  main = "Gráfica Nº25: Porcentaje de la Precipitación (mm) (Local)",
  xlab = "Precipitación (mm)",
  ylab = "Porcentaje (%)",
  names.arg = TDFPrecipitacion$MC,
  cex.names = 0.9,
  cex.main = 1.1,
  cex.lab = 1.1
)

# Gráfica Global hi

barplot(
  TDFPrecipitacion$hi,
  space = 0,
  col = "yellow",
  main = "Gráfica Nº26: Porcentaje de la Precipitación (mm)(Global)",
  xlab = "Precipitación (mm)",
  ylab = "Porcentaje (%)",
  names.arg = TDFPrecipitacion$MC,
  ylim = c(0, 100),
  cex.names = 0.9,
  cex.main = 1.1,
  cex.lab = 1.1
)

#caja

boxplot(
  Precipitacion,
  horizontal = TRUE,
  col = "pink",
  main = "Grafica Nº27: Distribución de la Precipitación (mm)",
  xlab = "Precipitación (mm)"
)

# Ojivas de Frecuencia (Ascendente y Descendente)
plot(
  Li, Nidsc,
  main = "Gráfica Nº28: Distribución de Frecuencias Ascendente y Descendente 
  de la Precipitación (mm)",
  xlab = "Precipitación (mm)",
  ylab = "Cantidad",
  xlim = c(min, max),
  col = "red",
  cex.axis = 0.8,
  type = "o",
  lwd = 3,
  las = 1,
  xaxt = "n"
)

lines(
  Ls, Niasc,
  col = "green",
  type = "o",
  lwd = 3
)

axis(1, at = round(seq(min, max, length.out = 10), 0))

# Diagrama de Ojiva Ascendente y Descendente Porcentual
plot(
  Li, Hidsc,
  main = "Gráfica Nº29: Distribución Porcentual Ascendente y Descendente
  de la Precipitación (mm)",
  xlab = "Precipitación (mm)",
  ylab = "Porcentaje (%)",
  xlim = c(min, max),
  col = "red",
  type = "o",
  lwd = 2,
  xaxt = "n"
)

lines(
  Ls, Hiasc,
  col = "blue",
  type = "o",
  lwd = 3
)

axis(1, at = round(seq(min, max, length.out = 10), 0))

# INDICADORES


library(e1071)
library(gt)


# --- INDICADORES DE TENDENCIA CENTRAL ---

# Mediana
Me <- median(Precipitacion)
Me
## [1] 199
# Media
X <- mean(Precipitacion)
X
## [1] 200.3166
# Moda
Tabla_Prec <- as.data.frame(table(Precipitacion))
max_frec <- max(Tabla_Prec$Freq)
moda <- Tabla_Prec$Precipitacion[Tabla_Prec$Freq == max_frec]
moda
## [1] 363.8
## 2108 Levels: 0 0.2 0.3 0.4 0.5 0.6 1 1.1 1.4 1.5 1.6 1.7 1.8 2 2.1 2.2 ... 399.7
#Indicadores de Dispersión

# Varianza
V <-var(Precipitacion)

# Desviación estandar
desv<-round(sd(Precipitacion), 2)

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

# Coeficiente de Asimetría
library(e1071)
As <- skewness(Precipitacion)
As
## [1] 0.01427213
# Curtosis
K <- kurtosis(Precipitacion)
K
## [1] -1.189601
Variable <- "Precipitación"
Rango <- "[0 ; 399.7]"

tabla_indicadores <- data.frame(
  Variable = "Precipitación",
  Rango = Rango,
  Media = round(X, 3),
  Mediana = round(Me, 3),
  Moda = paste(moda, collapse = ", "),
  Varianza = round(V, 3),
  Desv_Estandar = round(desv, 3),
  CV = round(CV, 2),
  Asimetria = round(As, 3),
  Curtosis = round(K, 3)
)

tabla_indicadores
library(gt)
library(dplyr)
tabla_indicadores %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nro. 7*"),
    subtitle = md("**Indicadores Estadísticos de Precipitación (mm) **")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo 3")
  ) %>%
  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. 7
**Indicadores Estadísticos de Precipitación (mm) **
Variable Rango Media Mediana Moda Varianza Desv_Estandar CV Asimetria Curtosis
Precipitación [0 ; 399.7] 200.317 199 363.8 13287.6 115.27 57.54 0.014 -1.19
Autor: Grupo 3