ANÁLISIS ESTADÍSTICO

Carga de datos y Librerias

setwd("C:/Users/Grace/Downloads")

datos <- read.csv(
  "Sedimentos Marinos (1).csv",
  header = TRUE,
  sep = ";",
  dec = "."
)

#Carga de librerías

library(gt)
library(dplyr)
library(knitr)
library(stringr)

Agrupación en regiones principales

datos <- datos %>%
  mutate(
    Region = case_when(
      str_detect(toupper(AREA), "CARIBBEAN|PUERTO RICO|OCULINA|MONA|DESECHEO|BARRACUDA") 
        ~ "Caribbean Region",
      
      str_detect(toupper(AREA), "GULF OF MAINE|MAINE|IPSWICH|BAY OF FUNDY") 
        ~ "Gulf of Maine Region",
      
      str_detect(toupper(AREA), "GULF OF MEXICO|APALACHICOLA|DAUPHIN|CHANDELEUR") 
        ~ "Gulf of Mexico",
      
      str_detect(toupper(AREA), "LONG ISLAND|NEW YORK|HUDSON|FIRE ISLAND|BARNEGAT|LISOUND") 
        ~ "Long Island & New York Bight",
      
      str_detect(toupper(AREA), "NORTH CAROLINA|SOUTH CAROLINA|CAPE HATTERAS") 
        ~ "Southeast USA",
      
      str_detect(toupper(AREA), "MASSACHUSETTS|MASS BAY|MASSBAY|BOSTON|CAPE COD|MARTHA|BUZZARDS|NANTUCKET|PROVINCETOWN|STELLWAGEN") 
        ~ "New England Shelf",
      
      str_detect(toupper(AREA), "RHODE ISLAND|BLOCK ISLAND|SOUTHERN NEW ENGLAND") 
        ~ "Southern New England & Rhode Island",
      
      str_detect(toupper(AREA), "WASHINGTON|CALIFORNIA|ALASKA|PORT VALDEZ|RESSURECTION|WESTERN") 
        ~ "Western USA",
      
      TRUE ~ "Otros / No clasificado"
    )
  )

# Orden fijo de las categorías (importante para la gráfica)
datos$Region <- factor(datos$Region, levels = c(
  "Caribbean Region",
  "Gulf of Maine Region",
  "Gulf of Mexico",
  "Long Island & New York Bight",
  "Mid-Atlantic Coast",              # ← esta puede quedar vacía o con pocos casos
  "New England Shelf",
  "Southeast USA",
  "Southern New England & Rhode Island",
  "Western USA",
  "Otros / No clasificado"
))

Tabla de distribución de probabilidad

## Tabla de distribución de probabilidad (regiones agrupadas)
tabla_region <- as.data.frame(table(datos$Region))
colnames(tabla_region) <- c("Region", "ni")

tabla_region <- tabla_region %>%
  mutate(
    hi = round(ni / sum(ni), 4),
    P = round(hi * 100, 2)
  ) %>%
  arrange(factor(Region, levels = levels(datos$Region)))

# Fila total
total <- data.frame(
  Region = "TOTAL",
  ni = sum(tabla_region$ni),
  hi = round(sum(tabla_region$hi), 4),
  P = round(sum(tabla_region$P), 2)
)

TablaFinalAG <- rbind(tabla_region, total)

# Tabla formateada
tabla_AG_gt <- TablaFinalAG %>%
  gt() %>%
  tab_header(
    title = md("**Tabla N° 1**"),
    subtitle = md("Distribución de probabilidad por Región Geográfica (agrupada)")
  ) %>%
  tab_source_note(source_note = md("Autor: Grupo 3")) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    heading.border.bottom.color = "black",
    heading.border.bottom.width = px(2),
    column_labels.border.bottom.color = "black",
    column_labels.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 = Region == "TOTAL")
  )

tabla_AG_gt
Tabla N° 1
Distribución de probabilidad por Región Geográfica (agrupada)
Region ni hi P
Caribbean Region 491 0.0346 3.46
Gulf of Maine Region 629 0.0443 4.43
Gulf of Mexico 992 0.0698 6.98
Long Island & New York Bight 3644 0.2565 25.65
Mid-Atlantic Coast 0 0.0000 0.00
New England Shelf 4410 0.3105 31.05
Southeast USA 1100 0.0774 7.74
Southern New England & Rhode Island 369 0.0260 2.60
Western USA 262 0.0184 1.84
Otros / No clasificado 2308 0.1625 16.25
TOTAL 14205 1.0000 100.00
Autor: Grupo 3

Gráfica

## Gráfica
P_global <- as.numeric(TablaFinalAG$P[TablaFinalAG$Region != "TOTAL"])

colores <- c(rep("#1f77b4", 9), "#999999")  # 9 azules + gris para Otros

barplot(
  P_global,
  main = "Gráfica N°1: Distribución por Región Geográfica Principal",
  xlab = "Región",
  ylab = "Probabilidad (%)",
  names.arg = TablaFinalAG$Region[TablaFinalAG$Region != "TOTAL"],
  col = colores,
  border = "white",
  las = 2,
  cex.names = 0.9,
  ylim = c(0, 100)
)

Cálculo de probabilidad

## Región más probable (cálculo con regiones agrupadas)
# Quitamos la fila TOTAL
tabla_sin_total <- TablaFinalAG[
  TablaFinalAG$Region != "TOTAL",
]

# Encontramos la región con mayor porcentaje
region_mayor <- tabla_sin_total$Region[
  which.max(tabla_sin_total$P)
]

prob_mayor <- tabla_sin_total$P[
  which.max(tabla_sin_total$P)
]

# Resultado explicativo en gráfico de texto
plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
text(
  x = 1, y = 1,
  labels = paste(
    "Cálculo de probabilidad (regiones agrupadas)\n\n",
    "¿Qué región geográfica es más probable\n",
    "que concentre la mayor cantidad de\n",
    "muestras de sedimentos marinos?\n\n",
    "R: ", region_mayor, "\n",
    "Probabilidad = ", prob_mayor, " (%)",
    sep = ""
  ),
  cex = 1.4,
  col = "black",
  font = 2
)