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))

# Cambio de variable a PROFUNDIDADE_SONDADOR_M
Datos <- Datos_Brutos %>%
  select(any_of(c("POCO", "PROFUNDIDADE_SONDADOR_M"))) %>%
  mutate(Variable_Analisis = as.numeric(gsub(",", ".", as.character(PROFUNDIDADE_SONDADOR_M))))

3 Selección de variable

Variable <- na.omit(Datos$Variable_Analisis)
Variable <- Variable[Variable > 0 & Variable < 15000]

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)

# Cálculo de ni usando cut
ni <- as.vector(table(cut(Variable, breaks = breaks_table, include.lowest = TRUE, right = FALSE)))

# Cálculo de vectores estadísticos
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

TDF_Sondador <- 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 = round(hi, 2),
  Ni_asc = Ni_asc, 
  Ni_desc = Ni_desc, 
  Hi_asc = round(Hi_asc, 2), 
  Hi_desc = round(Hi_desc, 2)
)

TDF_Sondador %>%
  gt(rowname_col = "Li") %>%
  tab_header(
    title = md("**DISTRIBUCIÓN DE FRECUENCIAS: PROFUNDIDAD SONDADOR**"),
    subtitle = md("Variable: **PROFUNDIDADE_SONDADOR_M**")
  ) %>%
  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 = list(cell_fill(color = "#2E4053"), cell_text(color = "white", weight = "bold")),
    locations = list(cells_title(), cells_column_labels(), cells_stubhead())
  ) %>%
  tab_options(
    table.border.top.color = "#2E4053",
    table.border.bottom.color = "#2E4053",
    column_labels.border.bottom.color = "#2E4053",
    data_row.padding = px(6)
  )
DISTRIBUCIÓN DE FRECUENCIAS: PROFUNDIDAD SONDADOR
Variable: PROFUNDIDADE_SONDADOR_M
Lím. Inf Lím. Sup Marca Clase (Xi) ni hi (%) Ni (Asc) Ni (Desc) Hi (Asc) Hi (Desc)
5.00 543.35 274.17 6,150 23.37 6,150 26,312 23.37 100.00
543.35 1081.69 812.52 6,823 25.93 12,973 20,162 49.30 76.63
1081.69 1620.04 1350.87 4,181 15.89 17,154 13,339 65.19 50.70
1620.04 2158.39 1889.21 2,117 8.05 19,271 9,158 73.24 34.81
2158.39 2696.73 2427.56 1,684 6.40 20,955 7,041 79.64 26.76
2696.73 3235.08 2965.91 2,131 8.10 23,086 5,357 87.74 20.36
3235.08 3773.43 3504.25 1,512 5.75 24,598 3,226 93.49 12.26
3773.43 4311.77 4042.60 687 2.61 25,285 1,714 96.10 6.51
4311.77 4850.12 4580.95 397 1.51 25,682 1,027 97.61 3.90
4850.12 5388.47 5119.29 290 1.10 25,972 630 98.71 2.39
5388.47 5926.81 5657.64 221 0.84 26,193 340 99.55 1.29
5926.81 6465.16 6195.99 80 0.30 26,273 119 99.85 0.45
6465.16 7003.51 6734.33 27 0.10 26,300 39 99.95 0.15
7003.51 7541.85 7272.68 9 0.03 26,309 12 99.99 0.05
7541.85 8080.20 7811.03 3 0.01 26,312 3 100.00 0.01
TOTAL 26312 99.99
Fuente: Datos ANP 2018

5.2 Tabla simplificada

# Frecuencia modificada de 400 en 400
min_val <- floor(min(Variable) / 400) * 400
max_val <- ceiling(max(Variable) / 400) * 400 
breaks_table <- seq(min_val, max_val, by = 400)
K <- length(breaks_table) - 1

# Cálculo de ni usando cut
ni <- as.vector(table(cut(Variable, breaks = breaks_table, include.lowest = TRUE, right = FALSE)))

