#UNIVERSIDAD NACIONAL DEL ALTIPLANO
#REGRESION AVANZADA
#FINESI
#TEMA: Regression Polynomial
library(knitr)
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
# BUSCAR LA RUTA DE EXCEL
Caso1 <- read_excel("E:/VII SEMESTRE/REGRESION AVANZADA/Trabajo 2/Caso1.xlsx")
Caso1
## # A tibble: 12 x 2
##        x     y
##    <dbl> <dbl>
##  1  4     24.6
##  2  4     24.7
##  3  4     23.9
##  4  5     39.5
##  5  5     39.6
##  6  6     57.1
##  7  6.5   67.1
##  8  6.5   67.2
##  9  6.75  67.2
## 10  7     77.9
## 11  7.1   80.1
## 12  7.3   84.7
head(Caso1)
## # A tibble: 6 x 2
##       x     y
##   <dbl> <dbl>
## 1     4  24.6
## 2     4  24.7
## 3     4  23.9
## 4     5  39.5
## 5     5  39.6
## 6     6  57.1
View(Caso1)

RPL <- as.numeric(Caso1$y)

#GRAFICO LOWESS
plot(Caso1$x ~ Caso1$y)
lines(lowess(Caso1$x ~ Caso1$y))

#MODELO POLINOMIAL
m2<- lm(x ~ poly(y, 2, raw = T), data = Caso1)
m3<- lm(x ~ y + I(y^2), data = Caso1)
summary(m2)
## 
## Call:
## lm(formula = x ~ poly(y, 2, raw = T), data = Caso1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.05267 -0.03117 -0.01789 -0.00567  0.20162 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           2.075e+00  1.589e-01  13.055 3.74e-07 ***
## poly(y, 2, raw = T)1  8.550e-02  6.941e-03  12.318 6.16e-07 ***
## poly(y, 2, raw = T)2 -2.812e-04  6.566e-05  -4.283  0.00204 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.07538 on 9 degrees of freedom
## Multiple R-squared:  0.9972, Adjusted R-squared:  0.9966 
## F-statistic:  1605 on 2 and 9 DF,  p-value: 3.234e-12
summary(m3)
## 
## Call:
## lm(formula = x ~ y + I(y^2), data = Caso1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.05267 -0.03117 -0.01789 -0.00567  0.20162 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.075e+00  1.589e-01  13.055 3.74e-07 ***
## y            8.550e-02  6.941e-03  12.318 6.16e-07 ***
## I(y^2)      -2.812e-04  6.566e-05  -4.283  0.00204 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.07538 on 9 degrees of freedom
## Multiple R-squared:  0.9972, Adjusted R-squared:  0.9966 
## F-statistic:  1605 on 2 and 9 DF,  p-value: 3.234e-12
#INTERPOLACION DE PUNTOS DENTRO DEL RANGO DEL PREDICTOR
limites <- range(Caso1$y)
nuevos_puntos <- seq(from = limites[1], to = limites[2], by = 1)
nuevos_puntos <- data.frame(y = nuevos_puntos)

#PREDICTION DE LA VARIABLE RESPUESTA Y DEL ERROR ESTANDAR
predicciones <- predict(m2, newdata = nuevos_puntos, se.fit = TRUE,
                        level = 0.95)
#CALCULO DEL INTERVALO DE CONFIANZA SUPERIOR E INFERIOR 95%
intervalo_conf <- data.frame(inferior = predicciones$fit -
                               1.96*predicciones$se.fit,
                             superior = predicciones$fit +
                               1.96*predicciones$se.fit)
#GRAFICO DE REGRESION POLINOMIAL
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.3

ggplot(data = Caso1, aes(x = x, y = y)) +
  geom_point(color = "grey30", alpha = 0.3) + 
  geom_smooth(method = "lm", formula = y ~ poly(x, 4), color = "red") +
  labs(title = "Regresion Polinomial") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))

#GRAFICO DE LINEA RECTA Y CURVA 
ggplot(Caso1, aes(x=x, y=y)) + 
  geom_point() +
  geom_smooth(method='lm', formula=y~x, se=FALSE, col='blue') +
  geom_smooth(method='lm', formula=y~x+I(x^2), se=FALSE, col='green') +
  theme_light()

#ANOVA
anova(m2, m3)
## Analysis of Variance Table
## 
## Model 1: x ~ poly(y, 2, raw = T)
## Model 2: x ~ y + I(y^2)
##   Res.Df      RSS Df Sum of Sq F Pr(>F)
## 1      9 0.051135                      
## 2      9 0.051135  0         0
#GRAFICO DE MODELO LINEAL Y MODELO CUADRATICO
par(mfrow=c(1, 2))
plot(m2, which=1, caption='Modelo Lineal')
plot(m3, which=1, caption='Modelo Cuadratico')