ANÁLISIS ESTADÍSTICO

1. CARGA DE LIBRERÍAS Y DATOS

#=========================ENCABEZADO================================
# TEMA: REGRESION POTENCIAL
# AUTOR: GRUPO 3
# FECHA: 03-2026
#===================================================================
library(dplyr)
library(knitr)
library(gt)

# Configuración de directorio y carga de archivo
setwd("C:/Users/HP/Documents/PROYECTO ESTADISTICA/RStudio")
datos <- read.csv("tablap.csv", header = TRUE, sep = ";", dec = ",")

2. TABLA PARES DE VALORES

# Extraer variables y asegurar formato numérico
profundidad <- as.numeric(datos$Vertical.depth.of.well)
produccion  <- as.numeric(datos$Total.gas.production.by.2023)

# Crear TPV y limpiar datos (Omitir NA, Ceros y Negativos)
TPV <- data.frame(profundidad = profundidad, produccion = produccion)
TPV <- na.omit(TPV)
TPV <- TPV[TPV$profundidad > 0 & TPV$produccion > 0, ]

# Ordenar la tabla por profundidad
TPV <- TPV[order(TPV$profundidad), ]

# --- RESETEAR NUMERACIÓN DE FILAS ---
row.names(TPV) <- NULL

# --- MOSTRAR TABLA (Primeros 20 registros con gt) ---
tabla_tpv_previa <- head(TPV, 20)
tabla_tpv_previa <- cbind(Nro = 1:nrow(tabla_tpv_previa), tabla_tpv_previa)

tabla_tpv_previa %>% 
  gt() %>%
  cols_label(Nro = "N°", profundidad = "Profundidad (x)", produccion = "Producción Gas (y)") %>%
  tab_header(title = md("**Tabla N° 4. Pares de Valores: Profundidad vs. Producción**")) %>%
  cols_align(align = "center") %>%
  tab_options(table.width = pct(80), column_labels.font.weight = "bold")
Tabla N° 4. Pares de Valores: Profundidad vs. Producción
Profundidad (x) Producción Gas (y)
1 2209 42633
2 2360 72314
3 2634 100773
4 2760 60378
5 2780 191668
6 2788 207041
7 2795 163134
8 2842 117873
9 2870 235741
10 2880 144451
11 2894 260381
12 2910 95627
13 2936 209400
14 2937 233491
15 2961 93692
16 2964 31559
17 2971 97053
18 2973 283745
19 2991 381001
20 3012 50227
# Definición de variables finales para el modelo (100% de datos)
x <- TPV$profundidad
y <- TPV$produccion

# Selección aleatoria del 10% solo para representación visual
set.seed(123)
indice_visual <- sample(1:nrow(TPV), nrow(TPV) / 10)

3. DIAGRAMA DE DISPERSION

plot(x[indice_visual], y[indice_visual],
     pch = 16,
     col = "blue",
     main = "Gráfica N°1: Diagrama de dispersión entre profundidad \n y producción total de gas",
     xlab = "Profundidad vertical del pozo",
     ylab = "Producción total de gas (2023)")

4. CONJETURA DE MODELO

# Transformación logarítmica para linealizar el modelo potencial
x1 <- log(x)
y1 <- log(y)

# Cálculo de parámetros sobre el 100% de los datos
regresion_Potencial <- lm(y1 ~ x1)
beta0 <- coef(regresion_Potencial)[1]
beta1 <- coef(regresion_Potencial)[2]

a <- exp(beta0) # Parámetro 'a'
b <- beta1      # Parámetro 'b' (exponente)

# Graficar comparación realidad vs modelo (Puntos representativos)
plot(x[indice_visual], y[indice_visual],
     pch = 16,
     col = "blue",
     main = "Gráfica N°2: Comparación de la realidad con el modelo potencial",
     xlab = "Profundidad vertical",
     ylab = "Producción de gas")

# Añadir curva del modelo
curve(a * x^b, from = min(x), to = max(x), add = TRUE, col = "red", lwd = 2)

# Mostrar Ecuación en el gráfico mediante mtext para seguir la estética limpia
eq_text <- paste0("Ecuación Potencial: Y = ", round(a, 3), " * x^", round(b, 2))
mtext(eq_text, side = 3, line = -2, cex = 1.2, col = "red", font = 2)

5. TEST DE APROBACION Y RESTRICCIONES

# Test de Pearson (sobre datos transformados)
r <- cor(x1, y1)

tabla_tests <- data.frame(
  Indicador = c("Coeficiente de Pearson (r)"),
  Valor = c(paste0(round(r * 100, 2), " %"))
)

# Imprimir la tabla de indicadores
tabla_tests %>% 
  gt() %>%
  tab_header(title = md("**Test de Aprobación del Modelo Potencial**")) %>%
  cols_align(align = "center") %>%
  tab_options(table.width = pct(60), column_labels.font.weight = "bold")
Test de Aprobación del Modelo Potencial
Indicador Valor
Coeficiente de Pearson (r) 74.31 %
# Generación del plot independiente para Restricciones
plot.new()
plot.window(xlim = c(0, 100), ylim = c(0, 100))

text(50, 85, "RESTRICCIONES DEL MODELO", cex = 1.4, font = 2, col = "#D9534F")

# Párrafos de texto explicativo estructurados de tu código base
parrafo_1 <- "El modelo potencial requiere valores estrictamente mayores a cero."
parrafo_2 <- "No admite profundidades ni producciones nulas o negativas."
parrafo_3 <- "Asimismo, la confiabilidad de las estimaciones disminuye notablemente"
parrafo_4 <- "si se intenta extrapolar la ecuación fuera del rango de datos observado."

text(50, 60, parrafo_1, cex = 1.1, font = 3, col = "black")
text(50, 48, parrafo_2, cex = 1.1, font = 3, col = "black")
text(50, 36, parrafo_3, cex = 1.1, font = 3, col = "black")
text(50, 24, parrafo_4, cex = 1.1, font = 3, col = "black")

rect(-6, 5, 106, 95, border = "#D9534F", lwd = 3)

6. CALCULO DE PRONOSTICOS

x_pronostico <- 5000 
T_Esp <- a * (x_pronostico^b)

plot.new()
plot.window(xlim = c(0, 100), ylim = c(0, 1))

rect(10, 0.4, 90, 0.6, col = "#E5E7E9", border = NA)
text(50, 0.85, "PRONÓSTICO DEL MODELO POTENCIAL", cex = 1.5, font = 2, col = "#2A9D8F")

texto_pregunta <- paste0("¿Cuál sería la producción si se tiene una profundidad de ", x_pronostico, "?")
text(50, 0.75, texto_pregunta, cex = 1.1, font = 3)

text(50, 0.5, paste0("R: ", round(T_Esp, 2)), cex = 1.6, font = 2, col = "#1F618D")
rect(10, 0.4, 90, 0.6, border = "#2A9D8F", lwd = 2)

7. CONCLUSION

## Entre la profundidad y la producción de gas existe una relación potencial, representada por el modelo f(x) = 0 * x^4.54. Ejemplo: A una profundidad de 5000, el modelo predice una producción de 759993.57.