knitr::opts_chunk$set(echo = TRUE)
setwd("C:/Users/LEO/Documents/ESTA")
Datos <- read.csv("tabela_de_pocos_janeiro_2018.csv", header = TRUE, sep = ";" , dec = ".", fileEncoding = "Latin1")

1 Carga de librerias

# 1. LIBRERÍAS Y CARGA DE DATOS
library(readxl)
library(dplyr)
library(gt)
library(e1071)

2 Carga de datos

Datos_Brutos <- read_xlsx("C:/Users/LEO/Documents/ESTA/tabela_de_pocos_janeiro_2018.xlsx", sheet = 1)
colnames(Datos_Brutos) <- trimws(colnames(Datos_Brutos))

3 Selección de variable

Datos <- Datos_Brutos %>%
  select(any_of(c("POCO", "COTA_ALTIMETRICA_M"))) %>%
  mutate(altitud_pozo_m = as.numeric(gsub(",", ".", as.character(COTA_ALTIMETRICA_M))))

Variable <- na.omit(Datos$altitud_pozo_m)

Variable <- Variable[Variable >= 0 & Variable < 5000]

if(length(Variable) == 0) {
  stop("ERROR: No hay datos válidos para la variable seleccionada.")
}

4 Frecuencia

N <- length(Variable)
K <- floor(1 + 3.322 * log10(N)) 
breaks_table <- seq(min(Variable), max(Variable), length.out = K + 1)

ni <- as.vector(table(cut(Variable, breaks = breaks_table, include.lowest = TRUE, right = FALSE)))
hi      <- (ni / sum(ni)) * 100 
Ni_asc  <- cumsum(ni)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_asc  <- cumsum(hi)
Hi_desc <- rev(cumsum(rev(hi)))

5 Tabla de Distribución de Frecuencia

A continuación se presenta la tabla de distribución de frecuencias obtenida para la Profundidad del Sondador.

5.1 Tabla general

# Creación de la Tabla de Distribución de Frecuencias (TDF)
TDF_Cota <- data.frame(
  Li      = round(breaks_table[1:K], 2), 
  Ls      = round(breaks_table[2:(K+1)], 2), 
  MC      = round((breaks_table[1:K] + breaks_table[2:(K+1)]) / 2, 2),            
  ni      = ni, 
  hi      = hi,
  Ni_asc  = Ni_asc, 
  Ni_desc = Ni_desc, 
  Hi_asc  = Hi_asc, 
  Hi_desc = Hi_desc
)
TDF_Cota %>%
  gt(rowname_col = "Li") %>% 
  tab_header(
    title = md("**DISTRIBUCIÓN DE FRECUENCIAS: COTA ALTIMÉTRICA**"),
    subtitle = md("Variable: **Cota altimetrica**")
  ) %>%
  tab_source_note(source_note = "Fuente: Datos ANP 2018") %>%
  
  grand_summary_rows(
    columns = c(ni, hi),
    
    fns = list("TOTAL" = ~sum(., na.rm = TRUE))
  ) %>%
  
  # Formateamos números enteros 
  fmt_number(
    columns = c(ni, Ni_asc, Ni_desc),
    decimals = 0,
    use_seps = TRUE
  ) %>%
  
  # Formateamos los porcentajes a 2 decimales 
  fmt_number(
    columns = c(hi, Hi_asc, Hi_desc),
    decimals = 2
  ) %>%
  
  cols_label(
    Ls = "Lím. Sup", MC = "Marca Clase (Xi)", 
    ni = "ni", hi = "hi (%)", 
    Ni_asc = "Ni (Asc)", Ni_desc = "Ni (Desc)",
    Hi_asc = "Hi (Asc)", Hi_desc = "Hi (Desc)"
  ) %>%
  
  tab_stubhead(label = "Lím. Inf") %>% 
  
  # Alineación de datos al centro
  cols_align(align = "center", columns = everything()) %>%
  
  # Alineación del Stub (Lím. Inf y la palabra TOTAL) al centro
  tab_style(
    style = cell_text(align = "center"),
    locations = cells_stub()
  ) %>%
  
  # Estética de los encabezados (Azul oscuro / Verde petróleo con texto blanco)
  tab_style(
    style = list(cell_fill(color = "#1F4E5B"), cell_text(color = "white", weight = "bold")), 
    locations = list(cells_title(), cells_column_labels(), cells_stubhead())
  ) %>%
  
  tab_options(
    table.border.top.style = "none",
    table.border.bottom.color = "#2E4053",
    column_labels.border.bottom.color = "#2E4053",
    data_row.padding = px(6)
  )
