Content
# サンプルデータ
head(sampleData)
cat("-----------------------------------------\n")
tail(sampleData)
Date MonetaryBase NIKKEI225
1 2014-08-01 242.3138 15424.59
2 2014-09-01 245.8169 16173.52
3 2014-10-01 255.7542 16413.76
4 2014-11-01 259.3603 17459.85
5 2014-12-01 267.4016 17450.77
6 2015-01-01 275.3859 17674.39
-----------------------------------------
Date MonetaryBase NIKKEI225
55 2019-02-01 493.0980 21385.16
56 2019-03-01 494.2027 21205.81
57 2019-04-01 507.3520 22258.73
58 2019-05-01 510.8086 20601.19
59 2019-06-01 512.9912 21275.92
60 2019-07-01 516.0145 21521.53
# 単位根検定 H0:非定常過程
# レベルデータ
apply(sampleData[, -1], 2, function(x) adf.test(x))
cat("-----------------------------------------\n")
# 一階差分
apply(sampleData[, -1], 2, function(x) adf.test(diff(x)))
$MonetaryBase
Augmented Dickey-Fuller Test
data: x
Dickey-Fuller = -0.9098, Lag order = 3, p-value = 0.9446
alternative hypothesis: stationary
$NIKKEI225
Augmented Dickey-Fuller Test
data: x
Dickey-Fuller = -1.8594, Lag order = 3, p-value = 0.6316
alternative hypothesis: stationary
-----------------------------------------
$MonetaryBase
Augmented Dickey-Fuller Test
data: diff(x)
Dickey-Fuller = -4.695, Lag order = 3, p-value = 0.01
alternative hypothesis: stationary
$NIKKEI225
Augmented Dickey-Fuller Test
data: diff(x)
Dickey-Fuller = -4.6609, Lag order = 3, p-value = 0.01
alternative hypothesis: stationary
# 共和分検定 H0:共和分関係無し
summary(ca.po(z = sampleData[, -1], type = "Pu"))
########################################
# Phillips and Ouliaris Unit Root Test #
########################################
Test of type Pu
detrending of series none
Call:
lm(formula = z[, 1] ~ z[, -1] - 1)
Residuals:
Min 1Q Median 3Q Max
-127.51 -55.49 24.46 43.70 78.16
Coefficients:
Estimate Std. Error t value Pr(>|t|)
z[, -1] 0.0210013 0.0004051 51.85 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 61.99 on 59 degrees of freedom
Multiple R-squared: 0.9785, Adjusted R-squared: 0.9782
F-statistic: 2688 on 1 and 59 DF, p-value: < 2.2e-16
Value of test-statistic is: 0.4714
Critical values of Pu are:
10pct 5pct 1pct
critical values 20.3933 25.9711 38.3413
# 相互相関関数を算出する関数です。
calculate_ccf <- function(x0, y0, lag, significantLevel = 0.05) {
if (lag < 0) {
x <- head(x0, lag)
y <- tail(y0, lag)
} else if (0 < lag) {
x <- tail(x0, -lag)
y <- head(y0, -lag)
} else {
x <- x0
y <- y0
}
mux <- mean(x0)
muy <- mean(y0)
dx <- mean((x0 - mux)^2)
dy <- mean((y0 - muy)^2)
n <- length(x0)
cxy <- sum((x - mux) * (y - muy))/n
ccf <- cxy/sqrt(dx * dy)
upperCI <- qnorm((1 + (1 - significantLevel))/2)/sqrt(length(x0))
lowerCI <- -qnorm((1 + (1 - significantLevel))/2)/sqrt(length(x0))
return(data.frame(lag = lag, ccf = ccf, upperCI = upperCI, lowerCI = lowerCI))
}
# 両系列とも一階差分を分析対象とします
sampleData_1stdiff <- data.frame(apply(sampleData[, c(2, 3)], 2, diff))
colnames(sampleData_1stdiff) <- paste0(colnames(sampleData_1stdiff), "_1stDifference")
# 正規性検定 H0:サンプルの分布は正規分布に従う
apply(sampleData_1stdiff, 2, function(x) ks.test(x, y = "pnorm", mean = mean(x), sd = sd(x)))
apply(sampleData_1stdiff, 2, function(x) ks.test(x, y = "pnorm", mean = 0, sd = sd(x)))
$MonetaryBase_1stDifference
One-sample Kolmogorov-Smirnov test
data: x
D = 0.10102, p-value = 0.5497
alternative hypothesis: two-sided
$NIKKEI225_1stDifference
One-sample Kolmogorov-Smirnov test
data: x
D = 0.14512, p-value = 0.1508
alternative hypothesis: two-sided
$MonetaryBase_1stDifference
One-sample Kolmogorov-Smirnov test
data: x
D = 0.37749, p-value = 4.608e-08
alternative hypothesis: two-sided
$NIKKEI225_1stDifference
One-sample Kolmogorov-Smirnov test
data: x
D = 0.18905, p-value = 0.0255
alternative hypothesis: two-sided
# qqplot
qqnorm(sampleData_1stdiff$MonetaryBase_1stDifference)
qqline(sampleData_1stdiff$MonetaryBase_1stDifference, col = 2)
qqnorm(sampleData_1stdiff$NIKKEI225_1stDifference)
qqline(sampleData_1stdiff$NIKKEI225_1stDifference, col = 2)
# マネタリーベースと日経平均株価の相互相関関数(ともに一階差分)
Reduce(function(x, y) rbind(x, y), lapply(-12:12, function(x) calculate_ccf(x0 = sampleData_1stdiff[, 1], y0 = sampleData_1stdiff[, 2], lag = x)))
lag ccf upperCI lowerCI
1 -12 0.17651259 0.2551656 -0.2551656
2 -11 -0.05035887 0.2551656 -0.2551656
3 -10 0.02943944 0.2551656 -0.2551656
4 -9 -0.02919705 0.2551656 -0.2551656
5 -8 -0.19637523 0.2551656 -0.2551656
6 -7 0.04750540 0.2551656 -0.2551656
7 -6 0.16908551 0.2551656 -0.2551656
8 -5 -0.05004518 0.2551656 -0.2551656
9 -4 0.03442756 0.2551656 -0.2551656
10 -3 0.06121210 0.2551656 -0.2551656
11 -2 -0.17472611 0.2551656 -0.2551656
12 -1 0.15722156 0.2551656 -0.2551656
13 0 0.11885352 0.2551656 -0.2551656
14 1 -0.08634910 0.2551656 -0.2551656
15 2 0.11239786 0.2551656 -0.2551656
16 3 -0.07274198 0.2551656 -0.2551656
17 4 -0.08717375 0.2551656 -0.2551656
18 5 0.09080077 0.2551656 -0.2551656
19 6 0.09233028 0.2551656 -0.2551656
20 7 -0.07591537 0.2551656 -0.2551656
21 8 0.04995646 0.2551656 -0.2551656
22 9 0.03494863 0.2551656 -0.2551656
23 10 -0.23544542 0.2551656 -0.2551656
24 11 0.05172521 0.2551656 -0.2551656
25 12 0.06126062 0.2551656 -0.2551656
# 何も関数を自作しなくてもRでは相互相関関数'ccf'が用意されています。
attach(sampleData_1stdiff)
print(ccf(x = MonetaryBase_1stDifference, y = NIKKEI225_1stDifference, lag.max = 12))
Autocorrelations of series 'X', by lag
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
0.177 -0.050 0.029 -0.029 -0.196 0.048 0.169 -0.050 0.034 0.061 -0.175 0.157 0.119 -0.086 0.112 -0.073 -0.087 0.091 0.092 -0.076 0.050 0.035 -0.235 0.052 0.061