#==============================ENCABEZADO===================================
# TEMA: REGRESION LINEAL
# AUTOR: GRUPO 4
# FECHA: 09-02-2026

#======= 1. CARGA DE DATOS Y LIBRERIAS ========
print("CARGA DE DATOS")
## [1] "CARGA DE DATOS"
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(knitr)
library(gt)

setwd("C:/Users/HP/Documents/PROYECTO ESTADISTICA/RStudio")
datos <- read.csv("tablap.csv", sep = ";", dec = ".", header = TRUE)


#======= 2. TABLAS PARES DE VALORES ========
print("TABLA DE PARES DE VALORES")
## [1] "TABLA DE PARES DE VALORES"
proyeccion <- as.numeric(datos$Projection.of.gas.production)
energia <- as.numeric(datos$Energy.of.gas)

TPV <- data.frame(proyeccion, energia)
TPV <- na.omit(TPV) 
TPV <- TPV[TPV$proyeccion > 0 & TPV$energia > 0, ] 

# Imprimir Tabla Limpia
print("Muestra de 20 registros - Tabla Limpia:")
## [1] "Muestra de 20 registros - Tabla Limpia:"
print(head(TPV, 20))
##     proyeccion     energia
## 1   1633387353   474611355
## 2   4426555771  3008671457
## 3  13091385802 11271020282
## 4   9769530918   396443607
## 5    933091875   863371043
## 6   1631138294  1631138294
## 7    980810831   980810831
## 8   2011515623  2011515623
## 9   6601197153  2004640765
## 10  3056030863  2997579933
## 11 15873504730 15058581965
## 12  8805580780  8805580780
## 13  3074886824   449971840
## 14  3875709307  1205817899
## 15   708679636   318703166
## 16  3531327643  3531327643
## 17  3385271356  2022706923
## 18  7575844581  7575844581
## 19   713981638   657975593
## 20  7057182189   383489940
TPV <- TPV[order(TPV$proyeccion), ] 

print("Muestra de 20 registros - Tabla Ordenada:")
## [1] "Muestra de 20 registros - Tabla Ordenada:"
print(head(TPV, 20))
##       proyeccion   energia
## 11690   39118078  38834973
## 5815    55410999  55271078
## 976     58830579  49028503
## 8916    61393806  43613428
## 1706    64048083  58445507
## 8321    64626873  56712423
## 1070    68977226  68922100
## 8861    69283250  69283250
## 12445   71873018  52568420
## 8515    76962805  70667114
## 10708   82115449  76076767
## 7059    82461679  45097070
## 9336    83772617  67591378
## 4426    85045529  43049470
## 10706   96588854  40469365
## 8397   100221731  43565708
## 8406   101995190  66009043
## 12317  102103301  50371156
## 4593   102876804 102847391
## 8829   103449307  96798948
#======= 3. DIAGRAMA DE DISPERSION ========
print("DIAGRAMA DE DISPERSION")
## [1] "DIAGRAMA DE DISPERSION"
x <- TPV$proyeccion
y <- TPV$energia

plot(x, y, pch = 16, col = "blue",
     main = "Grafica N.1 : Diagrama de dispersion",
     xlab = "Proyeccion de Produccion", ylab = "Energia del Gas")

#======= 4. CONJETURA DEL MODELO ========
print("CONJETURA DEL MODELO")
## [1] "CONJETURA DEL MODELO"
regresion_lineal <- lm(y ~ x)

plot(x, y, pch = 16, col = "blue",
     main = "Grafica N.2 : Modelo de Regresion Lineal",
     xlab = "Proyeccion de Produccion", ylab = "Energia del Gas")
abline(regresion_lineal, col = "red", lwd = 2)

a_val <- round(coef(regresion_lineal)[2], 8)
b_val <- round(coef(regresion_lineal)[1], 2)
legend("topleft", legend = paste("Y =", a_val, "x +", b_val), 
       bty = "n", text.col = "red", cex = 1.2, text.font = 2)

#======= 5. TEST DE APROBACION Y RESTRICCIONES ========
print("TEST PEARSON")
## [1] "TEST PEARSON"
r <- cor(x, y)
r2 <- r^2

# TABLA SIMPLE DE RESULTADOS
tabla_resumen <- data.frame(
  Indicador = c("Pearson (r)", "Determinacion (R2)", "Pendiente (a)", "Intercepto (b)"),
  Valor = c(round(r, 4), paste0(round(r2*100, 2), "%"), a_val, b_val)
)

kable(tabla_resumen, caption = "Resumen de Parametros del Modelo")
Resumen de Parametros del Modelo
Indicador Valor
Pearson (r) 0.9848
Determinacion (R2) 96.99%
Pendiente (a) 0.97273857
Intercepto (b) -151274479.5
#======= 6. CALCULO DE PRONOSTICOS ========
print("CALCULO DE PRONOSTICOS")
## [1] "CALCULO DE PRONOSTICOS"
valor_consulta <- 1.2e+10 
pronostico <- (a_val * valor_consulta) + b_val
cat("Para una proyeccion de", valor_consulta, ", la energia esperada es:", round(pronostico, 2), "\n")
## Para una proyeccion de 1.2e+10 , la energia esperada es: 11521588361
#======= 7. CONCLUSION ========
print("CONCLUSION")
## [1] "CONCLUSION"
cat("Entre la proyeccion de produccion y la energia del gas existe una relacion tipo 
lineal donde el modelo f(x) = 0.9727x - 151274479.5 siendo x la proyeccion de produccion
y y la energia del gas. En este caso si existe restriccion, ya que la energia del 
gas no puede tomar valores negativos; segun el modelo, para proyecciones menores 
a 155513015 unidades, el resultado seria fisicamente imposible. La energia del 
gas esta influenciada en un 96.99% por la proyeccion de produccion y el resto se debe 
a otros factores.")
## Entre la proyeccion de produccion y la energia del gas existe una relacion tipo 
## lineal donde el modelo f(x) = 0.9727x - 151274479.5 siendo x la proyeccion de produccion
## y y la energia del gas. En este caso si existe restriccion, ya que la energia del 
## gas no puede tomar valores negativos; segun el modelo, para proyecciones menores 
## a 155513015 unidades, el resultado seria fisicamente imposible. La energia del 
## gas esta influenciada en un 96.99% por la proyeccion de produccion y el resto se debe 
## a otros factores.