The process of identifying and resolving the causes and effects of a challenge or problem is known as causality analysis. In other words, it is a relationship between two occurrences or variables that can be explained. Unlike correlation, this valid explanation transforms potential into reality via causality. To suggest that one variable affects another indicates that the outcome of one event is directly influenced by the other.
In this article, we will analyse the causality relationship between the Apple and Walmart stocks collected from Yahoo Finance. From 30 June 2010 to 19 June 2014, day-wise data. The casuality analysis has some aspects by finding the elements with the suitable statistical methods, which are the Augmented Dickey-Fuller test (ADF) to check the stationarity of the time series, karl person correlation coefficient method to find out the correlation and Granger causality test used in this analysis.
The Required package to do further analysis.
library(dplyr)
library(readxl)
library(tseries)
library(aTSA)
library(lmtest)
library(corrplot)
Directly we import the excel file to the R console and df named data frame is created to separate the Date and close price of the stocks, Then the names of the columns are changed.
AAPL = read_excel("AAPL.xlsx")
WMT = read_excel("WMT.xlsx")
df = data.frame(AAPL$Date, AAPL$Close, WMT$Close)
df
summary(df)
colnames(df) = c("DATE","AAPL","WMT")
The correlation matrix describes the relationship between the apple and tesla, that is why we removed the date from the data frame and this correlation matrix is plotted by using the "corrplot" package.
df_RD = df[,-1 ] # Except Date
df_RD
cormatrix = cor(df_RD)
cormatrix
# PLOT
corrplot(cormatrix, method = "shade", tl.col = "black", addCoef.col = "black", cl.pos = "n", order = "AOE")
Time series analysis relies heavily on stationarity, so the Augmented Dickey-Fuller test (ADF) is used to find out the stationarity. The plot will explain it precisely.
par(mfrow = c(1,2))
plot(df$AAPL, type = 'l', main = "APPLE")
plot(df$WMT, type = 'l', main = "WAL_MART")
adf.test(df$AAPL)
adf.test(df$WMT)
By the result of the ADF test, both series are non-stationary. So we are changing the non-stationary series to stationary time series by using differentiation and again taking the Augmented Dicky Fuller test.
A = diff(df$AAPL)
str(A)
W = diff(df$WMT)
str(W)
par(mfrow = c(1,2))
plot(A, main = ("Diff of AAPL"), type = "l")
plot(W, main = ("Diff of WMT"), type = "l")
## again to take the Agumented Dicky Fuller test.
adf.test(A)
adf.test(W)
Now the series is ready to check the granger causality.
grangertest(A ~ W)
grangertest(W ~ A)
Conclusion
Apple(APPL) and Walmart(WMT) both stocks have a positive correlation. Then the time series are converted into stationary to check the granger causality. The result of the granger causality explains that both stocks affect one another indicating that the outcome of one event is directly influenced by the other.
Post a Comment
The more you ask questions, that will enrich the answer, so whats your question?