#UNIVERSIDAD NACIONAL DEL ALTIPLANO
#REGRESION AVANZADA
#FINESI
#TEMA: Regression Polynomial

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
cars <- read_excel("E:/VII SEMESTRE/REGRESION AVANZADA/cars.xlsx")
cars
## # A tibble: 6 x 2
##   speed  dist
##   <dbl> <dbl>
## 1     4     2
## 2     4    10
## 3     7     4
## 4     7    22
## 5     8    16
## 6     9    10
head(cars)
## # A tibble: 6 x 2
##   speed  dist
##   <dbl> <dbl>
## 1     4     2
## 2     4    10
## 3     7     4
## 4     7    22
## 5     8    16
## 6     9    10
plot(cars$dist ~ cars$speed)
lines(lowess(cars$dist ~ cars$speed))

m2<- lm(dist ~ poly(speed, 2, raw = T), data = cars)
m3<- lm(dist ~ speed + I(speed^2), data = cars)
summary(m2)
## 
## Call:
## lm(formula = dist ~ poly(speed, 2, raw = T), data = cars)
## 
## Residuals:
##      1      2      3      4      5      6 
## -3.910  4.090 -9.899  8.101  2.697 -1.079 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)              -27.5506    48.0265  -0.574    0.606
## poly(speed, 2, raw = T)1  11.6236    16.6044   0.700    0.534
## poly(speed, 2, raw = T)2  -0.8146     1.3269  -0.614    0.583
## 
## Residual standard error: 8.248 on 3 degrees of freedom
## Multiple R-squared:  0.2642, Adjusted R-squared:  -0.2264 
## F-statistic: 0.5385 on 2 and 3 DF,  p-value: 0.6312
summary(m3)
## 
## Call:
## lm(formula = dist ~ speed + I(speed^2), data = cars)
## 
## Residuals:
##      1      2      3      4      5      6 
## -3.910  4.090 -9.899  8.101  2.697 -1.079 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) -27.5506    48.0265  -0.574    0.606
## speed        11.6236    16.6044   0.700    0.534
## I(speed^2)   -0.8146     1.3269  -0.614    0.583
## 
## Residual standard error: 8.248 on 3 degrees of freedom
## Multiple R-squared:  0.2642, Adjusted R-squared:  -0.2264 
## F-statistic: 0.5385 on 2 and 3 DF,  p-value: 0.6312