# Construir un modelo de regrsion polinomial de tercer nivel
# para compararlo co nuevos modelos
library(readr)
library(ggplot2)
# Cartgar las funciones
source("https://raw.githubusercontent.com/rpizarrog/Ciencia-de-los-Datos-Descriptivo-Predictivo/refs/heads/main/scripts/funciones_para_regresion.R")
##
## 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
#Cargar los datos
datos_entrenamiento <- read.csv("https://raw.githubusercontent.com/rpizarrog/Ciencia-de-los-Datos-Descriptivo-Predictivo/refs/heads/main/datos/train_data.csv")
datos_validacion <- read.csv("https://raw.githubusercontent.com/rpizarrog/Ciencia-de-los-Datos-Descriptivo-Predictivo/refs/heads/main/datos/test_data.csv")
str(datos_entrenamiento)
## 'data.frame': 160 obs. of 2 variables:
## $ estatura: num 156 195 184 173 166 ...
## $ peso : num 80.1 95.3 87.6 86.5 73.1 ...
str(datos_validacion)
## 'data.frame': 40 obs. of 2 variables:
## $ estatura: num 175 159 180 162 150 ...
## $ peso : num 80.8 98.6 79.5 78.8 76.3 ...
# crear modelo polinomico de tercer nivel
nivel <- 3
modelo_poly3 <- lm(data = datos_entrenamiento, formula = peso ~ poly(estatura, nivel, raw = TRUE))
summary(modelo_poly3)
##
## Call:
## lm(formula = peso ~ poly(estatura, nivel, raw = TRUE), data = datos_entrenamiento)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.839 -6.798 0.227 5.924 39.575
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3710.9302711 1706.8555118 2.174 0.0312 *
## poly(estatura, nivel, raw = TRUE)1 -63.3723893 29.5028379 -2.148 0.0333 *
## poly(estatura, nivel, raw = TRUE)2 0.3648989 0.1693965 2.154 0.0328 *
## poly(estatura, nivel, raw = TRUE)3 -0.0006919 0.0003231 -2.141 0.0338 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.459 on 156 degrees of freedom
## Multiple R-squared: 0.413, Adjusted R-squared: 0.4017
## F-statistic: 36.59 on 3 and 156 DF, p-value: < 0.00000000000000022
# Visualizar curva
f_polinomial_curva(datos_entrenamiento, nivel)

# Hacer predicciones con el modelo
predicciones <- predict(modelo_poly3, datos_validacion)
predicciones
## 1 2 3 4 5 6 7 8
## 87.61538 78.71022 91.97873 79.50504 80.06812 98.44369 97.79673 94.40808
## 9 10 11 12 13 14 15 16
## 93.32450 93.91475 78.43810 98.49884 98.10347 82.68233 97.85061 83.63184
## 17 18 19 20 21 22 23 24
## 78.59094 78.44927 81.81926 78.73117 79.32924 81.05408 79.06173 90.23479
## 25 26 27 28 29 30 31 32
## 89.51742 83.69917 81.24057 97.60148 85.07799 78.43098 95.34859 85.25698
## 33 34 35 36 37 38 39 40
## 98.51030 95.94122 98.54227 98.54289 81.59092 95.19881 78.57603 96.98518
datos_comparar <- data.frame(reales = datos_validacion$peso, predicciones_poly3 = predicciones)
datos_comparar
## reales predicciones_poly3
## 1 80.81160 87.61538
## 2 98.55304 78.71022
## 3 79.47970 91.97873
## 4 78.76413 79.50504
## 5 76.30898 80.06812
## 6 82.48510 98.44369
## 7 80.99952 97.79673
## 8 83.75690 94.40808
## 9 102.71043 93.32450
## 10 93.10031 93.91475
## 11 73.66947 78.43810
## 12 107.86854 98.49884
## 13 92.77989 98.10347
## 14 76.65798 82.68233
## 15 99.55987 97.85061
## 16 90.95184 83.63184
## 17 82.36279 78.59094
## 18 78.41336 78.44927
## 19 104.71189 81.81926
## 20 100.01035 78.73117
## 21 82.68898 79.32924
## 22 80.15217 81.05408
## 23 67.99702 79.06173
## 24 85.68692 90.23479
## 25 86.19922 89.51742
## 26 94.25695 83.69917
## 27 62.52313 81.24057
## 28 95.87367 97.60148
## 29 79.67553 85.07799
## 30 99.32697 78.43098
## 31 100.57266 95.34859
## 32 76.90348 85.25698
## 33 103.18472 98.51030
## 34 82.44773 95.94122
## 35 105.17958 98.54227
## 36 110.35827 98.54289
## 37 88.96325 81.59092
## 38 86.34848 95.19881
## 39 84.07677 78.57603
## 40 87.12872 96.98518
# Cálculo manual del RMSE
rmse <- sqrt(mean((datos_comparar$reales - datos_comparar$predicciones_poly3)^2))
# Imprimir resultado
paste ("RMSE:", rmse)
## [1] "RMSE: 10.505147086411"
print ("comparado con otros modelos, ¿cuál es más eficiente conforme al estadístico RMSE ?")
## [1] "comparado con otros modelos, ¿cuál es más eficiente conforme al estadístico RMSE ?"