library(readr)
library(dplyr)
library(ggplot2)
library(readr)
library(ggplot2)
library(dplyr)
library(forecast)
library(smooth)
library(tidyquant)
library(plotly)
library(AER)
library(dynlm)
library(forecast)
library(readxl)
library(stargazer)
library(scales)
library(quantmod)
library(urca)
library(TSA)
library(quantmod)
library(tseries)
SP <- read_delim("SP.csv", ";",
escape_double = FALSE, trim_ws = TRUE)
#SP$Price_SP=SP$Price_SP*1000
RTC<- read_delim("rtc.csv", ";", escape_double = FALSE,
trim_ws = TRUE)
Gas <- read_delim("Gas.csv", ";", escape_double = FALSE,
trim_ws = TRUE)
USD <- read_delim("USD.csv", ";", escape_double = FALSE,
trim_ws = TRUE)
Brent <- read_delim("Brent.csv",
";", escape_double = FALSE, trim_ws = TRUE)
names(SP)[1] <- "Date"
names(RTC)[1] <- "Date"
names(Gas)[1] <- "Date"
names(USD)[1] <- "Date"
names(Brent)[1] <- "Date"
RTC$Цена[RTC$Цена>500] <- 222.39
SP =as.data.frame(SP)
SP$Price_SP=as.numeric(SP$Price_SP)
SP =as.data.frame(SP)
Brent$Цена[Brent$Date=="2021-03-17"] <- 60
library(lubridate)
plot(SP$Price_SP,type="l")
plot(Brent$Цена,type="l")
plot(Gas$Цена,type="l")
plot(USD$Цена,type="l")
plot(RTC$Цена,type="l")
SandP <- plot_ly(SP, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~ Date, y = ~ Price_SP) %>%
layout(showlegend = F, title='S&P',
xaxis = list(rangeslider = list(visible = F)))
SandP
Brents <- plot_ly(Brent, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~ Date, y = ~ Цена) %>%
layout(showlegend = F, title='Brent',
xaxis = list(rangeslider = list(visible = T)))
Brents
RTCs <- plot_ly(RTC, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~ Date, y = ~ Цена) %>%
layout(showlegend = F, title='RTC',
xaxis = list(rangeslider = list(visible = T)))
RTCs
USDs <- plot_ly(USD, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~ Date, y = ~ Цена) %>%
layout(showlegend = F, title='Курс',
xaxis = list(rangeslider = list(visible = T)))
USDs
Gass <- plot_ly(Gas, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~ Date, y = ~ Цена) %>%
layout(showlegend = F, title='Gas',
xaxis = list(rangeslider = list(visible = T)))
Gass
full_data=inner_join(Brent,Gas,by="Date")
full_data=inner_join(full_data,RTC,by="Date")
full_data=inner_join(full_data,SP,by="Date")
full_data=inner_join(full_data,USD,by="Date")
full_data=full_data %>% select(Date,Цена.x,Цена.y,Цена.x.x,Price_SP,Цена.y.y)
colnames(full_data) <- c("Date","Brent", "Gas","RTC","SP","USD")
library(tidyquant)
library(plotly)
fig <- plot_ly(full_data, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~Date, y = ~ Brent*1000, name = 'Brent')%>%
add_trace(x = ~Date, y = ~Gas*10000, name = 'Gas')%>%
add_trace(x = ~Date, y = ~RTC*1000, name = 'RTC')%>%
add_trace(x = ~Date, y = ~SP*10000, name = 'SP')%>%
add_trace(x = ~Date, y = ~USD*1000, name = 'USD')%>%
layout(title = 'Данные по индексам',yaxis = list(title = 'Стоимость в тыс.$'))
fig
fig <- plot_ly(full_data, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~Date, y = ~ Brent*1000, name = 'Brent')%>%
add_trace(x = ~Date, y = ~Gas*10000, name = 'Gas')%>%
add_trace(x = ~Date, y = ~RTC*1000, name = 'RTC')%>%
add_trace(x = ~Date, y = ~SP*1000, name = 'SP')%>%
add_trace(x = ~Date, y = ~USD*1000, name = 'USD')%>%
layout(title = 'Данные по индексам',yaxis = list(title = 'y'),
xaxis = list(dtick = "M3", tickformat="%b<br>%Y"), width = 1100)
options(warn = -1)
fig <- fig %>%
layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 1,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 1,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6')
fig <- fig %>% layout(legend = TRUE)
fig
library(urca)
ur.df(SP$Price_SP, lags = 0,
type = c("none"),
selectlags = c("AIC"))
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: 0.4504
ur.df(RTC$Цена, lags = 0,
type = c("none"),
selectlags = c("AIC"))
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -0.6254
ur.df(Gas$Цена, lags = 0,
type = c("none"),
selectlags = c("AIC"))
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.2999
ur.df(Brent$Цена, lags = 0,
type = c("none"),
selectlags = c("AIC"))
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.1418
ur.df(USD$Цена, lags = 0,
type = c("none"),
selectlags = c("AIC"))
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: 0.7633
qnorm(c(.1,.05,.01)/2)
## [1] -1.644854 -1.959964 -2.575829
Все данные превышает допустимый максимальный порог в 10%, поэтому мы можем утверждать, что изучаемые ряды нестационарны.
#perform augmented Dickey-Fuller test
adf.test(SP$Price_SP)
##
## Augmented Dickey-Fuller Test
##
## data: SP$Price_SP
## Dickey-Fuller = -2.4594, Lag order = 10, p-value = 0.3839
## alternative hypothesis: stationary
adf.test(RTC$Цена)
##
## Augmented Dickey-Fuller Test
##
## data: RTC$Цена
## Dickey-Fuller = -2.8351, Lag order = 10, p-value = 0.2248
## alternative hypothesis: stationary
adf.test(Gas$Цена)
##
## Augmented Dickey-Fuller Test
##
## data: Gas$Цена
## Dickey-Fuller = -2.1383, Lag order = 11, p-value = 0.5198
## alternative hypothesis: stationary
adf.test(Brent$Цена)
##
## Augmented Dickey-Fuller Test
##
## data: Brent$Цена
## Dickey-Fuller = -2.4304, Lag order = 10, p-value = 0.3961
## alternative hypothesis: stationary
adf.test(USD$Цена)
##
## Augmented Dickey-Fuller Test
##
## data: USD$Цена
## Dickey-Fuller = -3.3849, Lag order = 10, p-value = 0.05604
## alternative hypothesis: stationary
Так же как и стестом на единичный корень-мы не можем отвергнуть нулевую гипотезу о нестационарности рядов, так как p-value> 0.05.
Необходимо оценить коинтегрирующий коэффициент с помощью EG-ADF теста. Для начала предположим, что θ=1:
Выберем период, где визуально наблюдается самая сильная зависимость 2019:Q4-2020:Q4
library(dplyr)
times=full_data %>% filter(full_data$Date>"2019-01-01" & full_data$Date < "2020-12-31")
ur.df(window(times$RTC),
lags = 10,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.5337 1.1787
ur.df(window(times$SP),
lags = 10,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.1174 0.7969
ur.df(window(times$Brent),
lags = 11,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.2769 0.8289
ur.df(window(times$Gas),
lags = 10,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.8814 1.7812
ur.df(window(times$USD),
lags = 10,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.569 1.2776
ur.df(window(times$RTC) - window(times$SP),
lags = 6,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.5992 1.2861
ur.df(window(times$RTC) - window(times$Brent),
lags = 6,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.7242 1.4921
ur.df(window(times$RTC) - window(times$Gas),
lags = 6,
selectlags = "AIC",
type = "drift")
##
## ###############################################################
## # Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.5944 1.2806
ur.ers(window(times$RTC),
model="constant")
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.3709
ur.ers(window(times$SP),
model="constant")
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -0.1049
ur.ers(window(times$Brent),
model="constant")
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.2773
ur.ers(window(times$Gas),
model="constant")
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.797
ur.ers(window(times$USD),
model="constant")
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -0.8621
#Spread
ur.ers(window(times$RTC) - window(times$SP),
model = "constant",
lag.max = 10)
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.736
ur.ers(window(times$RTC) - window(times$Brent),
model = "constant",
lag.max = 10)
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.6491
ur.ers(window(times$RTC) - window(times$Gas),
model = "constant",
lag.max = 10)
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.725
ur.ers(window(times$RTC) - window(times$USD),
model = "constant",
lag.max = 10)
##
## ###############################################################
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test #
## ###############################################################
##
## The value of the test statistic is: -1.6282
critical values for the levels
10%, 5% and 1% are −3.12, −3.41 and − 3.96.
Таким образом гипотеза о нестационарности разности (spread) не отвергается, для всех пар с индексом RTC.