Skripsi-R_LaTeX
Kurnia Wanto
July 12, 2016
Contents
1 Libraries and Import Data 1
2 Pemodelan ARIMA 4
2.1 PlotNt . . . 4
2.2 Box–Cox Transformation . . . 5
2.3 PlotWt . . . 6
2.4 Identifikasi Model ARIMA . . . 7
2.5 Estimasi Parameter ARIMA . . . 8
2.6 Diagnosis Model ARIMA . . . 10
3 Analisis Intervensi 11 3.1 Identifikasi Orde Intervensi . . . 11
3.2 Estimasi Parameter Intervensi . . . 13
3.3 Diagnosis Model Intervensi . . . 14
3.4 Peramalan Model Intervensi . . . 15
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
1
Libraries and Import Data
# Libraries
---library(forecast) # --- BoxCox Arima auto.arima function is in forecast package
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base': ##
## as.Date, as.Date.numeric
## This is forecast 7.2
library(MASS) # --- boxcox function is in MASS package
library(FitAR) # --- LjungBoxTest function is in FitAR package
## Loading required package: lattice
## Loading required package: leaps
## Loading required package: ltsa
## Loading required package: bestglm
##
## Attaching package: 'FitAR'
## The following object is masked from 'package:forecast': ##
## BoxCox
library(tsoutliers) # --- tso function is in tsoutliers package library(lmtest) # --- coeftest function is in lmtest package library(stargazer) # --- stargazer function is in stargazer package
##
## Please cite as:
## Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2. http://CRAN.R-project.org/package=stargazer
library(TSA) # --- arimax function is in TSA package
## Loading required package: locfit
## locfit 1.5-9.1 2013-03-22
## Loading required package: mgcv
## Loading required package: nlme
##
## Attaching package: 'nlme'
## The following object is masked from 'package:forecast': ##
## getResponse
## Loading required package: tseries
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:forecast': ##
## fitted.Arima, plot.Arima
## The following objects are masked from 'package:timeDate': ##
## kurtosis, skewness
## The following objects are masked from 'package:stats': ##
## acf, arima
## The following object is masked from 'package:utils': ##
## tar
# 0_Import_Data.R
---options(width=80)
raw_data <- read.csv("/media/kurnia/Brain/Skripsi/DATA/World\ Bank/inflation.csv")
PDB_ID <- ts(raw_data$GDP, start=1970, end=2013)
#PDB_ID <- PDB_ID/1000000000 PDB_ID
## Time Series: ## Start = 1970 ## End = 2013 ## Frequency = 1
## [1] 9656740014 9849117953 11605084560 17171181163 27227710999
## [6] 32147953008 39328674730 48396143465 54298158340 55122620334
## [11] 78013206038 92473878832 94715163814 85369201879 87612439197
## [16] 87338874330 80060657612 75929617715 88787623310 101000000000
## [21] 114000000000 128000000000 139000000000 158000000000 177000000000
## [26] 202000000000 227000000000 216000000000 95445548017 140000000000
## [31] 165000000000 160000000000 196000000000 235000000000 257000000000 ## [36] 286000000000 365000000000 432000000000 510000000000 540000000000 ## [41] 709000000000 846000000000 877000000000 868000000000
# stat.desc(rwpdb)
plot(PDB_ID, xlab="Waktu", ylab="US Dollar", col="blue", type="l",
main="Plot PDB Indonesia 1970-2013") points(PDB_ID, cex = .5, col = "red") abline(v=1998, col=1, lty=2)
Plot PDB Indonesia 1970−2013
Waktu
US Dollar
1970
1980
1990
2000
2010
0e+00
4e+11
8e+11
1998
(t=29)
2
Pemodelan ARIMA
2.1
Plot
N
t# 1-Plot_Nt.R
---# Creating Nt
Nt <- ts(PDB_ID[1:28], start=1970, end=1997)
# Creating Layout 1,1;2,3
m <- rbind(c(1, 1), c(2, 3))
layout(m)
par(mar = c(3, 3, 1, 1))
plot(Nt, xlab="Waktu", ylab="US Dollar", col="blue", type="p",
main="Plot PDB Indonesia 1970-1997 (Nt)")
points(Nt, cex = .5, col = "red")
# Trend Linear
t <- 1970:1997
trend_PDB <- glm(Nt~t)
abline(trend_PDB, col="dark blue", lwd=2)
# ACF
acf(Nt, 20, xlim=c(1,20)) text(10,0.8, "ACF")
# PACF
Plot PDB Indonesia 1970−1997 (Nt)
Waktu
1970 1975 1980 1985 1990 1995
5.0e+10
# Show the ACF & PACF value acf(Nt, 27, plot=FALSE)
##
## Autocorrelations of series 'Nt', by lag ##
## 1 2 3 4 5 6 7 8 9 10 11
## 0.872 0.704 0.552 0.419 0.303 0.206 0.123 0.058 0.011 -0.021 -0.020
## 12 13 14 15 16 17 18 19 20 21 22
## -0.019 -0.031 -0.058 -0.095 -0.157 -0.233 -0.300 -0.336 -0.369 -0.390 -0.392
## 23 24 25 26 27
## -0.381 -0.350 -0.291 -0.206 -0.099
pacf(Nt, 27, plot=FALSE)
##
## Partial autocorrelations of series 'Nt', by lag ##
## 1 2 3 4 5 6 7 8 9 10 11
## 0.872 -0.231 -0.012 -0.034 -0.035 -0.022 -0.036 -0.001 -0.011 0.006 0.082
## 12 13 14 15 16 17 18 19 20 21 22
## -0.051 -0.055 -0.068 -0.056 -0.151 -0.104 -0.043 0.019 -0.105 -0.019 -0.023
## 23 24 25 26 27
## -0.040 0.010 0.062 0.080 0.113
2.2
Box–Cox Transformation
# - 2-BoxCox_Trans.R
t1 <- 1:length(PDB_ID)
# --- Search for optimal lambda par(mar = c(2, 4, 0, 1))
MASS::boxcox(lm(PDB_ID~t1), lambda= seq(-1,1,1/10),
ylab = "log-Likelihood")
−1.0
−0.5
0.0
0.5
1.0
−100
−80
−60
−40
log−Lik
elihood
95%
lambda.model <- forecast::BoxCox.lambda(PDB_ID)
# --- Box-Cox Transformation
Nt_box <- forecast::BoxCox(Nt, lambda = lambda.model)
# --- It is ambigous, either use lambda for Nt (0.5) or PDB_ID (0.2)
2.3
Plot
W
t# --- 3-Plot_Wt.R
---# --- Generating Wt
Wt <- as.numeric(diff(Nt_box))
# --- Creating Layout 1,1;2,3
m <- rbind(c(1, 1), c(2, 3))
layout(m)
par(mar = c(3, 3, 1, 1))
plot(Wt, xlab="Waktu", ylab="US Dollar",
col="blue", type="o",
)
points(Wt, cex = .5, col = "red")
# --- Trend Linear
t <- 1:27
trend_PDB <- glm(Wt~t)
abline(h=mean(Wt))
# --- ACF
0.4
Series Wt
ACF
2.4
Identifikasi Model ARIMA
# --- 4-Id_Model.R
---# Save ARIMA model
Nt_arima <- auto.arima(Nt, max.d=2, seasonal=FALSE, lambda=lambda.model,
stepwise=TRUE, trace=TRUE, max.p=5, max.q=5)
##
## ARIMA(0,1,0) with drift : 236.1509
## ARIMA(1,1,0) with drift : 234.0414
## ARIMA(0,1,1) with drift : 233.3114
## ARIMA(0,1,0) : 250.4358
## ARIMA(1,1,1) with drift : 235.8706
## ARIMA(0,1,2) with drift : 235.7134
## ARIMA(1,1,2) with drift : Inf
## ARIMA(0,1,1) : 240.4512
##
## Best model: ARIMA(0,1,1) with drift
2.5
Estimasi Parameter ARIMA
# --- 5-Est_Par.R
---# --- Creating All Possible Models
model_010 <- Arima(Nt, order=c(0,1,0), lambda = lambda.model, include.drift=FALSE)
model_110 <- Arima(Nt, order=c(1,1,0), lambda = lambda.model, include.drift=FALSE)
model_011 <- Arima(Nt, order=c(0,1,1), lambda = lambda.model, include.drift=FALSE)
model_111 <- Arima(Nt, order=c(1,1,1), lambda = lambda.model, include.drift=FALSE)
model_010d <- Arima(Nt, order=c(0,1,0), lambda = lambda.model, include.drift=TRUE)
model_110d <- Arima(Nt, order=c(1,1,0), lambda = lambda.model, include.drift=TRUE)
model_011d <- Arima(Nt, order=c(0,1,1), lambda = lambda.model, include.drift=TRUE)
model_111d <- Arima(Nt, order=c(1,1,1), lambda = lambda.model, include.drift=TRUE)
# --- Function for rounded value # AIC
raic <- function (model) { round(model$aic,2) }
# AICc
raicc <- function (model) { round(model$aicc,2) }
# BIC
rbic <- function (model) { round(model$bic,2) }
# --- Calculate parameters P-Value PValue <- function (model) {
(1-pnorm(abs(model$coef)/sqrt(diag(model$var.coef))))*2
}
# --- Estimating parameters for all possible models: #
# Generating Function
print(model_010$coef) cat("\nP-Values:\n") print(PValue(model_010))
# --- ARIMA(0,1,0) with drift EstimasiParamater(model_010d)
# --- ARIMA(1,1,0) with drift EstimasiParamater(model_110d)
## Parameter:
## ar1 drift
## 0.4123134 15.2953779
##
## P-Values:
## ar1 drift
## 0.022776839 0.003271827
# --- ARIMA(0,1,1)
## Parameter:
# --- ARIMA(0,1,1) with drift EstimasiParamater(model_011d)
## Parameter:
## ma1 drift
## 0.4436015 15.6496441
##
## P-Values:
## ma1 drift
## 0.002482773 0.000348842
# --- ARIMA(1,1,1)
EstimasiParamater(model_111)
## Parameter:
## ar1 ma1
## 0.6093421 0.1197083 ##
## P-Values:
## ar1 ma1
## 0.01929868 0.76162785
# --- ARIMA(1,1,1) with drift EstimasiParamater(model_111d)
## Parameter:
## ar1 ma1 drift
## 0.1523234 0.3491976 15.4399096
##
## P-Values:
## ar1 ma1 drift
## 0.634934540 0.201132736 0.001321079
2.6
Diagnosis Model ARIMA
# --- 6-Diag_Mod.R
---# --- Ljung-Box Test for Nt
# --- OR can use stats::Box.test, lag = min(10,n/5) <-- Rob J. Hyndman Box.test(Nt_arima$residuals, lag = round(length(Nt)/5,0),
##
## Box-Ljung test
##
## data: Nt_arima$residuals
## X-squared = 4.6519, df = 5, p-value = 0.4598
# --- Kolmogorov-Smirnov Test
ks.test(Nt_arima$residuals, "pnorm", mean(Nt_arima$residuals), sd(Nt_arima$residuals))
##
## One-sample Kolmogorov-Smirnov test
##
## data: Nt_arima$residuals
## D = 0.12779, p-value = 0.7032 ## alternative hypothesis: two-sided
3
Analisis Intervensi
3.1
Identifikasi Orde Intervensi
# --- 7-Id_Intv.R
---# --- Box-Cox Transformation on PDB_ID, not sure using lambda for Nt or PDB (?) PDB_box <- forecast::BoxCox(PDB_ID, lambda.model)
Nt_forecast <- forecast(Nt_arima, h = 16)
# --- h-step forecast for Nt Nt_forecast
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 1998 220497998535 197099056251 2.460650e+11 185541793986 2.605269e+11
## 1999 239159146510 196702923844 2.886377e+11 176804075377 3.179553e+11
## 2000 259061074266 201691531495 3.287799e+11 175728796103 3.713233e+11
## 2001 280264506146 209057531124 3.696427e+11 177709273908 4.254125e+11
## 2002 302832106925 218017345778 4.121581e+11 181528673216 4.816195e+11
## 2003 326828511902 228252002513 4.567977e+11 186676314234 5.406397e+11
## 2004 352320356974 239609173819 5.038712e+11 192893799595 6.029214e+11
## 2005 379376308699 252012697584 5.536162e+11 200037356226 6.688034e+11
## 2006 408067094348 265426772546 6.062350e+11 208023418758 7.385694e+11
## 2007 438465531944 279839323613 6.619105e+11 216803369306 8.124736e+11
## 2008 470646560285 295253378801 7.208153e+11 226350450744 8.907534e+11
## 2009 504687268962 311682226900 7.831165e+11 236652419316 9.736372e+11
## 2010 540666928360 329146527131 8.489786e+11 247707157733 1.061349e+12
## 2011 578667019651 347672499230 9.185654e+11 259519925416 1.154110e+12
## 2012 618771264767 367290745815 9.920414e+11 272101565937 1.252142e+12
# --- Identification intervention order with plot of model residuals
error_idintv <- rep(0,44)
error_idintv[1:28] <- model_011d$residuals
error_idintv[29:44] <- PDB_ID[29:44] - Nt_forecast$mean
plot(error_idintv, type="h", xlab="Waktu (T)", ylab = "Residual", xaxt = "n")
abline(h=c(-3*sd(model_011d$residuals), 3*sd(model_011d$residuals)), col="blue", lty=2) abline(v = 29, col = "red", lty = 3, lwd = 1.5)
text(29, 200, "T=29",cex = .8, pos = 2)
axis(1, at = c(0,10,20,30,40), labels = c("T-29" ,"T-19", "T-9", "T+1", "T+11"))
−1e+11
1e+11
Waktu (T)
Residual
T=29T−29
T−19
T−9
T+1
T+11
error_idintv
## [1] 4.858446e-01 -1.246534e+01 6.359059e+00 2.457999e+01 2.869940e+01
## [6] -7.179901e+00 1.422428e+01 6.659505e+00 -2.208022e+00 -1.249470e+01
## [11] 4.190256e+01 -7.418031e+00 -8.507764e+00 -2.844920e+01 1.075969e+00
## [16] -1.662309e+01 -2.195533e+01 -1.412280e+01 1.512103e+01 -1.582605e+00
## [21] 5.067219e+00 1.711697e+00 -2.171179e+00 7.913453e+00 1.363048e+00
## [26] 8.221202e+00 2.866832e+00 -2.641994e+01 -1.250525e+11 -9.915915e+10
## [31] -9.406107e+10 -1.202645e+11 -1.068321e+11 -9.182851e+10 -9.532036e+10
## [36] -9.337631e+10 -4.306709e+10 -6.465532e+09 3.935344e+10 3.531273e+10
## [41] 1.683331e+11 2.673330e+11 2.582287e+11 2.069343e+11
# --- Plot PDB_ID vs forecasting of Nt
plot(forecast(model_011d, h=16), main =NA, ylab="US Dollar" ) points(PDB_ID, cex=.5, col="dark red", pch=19)
lines(PDB_ID, col="red") abline(v=1998, lty=2)
text(1998, PDB_ID[41], "1998\n(t=29)", pos=2)
legend("topleft", legend = c("PDB_ID", "Peramalan Nt"), cex=0.75, lty=1,
US Dollar
1970
1980
1990
2000
2010
0.0e+00
8.0e+11
1998
(t=29)
PDB_IDPeramalan Nt
# --- Detecting Outliers with tsoutliers::tso
PDB_outlier <- tsoutliers::tso(PDB_box, types = c("AO","LS","TC"),
maxit.iloop=10, tsmethod = "auto.arima")
plot(PDB_outlier)
Original and adjusted series
600
1000
Outlier effects
−150
−50
0
1970
1980
1990
2000
2010
3.2
Estimasi Parameter Intervensi
# --- 8-Est_Intv.R
---# --- Pulse: Abrupt Temporary
cobalah2 <- TSA::arimax(PDB_box, order = c(0,1,1), xtransf = data.frame(
T29 = 1*(seq(PDB_ID)==29)), transfer = list(c(1,0)))
cobalah2
## ## Call:
## TSA::arimax(x = PDB_box, order = c(0, 1, 1), xtransf = data.frame(T29 = 1 *
##
## Coefficients:
## ma1 T29-AR1 T29-MA0
## 0.4394 0.2055 -112.4912
## s.e. 0.1182 0.0804 14.5695
##
## sigma^2 estimated as 496.7: log likelihood = -194.6, aic = 395.19
# --- Test significance of model coeficients coeftest(cobalah2)
##
## z test of coefficients: ##
## Estimate Std. Error z value Pr(>|z|)
## ma1 0.439418 0.118198 3.7176 0.0002011 ***
## T29-AR1 0.205494 0.080446 2.5544 0.0106362 *
## T29-MA0 -112.491185 14.569525 -7.7210 1.154e-14 ***
##
---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# stargazer(coeftest(cobalah2))
3.3
Diagnosis Model Intervensi
# 9-Diag_Intv.R
---# --- Generating f(It), based on cobalah2
pulse29 <- filter(1*(seq(PDB_ID)==29), filter = 0.2055,
method = "rec", sides = 1) * -112.4912
# --- Compute Model for PDB_ID
PDB_arima <- Arima(PDB_ID, lambda = lambda.model, order = c(0,1,1),
include.constant = TRUE, xreg = pulse29)
# --- Ljung-Box Test for Independence of residuals # , lag = min(10,n/5) <-- Rob J. Hyndman
Box.test(PDB_arima$residuals,
lag=round(length(PDB_ID)/5,0), type = "Ljung-Box", fitdf = 1)
##
## Box-Ljung test
##
## data: PDB_arima$residuals
## X-squared = 14.784, df = 8, p-value = 0.06348
# --- Kolmogorov-Smirnov Test for Normality of residuals ks.test(PDB_arima$residuals, "pnorm",
##
## One-sample Kolmogorov-Smirnov test
##
## data: PDB_arima$residuals
## D = 0.14849, p-value = 0.2597 ## alternative hypothesis: two-sided
3.4
Peramalan Model Intervensi
# --- Generating f(It)
pulse29 <- filter(1*(seq(PDB_ID)==29),
filter = 0.2055, method = "rec",
sides = 1)*-112.4912
# --- Compute Model for PDB_ID
PDB_arima <- Arima(PDB_ID, lambda = lambda.model,
order = c(0,1,1),
include.constant = TRUE, xreg = pulse29)
# --- Future Value of xreg
xreg.rob = forecast(auto.arima(pulse29), h=5)$mean
# --- Forecasting
forecast(PDB_arima, xreg = xreg.rob)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2014 9.045913e+11 818883650574 9.973193e+11 776215514467 1.049392e+12
## 2015 9.669871e+11 813820112964 1.142359e+12 740978902953 1.244971e+12
## 2016 1.032775e+12 827954842699 1.276198e+12 733510015370 1.422443e+12
## 2017 1.102090e+12 850383812003 1.410060e+12 737126698072 1.598875e+12
## 2018 1.175072e+12 878177165612 1.547203e+12 747311925422 1.779178e+12
plot(forecast(PDB_arima, xreg = xreg.rob), main=NA)