# Cálculo de vectores estadísticos
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_Sondador <- 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 = round(hi, 2),
  Ni_asc = Ni_asc, 
  Ni_desc = Ni_desc, 
  Hi_asc = round(Hi_asc, 2), 
  Hi_desc = round(Hi_desc, 2)
)

TDF_Sondador %>%
  gt(rowname_col = "Li") %>%
  tab_header(
    title = md("**DISTRIBUCIÓN DE FRECUENCIAS: PROFUNDIDAD SONDADOR**"),
    subtitle = md("Variable: **PROFUNDIDADE_SONDADOR_M** (Amplitud = 400)")
  ) %>%
  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 = list(cell_fill(color = "#2E4053"), cell_text(color = "white", weight = "bold")),
    locations = list(cells_title(), cells_column_labels(), cells_stubhead())
  ) %>%
  tab_options(
    table.border.top.color = "#2E4053",
    table.border.bottom.color = "#2E4053",
    column_labels.border.bottom.color = "#2E4053",
    data_row.padding = px(6)
  )
DISTRIBUCIÓN DE FRECUENCIAS: PROFUNDIDAD SONDADOR
Variable: PROFUNDIDADE_SONDADOR_M (Amplitud = 400)
Lím. Inf Lím. Sup Marca Clase (Xi) ni hi (%) Ni (Asc) Ni (Desc) Hi (Asc) Hi (Desc)
0 400 200 3,819 14.51 3,819 26,312 14.51 100.00
400 800 600 5,577 21.20 9,396 22,493 35.71 85.49
800 1200 1000 4,266 16.21 13,662 16,916 51.92 64.29
1200 1600 1400 3,355 12.75 17,017 12,650 64.67 48.08
1600 2000 1800 1,755 6.67 18,772 9,295 71.34 35.33
2000 2400 2200 1,196 4.55 19,968 7,540 75.89 28.66
2400 2800 2600 1,362 5.18 21,330 6,344 81.07 24.11
2800 3200 3000 1,642 6.24 22,972 4,982 87.31 18.93
3200 3600 3400 1,260 4.79 24,232 3,340 92.09 12.69
3600 4000 3800 718 2.73 24,950 2,080 94.82 7.91
4000 4400 4200 399 1.52 25,349 1,362 96.34 5.18
4400 4800 4600 296 1.12 25,645 963 97.47 3.66
4800 5200 5000 207 0.79 25,852 667 98.25 2.53
5200 5600 5400 229 0.87 26,081 460 99.12 1.75
5600 6000 5800 129 0.49 26,210 231 99.61 0.88
6000 6400 6200 56 0.21 26,266 102 99.83 0.39
6400 6800 6600 30 0.11 26,296 46 99.94 0.17
6800 7200 7000 12 0.05 26,308 16 99.98 0.06
7200 7600 7400 1 0.00 26,309 4 99.99 0.02
7600 8000 7800 2 0.01 26,311 3 100.00 0.01
8000 8400 8200 1 0.00 26,312 1 100.00 0.00
TOTAL 26312 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"
h_base <- hist(Variable, breaks = breaks_table, plot = FALSE)

# GRÁFICO 1: Histograma Absoluto
par(mar = c(8, 5, 4, 2)) 
plot(h_base, 
     main = "Gráfica No.1: Distribución Absoluta (Sondador)",
     xlab = "Profundidad Sondador (m)", ylab = "Frecuencia Absoluta",
     col = col_gris_azulado, border = "white", axes = FALSE,
     ylim = c(0, max(h_base$counts) * 1.1)) 
axis(1, at = round(h_base$breaks, 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))
plot(h_base, 
     main = "Gráfica N°2: Distribución Global (Sondador)",
     xlab = "Profundidad Sondador (m)", ylab = "Total Pozos",
     col = col_gris_azulado, border = "white", axes = FALSE, 
     ylim = c(0, sum(h_base$counts))) 
