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
)
head(data)
## 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
model_linier <- lm(
y ~ x,
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
model_poli2 <- lm(
y ~ x + I(x^2),
data = data
)
summary(model_poli2)
##
## Call:
## lm(formula = y ~ x + I(x^2), 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 ***
## x 1.80266 0.24855 7.253 1e-10 ***
## I(x^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_poli3 <- lm(
y ~ x + I(x^2) + I(x^3),
data = data
)
summary(model_poli3)
##
## Call:
## lm(formula = y ~ x + I(x^2) + I(x^3), 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 ***
## x 1.872854 0.616613 3.037 0.00307 **
## I(x^2) -0.292931 0.143693 -2.039 0.04424 *
## I(x^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 ~ x + I(x^2)
## Model 3: y ~ x + I(x^2) + I(x^3)
## 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
Nilai prediksi atau \(\hat{Y}\)
diperoleh menggunakan fungsi predict().
data$Y_topi_linier <- predict(model_linier)
data$Y_topi_poli2 <- predict(model_poli2)
data$Y_topi_poli3 <- predict(model_poli3)
hasil_y_topi <- data.frame(
X = data$x,
Y = data$y,
`Ŷ Linier` = data$Y_topi_linier,
`Ŷ Polinomial 2` = data$Y_topi_poli2,
`Ŷ Polinomial 3` = data$Y_topi_poli3,
check.names = FALSE
)
head(hasil_y_topi, 10)
## X Y Ŷ Linier Ŷ Polinomial 2 Ŷ Polinomial 3
## 1 0.0000000 3.879049 9.881709 5.339823 5.282801
## 2 0.1010101 4.738604 9.785721 5.519101 5.468991
## 3 0.2020202 8.509213 9.689734 5.692761 5.649210
## 4 0.3030303 5.719529 9.593746 5.860804 5.823466
## 5 0.4040404 6.017682 9.497758 6.023229 5.991767
## 6 0.5050505 9.363708 9.401770 6.180036 6.154119
## 7 0.6060606 7.023761 9.305782 6.331225 6.310530
## 8 0.7070707 3.734034 9.209794 6.476797 6.461007
## 9 0.8080808 5.046558 9.113806 6.616751 6.605557
## 10 0.9090909 5.678924 9.017818 6.751088 6.744188
hasil_y_topi
## X Y Ŷ Linier Ŷ Polinomial 2 Ŷ Polinomial 3
## 1 0.0000000 3.87904871 9.8817094 5.33982287 5.28280126
## 2 0.1010101 4.73860431 9.7857215 5.51910079 5.46899089
## 3 0.2020202 8.50921338 9.6897335 5.69276104 5.64921021
## 4 0.3030303 5.71952918 9.5937456 5.86080361 5.82346649
## 5 0.4040404 6.01768168 9.4977577 6.02322852 5.99176701
## 6 0.5050505 9.36370818 9.4017697 6.18003576 6.15411903
## 7 0.6060606 7.02376079 9.3057818 6.33122532 6.31052982
## 8 0.7070707 3.73403425 9.2097939 6.47679722 6.46100666
## 9 0.8080808 5.04655753 9.1138060 6.61675145 6.60555682
## 10 0.9090909 5.67892399 9.0178180 6.75108800 6.74418756
## 11 1.0101010 9.16227440 8.9218301 6.87980689 6.87690616
## 12 1.1111111 7.57147951 8.8258422 7.00290810 7.00371990
## 13 1.2121212 7.78501398 8.7298542 7.12039165 7.12463603
## 14 1.3131313 7.33033390 8.6338663 7.23225752 7.23966183
## 15 1.4141414 6.11666178 8.5378784 7.33850572 7.34880458
## 16 1.5151515 10.91542407 8.4418904 7.43913626 7.45207154
## 17 1.6161616 8.44443068 8.3459025 7.53414912 7.54946998
## 18 1.7171717 3.61650551 8.2499146 7.62354431 7.64100717
## 19 1.8181818 9.04733990 8.1539266 7.70732183 7.72669040
## 20 1.9191919 6.78781174 8.0579387 7.78548168 7.80652691
## 21 2.0202020 5.68039177 7.9619508 7.85802386 7.88052400
## 22 2.1212121 7.45661215 7.8659628 7.92494837 7.94868893
## 23 2.2222222 5.91095407 7.7699749 7.98625521 8.01102896
## 24 2.3232323 6.56945966 7.6739870 8.04194438 8.06755137
## 25 2.4242424 6.83532091 7.5779990 8.09201588 8.11826344
## 26 2.5252525 4.76404833 7.4820111 8.13646971 8.16317242
## 27 2.6262626 9.85892273 7.3860232 8.17530587 8.20228560
## 28 2.7272727 8.52988673 7.2900352 8.20852436 8.23561024
## 29 2.8282828 5.98053666 7.1940473 8.23612517 8.26315362
## 30 2.9292929 10.79198858 7.0980594 8.25810832 8.28492300
## 31 3.0303030 9.15871357 7.0020714 8.27447380 8.30092566
## 32 3.1313131 7.73094672 6.9060835 8.28522160 8.31116887
## 33 3.2323232 10.12052374 6.8100956 8.29035174 8.31565989
## 34 3.3333333 10.08960031 6.7141076 8.28986420 8.31440601
## 35 3.4343434 9.97343458 6.6181197 8.28375900 8.30741448
## 36 3.5353535 9.69837019 6.5221318 8.27203612 8.29469258
## 37 3.6363636 9.41362043 6.4261439 8.25469558 8.27624759
## 38 3.7373737 8.16053532 6.3301559 8.23173736 8.25208677
## 39 3.8383838 7.64488520 6.2341680 8.20316147 8.22221739
## 40 3.9393939 7.46219849 6.1381801 8.16896791 8.18664672
## 41 4.0404040 6.79393468 6.0421921 8.12915669 8.14538204
## 42 4.1414141 7.72160040 5.9462042 8.08372779 8.09843062
## 43 4.2424242 5.55460675 5.8502163 8.03268122 8.04579973
## 44 4.3434343 12.36515405 5.7542283 7.97601698 7.98749663
## 45 4.4444444 10.37888696 5.6582404 7.91373507 7.92352860
## 46 4.5454545 5.64634482 5.5622525 7.84583549 7.85390291
## 47 4.6464646 7.01026951 5.4662645 7.77231824 7.77862683
## 48 4.7474747 6.80008384 5.3702766 7.69318332 7.69770763
## 49 4.8484848 9.20455834 5.2742887 7.60843073 7.61115259
## 50 4.9494949 7.38300169 5.1783007 7.51806047 7.51896896
## 51 5.0505051 7.95536675 5.0823128 7.42207253 7.42116404
## 52 5.1515152 7.28450429 4.9863249 7.32046693 7.31774507
## 53 5.2525253 7.14260313 4.8903369 7.21324366 7.20871935
## 54 5.3535354 9.84617304 4.7943490 7.10040271 7.09409413
## 55 5.4545455 6.53192910 4.6983611 6.98194410 6.97387668
## 56 5.5555556 9.88479306 4.6023731 6.85786782 6.84807429
## 57 5.6565657 3.61660520 4.5063852 6.72817386 6.71669421
## 58 5.7575758 7.73947543 4.4103973 6.59286224 6.57974373
## 59 5.8585859 6.66797173 4.3144093 6.45193294 6.43723011
## 60 5.9595960 6.69603986 4.2184214 6.30538598 6.28916062
## 61 6.0606061 6.86120734 4.1224335 6.15322134 6.13554253
## 62 6.1616162 4.92893130 4.0264455 5.99543903 5.97638312
## 63 6.2626263 5.09269145 3.9304576 5.83203905 5.81168965
## 64 6.3636364 3.54136163 3.8344697 5.66302141 5.64146939
## 65 6.4646465 3.24821430 3.7384818 5.48838609 5.46572963
## 66 6.5656566 5.80601657 3.6424938 5.30813310 5.28447762
## 67 6.6666667 5.89641956 3.5465059 5.12226244 5.09772064
## 68 6.7676768 4.90092734 3.4505180 4.93077411 4.90546596
## 69 6.8686869 6.42825088 3.3545300 4.73366811 4.70772084
## 70 6.9696970 8.46656056 3.2585421 4.53094444 4.50449257
## 71 7.0707071 3.16088227 3.1625542 4.32260310 4.29578842
## 72 7.1717172 -0.70496157 3.0665662 4.10864409 4.08161564
## 73 7.2727273 5.68916300 2.9705783 3.88906741 3.86198152
## 74 7.3737374 2.01747237 2.8745904 3.66387306 3.63689332
## 75 7.4747475 1.81192277 2.7786024 3.43306103 3.40635832
## 76 7.5757576 4.98502704 2.6826145 3.19663134 3.17038378
## 77 7.6767677 2.10416075 2.5866266 2.95458398 2.92897699
## 78 7.7777778 -0.03402802 2.4906386 2.70691894 2.68214520
## 79 7.8787879 2.49759319 2.3946507 2.45363624 2.42989569
## 80 7.9797980 1.57866050 2.2986628 2.19473586 2.17223573
## 81 8.0808081 1.58330676 2.2026748 1.93021782 1.90917259
## 82 8.1818182 2.05155254 2.1066869 1.66008210 1.64071354
## 83 8.2828283 0.24276319 2.0106990 1.38432872 1.36686585
## 84 8.3838384 1.96980605 1.9147110 1.10295766 1.08763680
## 85 8.4848485 -0.06907230 1.8187231 0.81596894 0.80303366
## 86 8.5858586 0.72019080 1.7227352 0.52336254 0.51306368
## 87 8.6868687 1.92890913 1.6267472 0.22513847 0.21773416
## 88 8.7878788 0.27807648 1.5307593 -0.07870327 -0.08294765
## 89 8.8888889 -1.57778910 1.4347714 -0.38816268 -0.38897447
## 90 8.9898990 1.03192806 1.3387834 -0.70323975 -0.70033903
## 91 9.0909091 0.37543746 1.2427955 -1.02393450 -1.01703406
## 92 9.1919192 -0.86678123 1.1468076 -1.35024692 -1.33905229
## 93 9.2929293 -1.84423840 1.0508196 -1.68217701 -1.66638645
## 94 9.3939394 -3.94176257 0.9548317 -2.01972477 -1.99902927
## 95 9.4949495 -0.33501589 0.8588438 -2.36289021 -2.33697348
## 96 9.5959596 -4.63333215 0.7628559 -2.71167331 -2.68021179
## 97 9.6969697 0.55923899 0.6668679 -3.06607408 -3.02873696
## 98 9.7979798 -1.13894159 0.5708800 -3.42609252 -3.38254170
## 99 9.8989899 -5.07042123 0.4748921 -3.79172863 -3.74161874
## 100 10.0000000 -7.05284180 0.3789041 -4.16298242 -4.10596081
Residual didefinisikan sebagai:
\[ e_i = Y_i - \hat{Y}_i \]
data$residual_linier <- data$y - data$Y_topi_linier
data$residual_poli2 <- data$y - data$Y_topi_poli2
data$residual_poli3 <- data$y - data$Y_topi_poli3
Menampilkan hasil:
head(
data.frame(
Y = data$y,
`Ŷ Linier` = data$Y_topi_linier,
`e Linier` = data$residual_linier,
`Ŷ Polinomial 2` = data$Y_topi_poli2,
`e Polinomial 2` = data$residual_poli2,
`Ŷ Polinomial 3` = data$Y_topi_poli3,
`e Polinomial 3` = data$residual_poli3,
check.names = FALSE
),
10
)
## Y Ŷ Linier e Linier Ŷ Polinomial 2 e Polinomial 2 Ŷ Polinomial 3
## 1 3.879049 9.881709 -6.00266070 5.339823 -1.460774162 5.282801
## 2 4.738604 9.785721 -5.04711717 5.519101 -0.780496476 5.468991
## 3 8.509213 9.689734 -1.18052016 5.692761 2.816452348 5.649210
## 4 5.719529 9.593746 -3.87421643 5.860804 -0.141274434 5.823466
## 5 6.017682 9.497758 -3.48007600 6.023229 -0.005546837 5.991767
## 6 9.363708 9.401770 -0.03806157 6.180036 3.183672422 6.154119
## 7 7.023761 9.305782 -2.28202103 6.331225 0.692535462 6.310530
## 8 3.734034 9.209794 -5.47575963 6.476797 -2.742762972 6.461007
## 9 5.046558 9.113806 -4.06724842 6.616751 -1.570193912 6.605557
## 10 5.678924 9.017818 -3.33889403 6.751088 -1.072164008 6.744188
## e Polinomial 3
## 1 -1.40375256
## 2 -0.73038658
## 3 2.86000317
## 4 -0.10393731
## 5 0.02591467
## 6 3.20958915
## 7 0.71323096
## 8 -2.72697241
## 9 -1.55899928
## 10 -1.06526357
ggplot(
data,
aes(x = x, y = y)
) +
geom_point(
alpha = 0.5
) +
geom_line(
aes(
y = Y_topi_linier,
color = "Linier"
),
linewidth = 1
) +
geom_line(
aes(
y = Y_topi_poli2,
color = "Polinomial derajat 2"
),
linewidth = 1
) +
geom_line(
aes(
y = Y_topi_poli3,
color = "Polinomial derajat 3"
),
linewidth = 1
) +
labs(
title = "Perbandingan Regresi Linier dan Polinomial",
x = "X",
y = "Y",
color = "Model"
) +
theme_minimal()
RMSE digunakan untuk mengukur besarnya kesalahan prediksi model.
rmse <- function(actual, predicted) {
sqrt(
mean(
(actual - predicted)^2
)
)
}
Kemudian hitung RMSE masing-masing model:
hasil_rmse <- data.frame(
Model = c(
"Linier",
"Polinomial derajat 2",
"Polinomial derajat 3"
),
RMSE = c(
rmse(
data$y,
data$Y_topi_linier
),
rmse(
data$y,
data$Y_topi_poli2
),
rmse(
data$y,
data$Y_topi_poli3
)
)
)
hasil_rmse
## Model RMSE
## 1 Linier 2.761195
## 2 Polinomial derajat 2 1.800917
## 3 Polinomial derajat 3 1.800772
set.seed(42)
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) {
# Membuat data untuk model
data_cv <- data
# Membuat variabel polinomial
if (d == 1) {
data_cv$X1 <- data_cv$x
formula_cv <- y ~ X1
} else {
for (j in 1:d) {
data_cv[[paste0("X", j)]] <- data_cv$x^j
}
prediktor <- paste0("X", 1:d, collapse = " + ")
formula_cv <- as.formula(
paste("y ~", prediktor)
)
}
# Cross-validation
model_cv <- train(
formula_cv,
data = data_cv,
method = "lm",
trControl = kontrol
)
# Simpan RMSE
hasil_cv <- rbind(
hasil_cv,
data.frame(
derajat = d,
RMSE = model_cv$results$RMSE
)
)
}
hasil_cv
## derajat RMSE
## 1 1 2.757805
## 2 2 1.825068
## 3 3 1.859968
## 4 4 1.877628
## 5 5 1.871701
## 6 6 1.880719
Derajat optimal adalah derajat dengan nilai RMSE cross-validation terkecil.
derajat_optimal <- hasil_cv$derajat[
which.min(hasil_cv$RMSE)
]
cat(
"Derajat optimal berdasarkan Cross-Validation:",
derajat_optimal
)
## Derajat optimal berdasarkan Cross-Validation: 2
ggplot(
hasil_cv,
aes(
x = derajat,
y = RMSE
)
) +
geom_line(
linewidth = 1
) +
geom_point(
size = 3
) +
geom_vline(
xintercept = derajat_optimal,
linetype = "dashed"
) +
scale_x_continuous(
breaks = 1:derajat_max
) +
labs(
title = "Pemilihan Derajat Optimal dengan Cross-Validation",
x = "Derajat Polinomial",
y = "RMSE Cross-Validation"
) +
theme_minimal()
model_final <- lm(
y ~ poly(
x,
degree = derajat_optimal,
raw = TRUE
),
data = data
)
summary(model_final)
##
## Call:
## lm(formula = y ~ poly(x, degree = 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
## (Intercept) 5.33982 0.53777 9.929
## poly(x, degree = derajat_optimal, raw = TRUE)1 1.80266 0.24855 7.253
## poly(x, degree = derajat_optimal, raw = TRUE)2 -0.27529 0.02405 -11.447
## Pr(>|t|)
## (Intercept) <2e-16 ***
## poly(x, degree = derajat_optimal, raw = TRUE)1 1e-10 ***
## poly(x, degree = derajat_optimal, raw = TRUE)2 <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
data$Y_topi <- predict(model_final)
Menampilkan nilai aktual dan prediksi:
hasil_final <- data.frame(
X = data$x,
Y = data$y,
`Ŷ` = data$Y_topi,
Residual = data$y - data$Y_topi,
check.names = FALSE
)
head(hasil_final, 10)
## X Y Ŷ Residual
## 1 0.0000000 3.879049 5.339823 -1.460774162
## 2 0.1010101 4.738604 5.519101 -0.780496476
## 3 0.2020202 8.509213 5.692761 2.816452348
## 4 0.3030303 5.719529 5.860804 -0.141274434
## 5 0.4040404 6.017682 6.023229 -0.005546837
## 6 0.5050505 9.363708 6.180036 3.183672422
## 7 0.6060606 7.023761 6.331225 0.692535462
## 8 0.7070707 3.734034 6.476797 -2.742762972
## 9 0.8080808 5.046558 6.616751 -1.570193912
## 10 0.9090909 5.678924 6.751088 -1.072164008