#UNIVERSIDAD NACIONAL DEL ALTIPLANO
#REGRESION AVANZADA
#FINESI

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
Caso2 <- read_excel("E:/VII SEMESTRE/REGRESION AVANZADA/Trabajo 2/Caso2.xlsx")
Caso2
## # A tibble: 12 x 3
##        y    x1    x2
##    <dbl> <dbl> <dbl>
##  1  2.6   31    21  
##  2  2.4   31    21  
##  3 17.3   31.5  24  
##  4 15.6   31.5  24  
##  5 16.1   31.5  24  
##  6  5.36  30.5  22  
##  7  6.19  31.5  22  
##  8 10.2   30.5  23  
##  9  2.62  31    21.5
## 10  2.98  30.5  21.5
## 11  6.92  31    22.5
## 12  7.06  30.5  22.5
head(Caso2)
## # A tibble: 6 x 3
##       y    x1    x2
##   <dbl> <dbl> <dbl>
## 1  2.6   31      21
## 2  2.4   31      21
## 3 17.3   31.5    24
## 4 15.6   31.5    24
## 5 16.1   31.5    24
## 6  5.36  30.5    22
View(Caso2)

RPL <- as.numeric(Caso2$y)

#GRAFICO LOWESS
plot(Caso2$x1 ~ Caso2$y)
lines(lowess(Caso2$x1 ~ Caso2$y))

plot(Caso2$x2 ~ Caso2$y)
lines(lowess(Caso2$x2 ~ Caso2$y))
#MODELO POLINOMIAL
m2<- lm(x1 ~ poly(y, 2, raw = T), data = Caso2)
m3<- lm(x1 ~ y + I(y^2), data = Caso2)
m4<- lm(x2 ~ poly(y, 2, raw = T), data = Caso2)
m5<- lm(x2 ~ y + I(y^2), data = Caso2)
summary(m2)
## 
## Call:
## lm(formula = x1 ~ poly(y, 2, raw = T), data = Caso2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.3899 -0.2770  0.0729  0.1018  0.7239 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          31.137367   0.358085  86.955 1.78e-14 ***
## poly(y, 2, raw = T)1 -0.105936   0.097989  -1.081    0.308    
## poly(y, 2, raw = T)2  0.007686   0.004953   1.552    0.155    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3449 on 9 degrees of freedom
## Multiple R-squared:  0.4646, Adjusted R-squared:  0.3456 
## F-statistic: 3.905 on 2 and 9 DF,  p-value: 0.06013
summary(m3)
## 
## Call:
## lm(formula = x1 ~ y + I(y^2), data = Caso2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.3899 -0.2770  0.0729  0.1018  0.7239 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 31.137367   0.358085  86.955 1.78e-14 ***
## y           -0.105936   0.097989  -1.081    0.308    
## I(y^2)       0.007686   0.004953   1.552    0.155    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3449 on 9 degrees of freedom
## Multiple R-squared:  0.4646, Adjusted R-squared:  0.3456 
## F-statistic: 3.905 on 2 and 9 DF,  p-value: 0.06013
summary(m4)
## 
## Call:
## lm(formula = x2 ~ poly(y, 2, raw = T), data = Caso2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.22188 -0.10370  0.01841  0.09627  0.27211 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          20.390727   0.177311 115.000 1.44e-15 ***
## poly(y, 2, raw = T)1  0.338495   0.048521   6.976 6.49e-05 ***
## poly(y, 2, raw = T)2 -0.007239   0.002453  -2.952   0.0162 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1708 on 9 degrees of freedom
## Multiple R-squared:  0.9811, Adjusted R-squared:  0.9769 
## F-statistic:   234 on 2 and 9 DF,  p-value: 1.74e-08
summary(m5)
## 
## Call:
## lm(formula = x2 ~ y + I(y^2), data = Caso2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.22188 -0.10370  0.01841  0.09627  0.27211 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 20.390727   0.177311 115.000 1.44e-15 ***
## y            0.338495   0.048521   6.976 6.49e-05 ***
## I(y^2)      -0.007239   0.002453  -2.952   0.0162 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1708 on 9 degrees of freedom
## Multiple R-squared:  0.9811, Adjusted R-squared:  0.9769 
## F-statistic:   234 on 2 and 9 DF,  p-value: 1.74e-08
#INTERPOLATION DE PUNTOS DENTRO DEL RANGO DEL PREDICTOR
limites <- range(Caso2$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)
#CALC 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 = Caso2, aes(x = x1, 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))
## Warning: Computation failed in `stat_smooth()`:
## 'degree' must be less than number of unique points

ggplot(data = Caso2, aes(x = x2, 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(Caso2, aes(x = x1, 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()

ggplot(Caso2, aes(x = x2, 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: x1 ~ poly(y, 2, raw = T)
## Model 2: x1 ~ y + I(y^2)
##   Res.Df    RSS Df Sum of Sq F Pr(>F)
## 1      9 1.0708                      
## 2      9 1.0708  0         0
anova(m4, m5)
## Analysis of Variance Table
## 
## Model 1: x2 ~ poly(y, 2, raw = T)
## Model 2: x2 ~ y + I(y^2)
##   Res.Df     RSS Df Sum of Sq F Pr(>F)
## 1      9 0.26255                      
## 2      9 0.26255  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')

plot(m4, which=1, caption='Modelo Lineal')
plot(m5, which=1, caption='Modelo Cuadratico')