# 1. Configurar directorio y cargar datos
setwd("/cloud/project/proyecto")

datos <- read.csv("archivo depurado nuevo 12.csv",
                  header = TRUE, sep = ";", dec = ".")


# PROFUNDIDAD DEL SONDADOR 

# Extraer, convertir a numérico y limpiar la variable
prof_sond <- as.numeric(datos$PROFUNDIDADE_SONDADOR_M)   
## Warning: NAs introduced by coercion
prof_sond <- na.omit(prof_sond)

# Filtrar el rango solicitado
prof_sond <- prof_sond[prof_sond >= 2200 & prof_sond <= 8000]

# ↓↓↓―------------------------------------------------------- #
# Parámetros para la distribución de frecuencias
rango_ps   <- diff(range(prof_sond))
num_clases <- floor(1 + 3.3 * log10(length(prof_sond)))   # regla de Sturges
amplitud   <- rango_ps / num_clases

# Límites de clase
lim_inf <- seq(min(prof_sond), max(prof_sond) - amplitud, by = amplitud)
lim_sup <- c(lim_inf[-1], max(prof_sond))
marcas  <- (lim_inf + lim_sup) / 2

# Frecuencias absolutas
calc_freq <- function(x, a, b) {
  sapply(seq_along(a), \(i)
         if (i == length(a))
           sum(x >= a[i] & x <= b[i])
         else
           sum(x >= a[i] & x <  b[i]))
}
f_abs  <- calc_freq(prof_sond, lim_inf, lim_sup)
f_rel  <- f_abs / length(prof_sond) * 100

# Frecuencias acumuladas
f_acum_asc  <- cumsum(f_abs)
f_acum_desc <- rev(cumsum(rev(f_abs)))
fr_acum_asc <- cumsum(f_rel)
fr_acum_desc<- rev(cumsum(rev(f_rel)))

# Tabla resumen
tabla_prof_sond <- data.frame(
  Limite_Inferior       = round(lim_inf,  2),
  Limite_Superior       = round(lim_sup,  2),
  Marca_Clase           = round(marcas,   2),
  Frecuencia            = f_abs,
  Frec_Relativa_Pct     = round(f_rel,        2),
  Acum_Abs_Asc          = f_acum_asc,
  Acum_Abs_Desc         = f_acum_desc,
  Acum_Rel_Asc          = round(fr_acum_asc,  2),
  Acum_Rel_Desc         = round(fr_acum_desc, 2)
)

print("Tabla de distribución de frecuencias – Profundidad del Sondador")
## [1] "Tabla de distribución de frecuencias – Profundidad del Sondador"
print(tabla_prof_sond)
##    Limite_Inferior Limite_Superior Marca_Clase Frecuencia Frec_Relativa_Pct
## 1          2200.00         2617.54     2408.77       1183             18.19
## 2          2617.54         3035.08     2826.31       1495             22.98
## 3          3035.08         3452.62     3243.85       1494             22.97
## 4          3452.62         3870.15     3661.38        850             13.07
## 5          3870.15         4287.69     4078.92        476              7.32
## 6          4287.69         4705.23     4496.46        321              4.93
## 7          4705.23         5122.77     4914.00        214              3.29
## 8          5122.77         5540.31     5331.54        222              3.41
## 9          5540.31         5957.85     5749.08        149              2.29
## 10         5957.85         6375.38     6166.62         58              0.89
## 11         6375.38         6792.92     6584.15         30              0.46
## 12         6792.92         7210.46     7001.69         10              0.15
## 13         7210.46         7628.00     7419.23          3              0.05
##    Acum_Abs_Asc Acum_Abs_Desc Acum_Rel_Asc Acum_Rel_Desc
## 1          1183          6505        18.19        100.00
## 2          2678          5322        41.17         81.81
## 3          4172          3827        64.14         58.83
## 4          5022          2333        77.20         35.86
## 5          5498          1483        84.52         22.80
## 6          5819          1007        89.45         15.48
## 7          6033           686        92.74         10.55
## 8          6255           472        96.16          7.26
## 9          6404           250        98.45          3.84
## 10         6462           101        99.34          1.55
## 11         6492            43        99.80          0.66
## 12         6502            13        99.95          0.20
## 13         6505             3       100.00          0.05
# Histograma
hist(prof_sond,
     breaks = seq(min(prof_sond), max(prof_sond), by = amplitud),
     col    = "darkgreen",
     main   = "Gráfica No 02: Distribución de Profundidad del Sondador",
     xlab   = "Profundidad (m)",
     ylab   = "Cantidad")

# Ojivas ascendente y descendente
x_asc  <- c(min(lim_inf), lim_sup)
y_asc  <- c(0, f_acum_asc)
x_desc <- c(lim_inf, max(lim_sup))
y_desc <- c(f_acum_desc, 0)

plot(x_asc, y_asc, type = "b", col = "forestgreen", pch = 19,
     main = "Gráfica 2.2: Ojiva Ascendente y Descendente – Prof. Sondador",
     xlab = "Profundidad (m)",
     ylab = "Cantidad",
     xlim = range(c(x_asc, x_desc)),
     ylim = c(0, max(c(y_asc, y_desc))))
lines(x_desc, y_desc, type = "b", col = "firebrick", pch = 19)

# Boxplot para detectar outliers
boxplot(prof_sond, horizontal = TRUE, col = "orchid",
        main = "Distribución 2.3: Profundidad del Sondador (2200–8000 m)",
        xlab = "Profundidad (m)")

# Conclusion

conclusion <- "El análisis de la variable Profundidad del Sondador muestra una distribución de frecuencias claramente sesgada hacia las profundidades más bajas, con una mayor concentración de datos entre los 2200 m y los 3450 m"