I picked two stocks: Gold and Silver, and want to check whether they impact on each other due themselves stock prices flutuation.
gold = `GC=F` <- read.csv("~/Desktop/GC=F.csv", stringsAsFactors=TRUE)
silver = `SI=F` <- read.csv("~/Desktop/SI=F.csv", stringsAsFactors=TRUE)
gold.ts = ts(gold[,6], frequency = 12, start = c(2016, 01, 01), end = c(2020,12,01))
silver.ts = ts(silver[,6], frequency = 12, start = c(2016, 01, 01), end = c(2020,12,01))
par(mfrow=c(1,2))
plot.ts(gold.ts)
plot.ts(silver.ts)
#corrolation between two dataset
cor(gold.ts, silver.ts)
## [1] 0.7588244
gold.diff = diff(gold.ts)
silver.diff = diff(silver.ts)
par(mfrow=c(1,2))
autoplot(gold.ts)
autoplot(silver.ts)
d1 = data.frame(gold.diff,silver.diff)
d.ts = ts(d1, start = c(2016,01,01), end = c(2020,12,01),frequency = 12)
autoplot(d.ts)
differ =diff(d.ts)
autoplot(differ)
#Build vars
library(vars)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## The following objects are masked from 'package:fma':
##
## cement, housing, petrol
## Loading required package: strucchange
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
VARselect(differ,lag.max=8,type="const")[["selection"]]
## AIC(n) HQ(n) SC(n) FPE(n)
## 6 6 6 6
var1 = VAR(differ, p=1, type="const")
summary(var1)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: gold.diff, silver.diff
## Deterministic variables: const
## Sample size: 58
## Log Likelihood: -417.884
## Roots of the characteristic polynomial:
## 0.5353 0.5353
## Call:
## VAR(y = differ, p = 1, type = "const")
##
##
## Estimation results for equation gold.diff:
## ==========================================
## gold.diff = gold.diff.l1 + silver.diff.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## gold.diff.l1 -0.1261 0.1678 -0.751 0.45566
## silver.diff.l1 -14.4128 5.3216 -2.708 0.00899 **
## const 2.1136 8.1767 0.258 0.79699
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 62.25 on 55 degrees of freedom
## Multiple R-Squared: 0.32, Adjusted R-squared: 0.2953
## F-statistic: 12.94 on 2 and 55 DF, p-value: 2.479e-05
##
##
## Estimation results for equation silver.diff:
## ============================================
## silver.diff = gold.diff.l1 + silver.diff.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## gold.diff.l1 0.012989 0.005478 2.371 0.0213 *
## silver.diff.l1 -0.788102 0.173689 -4.537 3.13e-05 ***
## const 0.026112 0.266876 0.098 0.9224
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 2.032 on 55 degrees of freedom
## Multiple R-Squared: 0.2975, Adjusted R-squared: 0.2719
## F-statistic: 11.65 on 2 and 55 DF, p-value: 6.068e-05
##
##
##
## Covariance matrix of residuals:
## gold.diff silver.diff
## gold.diff 3874.68 95.323
## silver.diff 95.32 4.128
##
## Correlation matrix of residuals:
## gold.diff silver.diff
## gold.diff 1.0000 0.7538
## silver.diff 0.7538 1.0000
Var1.fc = forecast(var1)
autoplot(Var1.fc)