Modelo de Regresión Exponencial
setwd("/cloud/project/")
datos<-read.csv("DerramesEEUU.csv", header = TRUE, sep=";" , dec=",",na.strings ="-")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
x_var <- as.numeric(datos$RecuperacionLiquidoBarriles)
y_var <- as.numeric(datos$LiberacionInvoluntariaBarriles)
valid_index <- complete.cases(x_var, y_var) & x_var > 10 & x_var < 40 & y_var > 10 & y_var < 40
x_var <- x_var[valid_index]
y_var <- y_var[valid_index]
eliminar_outliers <- function(x) {
Q1 <- quantile(x, 0.25)
Q3 <- quantile(x, 0.75)
IQR <- Q3 - Q1
x >= (Q1 - 1.5 * IQR) & x <= (Q3 + 1.5 * IQR)
}
sin_outliers <- eliminar_outliers(x_var) & eliminar_outliers(y_var)
x_var <- x_var[sin_outliers]
y_var <- y_var[sin_outliers]
y_log <- log(y_var)
# -------- Ajuste del modelo exponencial --------
modelo_exp <- lm(y_log ~ x_var)
# -------- Coeficientes --------
B0 <- coef(modelo_exp)[1]
B1 <- coef(modelo_exp)[2]
a <- exp(B0)
b <- B1
cat("MODELO EXPONENCIAL:\n")
## MODELO EXPONENCIAL:
cat("LiberacionInvoluntaria =", round(a,3), "* exp(", round(b,6),
"* RecuperacionLiquidoBarriles )\n\n")
## LiberacionInvoluntaria = 8.604 * exp( 0.04302 * RecuperacionLiquidoBarriles )
plot(x_var, y_var,
col = "darkred", pch = 16,
xlab = "Recuperación de líquido (barriles)",
ylab = "Liberación involuntaria (barriles)",
main = "Regresión Exponencial: Recuperación vs Liberación")
x_seq <- seq(min(x_var), max(x_var), length.out = 200)
lines(x_seq, a * exp(b * x_seq), col = "blue", lwd = 2)
summary(modelo_exp)
##
## Call:
## lm(formula = y_log ~ x_var)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.24488 -0.08953 -0.01695 0.00321 0.61166
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.152281 0.034456 62.47 <2e-16 ***
## x_var 0.043020 0.001599 26.90 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1649 on 205 degrees of freedom
## Multiple R-squared: 0.7793, Adjusted R-squared: 0.7782
## F-statistic: 723.8 on 1 and 205 DF, p-value: < 2.2e-16
# -------- Correlación (r) --------
r <- cor(x_var, y_log, use = "complete.obs")
cat("Coeficiente de correlación (r):", round(r,4), "\n")
## Coeficiente de correlación (r): 0.8828
# -------- Coeficiente de determinación (R²) --------
R2 <- summary(modelo_exp)$r.squared
cat("Coeficiente de determinación (R²):", round(R2*100,2), "%\n\n")
## Coeficiente de determinación (R²): 77.93 %
x_pred <- 20
y_pred <- a * exp(b * x_pred)
cat("Para una recuperación de", x_pred,
"barriles, la liberación estimada es:",
round(y_pred,2), "barriles\n\n")
## Para una recuperación de 20 barriles, la liberación estimada es: 20.34 barriles