Causality analysis is the process of identifying, explaining, and resolving the underlying causes and effects of a problem or phenomenon. In simple terms,
 It examines whether one event or variable directly influences another. 
Unlike correlation—which only measures the strength of association—causality provides a valid and logical explanation that turns a possible relationship into a confirmed one. When we claim that one variable affects another, we are stating that changes in one variable directly shape the outcome of the other.

In this article, we examine the causal relationship between Apple and Walmart stock prices using historical data sourced from Yahoo Finance. The dataset consists of daily observations from June 30, 2010, to June 19, 2014. To understand whether one stock’s movement influences the other, we apply a series of statistical techniques. These include the Augmented Dickey-Fuller (ADF) test to evaluate stationarity, the Karl Pearson correlation coefficient to measure the strength of linear association, and the Granger Causality Test to determine whether past values of one stock can be used to predict the other. Together, these methods provide a structured and reliable framework for conducting causality analysis in financial time series.

To perform the causality analysis, we first load the required R packages:
library(dplyr)
library(readxl)
library(tseries)
library(aTSA)
library(lmtest)
library(corrplot)

We begin by importing the Excel files directly into the R console. This creates two data frames containing the historical closing prices of Apple and Walmart stocks. Next, we combine the relevant columns into a single data frame named df, separating the date and closing price values for both stocks. We then rename the columns for better readability.

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")

To compute the correlation between the two stocks, we remove the date column because correlation analysis requires numerical variables only. This refined dataset (df_RD) is then used to calculate the correlation matrix.

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")


Stationarity is a fundamental requirement in time series analysis, as many statistical models rely on stable means and variances over time. To verify whether a series is stationary, we use the Augmented Dickey-Fuller (ADF) test, one of the most widely applied methods in econometrics. The accompanying plot provides a clear visual representation of the series’ behaviour, making it easier to interpret the stationarity

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)



The results of the ADF test indicate that both time series are non-stationary in their original form. To address this, we transform the data into a stationary series by applying differencing, a common technique used to stabilize the mean and remove trends. After differencing, we perform the Augmented Dickey-Fuller test again to confirm whether the transformed series now meet the stationarity requirement.

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)



After transforming the series to achieve stationarity, the data are now ready for Granger causality testing. This step allows us to determine whether past values of one stock can be used to predict the future values of the other, providing insights into the directional relationship between the two time series.

grangertest(A ~ W)

grangertest(W ~ A)

Conclusion

The analysis reveals that Apple (AAPL) and Walmart (WMT) stock prices exhibit a positive correlation, indicating that they tend to move in the same direction. For the causality assessment, both time series were transformed into stationary series to meet the assumptions of time series modeling. The Granger causality test results indicate that each stock has a predictive effect on the other, suggesting that the movement of one stock can influence the future behavior of the other.

To explore the full implementation and source code, the R Markdown (Rmd) file is available on-

Post a Comment

The more questions you ask, the more comprehensive the answer becomes. What would you like to know?

Previous Post Next Post

Translate

AKSTATS

Learn it --> Do it 🖋 --> Get it 🏹