setwd("~/UCE/III/estadistica")
datos <- read.csv("Conjunto_Datos_Minerales.csv", header = TRUE, sep = ";", 
                  dec = ".")
Longitud <- datos$longitude

# Limpieza
Longitud <- gsub("\\.", "", Longitud)   # quita puntos (separador de miles)
Longitud <- gsub(",", ".", Longitud)    # cambia coma decimal a punto (opcional, si aplica)

# Conversión
Longitud <- as.numeric(Longitud)

# Eliminar NA
Longitud <- na.omit(Longitud)

# Calcular número de clases usando Sturges
n <- length(Longitud)
k <- ceiling(1 + log2(n))

# Construir intervalos
limites <- seq(from = min(Longitud),
               to = max(Longitud),
               length.out = k + 1)

# Calcular histograma con límites fijos
Histograma_Longitud <- hist(Longitud, breaks = limites,
                            main = "Gráfica no. 1: Distribución de cantidad de longitud",
                            xlab = "Longitud",
                            ylab = "Cantidad",
                            col = "pink")

# Extraer info
liminf <- limites[-length(limites)]
limsup <- limites[-1]
Mc <- Histograma_Longitud$mids
ni <- Histograma_Longitud$counts
hi_s <- (ni / sum(ni)) * 100
Ni_asc <- cumsum(ni)
Hi_asc <- cumsum(hi_s)
Ni_dsc <- rev(cumsum(rev(ni)))
Hi_dsc <- rev(cumsum(rev(hi_s)))

# Verificar longitudes
sapply(list(liminf, limsup, Mc, ni, hi_s, Ni_asc, Hi_asc, Ni_dsc, Hi_dsc), length)
## [1] 20 20 20 20 20 20 20 20 20
# Crear tabla
TDF <- data.frame(liminf, limsup, Mc, ni, hi_s, Ni_asc, Hi_asc, Ni_dsc, Hi_dsc)
print(TDF)
##        liminf     limsup         Mc     ni         hi_s Ni_asc       Hi_asc
## 1  -116878109 -110139211 -113508660      2 6.565708e-04      2 6.565708e-04
## 2  -110139211 -103400313 -106769762      0 0.000000e+00      2 6.565708e-04
## 3  -103400313  -96661415 -100030864      0 0.000000e+00      2 6.565708e-04
## 4   -96661415  -89922517  -93291966      0 0.000000e+00      2 6.565708e-04
## 5   -89922517  -83183619  -86553068      0 0.000000e+00      2 6.565708e-04
## 6   -83183619  -76444721  -79814170      0 0.000000e+00      2 6.565708e-04
## 7   -76444721  -69705823  -73075272      0 0.000000e+00      2 6.565708e-04
## 8   -69705823  -62966925  -66336374      0 0.000000e+00      2 6.565708e-04
## 9   -62966925  -56228027  -59597476      0 0.000000e+00      2 6.565708e-04
## 10  -56228027  -49489129  -52858578      0 0.000000e+00      2 6.565708e-04
## 11  -49489129  -42750231  -46119680      0 0.000000e+00      2 6.565708e-04
## 12  -42750231  -36011333  -39380782      0 0.000000e+00      2 6.565708e-04
## 13  -36011333  -29272435  -32641884      0 0.000000e+00      2 6.565708e-04
## 14  -29272435  -22533537  -25902986      0 0.000000e+00      2 6.565708e-04
## 15  -22533537  -15794639  -19164088   2318 7.609656e-01   2320 7.616221e-01
## 16  -15794639   -9055741  -12425190 192173 6.308759e+01 194493 6.384921e+01
## 17   -9055741   -2316843   -5686292  67653 2.220949e+01 262146 8.605870e+01
## 18   -2316843    4422055    1052606  35543 1.166825e+01 297689 9.772695e+01
## 19    4422055   11160953    7791504   3697 1.213671e+00 301386 9.894062e+01
## 20   11160953   17899851   14530402   3227 1.059377e+00 304613 1.000000e+02
##    Ni_dsc     Hi_dsc
## 1  304613 100.000000
## 2  304611  99.999343
## 3  304611  99.999343
## 4  304611  99.999343
## 5  304611  99.999343
## 6  304611  99.999343
## 7  304611  99.999343
## 8  304611  99.999343
## 9  304611  99.999343
## 10 304611  99.999343
## 11 304611  99.999343
## 12 304611  99.999343
## 13 304611  99.999343
## 14 304611  99.999343
## 15 304611  99.999343
## 16 302293  99.238378
## 17 110120  36.150788
## 18  42467  13.941296
## 19   6924   2.273048
## 20   3227   1.059377
#Ojivas absolutas (ascendente y descendente) intersecadas
plot(limsup, Ni_asc, type = "o", col = "blue",
     xlab = "Longitud",
     ylab = "Frecuencia acumulada",
     main = "Grafico No.1: de Ojivas absolutas (ascendente y descendente)")
lines(liminf, Ni_dsc, type = "o", col = "red")

# Ojivas relativas (ascendente y descendente) intersecadas
plot(limsup, Hi_asc, type = "o", col = "blue",
     xlab = "Longitud",
     ylab = "Frecuencia acumulada (%)",
     main = "Grafico No.2: Ojivas relativas (ascendente y descendente)")
lines(liminf, Hi_dsc, type = "o", col = "red")

# Boxplot
boxplot(Longitud,
        horizontal = TRUE,
        col = "lightgreen",
        main = "Boxplot de la variable Longitud",
        xlab = "Longitud")

# -----------------------------
# Boxplot
boxplot(Longitud,
        horizontal = TRUE,
        col = "lightgreen",
        main = "Grafico No.3 Variable Longitud",
        xlab = "Longitud")