axis(1, at = round(h_base$breaks, 0), las = 2, cex.axis = 0.7)
axis(2)
grid(nx=NA, ny=NULL, col="#D7DBDD", lty="dotted")

6.2 Gráficos Porcentuales

h_porc <- h_base
h_porc$counts <- (h_porc$counts / sum(h_porc$counts)) * 100

# GRÁFICO 3: Porcentajes (Local)
par(mar = c(8, 5, 4, 2))
plot(h_porc,
     main = "Gráfica N°3: Distribución Porcentual (Local)",
     xlab = "Profundidad Sondador (m)", ylab = "Porcentaje (%)",
     col = col_gris_azulado, border = "white", axes = FALSE, freq = TRUE,
     ylim = c(0, max(h_porc$counts)*1.2))
axis(1, at = round(h_base$breaks, 0), las = 2, cex.axis = 0.7)
axis(2)
text(x = h_base$mids, y = h_porc$counts, label = paste0(round(h_porc$counts, 1), "%"), pos = 3, cex = 0.6, col = col_ejes)

# GRÁFICO 4: Global Porcentual
par(mar = c(8, 5, 4, 2))
plot(h_porc,
     main = "Gráfica No.4: Distribución Porcentual (Global)",
     xlab = "Profundidad Sondador (m)", ylab = "% del Total", 
     col = col_gris_azulado, border = "white", axes = FALSE, freq = TRUE,
     ylim = c(0, 100))
axis(1, at = round(h_base$breaks, 0), las = 2, cex.axis = 0.7)
text(x = h_base$mids, y = h_porc$counts, label = paste0(round(h_porc$counts, 1), "%"), pos = 3, cex = 0.6, col = col_ejes)
axis(2)

6.3 Diagrama de Caja y Ojivas

# GRÁFICO 5: Boxplot
par(mar = c(5, 5, 4, 2))
boxplot(Variable, horizontal = TRUE, col = col_gris_azulado, 
        main = "Gráfica No.5: Diagrama de Caja (Sondador)",
        xlab = "Profundidad Sondador (m)", outline = TRUE, outpch = 19, outcol = "#C0392B") 
axis(1, at = pretty(Variable, n=20))

# 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 = "Profundidad Sondador (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 = "Profundidad Sondador (m)",
  Rango = paste0("[", round(min(Variable), 2), "; ", round(max(Variable), 2), "]"),
  X = media_val,
  Me = mediana_val,
  Mo = paste(round(TDF_Sondador$MC[TDF_Sondador$ni == max(TDF_Sondador$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**"), subtitle = "Variable: PROFUNDIDADE_SONDADOR_M") %>%
  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) %>%
  
  cols_width(
    Variable ~ px(140),
    Rango ~ px(120),
    Mo ~ px(100),
    Atipicos ~ px(220), 
    everything() ~ px(85) 
  ) %>%
  
  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: PROFUNDIDADE_SONDADOR_M
Variable Rango X Me Mo Varianza sd CV As K Valores atípicos
Profundidad Sondador (m) [5; 8080.2] 1,557.01 1,119.00 600 1,615,037.78 1,270.84 81.62 1.2423 1.10 586 [4931; 8080.2]

8 Conclusiones

Los valores de Profundidad Sondador (m) fluctúan entre 5 y 8080.2 y giran en torno a 1119.00, con una desviación estándar de 1270.84, con 586 valores atípicos en el rango de 4931 a 8080.2, siendo un conjunto de datos heterogéneo, cuyos valores se agrupan fuertemente en la parte baja de Profundidad Sondador (m). Por lo anterior, el comportamiento es perjudicial, ya que más del 64% de los pozos se concentran en los primeros 1200 metros, limitando el aprovechamiento de reservas profundas y la rentabilidad global de la perforación.