Tugas Individu 1 MPDW
Deskripsi Data
Indeks Pembangunan Manusia (IPM) adalah indikator yang digunakan untuk mengukur keberhasilan dalam upaya membangun kualitas hidup manusia. IPM dibangun melalui pendekatan tiga dimensi dasar yaitu umur panjang dan sehat, pengetahuan, dan kehidupan yang layak.
Memanggil Library
Diperlukan beberapa package untuk menjalankan regresi pada data time series dan untuk mendeteksi adanya autokolerasi dan penanganannya.
library(dplyr)
library(readxl)
library(ggplot2)
library(forecast)
library(TTR)
library(tseries)
library(lmtest) #uji-Durbin Watson
library(orcutt) #Cochrane-Orcutt
library(HoRM) #Hildreth Lu
library(lawstat)Deklarasi Data Indeks Pembangunan Manusia Provinsi Sumatera Barat Tahun 2010-2021
dtregresi <- read_excel("C:/Users/user/OneDrive/Documents/Semester 5/MPDW/Data IPM per provinsi 2010-2021.xlsx", sheet = "Sheet1")
dtregresi## # A tibble: 12 x 2
## x y
## <dbl> <dbl>
## 1 2010 67.2
## 2 2011 67.8
## 3 2012 68.4
## 4 2013 68.9
## 5 2014 69.4
## 6 2015 70.0
## 7 2016 70.7
## 8 2017 71.2
## 9 2018 71.7
## 10 2019 72.4
## 11 2020 72.4
## 12 2021 72.6
Eksplorasi Data
x <- dtregresi$x
y <- dtregresi$yTime Series Plot
q <- ggplot(dtregresi, aes(x=x, y=y)) +
geom_line(lwd=1.2,col="red3")
q + labs(x="Tahun",y = "Indeks Pembangunan",
title="Time Series Plot Indeks Pembangunan Manusia Provinsi Sumatera Barat ",
subtitle = "Periode 2010 - 2021") +
theme_bw() +
theme(
plot.title = element_text(size = 14L,
face = "bold",
hjust = 0.5),
plot.subtitle = element_text(size = 11L,
face = "plain",
hjust = 0.5)
)+ geom_point(size=2) + geom_text(aes(label=paste(y,"%")),vjust=-0.8,size=3)#korelasi x dan y
cor(x,y)## [1] 0.9932977
Model Regresi Data Time Series
model <- lm(y~x, data = dtregresi)
summary(model)##
## Call:
## lm(formula = y ~ x, data = dtregresi)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.45231 -0.09554 -0.03215 0.20099 0.33126
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -981.4216 38.6982 -25.36 2.08e-10 ***
## x 0.5218 0.0192 27.18 1.05e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2296 on 10 degrees of freedom
## Multiple R-squared: 0.9866, Adjusted R-squared: 0.9853
## F-statistic: 738.5 on 1 and 10 DF, p-value: 1.053e-10
Deteksi Autokorelasi
1. Residual Plot
#sisaan dan fitted value
resi1 <- residuals(model)
fit <- predict(model)
#Diagnostik dengan eksploratif
par(mfrow = c(2,2))
qqnorm(resi1)
qqline(resi1, col = "steelblue", lwd = 2)
plot(fit, resi1, col = "steelblue", pch = 20, xlab = "Sisaan",
ylab = "Fitted Values", main = "Sisaan vs Fitted Values")
abline(a = 0, b = 0, lwd = 2)
hist(resi1, col = "steelblue", main = "Histogram Residual")
plot(seq(1,12,1), resi1, col = "steelblue", pch = 20,
xlab = "Sisaan", ylab = "Order", main = "Sisaan vs Order")
lines(seq(1,12,1), resi1, col = "red")
abline(a = 0, b = 0, lwd = 2)Berdasarkan plot Sisaan vs Order tidak membentuk pola tertentu sehingga secara eksploratif tidak terlihat gejala autokolerasi
2. ACF dan PCAF Plot
#ACF dan PACF identifikasi autokorelasi
par(mfrow = c(1,2))
acf(resi1)
pacf(resi1) Dari plot ACF terlihat bahwa tidak terdapat garis hitam yang melewati batas atau berpotongan dengan garis biru sehingga dapat diasumsikan bahwa tidak terdapat autokorelasi dalam model regresi (kecuali lag=0, karena selalu memiliki rho=1)
Uji Statistik
1. Durbin Watson Test
Hipotesis yang diuji : H0 : tidak ada autokorelasi H1 : ada autokorelasi
lmtest::dwtest(model, alternative = 'two.sided') ##
## Durbin-Watson test
##
## data: model
## DW = 0.82777, p-value = 0.004736
## alternative hypothesis: true autocorrelation is not 0
Berdasarkan hasil uji statistik menggunakan Durbin Watson diperoleh p-value sebebsar 0.0047 < 0.05 yang berarti tolak H0 sehingga cukup bukti untuk menyatakan bahwa terdapat autokorelasi (positif) pada model regresi time series pada taraf nyata 5%. Oleh karena pada uji statistik terdeteksi adanya autokorelasi, maka diperlukan adanya penanganan menggunakan Cochrane-Orcutt dan Hildreth-lu
Penanganan Autokorelasi
1. Cochrane-Orcutt
modelco <- orcutt::cochrane.orcutt(model,convergence=6, max.iter = 10000)
modelco## Cochrane-orcutt estimation for first order autocorrelation
##
## Call:
## lm(formula = y ~ x, data = dtregresi)
##
## number of interaction: 1193
## rho 0.895751
##
## Durbin-Watson statistic
## (original): 0.82777 , p-value: 2.368e-03
## (transformed): 1.88748 , p-value: 2.778e-01
##
## coefficients:
## (Intercept) x
## -433.117072 0.250835
#rho optimum
rho <- modelco$rho
rho## [1] 0.8957505
y[-1]## [1] 67.81 68.36 68.91 69.36 69.98 70.73 71.24 71.73 72.39 72.38 72.65
y[-12]## [1] 67.25 67.81 68.36 68.91 69.36 69.98 70.73 71.24 71.73 72.39 72.38
#transformasi terhadap y dan x
(y.trans <- y[-1]-y[-12]*rho)## [1] 7.570779 7.619159 7.676496 7.633833 7.850745 8.045380 7.883567 7.916734
## [9] 8.137817 7.536621 7.815579
(x.trans <- x[-1]-x[-12]*rho)## [1] 210.5415 210.6457 210.7500 210.8542 210.9585 211.0627 211.1670 211.2712
## [9] 211.3755 211.4797 211.5840
Model Hasil Transformasi
modelcorho <- lm(y.trans~x.trans)
summary(modelcorho) ##
## Call:
## lm(formula = y.trans ~ x.trans)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.35768 -0.09587 -0.03476 0.08096 0.26967
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -45.1522 36.3343 -1.243 0.245
## x.trans 0.2508 0.1721 1.457 0.179
##
## Residual standard error: 0.1882 on 9 degrees of freedom
## Multiple R-squared: 0.1909, Adjusted R-squared: 0.101
## F-statistic: 2.123 on 1 and 9 DF, p-value: 0.1791
Pengecekan Autokorelasi
lmtest::dwtest(modelcorho, alternative = 'two.sided') ##
## Durbin-Watson test
##
## data: modelcorho
## DW = 1.8875, p-value = 0.5555
## alternative hypothesis: true autocorrelation is not 0
Transformasi Balik
cat("y = ", coef(modelcorho)[1]/(1-rho), "+", coef(modelcorho)[2]," x", sep = "") #persamaan regresi setelah di transformasi ke persamaan awal## y = -433.1171+0.2508351 x
2. Hildreth-lu
#Hildreth-Lu
hildreth.lu.func<- function(r, model){
x <- model.matrix(model)[,-1]
y <- model.response(model.frame(model))
n <- length(y)
t <- 2:n
y <- y[t]-r*y[t-1]
x <- x[t]-r*x[t-1]
return(lm(y~x))
}
#mencari rho yang meminimumkan SSE (iteratif)
r <- c(seq(0.1,0.999, by= 0.001))
tab <- data.frame("rho" = r, "SSE" = sapply(r, function(i){deviance(hildreth.lu.func(i, model))}))
tab$rho[which.min(tab$SSE)]#rho optimal## [1] 0.896
#grafik rho dan SSE
plot(tab$SSE ~ tab$rho , type = "l")
abline(v = tab[tab$SSE==min(tab$SSE),"rho"], lty = 3)Model Hasil Transformasi
# Model terbaik
modelhl <- hildreth.lu.func(0.896, model)
summary(modelhl)##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.35773 -0.09587 -0.03475 0.08096 0.26965
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -44.8943 36.3347 -1.236 0.248
## x 0.2501 0.1726 1.449 0.181
##
## Residual standard error: 0.1882 on 9 degrees of freedom
## Multiple R-squared: 0.1893, Adjusted R-squared: 0.09918
## F-statistic: 2.101 on 1 and 9 DF, p-value: 0.1811
Pengecekan Asumsi Autokolerasi
lmtest::dwtest(modelcorho,alternative = "two.sided")##
## Durbin-Watson test
##
## data: modelcorho
## DW = 1.8875, p-value = 0.5555
## alternative hypothesis: true autocorrelation is not 0
lmtest::dwtest(modelhl,alternative = "two.sided")##
## Durbin-Watson test
##
## data: modelhl
## DW = 1.8879, p-value = 0.556
## alternative hypothesis: true autocorrelation is not 0
Setelah dilakukan transformasi menggunakan Cochrane-Orcutt, perlu dilakukan pengecekan kembali terhadap asumsi autokorelasi. Berdasarkan hasil Durbin Watson Test diperoleh p-value sebesar 0.555 > 0.05 sehingga gagal tolak H0. Dapat disimpulkan bahwa hasil transformasi sudah memenuhi asumsi bebas autokorelasi atau tidak terdapat autokorelasi pada model regresi
Transformasi Balik
cat("y = ", coef(modelhl)[1]/(1-0.896), "+", coef(modelhl)[2]," Tahun", sep = "") #persamaan regresi setelah di transformasi ke persamaan awal## y = -431.6757+0.2501259 Tahun