Tugas PSD Sesi UTS
Data
library(rio)
data.areg <- import("https://raw.githubusercontent.com/akmalbasisjkusumah/MPDW/main/Data/datapsd.csv")
data.areg <- data.areg[,-c(1,13,14)]
colnames(data.areg)## [1] "Y" "X1" "X2" "X3" "X4" "X5" "X6" "X7" "X8" "X9" "X10"
colnames(data.areg) <- c("PPM", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10")
str(data.areg)## 'data.frame': 34 obs. of 11 variables:
## $ PPM: num 14.7 8.38 5.98 6.81 7.66 ...
## $ x1 : num 99.7 98.7 98.8 95.2 98.4 ...
## $ x2 : num 6.07 5.82 6.22 4.38 4.64 ...
## $ x3 : num 3.17 2.52 2.51 2.94 2.7 ...
## $ x4 : num 77.5 82.3 69.3 84.1 79.5 ...
## $ x5 : num 4.21 4.73 4.36 4.55 5.13 5.23 4.31 4.28 4.4 5.09 ...
## $ x6 : num 99.4 99.5 99.5 99.6 99.3 ...
## $ x7 : num 98 96.8 96.5 95.7 96.2 ...
## $ x8 : num 83.1 78.7 83.7 77.3 72.5 ...
## $ x9 : num 99.6 99.6 99.9 99.9 99.5 ...
## $ x10: num 44.3 47.6 47.7 47.9 49.5 ...
head(data.areg,10)## PPM x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
## 1 14.695 99.69 6.070 3.166460 77.48 4.21 99.44 97.96 83.10 99.62 44.28695
## 2 8.375 98.70 5.815 2.522610 82.30 4.73 99.50 96.78 78.66 99.57 47.61680
## 3 5.980 98.82 6.225 2.512539 69.27 4.36 99.55 96.52 83.71 99.86 47.65741
## 4 6.810 95.20 4.385 2.938564 84.06 4.55 99.61 95.72 77.32 99.92 47.90955
## 5 7.660 98.39 4.645 2.698941 79.54 5.13 99.33 96.20 72.53 99.52 49.51169
## 6 11.925 96.55 4.685 3.144446 78.62 5.23 99.38 95.06 70.93 99.50 49.55185
## 7 14.480 99.21 3.490 2.238094 79.58 4.31 99.49 97.73 79.31 99.49 50.36144
## 8 11.505 98.66 4.415 2.440486 83.65 4.28 99.56 95.63 71.14 99.32 47.81690
## 9 4.530 99.53 4.475 3.264884 91.63 4.40 99.41 93.00 68.42 99.18 48.90753
## 10 6.135 97.96 8.125 3.050172 87.74 5.09 99.28 99.01 84.54 99.72 53.03225
Eksplorasi Data
library(corrplot)## corrplot 0.92 loaded
kor <- cor(data.areg)
corrplot(kor, method = "square", type = "lower", diag = TRUE)
corrplot(kor, add=TRUE, method = 'number', type='upper',
tl.pos = "lt",
tl.col = "black")
Dari plot korelasi berikut didapatkan bahwa beberapa peubah penjelas (x)
tidak mempengaruhi peubah responnya. Yang tidak berpengaruh adalah
peubah x3, x5, x8, dan x10. Sehingga peubah-peubah tersebut tidak
disertakan pada model awal regresi.
Model Awal
fit <- lm(PPM ~ x1+x2+x4+x6+x7+x9, data = data.areg)
summary(fit)##
## Call:
## lm(formula = PPM ~ x1 + x2 + x4 + x6 + x7 + x9, data = data.areg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.5844 -2.3450 0.1494 1.7711 7.8937
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.87218 56.47300 0.671 0.5082
## x1 -0.06177 0.19422 -0.318 0.7529
## x2 -0.99761 0.52026 -1.918 0.0658 .
## x4 -0.27149 0.11827 -2.295 0.0297 *
## x6 0.15040 1.10607 0.136 0.8928
## x7 0.63230 0.35366 1.788 0.0850 .
## x9 -0.71149 0.57004 -1.248 0.2227
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.809 on 27 degrees of freedom
## Multiple R-squared: 0.572, Adjusted R-squared: 0.4768
## F-statistic: 6.013 on 6 and 27 DF, p-value: 0.0004317
model1 <- summary(fit)
anova(fit)## Analysis of Variance Table
##
## Response: PPM
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 357.58 357.58 24.6420 3.354e-05 ***
## x2 1 42.89 42.89 2.9556 0.09703 .
## x4 1 51.98 51.98 3.5823 0.06917 .
## x6 1 1.21 1.21 0.0832 0.77523
## x7 1 47.25 47.25 3.2564 0.08231 .
## x9 1 22.61 22.61 1.5579 0.22269
## Residuals 27 391.80 14.51
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Didapatkan model awal seperti diatas dengan peubah yang signifikan pada model hanya x4. Dapat diketahui juga bahwa nilai Adj. R-Squared 47,68%. Intersep merupakan nilai dari peubah respon ketika semua nilai peubah penjelasnya 0. Estimate merupakan koefisien dari peubah penjelasnya dengan makna bahwa setiap penambahan satu satuan peubah penjelas akan meningkatkan/menurunkan(Tergantung pada tanda koefisien) nilai peubah respon.
Deteksi Pencilan dan Leverage
library(olsrr)##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
#titik pencilan
sisaan<-residuals(fit)
stdr.sisaan<-rstandard(fit)
mut.stdr.sisaan<-abs(stdr.sisaan)
pencilll <- data.frame(sort(mut.stdr.sisaan))
pencilan <- data.frame(pencilll, pencilll>2)
pencilan## sort.mut.stdr.sisaan. sort.mut.stdr.sisaan..1
## 11 0.03208269 FALSE
## 10 0.10076922 FALSE
## 12 0.12838322 FALSE
## 2 0.15340447 FALSE
## 16 0.20680181 FALSE
## 26 0.28563803 FALSE
## 25 0.29641059 FALSE
## 9 0.30375081 FALSE
## 30 0.35358166 FALSE
## 13 0.38640749 FALSE
## 23 0.38802062 FALSE
## 15 0.39814576 FALSE
## 28 0.44583283 FALSE
## 6 0.47976960 FALSE
## 7 0.54088472 FALSE
## 8 0.55589352 FALSE
## 27 0.55934396 FALSE
## 4 0.65304697 FALSE
## 17 0.76400329 FALSE
## 5 0.78143176 FALSE
## 14 0.82555857 FALSE
## 18 0.97996913 FALSE
## 22 1.07770049 FALSE
## 1 1.13939545 FALSE
## 20 1.19738001 FALSE
## 24 1.21748879 FALSE
## 34 1.49325346 FALSE
## 31 1.54650308 FALSE
## 32 1.58183014 FALSE
## 19 1.60760194 FALSE
## 29 1.69126476 FALSE
## 3 1.73105128 FALSE
## 21 2.06896331 TRUE
## 33 2.40649777 TRUE
#titik leverage
hi<-hatvalues(fit, infl = influence(fit))
ambang_batas<-2*(10/34) #2p/n, p=10, n=34
leveragee <- data.frame(hi, hi>ambang_batas)
leveragee## hi hi...ambang_batas
## 1 0.12734347 FALSE
## 2 0.04584987 FALSE
## 3 0.24657847 FALSE
## 4 0.07077431 FALSE
## 5 0.07382134 FALSE
## 6 0.05592525 FALSE
## 7 0.19237301 FALSE
## 8 0.04738742 FALSE
## 9 0.18879108 FALSE
## 10 0.19074472 FALSE
## 11 0.19833398 FALSE
## 12 0.30339477 FALSE
## 13 0.07680496 FALSE
## 14 0.27102365 FALSE
## 15 0.13135635 FALSE
## 16 0.19671686 FALSE
## 17 0.17997014 FALSE
## 18 0.65711832 TRUE
## 19 0.22034887 FALSE
## 20 0.20612615 FALSE
## 21 0.30206125 FALSE
## 22 0.07969125 FALSE
## 23 0.14465533 FALSE
## 24 0.06275433 FALSE
## 25 0.10842827 FALSE
## 26 0.20949065 FALSE
## 27 0.31991966 FALSE
## 28 0.08470056 FALSE
## 29 0.21237887 FALSE
## 30 0.28101722 FALSE
## 31 0.13424305 FALSE
## 32 0.13588044 FALSE
## 33 0.25853616 FALSE
## 34 0.98545997 TRUE
#titik pencilan dan leverage
ols_plot_resid_lev(fit)
Dapat diketahui dari plot tersebut terdapat pencilan pada amatan ke 33
dan 21. sementara untuk leverage terdeteksi pada amatan ke 18 dan
34.
Deteksi Amatan Berpengaruh
#============Deteksi amatan berpengaruh =============
#Cook's Distance
di<-cooks.distance(fit)
f<-qf(0.05,10,24, lower.tail = F) # qf(p,db1,db2, lower.tail=F). db1=n-p, db2=n-p, lower.tail=F untuk p=taraf nyata(alpha) dan sebaliknya untuk p= (1-alpha)
data.frame(di, di>f)## di di...f
## 1 2.706352e-02 FALSE
## 2 1.615472e-04 FALSE
## 3 1.401003e-01 FALSE
## 4 4.640291e-03 FALSE
## 5 6.952984e-03 FALSE
## 6 1.947910e-03 FALSE
## 7 9.955078e-03 FALSE
## 8 2.195998e-03 FALSE
## 9 3.067507e-03 FALSE
## 10 3.419202e-04 FALSE
## 11 3.637871e-05 FALSE
## 12 1.025510e-03 FALSE
## 13 1.774552e-03 FALSE
## 14 3.619858e-02 FALSE
## 15 3.424487e-03 FALSE
## 16 1.496179e-03 FALSE
## 17 1.830051e-02 FALSE
## 18 2.629215e-01 FALSE
## 19 1.043445e-01 FALSE
## 20 5.317991e-02 FALSE
## 21 2.646581e-01 FALSE
## 22 1.436730e-02 FALSE
## 23 3.637516e-03 FALSE
## 24 1.417823e-02 FALSE
## 25 1.526425e-03 FALSE
## 26 3.088813e-03 FALSE
## 27 2.102522e-02 FALSE
## 28 2.627660e-03 FALSE
## 29 1.101841e-01 FALSE
## 30 6.980650e-03 FALSE
## 31 5.297847e-02 FALSE
## 32 5.620886e-02 FALSE
## 33 2.884724e-01 FALSE
## 34 2.158950e+01 TRUE
library(ggplot2)
cooks_crit = f
model_cooks <- cooks.distance(fit)
df <- data.frame(obs = names(model_cooks),
cooks = model_cooks)
ggplot(df, aes(y = cooks, x = obs)) +
geom_point() +
geom_hline(yintercept = cooks_crit, linetype="dashed") +
labs(title = "Cook's Distance",
subtitle = "Influential Observation ",
x = "Observation Number",
y = "Cook's")ols_plot_cooksd_bar(fit)#Deteksi amatan berpengaruh (dfbetas)
ols_plot_dfbetas(fit)#Deteksi amatan berpengaruh (dffits)
ols_plot_dffits(fit)Penanganan amatan tak berpengaruh
data.areg1 <- data.areg[-c(3,18,32),]
fit2 <- lm(PPM ~ x1+x2+x4+x6+x7+x9, data = data.areg1)
summary(fit2)##
## Call:
## lm(formula = PPM ~ x1 + x2 + x4 + x6 + x7 + x9, data = data.areg1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.598 -1.760 0.410 1.537 6.904
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 14.00906 53.11994 0.264 0.79424
## x1 0.02149 0.19292 0.111 0.91221
## x2 -1.15536 0.49163 -2.350 0.02733 *
## x4 -0.42336 0.12870 -3.289 0.00309 **
## x6 0.69755 1.15090 0.606 0.55014
## x7 0.95692 0.36556 2.618 0.01509 *
## x9 -1.27448 0.81635 -1.561 0.13157
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.504 on 24 degrees of freedom
## Multiple R-squared: 0.6606, Adjusted R-squared: 0.5758
## F-statistic: 7.786 on 6 and 24 DF, p-value: 0.0001003
model1 <- summary(fit2)
anova(fit2)## Analysis of Variance Table
##
## Response: PPM
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 372.66 372.66 30.3501 1.152e-05 ***
## x2 1 30.46 30.46 2.4805 0.12836
## x4 1 77.51 77.51 6.3123 0.01912 *
## x6 1 0.36 0.36 0.0293 0.86542
## x7 1 62.70 62.70 5.1062 0.03320 *
## x9 1 29.93 29.93 2.4373 0.13157
## Residuals 24 294.69 12.28
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
data.areg2 <- data.areg[-c(18,32,34),]
fit3 <- lm(PPM ~ x1+x2+x4+x6+x7+x9, data = data.areg2)
summary(fit3)##
## Call:
## lm(formula = PPM ~ x1 + x2 + x4 + x6 + x7 + x9, data = data.areg2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.096 -2.173 0.222 2.036 6.099
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 372.3404 223.3122 1.667 0.1084
## x1 0.0282 0.2016 0.140 0.8899
## x2 -1.0342 0.5139 -2.012 0.0555 .
## x4 -0.2747 0.1190 -2.309 0.0299 *
## x6 -3.2287 2.6280 -1.229 0.2311
## x7 1.0376 0.3844 2.699 0.0125 *
## x9 -1.1754 0.8351 -1.407 0.1721
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.592 on 24 degrees of freedom
## Multiple R-squared: 0.4923, Adjusted R-squared: 0.3654
## F-statistic: 3.879 on 6 and 24 DF, p-value: 0.00757
model1 <- summary(fit3)
anova(fit3)## Analysis of Variance Table
##
## Response: PPM
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 110.942 110.942 8.5970 0.007289 **
## x2 1 30.758 30.758 2.3834 0.135713
## x4 1 46.548 46.548 3.6071 0.069620 .
## x6 1 12.177 12.177 0.9436 0.341049
## x7 1 74.335 74.335 5.7603 0.024507 *
## x9 1 25.563 25.563 1.9809 0.172116
## Residuals 24 309.714 12.905
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
data.areg3 <- data.areg[-c(3,18,32,34),]
fit4 <- lm(PPM ~ x1+x2+x4+x6+x7+x9, data = data.areg3)
summary(fit4)##
## Call:
## lm(formula = PPM ~ x1 + x2 + x4 + x6 + x7 + x9, data = data.areg3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.0313 -1.4163 0.2792 2.2636 5.5154
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 293.76578 219.38103 1.339 0.19363
## x1 0.08906 0.19691 0.452 0.65530
## x2 -1.02859 0.49390 -2.083 0.04860 *
## x4 -0.38315 0.13045 -2.937 0.00740 **
## x6 -2.34161 2.57729 -0.909 0.37301
## x7 1.06747 0.36988 2.886 0.00834 **
## x9 -1.26689 0.80432 -1.575 0.12889
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.452 on 23 degrees of freedom
## Multiple R-squared: 0.5395, Adjusted R-squared: 0.4194
## F-statistic: 4.491 on 6 and 23 DF, p-value: 0.00377
model1 <- summary(fit4)
anova(fit4)## Analysis of Variance Table
##
## Response: PPM
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 104.074 104.074 8.7317 0.007102 **
## x2 1 27.892 27.892 2.3401 0.139718
## x4 1 77.362 77.362 6.4906 0.017992 *
## x6 1 5.112 5.112 0.4289 0.519026
## x7 1 77.138 77.138 6.4718 0.018140 *
## x9 1 29.571 29.571 2.4809 0.128889
## Residuals 23 274.139 11.919
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Didapatkan model terbaik adalah setelah menghapus amatan ke-3, 18, dan 32.
Seleksi Peubah
library(olsrr)
ols_step_backward_p(fit2)##
##
## Elimination Summary
## ------------------------------------------------------------------------
## Variable Adj.
## Step Removed R-Square R-Square C(p) AIC RMSE
## ------------------------------------------------------------------------
## 1 x1 0.6604 0.5925 5.0124 171.8004 3.4342
## 2 x6 0.6533 0.600 3.5151 170.4427 3.4026
## ------------------------------------------------------------------------
ols_step_forward_p(fit2)##
## Selection Summary
## ------------------------------------------------------------------------
## Variable Adj.
## Step Entered R-Square R-Square C(p) AIC RMSE
## ------------------------------------------------------------------------
## 1 x4 0.5063 0.4893 7.9125 175.4029 3.8448
## 2 x2 0.5491 0.5169 6.8855 174.5914 3.7393
## 3 x7 0.5932 0.5480 5.7681 173.4019 3.6170
## 4 x9 0.6533 0.6000 3.5151 170.4427 3.4026
## ------------------------------------------------------------------------
ols_step_both_p(fit2)##
## Stepwise Selection Summary
## ------------------------------------------------------------------------------------
## Added/ Adj.
## Step Variable Removed R-Square R-Square C(p) AIC RMSE
## ------------------------------------------------------------------------------------
## 1 x4 addition 0.506 0.489 7.9130 175.4029 3.8448
## ------------------------------------------------------------------------------------
Setelah dilakukan seleksi peubah didapatkan model baru yang memiliki aic dan R-Square lebih baik yaitu model yang menyertakan peubah x2, x4, x7, dan x9.
Model Baru
fit2new <- lm(PPM ~ x2+x4+x7+x9, data = data.areg1)
summary(fit2new)##
## Call:
## lm(formula = PPM ~ x2 + x4 + x7 + x9, data = data.areg1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.018 -2.099 0.129 1.661 6.707
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.77689 25.03273 1.229 0.229908
## x2 -1.20288 0.46226 -2.602 0.015093 *
## x4 -0.37555 0.09508 -3.950 0.000533 ***
## x7 0.94536 0.34809 2.716 0.011592 *
## x9 -0.74992 0.35310 -2.124 0.043362 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.403 on 26 degrees of freedom
## Multiple R-squared: 0.6533, Adjusted R-squared: 0.6
## F-statistic: 12.25 on 4 and 26 DF, p-value: 9.916e-06
Pengujian Asumsi Pada Sisaan
Asumsi Secara Ekslporatif
par(mfrow = c(2,2))
qqnorm(fit2new$residuals)
qqline(fit2new$residuals, col = "maroon", lwd = 2)
plot(fit2new$fitted.values, fit2new$residuals, col = "maroon", pch = 20, xlab = "Sisaan", ylab = "Fitted Values", main = "Sisaan vs Fitted Values")
abline(a = 0, b = 0, lwd = 2)
hist(fit2new$residuals, col = "maroon")
plot(fit2new$residuals, type="o", col = "maroon",
ylab = "Sisaan", xlab = "Order", main = "Sisaan vs Order")
Berdasarkan uji asumsi secara eksploratif dapat diamati bahwa semua
asumsi terpenuhi, untuk melihat kebenarannya dilakukan uji secara
formal.
Uji Asumsi Formal
# Asumsi GAUSS MARKOV
# 1 Nilai harapan sisaan sama dengan nol
t.test(fit2new$residuals,
mu = 0,
conf.level = 0.95) #asumsi nilai harapan sama dengan 0 terpenuhi##
## One Sample t-test
##
## data: fit2new$residuals
## t = 1.5721e-16, df = 30, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -1.161896 1.161896
## sample estimates:
## mean of x
## 8.944318e-17
# 2 Sisaan saling bebas
library(lmtest)## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(randtests)
runs.test(fit2new$residuals) #asumsi sisaan saling bebas terpenuhi##
## Runs Test
##
## data: fit2new$residuals
## statistic = 0, runs = 16, n1 = 15, n2 = 15, n = 30, p-value = 1
## alternative hypothesis: nonrandomness
# 3 Ragam sisaan homogen
library(lmtest)
bptest(fit2new)##
## studentized Breusch-Pagan test
##
## data: fit2new
## BP = 7.4709, df = 4, p-value = 0.113
# ASUMSI NORMALITAS SISAAN
shapiro.test(fit2new$residuals)##
## Shapiro-Wilk normality test
##
## data: fit2new$residuals
## W = 0.98997, p-value = 0.9898
nortest::ad.test(fit2new$residuals)##
## Anderson-Darling normality test
##
## data: fit2new$residuals
## A = 0.16852, p-value = 0.9283
ks.test(fit2new$residuals, "pnorm", mean=mean(fit2new$residuals), sd=sd(fit2new$residuals)) #asumsi normalitas terpenuhi##
## Exact one-sample Kolmogorov-Smirnov test
##
## data: fit2new$residuals
## D = 0.081377, p-value = 0.9759
## alternative hypothesis: two-sided
Setelah dilakukan uji secara formal didapatkan nilai p-value untuk semua uji lebih besar dari 0,05. Artinya semua asumsi pada sisaan model terpenuhi.
Mendeteksi Multikolinearitas
library(car)## Loading required package: carData
car::vif(fit2new)## x2 x4 x7 x9
## 1.356330 2.351274 3.651151 3.486138
Tidak terdapat multikolinearitas pada model karena semua nilai vif < 10
Regresi Ridge (glmnet)
Library
lapply(c("glmnet","lmridge"),library,character.only=T)[[1]]## Loading required package: Matrix
## Loaded glmnet 4.1-8
##
## Attaching package: 'lmridge'
## The following object is masked from 'package:car':
##
## vif
## [1] "glmnet" "Matrix" "car" "carData" "randtests" "lmtest"
## [7] "zoo" "ggplot2" "olsrr" "corrplot" "rio" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
Cross validation (CV)
x <- data.matrix(data.areg1[,c('x2','x4','x7','x9')])
y <- data.areg1$PPM
cv.r<-cv.glmnet(x,y,alpha=0);plot(cv.r)Best Model
best.lr<-cv.r$lambda.min
bestridge<-glmnet(x,y,alpha=0,lambda=best.lr);coef(bestridge)## 5 x 1 sparse Matrix of class "dgCMatrix"
## s0
## (Intercept) 46.7799833
## x2 -0.8320764
## x4 -0.2849838
## x7 0.4196707
## x9 -0.4974478
R-Squared
rsq<-function(bestmodel,bestlambda,x,y){
#y duga
y.duga <- predict(bestmodel, s = bestlambda, newx = x)
#JKG dan JKT
jkt <- sum((y - mean(y))^2)
jkg <- sum((y.duga- y)^2)
#find R-Squared
rsq <- 1 - jkg/jkt
return(rsq)
}rsq(bestridge,best.lr,x,y)## [1] 0.6157579
Regresi Ridge (lmridge)
lmr<-lmridge(y~x,data.areg1,scaling="centered");plot(lmr);vif(lmr)## xx2 xx4 xx7 xx9
## k=0 0.01846 0.00078 0.01047 0.01077
summary(lmr)##
## Call:
## lmridge.default(formula = y ~ x, data = data.areg1, scaling = "centered")
##
##
## Coefficients: for Ridge parameter K= 0
## Estimate Estimate (Sc) StdErr (Sc) t-value (Sc) Pr(>|t|)
## Intercept 30.7769 30.7769 47.7981 0.6439 0.5253
## xx2 -1.2029 -1.2029 0.4536 -2.6518 0.0135 *
## xx4 -0.3756 -0.3755 0.0933 -4.0248 0.0004 ***
## xx7 0.9454 0.9454 0.3416 2.7676 0.0103 *
## xx9 -0.7499 -0.7499 0.3465 -2.1643 0.0398 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Ridge Summary
## R2 adj-R2 DF ridge F AIC BIC
## 0.65330 0.61480 3.99999 12.72090 78.46849 190.65803
## Ridge minimum MSE= 0.4512104 at K= 0
## P-value for F-test ( 3.99999 , 27.00004 ) = 6.03948e-06
## -------------------------------------------------------------------
rsq(lmr,best.lr,as.matrix(data.areg1[,-1]),data.areg1$PPM)## [1] 0.6533285
Regresi Lasso
Cross validation (CV)
cv.l<-cv.glmnet(x,y,alpha=1);plot(cv.l)Best model
best.ll<-cv.l$lambda.min
bestlasso<-glmnet(x,y,alpha=1,lambda=best.ll);coef(bestlasso)## 5 x 1 sparse Matrix of class "dgCMatrix"
## s0
## (Intercept) 37.9165268
## x2 -0.1301254
## x4 -0.2463224
## x7 .
## x9 -0.0690297
R-Squared
rsq(bestlasso,best.ll,x,y)## [1] 0.480572
Perbandingan Model
RSE Model Regresi Lasso
# Prediksi model Lasso pada data pelatihan
train_predictionsLasso <- predict(bestlasso,newx = x)
# Hitung residu (selisih antara prediksi dan nilai sebenarnya)
residualsLasso <- y - train_predictionsLasso
# Hitung varian residu
dfLasso <- length(y) - length(bestlasso$beta)
residual_varianceLasso <- sum(residualsLasso^2) / dfLasso
# Hitung RSE
rseLasso <- sqrt(residual_varianceLasso)
# Tampilkan hasil RSE
print(paste("Residual Standard Error (RSE):",rseLasso))## [1] "Residual Standard Error (RSE): 4.08711509567627"
RSE Model Regresi Ridge
# Prediksi model ridge pada data pelatihan
train_predictionsridge <- predict(bestridge,newx = x)
# Hitung residu (selisih antara prediksi dan nilai sebenarnya)
residualsridge <- y - train_predictionsridge
# Hitung varian residu
dfridge <- length(y) - length(bestridge$beta)
residual_varianceridge <- sum(residualsridge^2) / dfridge
# Hitung RSE
rseridge <- sqrt(residual_varianceridge)
# Tampilkan hasil RSE
print(paste("Residual Standard Error (RSE):",rseridge))## [1] "Residual Standard Error (RSE): 3.51525330870488"
Diakhir kita akan membandingkan nilai setiap RSE dan R-Square diperoleh :
| Model Regresi | R-Square | RSE |
|---|---|---|
| fit | 47.68% | 3.8090 |
| fit2new | 60% | 3.4030 |
| Model Ridge | 61.48% | 3.8013 |
| Model Lasso | 53.07% | 3.8852 |
INTEPRETASI
Dari residual standard error (RSE) yang diperoleh pada setiap model yang dicobakan maka kita akan memilih yang memiliki RSE terkecil. Pada analisis kali ini didapatkan model terbaiknya adalah pada model fit2new atau model setelah dilakukannya seleksi peubah menggunakan forward stepwise. Sementara metode penyusutan tidak begitu berpengaruh karena dari awal tidak terjadi multikolinearitas.