DISTRIBUCIÓN DE FRECUENCIAS: COTA ALTIMÉTRICA
Variable: Cota altimetrica
Lím. Inf Lím. Sup Marca Clase (Xi) ni hi (%) Ni (Asc) Ni (Desc) Hi (Asc) Hi (Desc)
0.00 314.07 157.04 10,862 99.49 10,862 10,918 99.49 100.00
314.07 628.14 471.11 16 0.15 10,878 56 99.63 0.51
628.14 942.21 785.18 29 0.27 10,907 40 99.90 0.37
942.21 1256.29 1099.25 8 0.07 10,915 11 99.97 0.10
1256.29 1570.36 1413.32 1 0.01 10,916 3 99.98 0.03
1570.36 1884.43 1727.39 0 0.00 10,916 2 99.98 0.02
1884.43 2198.50 2041.46 0 0.00 10,916 2 99.98 0.02
2198.50 2512.57 2355.54 0 0.00 10,916 2 99.98 0.02
2512.57 2826.64 2669.61 0 0.00 10,916 2 99.98 0.02
2826.64 3140.71 2983.68 0 0.00 10,916 2 99.98 0.02
3140.71 3454.79 3297.75 0 0.00 10,916 2 99.98 0.02
3454.79 3768.86 3611.82 0 0.00 10,916 2 99.98 0.02
3768.86 4082.93 3925.89 0 0.00 10,916 2 99.98 0.02
4082.93 4397.00 4239.96 2 0.02 10,918 2 100.00 0.02
TOTAL 10918 100
Fuente: Datos ANP 2018

5.2 Tabla simplificada

min_val <- 0
max_val <- ceiling(max(Variable) / 400) * 400 
breaks_table <- seq(min_val, max_val, by = 400)
K <- length(breaks_table) - 1

ni      <- as.vector(table(cut(Variable, breaks = breaks_table, include.lowest = TRUE, right = FALSE)))
hi      <- (ni / sum(ni)) * 100 
Ni_asc  <- cumsum(ni)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_asc  <- cumsum(hi)
Hi_desc <- rev(cumsum(rev(hi)))

TDF_Cota <- data.frame(
  Li      = round(breaks_table[1:K], 2), 
  Ls      = round(breaks_table[2:(K+1)], 2), 
  MC      = round((breaks_table[1:K] + breaks_table[2:(K+1)]) / 2, 2),            
  ni      = ni, 
  hi      = hi,
  Ni_asc  = Ni_asc, 
  Ni_desc = Ni_desc, 
  Hi_asc  = Hi_asc, 
  Hi_desc = Hi_desc
)

TDF_Cota %>%
  gt(rowname_col = "Li") %>% 
  tab_header(
    title = md("**DISTRIBUCIÓN DE FRECUENCIAS: COTA ALTIMÉTRICA (AMPLITUD 400)**"),
    subtitle = md("Variable: **Cota altimétrica**")
  ) %>%
  tab_source_note(source_note = "Fuente: Datos ANP 2018") %>%
  
  grand_summary_rows(
    columns = c(ni, hi),
    fns = list("TOTAL" = ~sum(., na.rm = TRUE))
  ) %>%
  
  fmt_number(
    columns = c(ni, Ni_asc, Ni_desc),
    decimals = 0,
    use_seps = TRUE
  ) %>%
  
  fmt_number(
    columns = c(hi, Hi_asc, Hi_desc),
    decimals = 2
  ) %>%
  
  cols_label(
    Ls = "Lím. Sup", MC = "Marca Clase (Xi)", 
    ni = "ni", hi = "hi (%)", 
    Ni_asc = "Ni (Asc)", Ni_desc = "Ni (Desc)",
    Hi_asc = "Hi (Asc)", Hi_desc = "Hi (Desc)"
  ) %>%
  
  tab_stubhead(label = "Lím. Inf") %>% 
  cols_align(align = "center", columns = everything()) %>%
  
  tab_style(
    style = cell_text(align = "center"),
    locations = cells_stub()
  ) %>%
  
  tab_style(
    style = list(cell_fill(color = "#1F4E5B"), cell_text(color = "white", weight = "bold")), 
    locations = list(cells_title(), cells_column_labels(), cells_stubhead())
  ) %>%
  
  tab_options(
    table.border.top.style = "none",
    table.border.bottom.color = "#2E4053",
    column_labels.border.bottom.color = "#2E4053",
    data_row.padding = px(6)
  )
