# Librerias
library(readxl)
library(dplyr)
library(stringr)
library(gt)
library(e1071)
library(lubridate)
library(MASS)
library(knitr)
setwd("C:/Users/majke/Downloads/Proyecto Estadistica/RMARKDOWN")
archivo <- "tabela_de_pocos_janeiro_2018.xlsx"
tryCatch({
Datos_Brutos <- read_excel(archivo)
}, error = function(e) {
stop("Error al leer el archivo Excel. Verifica la ruta y el nombre.")
})Se seleccionó la Profundidad Vertical como variable independiente (X) debido a que representa el objetivo geológico fijo (la ubicación conocida del yacimiento).
Por consiguiente, la Profundidad de Perforación funciona como la variable dependiente (Y), pues representa la longitud operativa necesaria para alcanzar dicho objetivo.
datos_model <- Datos_Brutos %>%
dplyr::select(PROFUNDIDADE_VERTICAL_M, PROFUNDIDADE_SONDADOR_M) %>%
mutate(
x = abs(as.numeric(str_replace(as.character(PROFUNDIDADE_VERTICAL_M), ",", "."))),
y = abs(as.numeric(str_replace(as.character(PROFUNDIDADE_SONDADOR_M), ",", ".")))
) %>%
filter(!is.na(x) & !is.na(y) & x > 0 & y > 0 & x <= 15000)par(mar = c(5, 5, 4, 2))
plot(
datos_model$x, datos_model$y,
main = "Gráfica N°1: Dispersión de Prof. de Perforación en función de Prof. Vertical",
xlab = "Profundidad Vertical (m)",
ylab = "Profundidad de Perforación (m)",
col = "#2E4053", pch = 16, cex = 0.7,
frame.plot = FALSE
)
grid(nx = NULL, ny = NULL, col = "#D7DBDD", lty = "dotted")Se plantea el modelo matemático: y = mx + b
modelo <- lm(y ~ x, data = datos_model)
m <- coef(modelo)[2] # Pendiente
b <- coef(modelo)[1] # Intercepto
r <- cor(datos_model$x, datos_model$y) # Pearson
r2 <- summary(modelo)$r.squared # R²
ecuacion_txt <- paste0(
"y = ", round(m, 4), "x + ", round(b, 4)
)Se presenta el ajuste lineal incluyendo la banda de incertidumbre estadística (Intervalo de Confianza del 95%).
par(mar = c(5, 5, 4, 2))
plot(
datos_model$x, datos_model$y,
main = "Gráfica N°2: Modelo Lineal: Prof. de Perforación en función de Prof. Vertical",
xlab = "Profundidad Vertical (m)",
ylab = "Profundidad de Perforación (m)",
col = "#2E4053", pch = 16, cex = 0.6,
frame.plot = FALSE
)
grid(nx = NULL, ny = NULL, col = "#D7DBDD", lty = "dotted")
x_seq <- seq(min(datos_model$x), max(datos_model$x), length.out = 500)
predicciones <- predict(modelo,
newdata = data.frame(x = x_seq),
interval = "confidence",
level = 0.95)
# Banda de intervalo de confianza
polygon(
c(x_seq, rev(x_seq)),
c(predicciones[, "lwr"], rev(predicciones[, "upr"])),
col = rgb(0.5, 0.5, 0.5, 0.2), border = NA
)
# Línea del modelo
lines(x_seq, predicciones[, "fit"], col = "#C0392B", lwd = 3)
legend(
"topleft",
legend = c("Datos Reales", "Modelo Lineal", "I.C. 95%"),
col = c("#2E4053", "#C0392B", "gray"),
pch = c(16, NA, 15),
lwd = c(NA, 3, NA),
pt.cex = c(0.7, NA, 2),
bty = "n"
)La ecuación resultante es: y = 1.0371x + 157.033
tabla <- data.frame(
Variable = c("Prof. Vertical", "Prof. Perforación"),
Tipo = c("Independiente (X)", "Dependiente (Y)"),
Pearson = c(paste0(round(r * 100, 2), "%"), "—"),
R2 = c(paste0(round(r2 * 100, 2), "%"), "—"),
Intercepto = c(sprintf("%.4f", b), "—"),
Pendiente = c(sprintf("%.4f", m), "—"),
Ecuacion = c(ecuacion_txt, "—")
)
tabla %>%
gt() %>%
tab_header(
title = md("**RESUMEN DEL MODELO DE REGRESIÓN LINEAL**"),
subtitle = "Parámetros y Bondad de Ajuste"
) %>%
tab_source_note(source_note = "Autor: Anahi Macias") %>%
cols_label(
Variable = "Variable",
Tipo = "Tipo",
Pearson = "Pearson (r)",
R2 = "R²",
Intercepto = "Intercepto (b)",
Pendiente = "Pendiente (m)",
Ecuacion = "Ecuación"
) %>%
cols_align(align = "center", columns = everything()) %>%
tab_style(
style = list(
cell_fill(color = "#2E4053"),
cell_text(color = "white", weight = "bold")
),
# CORRECCIÓN: cells_title() sin groups= no aplica al subtítulo
locations = cells_title(groups = c("title", "subtitle"))
) %>%
tab_style(
style = list(
cell_fill(color = "#F2F3F4"),
cell_text(weight = "bold", color = "#2E4053")
),
locations = cells_column_labels()
) %>%
tab_options(
table.border.top.color = "#2E4053",
table.border.bottom.color = "#2E4053",
column_labels.border.bottom.color = "#2E4053",
data_row.padding = px(8)
)| RESUMEN DEL MODELO DE REGRESIÓN LINEAL | ||||||
| Parámetros y Bondad de Ajuste | ||||||
| Variable | Tipo | Pearson (r) | R² | Intercepto (b) | Pendiente (m) | Ecuación |
|---|---|---|---|---|---|---|
| Prof. Vertical | Independiente (X) | 95.44% | 91.09% | 157.0330 | 1.0371 | y = 1.0371x + 157.033 |
| Prof. Perforación | Dependiente (Y) | — | — | — | — | — |
| Autor: Anahi Macias | ||||||
Entre la profundidad vertical y la profundidad de perforación existe una relación de tipo lineal cuya ecuación matemática está representada por y = 1.0371x + 157.033, siendo ‘x’ la profundidad vertical en metros y ‘y’ la profundidad de perforación en metros, donde no existen restricciones.
El modelo presenta un coeficiente de Pearson de 95.44% y un coeficiente de determinación R² de 91.09%, lo que indica que la profundidad vertical explica la mayor parte de la variabilidad en la profundidad de perforación.
Por ejemplo, para una profundidad vertical de 3,000 m se estima una profundidad de perforación de 3268.32 m.