# Cargar librerías
library(moments)
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(gt)
# Cargar datos
datos <- read.csv("C:/Users/arian/OneDrive/Documentos/Universidad/3er SEMESTRE/estadistica/2026/datos/dataset_geologico_limpio_80.csv")
cat("Dimensiones:", nrow(datos), "observaciones:", ncol(datos), "variables\n\n")
## Dimensiones: 27784 observaciones: 58 variables
# Extraer variable
clasificacion_raw <- datos$CLASSIFICATION
cat("Primeros valores de CLASSIFICATION:\n")
## Primeros valores de CLASSIFICATION:
print(head(clasificacion_raw))
## [1] "GRAVELLY SEDIMENT" "SAND" "SAND"
## [4] "SAND" "GRAVELLY SEDIMENT" "SILTY SAND"
# Convertir a texto
clasificacion_raw <- as.character(clasificacion_raw)
# Eliminar espacios al inicio y final
clasificacion_raw <- trimws(clasificacion_raw)
# Convertir todo a mayúsculas
clasificacion_raw <- toupper(clasificacion_raw)
# Eliminar NA y cadenas vacías
clasificacion <- clasificacion_raw[
!is.na(clasificacion_raw) &
clasificacion_raw != ""
]
# Número de observaciones válidas
n <- length(clasificacion)
cat(
"Número de observaciones válidas (n):",
n,
"\n\n"
)
## Número de observaciones válidas (n): 26333
# Ver categorías después de limpiar
cat("Categorías encontradas:\n")
## Categorías encontradas:
print(sort(unique(clasificacion)))
## [1] "BEDROCK" "BOULDERS" "CLAY"
## [4] "CLAYEY SAND" "CLAYEY SILT" "GRAVEL"
## [7] "GRAVEL > 10%" "GRAVELLY SEDIMENT" "MUD"
## [10] "MUDDY SAND" "SAND" "SAND SILT CLAY"
## [13] "SANDY CLAY" "SANDY SILT" "SILT"
## [16] "SILTY CLAY" "SILTY SAND"
La variable CLASSIFICATION representa la clasificación sedimentaria asignada a cada muestra de sedimento marino según sus características granulométricas. Para este análisis se considera una variable cualitativa ordinal, debido a que las categorías pueden organizarse siguiendo un orden basado en el tamaño de las partículas sedimentarias, desde materiales de grano más grueso, como la grava, hasta materiales de grano más fino, como el limo y la arcilla. Para el análisis se eliminan los valores faltantes y se conservan únicamente las observaciones que presentan una clasificación válida.
Tabla de Distribucion de Frecuencia Simple
n <- length(clasificacion)
ni <- table(clasificacion)
hi <- prop.table(ni) * 100
Ni_asc <- cumsum(ni)
Hi_asc <- cumsum(hi)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_desc <- rev(cumsum(rev(hi)))
TablaFrecuencias_CLASSIFICATION <- data.frame(
clasif = names(ni),
ni = as.numeric(ni),
hi = round(as.numeric(hi), 2),
Ni_asc = as.numeric(Ni_asc),
Hi_asc = round(as.numeric(Hi_asc), 2),
Ni_desc = as.numeric(Ni_desc),
Hi_desc = round(as.numeric(Hi_desc), 2)
)
TablaFrecuencias_CLASSIFICATION[nrow(TablaFrecuencias_CLASSIFICATION) + 1, ] <-
c("TOTAL", sum(ni), sum(hi), NA, NA, NA, NA)
TablaFrecuencias_CLASSIFICATION
## clasif ni hi Ni_asc Hi_asc Ni_desc Hi_desc
## 1 BEDROCK 21 0.08 21 0.08 26333 100
## 2 BOULDERS 143 0.54 164 0.62 26312 99.92
## 3 CLAY 284 1.08 448 1.7 26169 99.38
## 4 CLAYEY SAND 147 0.56 595 2.26 25885 98.3
## 5 CLAYEY SILT 3585 13.61 4180 15.87 25738 97.74
## 6 GRAVEL 994 3.77 5174 19.65 22153 84.13
## 7 GRAVEL > 10% 66 0.25 5240 19.9 21159 80.35
## 8 GRAVELLY SEDIMENT 2465 9.36 7705 29.26 21093 80.1
## 9 MUD 4 0.02 7709 29.28 18628 70.74
## 10 MUDDY SAND 1 0 7710 29.28 18624 70.72
## 11 SAND 11652 44.25 19362 73.53 18623 70.72
## 12 SAND SILT CLAY 1227 4.66 20589 78.19 6971 26.47
## 13 SANDY CLAY 21 0.08 20610 78.27 5744 21.81
## 14 SANDY SILT 1326 5.04 21936 83.3 5723 21.73
## 15 SILT 606 2.3 22542 85.6 4397 16.7
## 16 SILTY CLAY 1579 6 24121 91.6 3791 14.4
## 17 SILTY SAND 2212 8.4 26333 100 2212 8.4
## 18 TOTAL 26333 100 <NA> <NA> <NA> <NA>
TablaFrecuencias_CLASSIFICATION %>%
gt() %>%
tab_header(
title = md("**Tabla Nº1**"),
subtitle = md("**Distribución de frecuencias de la variable CLASSIFICATION en depósitos marinos**")
) %>%
tab_source_note(
source_note = md("**Autor: Grupo 2**")
) %>%
cols_label(
clasif = "Mes",
ni = "Frecuencia (ni)",
hi = "Frecuencia relativa hi (%)",
Ni_asc = "Ni Asc",
Hi_asc = "Hi Asc (%)",
Ni_desc = "Ni Desc",
Hi_desc = "Hi Desc (%)"
) %>%
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 = clasif == "TOTAL"
)
)
| Tabla Nº1 | ||||||
| Distribución de frecuencias de la variable CLASSIFICATION en depósitos marinos | ||||||
| Mes | Frecuencia (ni) | Frecuencia relativa hi (%) | Ni Asc | Hi Asc (%) | Ni Desc | Hi Desc (%) |
|---|---|---|---|---|---|---|
| BEDROCK | 21 | 0.08 | 21 | 0.08 | 26333 | 100 |
| BOULDERS | 143 | 0.54 | 164 | 0.62 | 26312 | 99.92 |
| CLAY | 284 | 1.08 | 448 | 1.7 | 26169 | 99.38 |
| CLAYEY SAND | 147 | 0.56 | 595 | 2.26 | 25885 | 98.3 |
| CLAYEY SILT | 3585 | 13.61 | 4180 | 15.87 | 25738 | 97.74 |
| GRAVEL | 994 | 3.77 | 5174 | 19.65 | 22153 | 84.13 |
| GRAVEL > 10% | 66 | 0.25 | 5240 | 19.9 | 21159 | 80.35 |
| GRAVELLY SEDIMENT | 2465 | 9.36 | 7705 | 29.26 | 21093 | 80.1 |
| MUD | 4 | 0.02 | 7709 | 29.28 | 18628 | 70.74 |
| MUDDY SAND | 1 | 0 | 7710 | 29.28 | 18624 | 70.72 |
| SAND | 11652 | 44.25 | 19362 | 73.53 | 18623 | 70.72 |
| SAND SILT CLAY | 1227 | 4.66 | 20589 | 78.19 | 6971 | 26.47 |
| SANDY CLAY | 21 | 0.08 | 20610 | 78.27 | 5744 | 21.81 |
| SANDY SILT | 1326 | 5.04 | 21936 | 83.3 | 5723 | 21.73 |
| SILT | 606 | 2.3 | 22542 | 85.6 | 4397 | 16.7 |
| SILTY CLAY | 1579 | 6 | 24121 | 91.6 | 3791 | 14.4 |
| SILTY SAND | 2212 | 8.4 | 26333 | 100 | 2212 | 8.4 |
| TOTAL | 26333 | 100 | NA | NA | NA | NA |
| Autor: Grupo 2 | ||||||
La tabla original presenta 17 categorías sedimentarias, lo que dificulta su representación e interpretación gráfica. Por esta razón, las categorías se agrupan considerando el tipo de sedimento predominante y su relación con el tamaño de grano, obteniendo cinco grupos principales: GRAVA, ARENA, MIXTO, LIMO y ARCILLA. La agrupación permite reducir el número de categorías y representar de manera más clara la distribución de las muestras, manteniendo el criterio granulométrico desde materiales más gruesos hacia materiales más finos. El grupo MIXTO reúne clasificaciones que contienen una combinación de diferentes fracciones sedimentarias y que no pueden asignarse de manera adecuada a una única categoría principal.
Tabla de Distribucion Agrupada
# AGRUPACIÓN DE CLASSIFICATION
clasificacion_agrupada <- case_when(
# GRAVA / MATERIAL GRUESO
clasificacion %in% c(
"BEDROCK",
"BOULDERS",
"GRAVEL",
"GRAVEL > 10%",
"GRAVELLY SEDIMENT"
) ~ "GRAVA",
# ARENA
clasificacion %in% c(
"SAND",
"CLAYEY SAND",
"MUDDY SAND",
"SILTY SAND"
) ~ "ARENA",
# MEZCLA
clasificacion %in% c(
"SAND SILT CLAY",
"MUD"
) ~ "MIXTO",
# LIMO
clasificacion %in% c(
"SILT",
"CLAYEY SILT",
"SANDY SILT"
) ~ "LIMO",
# ARCILLA
clasificacion %in% c(
"CLAY",
"SANDY CLAY",
"SILTY CLAY"
) ~ "ARCILLA",
TRUE ~ NA_character_
)
clasificacion_agrupada <- factor(
clasificacion_agrupada,
levels = c(
"GRAVA",
"ARENA",
"MIXTO",
"LIMO",
"ARCILLA"
),
ordered = TRUE
)
# Frecuencia absoluta
ni_grupo <- table(clasificacion_agrupada)
# Frecuencia relativa
hi_grupo <- prop.table(ni_grupo) * 100
# Acumuladas ascendentes
Ni_asc_grupo <- cumsum(ni_grupo)
Hi_asc_grupo <- (
Ni_asc_grupo / sum(ni_grupo)
) * 100
# Acumuladas descendentes
Ni_desc_grupo <- rev(
cumsum(
rev(ni_grupo)
)
)
Hi_desc_grupo <- (
Ni_desc_grupo / sum(ni_grupo)
) * 100
# Crear tabla
TablaClasificacionAgrupada <- data.frame(
Clasificacion = names(ni_grupo),
ni = as.numeric(ni_grupo),
hi = round(as.numeric(hi_grupo), 2),
Ni_asc = as.numeric(Ni_asc_grupo),
Hi_asc = round(as.numeric(Hi_asc_grupo), 2),
Ni_desc = as.numeric(Ni_desc_grupo),
Hi_desc = round(as.numeric(Hi_desc_grupo), 2)
)
# TOTAL
TablaClasificacionAgrupada[
nrow(TablaClasificacionAgrupada) + 1,
] <- c(
"TOTAL",
sum(ni_grupo),
100,
NA,
NA,
NA,
NA
)
TablaClasificacionAgrupada
## Clasificacion ni hi Ni_asc Hi_asc Ni_desc Hi_desc
## 1 GRAVA 3689 14.01 3689 14.01 26333 100
## 2 ARENA 14012 53.21 17701 67.22 22644 85.99
## 3 MIXTO 1231 4.67 18932 71.89 8632 32.78
## 4 LIMO 5517 20.95 24449 92.85 7401 28.11
## 5 ARCILLA 1884 7.15 26333 100 1884 7.15
## 6 TOTAL 26333 100 <NA> <NA> <NA> <NA>
# TABLA MEJORADA - CLASSIFICATION AGRUPADA
TablaClasificacionAgrupada_gt <- TablaClasificacionAgrupada %>%
gt() %>%
tab_header(
title = md("**Tabla Nº2**"),
subtitle = md(
"**Distribución de frecuencias agrupada de la variable CLASSIFICATION**"
)
) %>%
tab_source_note(
source_note = md("**Autor: Grupo 2**")
) %>%
cols_label(
Clasificacion = "Clasificación",
ni = "Frecuencia (ni)",
hi = "hi (%)",
Ni_asc = "Ni ↑",
Hi_asc = "Hi ↑ (%)",
Ni_desc = "Ni ↓",
Hi_desc = "Hi ↓ (%)"
) %>%
# Porcentajes con 2 decimales
fmt_number(
columns = c(
hi,
Hi_asc,
Hi_desc
),
decimals = 2
) %>%
# Frecuencias sin decimales
fmt_number(
columns = c(
ni,
Ni_asc,
Ni_desc
),
decimals = 0
) %>%
# Resaltar fila TOTAL
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(
rows = Clasificacion == "TOTAL"
)
) %>%
# Formato general
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",
table.font.size = px(13),
heading.title.font.size = px(18),
heading.subtitle.font.size = px(14),
table.width = pct(100)
)
TablaClasificacionAgrupada_gt
| Tabla Nº2 | ||||||
| Distribución de frecuencias agrupada de la variable CLASSIFICATION | ||||||
| Clasificación | Frecuencia (ni) | hi (%) | Ni ↑ | Hi ↑ (%) | Ni ↓ | Hi ↓ (%) |
|---|---|---|---|---|---|---|
| GRAVA | 3689 | 14.01 | 3689 | 14.01 | 26333 | 100 |
| ARENA | 14012 | 53.21 | 17701 | 67.22 | 22644 | 85.99 |
| MIXTO | 1231 | 4.67 | 18932 | 71.89 | 8632 | 32.78 |
| LIMO | 5517 | 20.95 | 24449 | 92.85 | 7401 | 28.11 |
| ARCILLA | 1884 | 7.15 | 26333 | 100 | 1884 | 7.15 |
| TOTAL | 26333 | 100 | NA | NA | NA | NA |
| Autor: Grupo 2 | ||||||
# Datos para las gráficas
tabla_graf <- TablaClasificacionAgrupada[
TablaClasificacionAgrupada$Clasificacion != "TOTAL",
]
# Asegurar variables numéricas
tabla_graf$ni <- as.numeric(tabla_graf$ni)
tabla_graf$hi <- as.numeric(tabla_graf$hi)
tabla_graf$Ni_asc <- as.numeric(tabla_graf$Ni_asc)
tabla_graf$Hi_asc <- as.numeric(tabla_graf$Hi_asc)
tabla_graf$Ni_desc <- as.numeric(tabla_graf$Ni_desc)
tabla_graf$Hi_desc <- as.numeric(tabla_graf$Hi_desc)
# Total de observaciones
n <- sum(tabla_graf$ni)
par(mfrow = c(1,1))
par(mar = c(6,4,4,2))
barplot(
tabla_graf$ni,
names.arg = tabla_graf$Clasificacion,
las = 2,
cex.names = 0.8,
col = "lightgreen",
border = "black",
main = "Grafica 1: Distribucion de Cantidad por
Clasificacion Sedimentaria",
xlab = "Clasificacion",
ylab = "Cantidad (ni)",
ylim = c(0, max(tabla_graf$ni) * 1.15)
)
abline(h = 0, lwd = 1)
par(mfrow = c(1,1))
par(mar = c(6,4,4,2))
barplot(
tabla_graf$ni,
names.arg = tabla_graf$Clasificacion,
las = 2,
cex.names = 0.8,
col = "skyblue",
border = "black",
main = "Grafica 2: Distribucion Global de Cantidad por
Clasificacion Sedimentaria",
xlab = "Clasificacion",
ylab = "Cantidad (ni)",
ylim = c(0, n)
)
abline(h = 0, lwd = 1)
par(mfrow = c(1,1))
par(mar = c(6,4,4,2))
barplot(
tabla_graf$hi,
names.arg = tabla_graf$Clasificacion,
las = 2,
cex.names = 0.8,
col = "orchid",
border = "black",
main = "Grafica 3: Distribucion Porcentual por
Clasificacion Sedimentaria",
xlab = "Clasificacion",
ylab = "Porcentaje (%)",
ylim = c(0, max(tabla_graf$hi) * 1.15)
)
abline(h = 0, lwd = 1)
par(mfrow = c(1,1))
par(mar = c(6,4,4,2))
barplot(
tabla_graf$hi,
names.arg = tabla_graf$Clasificacion,
las = 2,
cex.names = 0.8,
col = "green",
border = "black",
main = "Grafica 4: Distribucion Global Porcentual por
Clasificacion Sedimentaria",
xlab = "Clasificacion",
ylab = "Porcentaje (%)",
ylim = c(0, 100)
)
abline(h = 0, lwd = 1)
layout(
matrix(c(1,2), nrow = 1),
widths = c(2.2, 1)
)
par(mar = c(2,2,4,1))
colores <- rainbow(nrow(tabla_graf))
pie(
tabla_graf$hi,
labels = paste0(
round(tabla_graf$hi, 2),
"%"
),
radius = 1,
col = colores,
main = "Grafica 5: Distribucion Porcentual de la
Clasificacion Sedimentaria",
cex.main = 0.9,
cex = 0.8
)
par(mar = c(2,0,4,2))
plot.new()
legend(
"center",
legend = tabla_graf$Clasificacion,
fill = colores,
title = "Clasificacion",
cex = 0.8,
bg = "white",
box.lwd = 0.7
)
layout(1)
# Tabla sin fila TOTAL
tabla_ind <- TablaClasificacionAgrupada[
TablaClasificacionAgrupada$Clasificacion != "TOTAL",
]
# Asegurar valores numéricos
tabla_ind$ni <- as.numeric(tabla_ind$ni)
tabla_ind$Hi_asc <- as.numeric(tabla_ind$Hi_asc)
# MODA
moda <- tabla_ind$Clasificacion[
which.max(tabla_ind$ni)
]
# MEDIANA ORDINAL
mediana_ordinal <- tabla_ind$Clasificacion[
which(tabla_ind$Hi_asc >= 50)[1]
]
# Mostrar resultados
cat("Moda:", moda, "\n")
## Moda: ARENA
cat("Mediana ordinal:", mediana_ordinal, "\n")
## Mediana ordinal: ARENA
# Crear tabla de indicadores
TablaIndicadores <- data.frame(
Indicador = c(
"Moda",
"Mediana ordinal"
),
Categoria = c(
moda,
mediana_ordinal
)
)
TablaIndicadores
## Indicador Categoria
## 1 Moda ARENA
## 2 Mediana ordinal ARENA
TablaIndicadores_gt <- TablaIndicadores %>%
gt() %>%
tab_header(
title = md("**Tabla Nº3**"),
subtitle = md(
"**Indicadores de posición de la variable CLASSIFICATION**"
)
) %>%
tab_source_note(
source_note = md("**Autor: Grupo 2**")
) %>%
cols_label(
Indicador = "Indicador de posición",
Categoria = "Clasificación sedimentaria"
) %>%
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",
table.font.size = px(13),
heading.title.font.size = px(18),
heading.subtitle.font.size = px(14),
table.width = pct(100)
)
TablaIndicadores_gt
| Tabla Nº3 | |
| Indicadores de posición de la variable CLASSIFICATION | |
| Indicador de posición | Clasificación sedimentaria |
|---|---|
| Moda | ARENA |
| Mediana ordinal | ARENA |
| Autor: Grupo 2 | |
La variable CLASSIFICATION presenta como clasificación sedimentaria más frecuente la categoría ARENA. Esto permite identificar que una parte importante de las muestras analizadas corresponde a sedimentos con predominio de partículas arenosas, reflejando una mayor presencia de este tipo de material dentro de los depósitos marinos estudiados.