Categories

# Testing the Correlation between Time Series Variables

In this article, we will examine whether the gasoline prices are related to the variables that are thought to affect gasoline prices the most by the Turkish people. One of the variables is the Brent crude oil prices that are averaged monthly in dollars; the other is the dollar exchange rate in Turkish currency (TL) that are averaged per month as well.

These variables will be shown brent and dollar respectively in the dataset. The range of the dataset is between 2013 and 2020. The brief data frame is shown below. (All the coding in the article is based on R language)

The T-test is used to examine whether the population correlation coefficient is zero or not. The pre-acceptance is that the sample is normally distributed. This pre-acceptance is violated in some situations, in those cases, an alternative non-parametric test is needed. The Spearman’s rank correlation test takes over here; because profit or price data generally do not show normal distribution. Therefore, it is not appropriate to use the Pearson correlation coefficient test in our dataset.

Spearman’s rank correlation test consider ranking while it measures the correlation between two variables. The value is as between +1 and -1 as is the Pearson correlation coefficient- $rho_s$– The two-way hypothesis test is described as:

$H_0: rho_s=0$

$H_A: rho_s ne 0$

First of all, the sample spearman rank correlation coefficient$r_s$– is calculated to execute the test; this happens in a couple of steps.

• Gasoline prices are ranked from small to big; in the case of equality, the ranking of equal observations are averaged and the ranking continues from where it left off. The same process is executed for Brent prices.
 `(dplyr)` `df_spearman<- df %>%` `(` ` ``rank_gasoline=``(gasoline),` ` ``rank_brent=``(brent),` ` ``d=rank_gasoline-rank_brent,` ` ``d_square=d^2) %>%` ` ``(-dollar)` `(df_spearman)`
• The difference between the rankings of each binary observation is calculated as :

$Sigma d_i=0$

 `(df_spearman\$d)`
• Later, the squares of the difference are summed:
$Sigma d_i^2=69107$
 `d_square_sum <-` `(df_spearman\$d_square)` `d_square_sum`

Spearman rank correlation coefficient$r_s$– is formulated as:

$r_s=1- frac {6Sigma d_i^2} {n(n^2-1)}$

 `n <-` `(df)` `rho_s <- (1-(6*(``(d_square_sum)))/(n*(n^2-1))) %>%` `(2)`

This result shows us that there is a positive and weak relation between gasoline and brent prices. Let’s examine this result is at a significance level of %5 and find if the alternative hypothesis is true.

The point we have to look at is highlighted in the chart above for – $alpha=0.05$– and n=84; because of the ${r_s}=0.3 geq 0.215$ – the null hypothesis –$H_0: rho_s=0$– is rejected and at the %5 significance level, we can say that although it is weak there is a positive relation between gasoline and brent prices.

Let’s check the results with another way by calling the function ggscatter.

 `(``"ggpubr"``)` `(df, x =` `"brent"``, y =` `"gasoline"``,` ` ``color =` `"blue"``, cor.coef =` `TRUE``,` ` ``cor.method =` `"spearman"``,` ` ``xlab =` `"Brent (TL)"``, ylab =` `"Gasoline (TL)"``)`

As we can see in the chart above, spearman’s ranked correlation coefficient (R=0.3) is the same we found before; and p-value (0.0055) less than 0.05 significance level which means the alternative hypothesis is true –$H_A: rho_s ne 0$

Finally, we will examine the relation between gasoline and dollar (USD/TRY)

 `(df, x =` `"dollar"``, y =` `"gasoline"``,` ` ``color =` `"red"``, cor.coef =` `TRUE``,` ` ``cor.method =` `"spearman"``,` ` ``xlab =` `"USD/TRY"``, ylab =` `"Gasoline (TL)"``)`

The graphic above appears to have a strong positive relationship between gasoline and the dollar. P-value value less than 0.05 indicates that the result is significant and one more time null hypothesis is rejected.

References

• Sanjiv Jaggia, Alison Kelly (2013). Business Intelligence: Communicating with Numbers.
• STHDA: Correlation Test Between Two Variables in R
• Original Article