#POLYNOMIAL REGRESSION
#Given Data
x <- c(1,3,4,6,8,10,12,14,16,18)
y <- c(1.5,2.9,5.2,9.3,15.4,22.8,30.5,39.2,49.6,60.1)
model <- lm(y ~ poly(x, 3, raw = TRUE))
summary(model)
##
## Call:
## lm(formula = y ~ poly(x, 3, raw = TRUE))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.46359 -0.13153 0.08076 0.16107 0.42804
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.8953649 0.4787151 1.870 0.110625
## poly(x, 3, raw = TRUE)1 0.1607443 0.2189731 0.734 0.490579
## poly(x, 3, raw = TRUE)2 0.2300837 0.0266152 8.645 0.000132 ***
## poly(x, 3, raw = TRUE)3 -0.0031392 0.0009199 -3.412 0.014276 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3447 on 6 degrees of freedom
## Multiple R-squared: 0.9998, Adjusted R-squared: 0.9997
## F-statistic: 1.073e+04 on 3 and 6 DF, p-value: 1.414e-11
plot(x, y, main = "Cubic Polynomial Regression", pch = 16)
x_pred <- seq(min(x), max(x), length.out = 100)
y_pred <- predict(model, newdata = data.frame(x = x_pred))
lines(x_pred, y_pred, col = 'red', lwd = 2)
#Drawing the linear model
model1 <- lm(y ~ poly(x, 1, raw = TRUE))
summary(model1)
##
## Call:
## lm(formula = y ~ poly(x, 1, raw = TRUE))
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.0348 -3.0784 -0.7475 1.8050 6.6540
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -8.6667 2.4958 -3.473 0.00841 **
## poly(x, 1, raw = TRUE) 3.5127 0.2331 15.067 3.72e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.035 on 8 degrees of freedom
## Multiple R-squared: 0.966, Adjusted R-squared: 0.9617
## F-statistic: 227 on 1 and 8 DF, p-value: 3.723e-07
plot(x, y, main = "Linear Polynomial Regression", pch = 16)
x_pred <- seq(min(x), max(x), length.out = 100)
yl_pred <- predict(model1, newdata = data.frame(x = x_pred))
lines(x_pred, yl_pred, col = "brown", lwd = 2)
#Drawing the quadratic model
model2 <- lm(y ~ poly(x, 2, raw = TRUE))
summary(model2)
##
## Call:
## lm(formula = y ~ poly(x, 2, raw = TRUE))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.6394 -0.3830 -0.0457 0.3275 0.7873
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.279403 0.528122 -0.529 0.613127
## poly(x, 2, raw = TRUE)1 0.851613 0.132455 6.429 0.000357 ***
## poly(x, 2, raw = TRUE)2 0.140441 0.006788 20.688 1.55e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5472 on 7 degrees of freedom
## Multiple R-squared: 0.9995, Adjusted R-squared: 0.9993
## F-statistic: 6386 on 2 and 7 DF, p-value: 3.847e-12
plot(x, y, main = "Quadratic Polynomial Regression", pch = 16)
x_pred <- seq(min(x), max(x), length.out = 100)
yq_pred <- predict(model2, newdata = data.frame(x = x_pred))
lines(x_pred, yq_pred, col = "orange", lwd = 2)
#Comparing R-squared values
R3 <- 0.9998
R2 <- 0.9995
R1 <- 0.9660
cat("From the given summary of the data, comparing the values, the best fit R-square value is:", max(R3, R2, R1))
## From the given summary of the data, comparing the values, the best fit R-square value is: 0.9998
#Now the new dataset
Model <- lm(y ~ poly(x, 2, raw = TRUE))
new <- data.frame(x = c(11, 12, 13))
y_prediction <- predict(Model, new)
print(y_prediction)
## 1 2 3
## 26.08173 30.16349 34.52613