#Estadistica Descriptiva

#Alexander Sailema

library(gt)
library(dplyr)
#Cargar Datos

setwd("~/")
datos<-read.csv("soil_pollution_diseases.csv",header = TRUE,dec = ".",
                sep = ",")

#Extracción Variable Cuantitativa Continua

Precipitación <- datos$Rainfall_mm

min <-min(Precipitación)
max <-max(Precipitación)
R <-max-min
K <- floor(1+3.33*log10(length(Precipitación)))
A <-R/K

Li <-round(seq(from=min,to=max-A,by=A),2)
Ls <-round(seq(from=min+A,to=max,by=A),2)
Mc <-(Li+Ls)/2

ni<-c()
for (i in 1:K) {
  if (i < K) {
    ni[i] <- length(subset(Precipitación, Precipitación >= Li[i] & Precipitación < Ls[i]))
  } else {
    ni[i] <- length(subset(Precipitación, Precipitación >= Li[i] & Precipitación <= Ls[i]))
  }
}

sum(ni)
## [1] 3000
hi <-ni/sum(ni)*100
Ni_asc<-cumsum(ni)
Hi_asc<-cumsum(hi)
Ni_desc<-rev(cumsum(rev(ni)))
Hi_desc<-rev(cumsum(rev(hi)))

TDF_hi <-ni/sum(ni)*100
Ni_asc<-cumsum(ni)
Hi_asc<-cumsum(hi)
Ni_desc<-rev(cumsum(rev(ni)))
Hi_desc<-rev(cumsum(rev(hi)))

TDF_Precipitación <- data.frame(
  Li, Ls, Mc, ni, round(hi, 3), Ni_asc, Ni_desc, round(Hi_asc, 3), round(Hi_desc, 2)
)

colnames(TDF_Precipitación) <- c("Li","Ls","Mc","ni","hi","Ni_asc","Ni_desc","Hi_asc(%)","Hi_desc(%)")

#Crear fila de totales

totales<-c(
  Li="TOTAL",
  Ls="-",
  Mc="-",
  ni=sum(ni),
  hi=sum(hi),
  Ni_asc="-",
  Ni_desc="-",
  Hi_asc="-",
  Hi_desc="-")

TDF_Precipitación <-rbind(TDF_Precipitación,totales) 

TDF_Precipitación %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nro. 5*"),
    subtitle = md("**Tabla de distribución de frecuencias de la Precipitación (mm) en Suelo**")
  ) %>%
  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. 5
Tabla de distribución de frecuencias de la Precipitación (mm) en Suelo
Li Ls Mc ni hi Ni_asc Ni_desc Hi_asc(%) Hi_desc(%)
0 333.08 166.54 238 7.933 238 3000 7.933 100
333.08 666.17 499.625 251 8.367 489 2762 16.3 92.07
666.17 999.25 832.71 256 8.533 745 2511 24.833 83.7
999.25 1332.33 1165.79 250 8.333 995 2255 33.167 75.17
1332.33 1665.42 1498.875 241 8.033 1236 2005 41.2 66.83
1665.42 1998.5 1831.96 268 8.933 1504 1764 50.133 58.8
1998.5 2331.58 2165.04 263 8.767 1767 1496 58.9 49.87
2331.58 2664.67 2498.125 244 8.133 2011 1233 67.033 41.1
2664.67 2997.75 2831.21 239 7.967 2250 989 75 32.97
2997.75 3330.83 3164.29 238 7.933 2488 750 82.933 25
3330.83 3663.92 3497.375 245 8.167 2733 512 91.1 17.07
3663.92 3997 3830.46 267 8.9 3000 267 100 8.9
TOTAL - - 3000 100 - - - -
Autor: Grupo 3
hist(Precipitación)

#Histograma



#Simplificación con el histograma

Hist_Precipitación<-hist(Precipitación,breaks = 8,plot = F)
k<-length(Hist_Precipitación$breaks)
Li<-Hist_Precipitación$breaks[1:(length(Hist_Precipitación$breaks)-1)]
Ls<-Hist_Precipitación$breaks[2:length(Hist_Precipitación$breaks)]
ni<-Hist_Precipitación$counts
sum(ni)
## [1] 3000
Mc<-Hist_Precipitación$mids
hi<-(ni/sum(ni))
sum(hi)
## [1] 1
Ni_asc<-cumsum(ni)
Hi_asc<-cumsum(hi)
Ni_desc<-rev(cumsum(rev(ni)))
Hi_desc<-rev(cumsum(rev(hi)))
TDF_Precipitación<-data.frame(Li=round(Li,2),
                            Ls=round(Ls,2),
                            Mc=round(Mc,2),
                            ni=ni,
                            hi=hi*100,
                            Ni_asc=Ni_asc,
                            Ni_desc=Ni_desc,
                            Hi_asc=round(Hi_asc*100,2),
                            Hi_desc=round(Hi_desc*100,2))
colnames(TDF_Precipitación)<-c("Lim inf","Lim sup","MC","ni","hi(%)","Ni asc","Ni desc","Hi asc(%)","Hi desc(%)")

#Crear fila de totales
totales<-c(Li="TOTAL",
           Ls="-",
           Mc="-",
           ni = sum(as.numeric(TDF_Precipitación$ni)),
           hi = sum(as.numeric(TDF_Precipitación$hi)),
           Ni_asc="-",
           Ni_desc="-",
           Hi_asc="-",
           Hi_desc="-")

