set.seed(123)
n<-100
x<-seq(0,10,length.out=n)
y<-5+2*x-0.3*x^2 +rnorm(n,mean=0,sd=2)
data<-data.frame(x=x,y=y)
#Menampilkan 10 baris pertama dari 100 pengamatan
head(data,10)
##            x        y
## 1  0.0000000 3.879049
## 2  0.1010101 4.738604
## 3  0.2020202 8.509213
## 4  0.3030303 5.719529
## 5  0.4040404 6.017682
## 6  0.5050505 9.363708
## 7  0.6060606 7.023761
## 8  0.7070707 3.734034
## 9  0.8080808 5.046558
## 10 0.9090909 5.678924
model_linier <- lm(y~x, data=data)
model_poli2<- lm(y~poly(x,2,raw=TRUE),data=data)
model_poli3<- lm(y~poly(x,3,raw=TRUE),data=data)
summary(model_linier)
## 
## Call:
## lm(formula = y ~ x, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.4317 -1.4051 -0.0466  2.2166  6.6109 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.88171    0.55369  17.847   <2e-16 ***
## x           -0.95028    0.09566  -9.934   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.789 on 98 degrees of freedom
## Multiple R-squared:  0.5017, Adjusted R-squared:  0.4967 
## F-statistic: 98.68 on 1 and 98 DF,  p-value: < 2.2e-16
summary(model_poli2)
## 
## Call:
## lm(formula = y ~ poly(x, 2, raw = TRUE), data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8136 -1.1977 -0.0533  1.3549  4.3891 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              5.33982    0.53777   9.929   <2e-16 ***
## poly(x, 2, raw = TRUE)1  1.80266    0.24855   7.253    1e-10 ***
## poly(x, 2, raw = TRUE)2 -0.27529    0.02405 -11.447   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.829 on 97 degrees of freedom
## Multiple R-squared:  0.788,  Adjusted R-squared:  0.7837 
## F-statistic: 180.3 on 2 and 97 DF,  p-value: < 2.2e-16
summary(model_poli3)
## 
## Call:
## lm(formula = y ~ poly(x, 3, raw = TRUE), data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.7866 -1.1996 -0.0497  1.3386  4.3777 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              5.282801   0.708433   7.457 3.93e-11 ***
## poly(x, 3, raw = TRUE)1  1.872854   0.616613   3.037  0.00307 ** 
## poly(x, 3, raw = TRUE)2 -0.292931   0.143693  -2.039  0.04424 *  
## poly(x, 3, raw = TRUE)3  0.001176   0.009443   0.125  0.90117    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.838 on 96 degrees of freedom
## Multiple R-squared:  0.7881, Adjusted R-squared:  0.7815 
## F-statistic:   119 on 3 and 96 DF,  p-value: < 2.2e-16
anova(model_linier, model_poli2, model_poli3)
## Analysis of Variance Table
## 
## Model 1: y ~ x
## Model 2: y ~ poly(x, 2, raw = TRUE)
## Model 3: y ~ poly(x, 3, raw = TRUE)
##   Res.Df    RSS Df Sum of Sq        F Pr(>F)    
## 1     98 762.42                                 
## 2     97 324.33  1    438.09 129.6931 <2e-16 ***
## 3     96 324.28  1      0.05   0.0155 0.9012    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(model_linier, model_poli2, model_poli3)
##              df      AIC
## model_linier  3 492.9204
## model_poli2   4 409.4469
## model_poli3   5 411.4307
library(ggplot2)
data$pred_linier <- predict(model_linier)
data$pred_poli2 <- predict(model_poli2)
data$pred_poli3 <- predict(model_poli3)
ggplot(data, aes(x = x, y = y)) +
geom_point(alpha = 0.5) +
geom_line(aes(y = pred_linier, color = "Linier"), linewidth = 1) +
geom_line(aes(y = pred_poli2, color = "Polinomial derajat 2"), linewidth
= 1) +
geom_line(aes(y = pred_poli3, color = "Polinomial derajat 3"), linewidth
= 1) +
labs(title = "Perbandingan Regresi Linier vs Polinomial",
x = "X", y = "Y", color = "Model") +
theme_minimal()

