How to create polynomial regression model in R?

A Polynomial regression model is the type of model in which the dependent variable does not have linear relationship with the independent variables rather they have nth degree relationship. For example, a dependent variable x can depend on an independent variable y-square. There are two ways to create a polynomial regression in R, first one is using polym function and second one is using I() function.
set.seed(322)
x1<−rnorm(20,1,0.5)
x2<−rnorm(20,5,0.98)
y1<−rnorm(20,8,2.15)
Model1<−lm(y1~polym(x1,x2,degree=2,raw=TRUE))
summary(Model1)
Call:
lm(formula = y1 ~ polym(x1, x2, degree = 2, raw = TRUE))
Residuals:
Min 1Q Median 3Q Max
−4.2038 −0.7669 −0.2619 1.2505 6.8684
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 11.2809 17.0298 0.662 0.518
polym(x1, x2, degree = 2, raw = TRUE)1.0 −2.9603 6.5583 −0.451 0.659
polym(x1, x2, degree = 2, raw = TRUE)2.0 1.9913 1.9570 1.017 0.326
polym(x1, x2, degree = 2, raw = TRUE)0.1 −1.3573 6.1738 −0.220 0.829
polym(x1, x2, degree = 2, raw = TRUE)1.1 −0.5574 1.2127 −0.460 0.653
polym(x1, x2, degree = 2, raw = TRUE)0.2 0.2383 0.5876 0.406 0.691
Residual standard error: 2.721 on 14 degrees of freedom
Multiple R−squared: 0.205, Adjusted R−squared: −0.0789
F−statistic: 0.7221 on 5 and 14 DF, p−value: 0.6178
Model_1_M2<−lm(y1 ~ x1 + x2 + I(x1^2) + I(x2^2) + x1:x2)
summary(Model_1_M2)
Call:
lm(formula = y1 ~ x1 + x2 + I(x1^2) + I(x2^2) + x1:x2)
Residuals:
Min 1Q Median 3Q Max
−4.2038 −0.7669 −0.2619 1.2505 6.8684
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 11.2809 17.0298 0.662 0.518
x1 −2.9603 6.5583 −0.451 0.659
x2 −1.3573 6.1738 −0.220 0.829
I(x1^2) 1.9913 1.9570 1.017 0.326
I(x2^2) 0.2383 0.5876 0.406 0.691
x1:x2 −0.5574 1.2127 −0.460 0.653
Residual standard error: 2.721 on 14 degrees of freedom
Multiple R−squared: 0.205, Adjusted R−squared: −0.0789
F−statistic: 0.7221 on 5 and 14 DF, p−value: 0.6178
Third degree polynomial regression model −
Model1_3degree<−lm(y1~polym(x1,x2,degree=3,raw=TRUE))
summary(Model1_3degree)
Call:
lm(formula = y1 ~ polym(x1, x2, degree = 3, raw = TRUE))
Residuals:
Min 1Q Median 3Q Max
−4.4845 −0.8435 −0.2514 0.8108 6.7156
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 63.0178 115.9156 0.544 0.599
polym(x1, x2, degree = 3, raw = TRUE)1.0 33.3374 83.3353 0.400 0.698
polym(x1, x2, degree = 3, raw = TRUE)2.0 −10.2012 42.4193 −0.240 0.815
polym(x1, x2, degree = 3, raw = TRUE)3.0 −1.4147 6.4873 −0.218 0.832
polym(x1, x2, degree = 3, raw = TRUE)0.1 −42.6725 72.9322 −0.585 0.571
polym(x1, x2, degree = 3, raw = TRUE)1.1 −8.9795 22.7650 −0.394 0.702
polym(x1, x2, degree = 3, raw = TRUE)2.1 2.8923 7.6277 0.379 0.712
polym(x1, x2, degree = 3, raw = TRUE)0.2 9.6863 14.2095 0.682 0.511
polym(x1, x2, degree = 3, raw = TRUE)1.2 0.2289 2.6744 0.086 0.933
polym(x1, x2, degree = 3, raw = TRUE)0.3 −0.6544 0.8341 −0.785 0.451
Residual standard error: 3.055 on 10 degrees of freedom
Multiple R−squared: 0.2841, Adjusted R−squared: −0.3602
F−statistic: 0.441 on 9 and 10 DF, p−value: 0.8833
Model1_4degree<−lm(y1~polym(x1,x2,degree=4,raw=TRUE))
summary(Model1_4degree)
Call:
lm(formula = y1 ~ polym(x1, x2, degree = 4, raw = TRUE))
Residuals:
1 2 3 4 5 6 7 8
4.59666 −0.41485 −0.62921 −0.62414 −0.49045 2.15614 −0.42311 −0.12903
9 10 11 12 13 14 15 16
2.27613 0.60005 −1.94649 1.79153 0.01765 0.03866 −1.40706 0.85596
17 18 19 20
0.51553 −3.71274 0.05606 −3.12731
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) −1114.793 2124.374 −0.525 0.622
polym(x1, x2, degree = 4, raw = TRUE)1.0 −263.858 2131.701 −0.124 0.906
polym(x1, x2, degree = 4, raw = TRUE)2.0 −267.502 1250.139 −0.214 0.839
polym(x1, x2, degree = 4, raw = TRUE)3.0 317.739 433.932 0.732 0.497
polym(x1, x2, degree = 4, raw = TRUE)4.0 −6.803 40.546 −0.168 0.873
polym(x1, x2, degree = 4, raw = TRUE)0.1 967.989 2009.940 0.482 0.650
polym(x1, x2, degree = 4, raw = TRUE)1.1 256.227 869.447 0.295 0.780
polym(x1, x2, degree = 4, raw = TRUE)2.1 −125.888 473.845 −0.266 0.801
polym(x1, x2, degree = 4, raw = TRUE)3.1 −59.450 70.623 −0.842 0.438
polym(x1, x2, degree = 4, raw = TRUE)0.2 −314.183 674.159 −0.466 0.661
polym(x1, x2, degree = 4, raw = TRUE)1.2 −18.033 112.576 −0.160 0.879
polym(x1, x2, degree = 4, raw = TRUE)2.2 34.781 57.232 0.608 0.570
polym(x1, x2, degree = 4, raw = TRUE)0.3 41.854 91.862 0.456 0.668
polym(x1, x2, degree = 4, raw = TRUE)1.3 −4.360 9.895 −0.441 0.678
polym(x1, x2, degree = 4, raw = TRUE)0.4 −1.763 4.178 −0.422 0.690
Residual standard error: 3.64 on 5 degrees of freedom
Multiple R−squared: 0.4917, Adjusted R−squared: −0.9315
F−statistic: 0.3455 on 14 and 5 DF, p−value: 0.9466