TDF_Precipitación<-rbind(TDF_Precipitación,totales)

TDF_Precipitación %>%
  gt() %>%
  tab_header(
    title = md("*Tabla Nro. 6*"),
    subtitle = md("**Tabla Simplificada de distribución de frecuencias de 
                  la Precipitación en Suelo**")
  ) %>%
  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. 6
Tabla Simplificada de distribución de frecuencias de la Precipitación en Suelo
Lim inf Lim sup MC ni hi(%) Ni asc Ni desc Hi asc(%) Hi desc(%)
0 500 250 365 12.1666666666667 365 3000 12.17 100
500 1000 750 382 12.7333333333333 747 2635 24.9 87.83
1000 1500 1250 367 12.2333333333333 1114 2253 37.13 75.1
1500 2000 1750 392 13.0666666666667 1506 1886 50.2 62.87
2000 2500 2250 385 12.8333333333333 1891 1494 63.03 49.8
2500 3000 2750 360 12 2251 1109 75.03 36.97
3000 3500 3250 349 11.6333333333333 2600 749 86.67 24.97
3500 4000 3750 400 13.3333333333333 3000 400 100 13.33
TOTAL - - 3000 100 - - - -
Autor: Grupo 3
#Histograma

Hist_Precipitación<-hist(Precipitación,breaks = 8,plot = F)
hist(Precipitación,breaks = 10,
     main = "Gráfica N°23: Distribución de la Precipitación en Suelo",
     xlab = "Precipitación",
     ylab = "Cantidad",
     ylim = c(0,500),
     col = "red",
     cex.main=0.9,
     cex.lab=1,
     cex.axis=0.9,
     xaxt="n")
axis(1,at=Hist_Precipitación$breaks,labels = Hist_Precipitación$breaks,las=1,
     cex.axis=0.9)

#Gráficas
#Ni Global
hist(Precipitación, breaks = 10,
     main = "Gráfica N°24: Distribución de la Precipitación (mm)",
     xlab = "Precipitación",
     ylab = "Cantidad",
     ylim = c(0, length(Precipitación)),
     col = "green",
     cex.main = 0.9,
     cex.lab = 1,
     cex.axis = 0.9,
     xaxt = "n")
axis(1, at = Hist_Precipitación$breaks,
     labels = Hist_Precipitación$breaks, las = 1,
     cex.axis = 0.9)

# Filtrar solo las filas numéricas (excluyendo la fila "TOTAL")

TDF_Precipitación$hi <- as.numeric(TDF_Precipitación$hi)
datos_grafico <- subset(TDF_Precipitación, !(MC %in% c("-", "TOTAL")))

#Hi Global
barplot(datos_grafico$hi,
        space = 0,
        col = "skyblue",
        main = "Gráfica N°25: Distribución porcentual de frecuencias de la 
        Precipitación en suelo",
        xlab = "Precipitación mm",
        ylab = "Porcentaje (%)",
        names.arg = datos_grafico$MC,
        ylim = c(0, 100))

#Ni Local
hist(Precipitación, breaks = 10,
     main = "Gráfica N°26 Distribución para la Precipitación del suelo ",
     xlab = "Precipitación mm",
     ylab = "Cantidad",
     ylim = c(0,max(ni)),
     col = "yellow",
     cex.main = 0.9,
     cex.lab = 1,
     cex.axis = 0.9,
     xaxt = "n")

axis(1, at = Hist_Precipitación$breaks,
     labels = Hist_Precipitación$breaks, las = 1,
     cex.axis = 0.9)

#Hi local
barplot(datos_grafico$hi,
        space = 0,
        col = "blue",
        main = "Gráfica N°27: Distribución porcentual de frecuencias de la
        Precipitación en suelo",
        xlab = "Precipitación mm",
        ylab = "Porcentaje (%)",
        names.arg = datos_grafico$MC,
        ylim = c(0, 20))

# Diagrama de Caja

boxplot(Precipitación,
        horizontal = TRUE,
        main = "Gráfica N°28 Distribución de Frecuencia para la 
        Precipitación en Suelo ",
        xlab = " Precipitación mm ",
        col = "brown",
        outline = TRUE,
        pch = 1)

# Diagrama de Ojiva Ascendente y Descendente Ni

plot(Li ,Ni_desc,
     main = "Gráfica N°29: Distribución de frecuencias Ascendente y descendente 
      para la Precipitación en suelo",
     xlab = " Precipitación mm ",
     ylab = "Cantidad",
     xlim = c(0,3900),
     col = "red",
     cex.axis=0.8,
     type = "o",
     lwd = 3,
     las=1,
     xaxt="n")
lines(Ls,Ni_asc,
      col = "orange",
      type = "o",
      lwd = 3)
axis(1, at = seq(0, 3900, by = 500))

# Diagrama de Ojiva Ascendente y Descendente Porcentual

plot(Li, Hi_desc * 100,
     main = "Gráfica N°30: Distribución de frecuencia Ascendente y Descendente 
     porcentual para la Precipitación en suelo ",
     xlab = " Precipitación mm ",
     ylab = "Porcentaje (%)",
     xlim = c(0,3900),
     col = "red",
     type = "o",
     lwd = 2,
     xaxt="n")
lines(Ls, Hi_asc * 100,
      col = "orange",
      type = "o",
      lwd = 3)
axis(1, at = seq(0,3900,by=500))