rmse <- function(actual, predicted) sqrt(mean((actual - predicted)^2))
data.frame(
Model = c("Linier", "Polinomial derajat 2", "Polinomial derajat 3"),
RMSE = c(rmse(data$y, data$pred_linier),
rmse(data$y, data$pred_poli2),
rmse(data$y, data$pred_poli3))
)
##                  Model     RMSE
## 1               Linier 2.761195
## 2 Polinomial derajat 2 1.800917
## 3 Polinomial derajat 3 1.800772
library(caret)
## Loading required package: lattice
kontrol <- trainControl(method = "repeatedcv",number = 10,repeats = 5)
derajat_max <- 6
hasil_cv <- data.frame(
derajat = integer(),
RMSE = numeric()
)
for (d in 1:derajat_max) {
data_cv <- data[, c("x", "y")]
# Membuat variabel polynomial
if (d == 1) {
data_cv$x_poly <- data_cv$x
} else {
poly_x <- poly(data_cv$x, degree = d, raw = TRUE)
data_cv$x_poly <- poly_x[, 1]
for (j in 2:d) {
data_cv[[paste0("x_poly", j)]] <- poly_x[, j]
}
}
# Formula sesuai derajat
if (d == 1) {
formula_model <- y ~ x_poly
} else {
variabel <- paste0("x_poly", 2:d)
formula_model <- as.formula(
paste("y ~ x_poly +", paste(variabel, collapse = " + "))
)
}
# Seed yang sama untuk setiap derajat agar pembagian fold-nya identik
set.seed(42)
model_cv <- train(
formula_model,
data = data_cv,
method = "lm",
trControl = kontrol
)
hasil_cv <- rbind(
hasil_cv,
data.frame(
derajat = d,
RMSE = min(model_cv$results$RMSE)
)
)
}
hasil_cv
##   derajat     RMSE
## 1       1 2.757805
## 2       2 1.831405
## 3       3 1.857737
## 4       4 1.887214
## 5       5 1.901895
## 6       6 1.886359
derajat_optimal <- hasil_cv$derajat[which.min(hasil_cv$RMSE)]
cat("Derajat optimal berdasarkan CV:", derajat_optimal, "\n")
## Derajat optimal berdasarkan CV: 2
## Derajat optimal berdasarkan CV: 2
ggplot(hasil_cv, aes(x = derajat, y = RMSE)) +
geom_line(
color = "steelblue",
linewidth = 1
) +
geom_point(
size = 3,
color = "steelblue"
) +
geom_vline(
xintercept = derajat_optimal,
linetype = "dashed",
color = "firebrick"
) +
scale_x_continuous(
breaks = 1:derajat_max
) +
labs(
title = "Pemilihan Derajat Optimal via 10-Fold Cross-Validation",
x = "Derajat Polinomial",
y = "RMSE Rata-rata (Validasi Silang)"
) +
theme_minimal()

