Multivariate TS Analysis
rm(list = ls())
require(tidyverse)
## Zorunlu paket yükleniyor: tidyverse
## Warning: package 'tidyverse' was built under R version 4.2.2
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## Warning: package 'ggplot2' was built under R version 4.2.2
## Warning: package 'tidyr' was built under R version 4.2.2
## Warning: package 'readr' was built under R version 4.2.2
## Warning: package 'purrr' was built under R version 4.2.2
## Warning: package 'dplyr' was built under R version 4.2.2
## Warning: package 'forcats' was built under R version 4.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
require(tidymodels)
## Zorunlu paket yükleniyor: tidymodels
## Warning: package 'tidymodels' was built under R version 4.2.2
## ── Attaching packages ────────────────────────────────────── tidymodels 1.0.0 ──
## ✔ broom 1.0.1 ✔ rsample 1.1.0
## ✔ dials 1.1.0 ✔ tune 1.0.1
## ✔ infer 1.0.4 ✔ workflows 1.1.2
## ✔ modeldata 1.0.1 ✔ workflowsets 1.0.0
## ✔ parsnip 1.0.3 ✔ yardstick 1.1.0
## ✔ recipes 1.0.3
## Warning: package 'broom' was built under R version 4.2.2
## Warning: package 'dials' was built under R version 4.2.2
## Warning: package 'scales' was built under R version 4.2.2
## Warning: package 'infer' was built under R version 4.2.2
## Warning: package 'modeldata' was built under R version 4.2.2
## Warning: package 'parsnip' was built under R version 4.2.2
## Warning: package 'recipes' was built under R version 4.2.2
## Warning: package 'rsample' was built under R version 4.2.2
## Warning: package 'tune' was built under R version 4.2.2
## Warning: package 'workflows' was built under R version 4.2.2
## Warning: package 'workflowsets' was built under R version 4.2.2
## Warning: package 'yardstick' was built under R version 4.2.2
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ scales::discard() masks purrr::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ recipes::fixed() masks stringr::fixed()
## ✖ dplyr::lag() masks stats::lag()
## ✖ yardstick::spec() masks readr::spec()
## ✖ recipes::step() masks stats::step()
## • Learn how to get started at https://www.tidymodels.org/start/
require(data.table)
## Zorunlu paket yükleniyor: data.table
## Warning: package 'data.table' was built under R version 4.2.2
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## The following object is masked from 'package:purrr':
##
## transpose
require(tidyposterior)
## Zorunlu paket yükleniyor: tidyposterior
## Warning: package 'tidyposterior' was built under R version 4.2.2
require(tsibble)
## Zorunlu paket yükleniyor: tsibble
## Warning: package 'tsibble' was built under R version 4.2.2
##
## Attaching package: 'tsibble'
## The following object is masked from 'package:data.table':
##
## key
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
require(fable)
## Zorunlu paket yükleniyor: fable
## Warning: package 'fable' was built under R version 4.2.2
## Zorunlu paket yükleniyor: fabletools
## Warning: package 'fabletools' was built under R version 4.2.2
##
## Attaching package: 'fabletools'
## The following object is masked from 'package:yardstick':
##
## accuracy
## The following object is masked from 'package:parsnip':
##
## null_model
## The following objects are masked from 'package:infer':
##
## generate, hypothesize
require(ggfortify)
## Zorunlu paket yükleniyor: ggfortify
## Warning: package 'ggfortify' was built under R version 4.2.2
## Registered S3 method overwritten by 'ggfortify':
## method from
## autoplot.glmnet parsnip
require(forecast)
## Zorunlu paket yükleniyor: forecast
## Warning: package 'forecast' was built under R version 4.2.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## autoplot.Arima ggfortify
## autoplot.acf ggfortify
## autoplot.ar ggfortify
## autoplot.bats ggfortify
## autoplot.decomposed.ts ggfortify
## autoplot.ets ggfortify
## autoplot.forecast ggfortify
## autoplot.stl ggfortify
## autoplot.ts ggfortify
## fitted.ar ggfortify
## fortify.ts ggfortify
## residuals.ar ggfortify
##
## Attaching package: 'forecast'
## The following objects are masked from 'package:fabletools':
##
## accuracy, forecast
## The following object is masked from 'package:yardstick':
##
## accuracy
require(tseries)
## Zorunlu paket yükleniyor: tseries
## Warning: package 'tseries' was built under R version 4.2.2
require(chron)
## Zorunlu paket yükleniyor: chron
## Warning: package 'chron' was built under R version 4.2.2
##
## Attaching package: 'chron'
## The following object is masked from 'package:tseries':
##
## is.weekend
require(lubridate)
## Zorunlu paket yükleniyor: lubridate
## Warning: package 'lubridate' was built under R version 4.2.2
## Zorunlu paket yükleniyor: timechange
## Warning: package 'timechange' was built under R version 4.2.2
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:chron':
##
## days, hours, minutes, seconds, years
## The following object is masked from 'package:tsibble':
##
## interval
## The following objects are masked from 'package:data.table':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
require(directlabels)
## Zorunlu paket yükleniyor: directlabels
## Warning: package 'directlabels' was built under R version 4.2.2
require(zoo)
## Zorunlu paket yükleniyor: zoo
## Warning: package 'zoo' was built under R version 4.2.2
##
## Attaching package: 'zoo'
## The following object is masked from 'package:tsibble':
##
## index
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
require(lmtest)
## Zorunlu paket yükleniyor: lmtest
## Warning: package 'lmtest' was built under R version 4.2.2
require(TTR)
## Zorunlu paket yükleniyor: TTR
## Warning: package 'TTR' was built under R version 4.2.2
##
## Attaching package: 'TTR'
## The following object is masked from 'package:dials':
##
## momentum
require(MTS)
## Zorunlu paket yükleniyor: MTS
## Warning: package 'MTS' was built under R version 4.2.2
##
## Attaching package: 'MTS'
## The following object is masked from 'package:TTR':
##
## VMA
## The following object is masked from 'package:fable':
##
## VAR
require(vars)
## Zorunlu paket yükleniyor: vars
## Warning: package 'vars' was built under R version 4.2.2
## Zorunlu paket yükleniyor: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Zorunlu paket yükleniyor: strucchange
## Warning: package 'strucchange' was built under R version 4.2.2
## Zorunlu paket yükleniyor: sandwich
## Warning: package 'sandwich' was built under R version 4.2.2
##
## Attaching package: 'strucchange'
## The following object is masked from 'package:stringr':
##
## boundary
## Zorunlu paket yükleniyor: urca
## Warning: package 'urca' was built under R version 4.2.2
##
## Attaching package: 'vars'
## The following object is masked from 'package:MTS':
##
## VAR
## The following object is masked from 'package:fable':
##
## VAR
require(fUnitRoots)
## Zorunlu paket yükleniyor: fUnitRoots
## Warning: package 'fUnitRoots' was built under R version 4.2.2
##
## Attaching package: 'fUnitRoots'
## The following objects are masked from 'package:urca':
##
## punitroot, qunitroot, unitrootTable
require(lattice)
## Zorunlu paket yükleniyor: lattice
require(grid)
## Zorunlu paket yükleniyor: grid
set.seed(100)
x = rnorm(50, 1)
y = rnorm(100, 51)
veri = data.frame(x, y)
class(veri)
## [1] "data.frame"
İnflation_and_GDP = ts(veri,
frequency = 12,
start = c(1972, 4))
plot(İnflation_and_GDP)
theme_set(theme_bw())
autoplot(İnflation_and_GDP) +
ggtitle("Time Series Plot of the `inflation_and_GDP' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))
Veri setinin ilk 6 satırını göstermek için head fonksiyonunu kullanıyoruz.
head(İnflation_and_GDP)
## x y
## Apr 1972 0.4978076 50.55294
## May 1972 1.1315312 49.26140
## Jun 1972 0.9210829 51.17886
## Jul 1972 1.8867848 52.89747
## Aug 1972 1.1169713 48.72807
## Sep 1972 1.3186301 51.98046
Kullandığım veri setinin yapısını anlamak için class fonksiyonunu kullandım
class(İnflation_and_GDP)
## [1] "mts" "ts" "matrix"
plot(İnflation_and_GDP)
autoplot(İnflation_and_GDP) +
ggtitle("Time Series Plot of the `İnflation_and_GDP' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))
Veri çerçevesini veya matrisi girdi olarak almak ve vektör, liste veya dizi halinde çıktı verilmesi için apply fonksiyonunu kullandım. Apply işlevi, öncelikle döngü yapılarının açık kullanımlarından kaçınmak için kullanılır.
apply(İnflation_and_GDP, 2, adf.test)
## Warning in FUN(newX[, i], ...): p-value smaller than printed p-value
## Warning in FUN(newX[, i], ...): p-value smaller than printed p-value
## $x
##
## Augmented Dickey-Fuller Test
##
## data: newX[, i]
## Dickey-Fuller = -7.1564, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
##
##
## $y
##
## Augmented Dickey-Fuller Test
##
## data: newX[, i]
## Dickey-Fuller = -5.2529, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
apply(İnflation_and_GDP, 2, adfTest,
lags=0,
type="c",
title = "ADF Test for İnflation_and_GDP Data")
## Warning in FUN(newX[, i], ...): p-value smaller than printed p-value
## Warning in FUN(newX[, i], ...): p-value smaller than printed p-value
## $x
##
## Title:
## ADF Test for İnflation_and_GDP Data
##
## Test Results:
## PARAMETER:
## Lag Order: 0
## STATISTIC:
## Dickey-Fuller: -13.2893
## P VALUE:
## 0.01
##
## Description:
## Tue Dec 20 21:19:51 2022 by user: User1
##
##
## $y
##
## Title:
## ADF Test for İnflation_and_GDP Data
##
## Test Results:
## PARAMETER:
## Lag Order: 0
## STATISTIC:
## Dickey-Fuller: -12.3434
## P VALUE:
## 0.01
##
## Description:
## Tue Dec 20 21:19:51 2022 by user: User1
stnry fonksiyonu, kullanılan verinin durağan olup olmadığını test etmek için kullanılır.
diffm fonksiyonu, çok değişkenli zaman serisinin farkına bakıldığı zaman kullanılır.
stnry = diffM(İnflation_and_GDP)
Kullanılan veri setinin zaman serisi grafiği
plot.ts(İnflation_and_GDP)
Kullandığım veri setini görselleştirmek için autoplot fonksiyonunu kullandım
autoplot(ts(İnflation_and_GDP,
start = c(1972,50),
frequency = 100)) +
ggtitle("Time Series Plot of the stationary `İnflation_and_GDP' Time-Series")
VAR MODELİ
library(vars)
varmodeli<-var(2:length(İnflation_and_GDP),)
Uygun bir gecikme seçmeme yardımcı olabilmesi için VARselect fonksiyonunu kullandım
VARselect(İnflation_and_GDP,
type = "none",
lag.max = 10)
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 5 5 3 5
##
## $criteria
## 1 2 3 4 5 6
## AIC(n) 0.3082514 -0.02417370 -0.18939313 -0.27896542 -0.3500788 -0.32390608
## HQ(n) 0.3530545 0.06543254 -0.05498377 -0.09975294 -0.1260632 -0.05508736
## SC(n) 0.4193541 0.19803160 0.14391482 0.16544519 0.2054344 0.34270983
## FPE(n) 1.3610631 0.97623054 0.82778898 0.75727826 0.7059321 0.72563261
## 7 8 9 10
## AIC(n) -0.322082463 -0.28256713 -0.2032457 -0.2043306
## HQ(n) -0.008460623 0.07585783 0.1999823 0.2437006
## SC(n) 0.455636101 0.60625409 0.7966781 0.9066959
## FPE(n) 0.728338354 0.75962511 0.8250465 0.8275802
var.a <- vars::VAR(İnflation_and_GDP,
lag.max = 10,
ic = "AIC",
type = "none")
R’deki bir vektör, veri çerçevesi, regresyon modelindeki değerleri hızlı bir şekilde özetlemek için summary fonksiyonunu kullandım
summary(var.a)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: x, y
## Deterministic variables: none
## Sample size: 95
## Log Likelihood: -231.854
## Roots of the characteristic polynomial:
## 1 0.8707 0.8651 0.8651 0.775 0.775 0.7098 0.7098 0.677 0.677
## Call:
## vars::VAR(y = İnflation_and_GDP, type = "none", lag.max = 10,
## ic = "AIC")
##
##
## Estimation results for equation x:
## ==================================
## x = x.l1 + y.l1 + x.l2 + y.l2 + x.l3 + y.l3 + x.l4 + y.l4 + x.l5 + y.l5
##
## Estimate Std. Error t value Pr(>|t|)
## x.l1 -0.275181 0.109452 -2.514 0.0138 *
## y.l1 -0.056934 0.071610 -0.795 0.4288
## x.l2 0.098886 0.110553 0.894 0.3736
## y.l2 -0.004228 0.066418 -0.064 0.9494
## x.l3 -0.255254 0.114895 -2.222 0.0290 *
## y.l3 0.052462 0.065520 0.801 0.4255
## x.l4 -0.272833 0.115330 -2.366 0.0203 *
## y.l4 0.071553 0.065730 1.089 0.2794
## x.l5 -0.286365 0.117363 -2.440 0.0168 *
## y.l5 -0.020389 0.068712 -0.297 0.7674
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.7261 on 85 degrees of freedom
## Multiple R-Squared: 0.7447, Adjusted R-squared: 0.7146
## F-statistic: 24.79 on 10 and 85 DF, p-value: < 2.2e-16
##
##
## Estimation results for equation y:
## ==================================
## y = x.l1 + y.l1 + x.l2 + y.l2 + x.l3 + y.l3 + x.l4 + y.l4 + x.l5 + y.l5
##
## Estimate Std. Error t value Pr(>|t|)
## x.l1 -0.20083 0.15711 -1.278 0.20465
## y.l1 0.02043 0.10279 0.199 0.84296
## x.l2 -0.47607 0.15869 -3.000 0.00354 **
## y.l2 0.22983 0.09534 2.411 0.01808 *
## x.l3 -0.23600 0.16493 -1.431 0.15612
## y.l3 0.24821 0.09405 2.639 0.00989 **
## x.l4 0.20012 0.16555 1.209 0.23010
## y.l4 0.21643 0.09435 2.294 0.02427 *
## x.l5 -0.16206 0.16847 -0.962 0.33879
## y.l5 0.30415 0.09863 3.084 0.00276 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.042 on 85 degrees of freedom
## Multiple R-Squared: 0.9996, Adjusted R-squared: 0.9996
## F-statistic: 2.269e+04 on 10 and 85 DF, p-value: < 2.2e-16
##
##
##
## Covariance matrix of residuals:
## x y
## x 0.52722 0.09188
## y 0.09188 1.08635
##
## Correlation matrix of residuals:
## x y
## x 1.0000 0.1214
## y 0.1214 1.0000
serial.test fonksiyonu, seri olarak ilişkili hatalar için çok değişkenli Portmanteau- ve Breusch-Godfrey testini hesaplar.
Stabil bir VAR(p)’de hh mertebesine kadar seri olarak ilişkili bozulmaların yokluğunu test etmek için Portmanteau istatistiği şu şekilde tanımlanır:
serial.test(var.a)
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var.a
## Chi-squared = 55.987, df = 44, p-value = 0.1062
Özellikle veri setinin son n satırını görüntülemek için tail fonksiyonunu kullandım
tail(İnflation_and_GDP)
## x y
## Feb 1980 0.4777166 50.01817
## Mar 1980 2.3222310 49.88636
## Apr 1980 0.6365597 50.56265
## May 1980 2.3190657 50.48389
## Jun 1980 1.0437791 51.41900
## Jul 1980 -0.8786559 51.13416
x = cumsum(x) + 5473.72
par(mar = c(2.5,2.5,1,2.5))
plot.ts(x)
DAXinv =ts(c(İnflation_and_GDP[,1], x),
start = c(1972,50),
frequency = 100)
plot(DAXinv)