library(ggplot2)
library(forecast)
library(lmtest)
library(readxl)
library(tseries)
library(stats)
library(readxl)
Veri <- read_excel("ZamanSerisi_Veri.xlsx")
data=ts(Veri, start = 2014, frequency=12)
autoplot(data)
acf(data,lag.max = 30)
pacf(data,lag.max = 30)
Durağanlaştırma, bir fark aldık.
data_diff <- diff(data, differences=1)
acf(data_diff,lag.max = 30)
pacf(data_diff,lag.max = 30)
adf.test(data_diff)
##
## Augmented Dickey-Fuller Test
##
## data: data_diff
## Dickey-Fuller = -5.9724, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
adf.test(data_diff)
##
## Augmented Dickey-Fuller Test
##
## data: data_diff
## Dickey-Fuller = -5.9724, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
arima_model = arima(data_diff, order = c(1,1,2))
coeftest(arima_model)
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.308830 0.113130 2.7299 0.006336 **
## ma1 -1.902623 0.081508 -23.3428 < 2.2e-16 ***
## ma2 0.902792 0.077818 11.6013 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
arima_model2 = arima(data_diff, order = c(2,1,2))
coeftest(arima_model2)
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 -1.399928 0.090759 -15.4247 < 2.2e-16 ***
## ar2 -0.431555 0.088229 -4.8913 1.002e-06 ***
## ma1 -0.012430 0.051593 -0.2409 0.8096
## ma2 -0.987570 0.051458 -19.1919 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tsdiag(arima_model)
Box.test(arima_model$residuals, lag=12)
##
## Box-Pierce test
##
## data: arima_model$residuals
## X-squared = 22.575, df = 12, p-value = 0.03156
Box.test(arima_model$residuals, lag=24)
##
## Box-Pierce test
##
## data: arima_model$residuals
## X-squared = 39.896, df = 24, p-value = 0.02195
Box.test(arima_model$residuals, lag=36)
##
## Box-Pierce test
##
## data: arima_model$residuals
## X-squared = 54.042, df = 36, p-value = 0.02715
Box.test(arima_model2$residuals, lag=48)
##
## Box-Pierce test
##
## data: arima_model2$residuals
## X-squared = 59.4, df = 48, p-value = 0.1252
shapiro.test(arima_model$residuals)
##
## Shapiro-Wilk normality test
##
## data: arima_model$residuals
## W = 0.91891, p-value = 5.447e-06
qqnorm(arima_model$residuals)
checkresiduals(arima_model)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,1,2)
## Q* = 40.293, df = 19, p-value = 0.002995
##
## Model df: 3. Total lags used: 22
tsdiag(arima_model2)
Box.test(arima_model2$residuals, lag=6)
##
## Box-Pierce test
##
## data: arima_model2$residuals
## X-squared = 7.1974, df = 6, p-value = 0.303
Box.test(arima_model2$residuals, lag=12)
##
## Box-Pierce test
##
## data: arima_model2$residuals
## X-squared = 28.875, df = 12, p-value = 0.004112
Box.test(arima_model2$residuals, lag=24)
##
## Box-Pierce test
##
## data: arima_model2$residuals
## X-squared = 42.226, df = 24, p-value = 0.01217
Box.test(arima_model2$residuals, lag=36)
##
## Box-Pierce test
##
## data: arima_model2$residuals
## X-squared = 53.318, df = 36, p-value = 0.03153
Box.test(arima_model2$residuals, lag=48)
##
## Box-Pierce test
##
## data: arima_model2$residuals
## X-squared = 59.4, df = 48, p-value = 0.1252
shapiro.test(arima_model2$residuals)
##
## Shapiro-Wilk normality test
##
## data: arima_model2$residuals
## W = 0.91231, p-value = 2.405e-06
qqnorm(arima_model2$residuals)
checkresiduals(arima_model2)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(2,1,2)
## Q* = 41.766, df = 18, p-value = 0.001192
##
## Model df: 4. Total lags used: 22
forecast(arima_model, h = 12)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Mar 2023 -0.11316955 -1.998229 1.771890 -2.996120 2.769780
## Apr 2023 -0.05658782 -2.240033 2.126857 -3.395879 3.282703
## May 2023 -0.03911368 -2.248604 2.170376 -3.418237 3.340010
## Jun 2023 -0.03371714 -2.245580 2.178145 -3.416469 3.349035
## Jul 2023 -0.03205053 -2.244110 2.180009 -3.415103 3.351002
## Aug 2023 -0.03153583 -2.243605 2.180533 -3.414603 3.351532
## Sep 2023 -0.03137688 -2.243444 2.180690 -3.414441 3.351688
## Oct 2023 -0.03132779 -2.243394 2.180738 -3.414391 3.351735
## Nov 2023 -0.03131263 -2.243378 2.180753 -3.414375 3.351750
## Dec 2023 -0.03130795 -2.243374 2.180758 -3.414370 3.351755
## Jan 2024 -0.03130650 -2.243372 2.180759 -3.414369 3.351756
## Feb 2024 -0.03130605 -2.243372 2.180760 -3.414369 3.351757
plot(forecast(arima_model, h = 12))
accuracy(forecast(arima_model, h = 12))
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.03532528 1.457396 0.9825079 NaN Inf 0.7935326 -0.05305084
auto.arima(data_diff)
## Series: data_diff
## ARIMA(2,0,1)(1,0,0)[12] with zero mean
##
## Coefficients:
## ar1 ar2 ma1 sar1
## 0.2699 0.1675 -0.9289 0.3945
## s.e. 0.1064 0.1033 0.0454 0.0914
##
## sigma^2 = 1.862: log likelihood = -187.93
## AIC=385.87 AICc=386.45 BIC=399.32
coeftest(auto.arima(data_diff))
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.269914 0.106442 2.5358 0.01122 *
## ar2 0.167480 0.103274 1.6217 0.10487
## ma1 -0.928901 0.045437 -20.4437 < 2.2e-16 ***
## sar1 0.394541 0.091398 4.3167 1.584e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
arima_model_auto <- auto.arima(data_diff)
tsdiag(arima_model_auto)
Box.test(arima_model_auto$residuals, lag=12)
##
## Box-Pierce test
##
## data: arima_model_auto$residuals
## X-squared = 6.0061, df = 12, p-value = 0.9158
Box.test(arima_model_auto$residuals, lag=24)
##
## Box-Pierce test
##
## data: arima_model_auto$residuals
## X-squared = 14.567, df = 24, p-value = 0.9328
shapiro.test(arima_model_auto$residuals)
##
## Shapiro-Wilk normality test
##
## data: arima_model_auto$residuals
## W = 0.86976, p-value = 2.452e-08
qqnorm(arima_model_auto$residuals)
checkresiduals(arima_model_auto)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(2,0,1)(1,0,0)[12] with zero mean
## Q* = 15.166, df = 18, p-value = 0.6505
##
## Model df: 4. Total lags used: 22
forecast(arima_model_auto, h = 12)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Mar 2023 0.18280348 -1.5661675 1.9317745 -2.492017 2.857624
## Apr 2023 -0.04599841 -2.1405786 2.0485817 -3.249382 3.157386
## May 2023 -1.02980239 -3.1244614 1.0648566 -4.233307 2.173702
## Jun 2023 1.58663685 -0.5173531 3.6906269 -1.631138 4.804412
## Jul 2023 -1.21485017 -3.3195978 0.8898975 -4.433784 2.004084
## Aug 2023 1.50289388 -0.6024100 3.6081977 -1.716891 4.722678
## Sep 2023 -0.51054279 -2.6159671 1.5948815 -3.730511 2.709426
## Oct 2023 -0.11711564 -2.2225877 1.9883564 -3.337157 3.102926
## Nov 2023 -0.07817641 -2.1836622 2.0273094 -3.298239 3.141886
## Dec 2023 -0.11795591 -2.2234463 1.9875345 -3.338026 3.102114
## Jan 2024 0.03968627 -2.0658056 2.1451781 -3.180386 3.259758
## Feb 2024 -0.07877741 -2.1842697 2.0267149 -3.298850 3.141295
accuracy(arima_model_auto, h = 12)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.130317 1.339454 0.8571919 NaN Inf 0.6923199 -0.01192143
plot(forecast(arima_model_auto, h = 12))