model_final <- lm(y ~ poly(x, derajat_optimal, raw = TRUE), data = data)
summary(model_final)
## 
## Call:
## lm(formula = y ~ poly(x, derajat_optimal, raw = TRUE), data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8136 -1.1977 -0.0533  1.3549  4.3891 
## 
## Coefficients:
##                                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                            5.33982    0.53777   9.929   <2e-16 ***
## poly(x, derajat_optimal, raw = TRUE)1  1.80266    0.24855   7.253    1e-10 ***
## poly(x, derajat_optimal, raw = TRUE)2 -0.27529    0.02405 -11.447   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.829 on 97 degrees of freedom
## Multiple R-squared:  0.788,  Adjusted R-squared:  0.7837 
## F-statistic: 180.3 on 2 and 97 DF,  p-value: < 2.2e-16
model_linier <- lm(y ~ x, data = data)
model_poli2 <- lm(y ~ x + I(x^2), data = data)
model_poli3 <- lm(y ~ x + I(x^2) + I(x^3), data = data)
data$y_topi_linier <- predict(model_linier)
data$y_topi_poli2 <- predict(model_poli2)
data$y_topi_poli3 <- predict(model_poli3)
data
##              x           y pred_linier  pred_poli2  pred_poli3 y_topi_linier
## 1    0.0000000  3.87904871   9.8817094  5.33982287  5.28280126     9.8817094
## 2    0.1010101  4.73860431   9.7857215  5.51910079  5.46899089     9.7857215
## 3    0.2020202  8.50921338   9.6897335  5.69276104  5.64921021     9.6897335
## 4    0.3030303  5.71952918   9.5937456  5.86080361  5.82346649     9.5937456
## 5    0.4040404  6.01768168   9.4977577  6.02322852  5.99176701     9.4977577
## 6    0.5050505  9.36370818   9.4017697  6.18003576  6.15411903     9.4017697
## 7    0.6060606  7.02376079   9.3057818  6.33122532  6.31052982     9.3057818
## 8    0.7070707  3.73403425   9.2097939  6.47679722  6.46100666     9.2097939
## 9    0.8080808  5.04655753   9.1138060  6.61675145  6.60555682     9.1138060
## 10   0.9090909  5.67892399   9.0178180  6.75108800  6.74418756     9.0178180
## 11   1.0101010  9.16227440   8.9218301  6.87980689  6.87690616     8.9218301
## 12   1.1111111  7.57147951   8.8258422  7.00290810  7.00371990     8.8258422
## 13   1.2121212  7.78501398   8.7298542  7.12039165  7.12463603     8.7298542
## 14   1.3131313  7.33033390   8.6338663  7.23225752  7.23966183     8.6338663
## 15   1.4141414  6.11666178   8.5378784  7.33850572  7.34880458     8.5378784
## 16   1.5151515 10.91542407   8.4418904  7.43913626  7.45207154     8.4418904
## 17   1.6161616  8.44443068   8.3459025  7.53414912  7.54946998     8.3459025
## 18   1.7171717  3.61650551   8.2499146  7.62354431  7.64100717     8.2499146
## 19   1.8181818  9.04733990   8.1539266  7.70732183  7.72669040     8.1539266
## 20   1.9191919  6.78781174   8.0579387  7.78548168  7.80652691     8.0579387
## 21   2.0202020  5.68039177   7.9619508  7.85802386  7.88052400     7.9619508
## 22   2.1212121  7.45661215   7.8659628  7.92494837  7.94868893     7.8659628
## 23   2.2222222  5.91095407   7.7699749  7.98625521  8.01102896     7.7699749
## 24   2.3232323  6.56945966   7.6739870  8.04194438  8.06755137     7.6739870
## 25   2.4242424  6.83532091   7.5779990  8.09201588  8.11826344     7.5779990
## 26   2.5252525  4.76404833   7.4820111  8.13646971  8.16317242     7.4820111
## 27   2.6262626  9.85892273   7.3860232  8.17530587  8.20228560     7.3860232
## 28   2.7272727  8.52988673   7.2900352  8.20852436  8.23561024     7.2900352
## 29   2.8282828  5.98053666   7.1940473  8.23612517  8.26315362     7.1940473
## 30   2.9292929 10.79198858   7.0980594  8.25810832  8.28492300     7.0980594
## 31   3.0303030  9.15871357   7.0020714  8.27447380  8.30092566     7.0020714
## 32   3.1313131  7.73094672   6.9060835  8.28522160  8.31116887     6.9060835
## 33   3.2323232 10.12052374   6.8100956  8.29035174  8.31565989     6.8100956
## 34   3.3333333 10.08960031   6.7141076  8.28986420  8.31440601     6.7141076
## 35   3.4343434  9.97343458   6.6181197  8.28375900  8.30741448     6.6181197
## 36   3.5353535  9.69837019   6.5221318  8.27203612  8.29469258     6.5221318
## 37   3.6363636  9.41362043   6.4261439  8.25469558  8.27624759     6.4261439
## 38   3.7373737  8.16053532   6.3301559  8.23173736  8.25208677     6.3301559
## 39   3.8383838  7.64488520   6.2341680  8.20316147  8.22221739     6.2341680
## 40   3.9393939  7.46219849   6.1381801  8.16896791  8.18664672     6.1381801
## 41   4.0404040  6.79393468   6.0421921  8.12915669  8.14538204     6.0421921
## 42   4.1414141  7.72160040   5.9462042  8.08372779  8.09843062     5.9462042
## 43   4.2424242  5.55460675   5.8502163  8.03268122  8.04579973     5.8502163
## 44   4.3434343 12.36515405   5.7542283  7.97601698  7.98749663     5.7542283
## 45   4.4444444 10.37888696   5.6582404  7.91373507  7.92352860     5.6582404
## 46   4.5454545  5.64634482   5.5622525  7.84583549  7.85390291     5.5622525
## 47   4.6464646  7.01026951   5.4662645  7.77231824  7.77862683     5.4662645
## 48   4.7474747  6.80008384   5.3702766  7.69318332  7.69770763     5.3702766
## 49   4.8484848  9.20455834   5.2742887  7.60843073  7.61115259     5.2742887
## 50   4.9494949  7.38300169   5.1783007  7.51806047  7.51896896     5.1783007
## 51   5.0505051  7.95536675   5.0823128  7.42207253  7.42116404     5.0823128
## 52   5.1515152  7.28450429   4.9863249  7.32046693  7.31774507     4.9863249
## 53   5.2525253  7.14260313   4.8903369  7.21324366  7.20871935     4.8903369
## 54   5.3535354  9.84617304   4.7943490  7.10040271  7.09409413     4.7943490
## 55   5.4545455  6.53192910   4.6983611  6.98194410  6.97387668     4.6983611
## 56   5.5555556  9.88479306   4.6023731  6.85786782  6.84807429     4.6023731
## 57   5.6565657  3.61660520   4.5063852  6.72817386  6.71669421     4.5063852
## 58   5.7575758  7.73947543   4.4103973  6.59286224  6.57974373     4.4103973
## 59   5.8585859  6.66797173   4.3144093  6.45193294  6.43723011     4.3144093
## 60   5.9595960  6.69603986   4.2184214  6.30538598  6.28916062     4.2184214
## 61   6.0606061  6.86120734   4.1224335  6.15322134  6.13554253     4.1224335
## 62   6.1616162  4.92893130   4.0264455  5.99543903  5.97638312     4.0264455
## 63   6.2626263  5.09269145   3.9304576  5.83203905  5.81168965     3.9304576
## 64   6.3636364  3.54136163   3.8344697  5.66302141  5.64146939     3.8344697
## 65   6.4646465  3.24821430   3.7384818  5.48838609  5.46572963     3.7384818
## 66   6.5656566  5.80601657   3.6424938  5.30813310  5.28447762     3.6424938
## 67   6.6666667  5.89641956   3.5465059  5.12226244  5.09772064     3.5465059
## 68   6.7676768  4.90092734   3.4505180  4.93077411  4.90546596     3.4505180
## 69   6.8686869  6.42825088   3.3545300  4.73366811  4.70772084     3.3545300
## 70   6.9696970  8.46656056   3.2585421  4.53094444  4.50449257     3.2585421
## 71   7.0707071  3.16088227   3.1625542  4.32260310  4.29578842     3.1625542
## 72   7.1717172 -0.70496157   3.0665662  4.10864409  4.08161564     3.0665662
## 73   7.2727273  5.68916300   2.9705783  3.88906741  3.86198152     2.9705783
## 74   7.3737374  2.01747237   2.8745904  3.66387306  3.63689332     2.8745904
## 75   7.4747475  1.81192277   2.7786024  3.43306103  3.40635832     2.7786024
## 76   7.5757576  4.98502704   2.6826145  3.19663134  3.17038378     2.6826145
## 77   7.6767677  2.10416075   2.5866266  2.95458398  2.92897699     2.5866266
## 78   7.7777778 -0.03402802   2.4906386  2.70691894  2.68214520     2.4906386
## 79   7.8787879  2.49759319   2.3946507  2.45363624  2.42989569     2.3946507
## 80   7.9797980  1.57866050   2.2986628  2.19473586  2.17223573     2.2986628
## 81   8.0808081  1.58330676   2.2026748  1.93021782  1.90917259     2.2026748
## 82   8.1818182  2.05155254   2.1066869  1.66008210  1.64071354     2.1066869
## 83   8.2828283  0.24276319   2.0106990  1.38432872  1.36686585     2.0106990
## 84   8.3838384  1.96980605   1.9147110  1.10295766  1.08763680     1.9147110
## 85   8.4848485 -0.06907230   1.8187231  0.81596894  0.80303366     1.8187231
## 86   8.5858586  0.72019080   1.7227352  0.52336254  0.51306368     1.7227352
## 87   8.6868687  1.92890913   1.6267472  0.22513847  0.21773416     1.6267472
## 88   8.7878788  0.27807648   1.5307593 -0.07870327 -0.08294765     1.5307593
## 89   8.8888889 -1.57778910   1.4347714 -0.38816268 -0.38897447     1.4347714
## 90   8.9898990  1.03192806   1.3387834 -0.70323975 -0.70033903     1.3387834
## 91   9.0909091  0.37543746   1.2427955 -1.02393450 -1.01703406     1.2427955
## 92   9.1919192 -0.86678123   1.1468076 -1.35024692 -1.33905229     1.1468076
## 93   9.2929293 -1.84423840   1.0508196 -1.68217701 -1.66638645     1.0508196
## 94   9.3939394 -3.94176257   0.9548317 -2.01972477 -1.99902927     0.9548317
## 95   9.4949495 -0.33501589   0.8588438 -2.36289021 -2.33697348     0.8588438
## 96   9.5959596 -4.63333215   0.7628559 -2.71167331 -2.68021179     0.7628559
## 97   9.6969697  0.55923899   0.6668679 -3.06607408 -3.02873696     0.6668679
## 98   9.7979798 -1.13894159   0.5708800 -3.42609252 -3.38254170     0.5708800
## 99   9.8989899 -5.07042123   0.4748921 -3.79172863 -3.74161874     0.4748921
## 100 10.0000000 -7.05284180   0.3789041 -4.16298242 -4.10596081     0.3789041
##     y_topi_poli2 y_topi_poli3
## 1     5.33982287   5.28280126
## 2     5.51910079   5.46899089
## 3     5.69276104   5.64921021
## 4     5.86080361   5.82346649
## 5     6.02322852   5.99176701
## 6     6.18003576   6.15411903
## 7     6.33122532   6.31052982
## 8     6.47679722   6.46100666
## 9     6.61675145   6.60555682
## 10    6.75108800   6.74418756
## 11    6.87980689   6.87690616
## 12    7.00290810   7.00371990
## 13    7.12039165   7.12463603
## 14    7.23225752   7.23966183
## 15    7.33850572   7.34880458
## 16    7.43913626   7.45207154
## 17    7.53414912   7.54946998
## 18    7.62354431   7.64100717
## 19    7.70732183   7.72669040
## 20    7.78548168   7.80652691
## 21    7.85802386   7.88052400
## 22    7.92494837   7.94868893
## 23    7.98625521   8.01102896
## 24    8.04194438   8.06755137
## 25    8.09201588   8.11826344
## 26    8.13646971   8.16317242
## 27    8.17530587   8.20228560
## 28    8.20852436   8.23561024
## 29    8.23612517   8.26315362
## 30    8.25810832   8.28492300
## 31    8.27447380   8.30092566
## 32    8.28522160   8.31116887
## 33    8.29035174   8.31565989
## 34    8.28986420   8.31440601
## 35    8.28375900   8.30741448
## 36    8.27203612   8.29469258
## 37    8.25469558   8.27624759
## 38    8.23173736   8.25208677
## 39    8.20316147   8.22221739
## 40    8.16896791   8.18664672
## 41    8.12915669   8.14538204
## 42    8.08372779   8.09843062
## 43    8.03268122   8.04579973
## 44    7.97601698   7.98749663
## 45    7.91373507   7.92352860
## 46    7.84583549   7.85390291
## 47    7.77231824   7.77862683
## 48    7.69318332   7.69770763
## 49    7.60843073   7.61115259
## 50    7.51806047   7.51896896
## 51    7.42207253   7.42116404
## 52    7.32046693   7.31774507
## 53    7.21324366   7.20871935
## 54    7.10040271   7.09409413
## 55    6.98194410   6.97387668
## 56    6.85786782   6.84807429
## 57    6.72817386   6.71669421
## 58    6.59286224   6.57974373
## 59    6.45193294   6.43723011
## 60    6.30538598   6.28916062
## 61    6.15322134   6.13554253
## 62    5.99543903   5.97638312
## 63    5.83203905   5.81168965
## 64    5.66302141   5.64146939
## 65    5.48838609   5.46572963
## 66    5.30813310   5.28447762
## 67    5.12226244   5.09772064
## 68    4.93077411   4.90546596
## 69    4.73366811   4.70772084
## 70    4.53094444   4.50449257
## 71    4.32260310   4.29578842
## 72    4.10864409   4.08161564
## 73    3.88906741   3.86198152
## 74    3.66387306   3.63689332
## 75    3.43306103   3.40635832
## 76    3.19663134   3.17038378
## 77    2.95458398   2.92897699
## 78    2.70691894   2.68214520
## 79    2.45363624   2.42989569
## 80    2.19473586   2.17223573
## 81    1.93021782   1.90917259
## 82    1.66008210   1.64071354
## 83    1.38432872   1.36686585
## 84    1.10295766   1.08763680
## 85    0.81596894   0.80303366
## 86    0.52336254   0.51306368
## 87    0.22513847   0.21773416
## 88   -0.07870327  -0.08294765
## 89   -0.38816268  -0.38897447
## 90   -0.70323975  -0.70033903
## 91   -1.02393450  -1.01703406
## 92   -1.35024692  -1.33905229
## 93   -1.68217701  -1.66638645
## 94   -2.01972477  -1.99902927
## 95   -2.36289021  -2.33697348
## 96   -2.71167331  -2.68021179
## 97   -3.06607408  -3.02873696
## 98   -3.42609252  -3.38254170
## 99   -3.79172863  -3.74161874
## 100  -4.16298242  -4.10596081