DISTRIBUCIÓN DE FRECUENCIAS: COTA ALTIMÉTRICA (AMPLITUD 400)
Variable: Cota altimétrica
Lím. Inf Lím. Sup Marca Clase (Xi) ni hi (%) Ni (Asc) Ni (Desc) Hi (Asc) Hi (Desc)
0 400 200 10,867 99.53 10,867 10,918 99.53 100.00
400 800 600 22 0.20 10,889 51 99.73 0.47
800 1200 1000 26 0.24 10,915 29 99.97 0.27
1200 1600 1400 1 0.01 10,916 3 99.98 0.03
1600 2000 1800 0 0.00 10,916 2 99.98 0.02
2000 2400 2200 0 0.00 10,916 2 99.98 0.02
2400 2800 2600 0 0.00 10,916 2 99.98 0.02
2800 3200 3000 0 0.00 10,916 2 99.98 0.02
3200 3600 3400 0 0.00 10,916 2 99.98 0.02
3600 4000 3800 0 0.00 10,916 2 99.98 0.02
4000 4400 4200 2 0.02 10,918 2 100.00 0.02
TOTAL 10918 100
Fuente: Datos ANP 2018

6 Gráficas de Distribución de Frecuencia

6.1 Histogramas de Frecuencia

col_gris_azulado <- "#5D6D7E"
col_ejes <- "#2E4053"

# GRÁFICO 1: Histograma Absoluto 
par(mar = c(8, 5, 4, 2)) 
h1_abs <- hist(
  Variable, 
  breaks = breaks_table, 
  main = "Gráfica No.1: Distribución Absoluta (Cota Altimétrica)",
  xlab = "Altitud Pozo (m)", 
  ylab = "Frecuencia Absoluta",
  col = col_gris_azulado, 
  border = "white", 
  axes = FALSE,
  ylim = c(0, max(ni) * 1.1)
)
axis(1, at = round(breaks_table, 0), las = 2, cex.axis = 0.7)
axis(2)
grid(nx = NA, ny = NULL, col = "#D7DBDD", lty = "dotted")

# GRÁFICO 2: Histograma Global 
par(mar = c(8, 5, 4, 2))
h1_glob <- hist(
  Variable, 
  breaks = breaks_table, 
  main = "Gráfica N°2: Distribución Global",
  xlab = "Altitud Pozo (m)", 
  ylab = "Frecuencia Total",
  col = col_gris_azulado, 
  border = "white", 
  axes = FALSE, 
  ylim = c(0, sum(ni))
)
axis(1, at = round(breaks_table, 0), las = 2, cex.axis = 0.7)
axis(2)
grid(nx = NA, ny = NULL, col = "#D7DBDD", lty = "dotted")

6.1.0.1 Gráficos Porcentuales

# GRÁFICO 3: Porcentajes (Local) 
par(mar = c(8, 5, 4, 2))

# 1. Calculamos el histograma base
h3_obj <- hist(Variable, breaks = breaks_table, plot = FALSE)

# 2. Forzamos los counts a que sean los porcentajes reales (0 a 100)
h3_obj$counts <- (h3_obj$counts / sum(h3_obj$counts)) * 100

# 3. Graficamos con freq = TRUE para que respete la altura porcentual exacta
plot(h3_obj, col = col_gris_azulado, border = "white", axes = FALSE, freq = TRUE,
     main = "Gráfica N°3: Distribución Porcentual (Local)",
     xlab = "Altitud Pozo (m)", ylab = "Porcentaje (%)",
     ylim = c(0, 105))

axis(1, at = breaks_table, las = 2, cex.axis = 0.7)
axis(2, at = seq(0, 100, by = 20), labels = paste0(seq(0, 100, by = 20), "%"))

# 4. Ponemos el texto exactamente encima de cada barra
text(
  x = h3_obj$mids, 
  y = h3_obj$counts, 
  label = paste0(round(h3_obj$counts, 1), "%"), 
  pos = 3, 
  cex = 0.6, 
  col = col_ejes, 
  xpd = TRUE
)
box(bty = "l")
grid(nx = NA, ny = NULL, col = "#D7DBDD", lty = "dotted")

# GRÁFICO 4: Global Porcentual 
par(mar = c(8, 5, 4, 2))

h4_obj <- hist(Variable, breaks = breaks_table, plot = FALSE)
h4_obj$counts <- (h4_obj$counts / sum(h4_obj$counts)) * 100

plot(h4_obj, col = col_gris_azulado, border = "white", axes = FALSE, freq = TRUE,
     main = "Gráfica No.4: Distribución Porcentual (Global)",
     xlab = "Altitud Pozo (m)", ylab = "% del Total",
     ylim = c(0, 105))

axis(1, at = breaks_table, las = 2, cex.axis = 0.7)
axis(2, at = seq(0, 100, by = 20), labels = paste0(seq(0, 100, by = 20), "%"))

text(
  x = h4_obj$mids, 
  y = h4_obj$counts, 
  label = paste0(round(h4_obj$counts, 1), "%"), 
  pos = 3, 
  cex = 0.6, 
  col = col_ejes, 
  xpd = TRUE
)
box(bty = "l")
grid(nx = NA, ny = NULL, col = "#D7DBDD", lty = "dotted")

6.2 Diagrama de Caja y Ojivas

# GRÁFICO 5: Boxplot
par(mar = c(8, 5, 4, 2))
boxplot(Variable, horizontal = TRUE, col = col_gris_azulado, 
        main = "Gráfica No.5: Diagrama de Caja (Cota Altimétrica)",
        xlab = "Cota Altimétrica (m)", outline = TRUE, outpch = 19, 
        outcol = "#C0392B", axes = FALSE, xlim = c(0.7, 1.3),
        ylim = c(0, 500)) 

axis(1, at = seq(0, 500, by = 50), las = 2, cex.axis = 0.7)
box()

# GRÁFICO 6: Ojivas
par(mar = c(5, 5, 4, 8), xpd = TRUE) 
x_vals <- breaks_table
plot(x_vals, c(0, Ni_asc), type = "o", col = "#2E4053", lwd=2, pch=19, axes=F,
     main = "Gráfica No.6: Ojivas Ascendente y Descendente",
     xlab = "Altitud Pozo (m)", ylab = "Frecuencia acumulada")
lines(x_vals, c(Ni_desc, 0), type = "o", col = "#C0392B", lwd=2, pch=19)
axis(1, at = round(breaks_table,0), las=2, cex.axis=0.6)
axis(2)
legend("right", legend = c("Asc", "Desc"), col = c("#2E4053", "#C0392B"), lty = 1, pch = 19, inset = c(-0.15, 0), bty="n")
grid()

7 Indicadores Estadísticos

media_val   <- mean(Variable)
mediana_val <- median(Variable)
sd_val      <- sd(Variable)

status_atipicos <- if(length(boxplot.stats(Variable)$out) > 0) {
  paste0(length(boxplot.stats(Variable)$out), " [", round(min(boxplot.stats(Variable)$out), 2), "; ", round(max(boxplot.stats(Variable)$out), 2), "]")
} else { "0 (Sin atípicos)" }

df_resumen <- data.frame(
  Variable = "Cota altimetrica",
  Rango = paste0("[", round(min(Variable), 2), "; ", round(max(Variable), 2), "]"),
  X = media_val,
  Me = mediana_val,
  Mo = paste(round(TDF_Cota$MC[TDF_Cota$ni == max(TDF_Cota$ni)], 2), collapse = ", "),
  Varianza = var(Variable),
  sd = sd_val,
  CV = (sd_val / abs(media_val)) * 100,
  As = skewness(Variable, type = 2),
  K = kurtosis(Variable, type = 2),
  Atipicos = status_atipicos
)

df_resumen %>%
  gt() %>%
  tab_header(title = md("**CONCLUSIONES Y ESTADÍSTICOS**")) %>%
  cols_label(
    X = "X",
    Me = "Me",
    Mo = "Mo",
    sd = "sd",
    CV = "CV",
    As = "As",
    K = "K",
    Atipicos = "Valores atípicos"
  ) %>%
  fmt_number(columns = c(X, Me, Varianza, sd, CV, K), decimals = 2) %>%
  fmt_number(columns = As, decimals = 4) %>%
  tab_options(
    column_labels.background.color = "#2E4053",
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    heading.border.bottom.style = "solid",
    column_labels.border.top.style = "solid",
    column_labels.border.bottom.style = "solid",
    data_row.padding = px(8)
  ) %>%
  tab_style(
    style = list(
      cell_text(weight = "bold", color = "white"),
      cell_borders(sides = c("left", "right"), color = "#D3D3D3", weight = px(1))
    ),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_borders(sides = c("left", "right"), color = "#D3D3D3", weight = px(1)),
    locations = cells_body()
  )
CONCLUSIONES Y ESTADÍSTICOS
Variable Rango X Me Mo Varianza sd CV As K Valores atípicos
Cota altimetrica [0; 4397] 36.51 12.29 200 8,200.43 90.56 248.03 23.3593 976.74 443 [132.08; 4397]

8 Conclusiones

Los valores de Cota altimetrica fluctúan entre 0 y 4397 y giran en torno a 12.29, con una desviación estándar de 90.56, con 443 valores atípicos en el rango de 132.08 a 4397, siendo un conjunto de datos heterogéneo, cuyos valores se agrupan fuertemente en la parte baja de Cota altimetrica. Por lo anterior, el comportamiento es perjudicial, ya que más del 99% de los pozos se concentran en cotas muy bajas, limitando la diversificación altimétrica y evidenciando una dependencia casi total de zonas de baja altura que restringen la exploración en diferentes relieves topográficos.