# This section deals with test for stationarity in time series data (Food Price Index) from January 2007 to April 2024 in Nigeria. The stationarity test is carried out by data visualization using time plot of the data, the ACF plot and the PACF plot of the Food Price Index. Then, Unit Root Test, that is Augmented Dickey Fuller (ADF) Test is performed to ascertain the stationarity of the time series data.
# Next is to load the installed packages to R Studio using library() function
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(urca)
# Import the Food Price Index into R using read.csv() function
FPI=read.csv("foodprice.csv") # Attach the name as FPI and save the file in Excel as CSV (Comma delimited)

# Convert the data set to time series data
FPI_ts=ts(FPI,start = c(2007), frequency = 12) # For Monthly time series data
FPI_ts
##       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 2007 0.52 0.51 0.47 0.49 0.49 0.48 0.49 0.49 0.52 0.53 0.56 0.59
## 2008 0.58 0.62 0.62 0.62 0.61 0.63 0.67 0.67 0.66 0.68 0.66 0.60
## 2009 0.62 0.64 0.64 0.64 0.65 0.66 0.66 0.70 0.73 0.70 0.69 0.69
## 2010 0.70 0.70 0.68 0.69 0.70 0.74 0.71 0.72 0.69 0.69 0.69 0.72
## 2011 0.68 0.70 0.70 0.73 0.76 0.76 0.78 0.78 0.80 0.82 0.81 0.79
## 2012 0.81 0.81 0.82 0.83 0.83 0.83 0.80 0.83 0.83 0.78 0.76 0.76
## 2013 0.77 0.76 0.77 0.78 0.79 0.79 0.79 0.78 0.75 0.73 0.73 0.72
## 2014 0.71 0.72 0.69 0.71 0.68 0.68 0.67 0.68 0.67 0.66 0.66 0.65
## 2015 0.64 0.63 0.61 0.64 0.65 0.67 0.66 0.66 0.66 0.67 0.66 0.66
## 2016 0.66 0.68 0.72 0.76 0.77 0.83 0.90 0.95 1.03 1.04 1.01 1.03
## 2017 1.02 1.11 1.10 1.12 1.17 1.18 1.19 1.20 1.19 1.13 1.04 0.99
## 2018 0.98 1.01 1.01 1.02 1.03 1.04 1.03 1.03 1.01 0.99 0.97 0.93
## 2019 0.91 0.92 0.91 0.92 0.90 0.88 0.87 0.87 0.87 0.89 0.91 0.91
## 2020 0.89 0.90 0.90 0.97 1.06 1.08 1.12 1.22 1.31 1.33 1.28 1.25
## 2021 1.25 1.34 1.37 1.44 1.45 1.48 1.57 1.61 1.61 1.57 1.50 1.47
## 2022 1.50 1.58 1.55 1.59 1.58 1.56 1.62 1.65 1.63 1.61 1.61 1.63
## 2023 1.60 1.60 1.54 1.56 1.57 1.60 1.74 1.83 1.80 1.79 1.81 1.80
## 2024 1.80 1.88 2.03 2.09
head(FPI_ts) # Display the first 6 of the whole data
##       Jan  Feb  Mar  Apr  May  Jun
## 2007 0.52 0.51 0.47 0.49 0.49 0.48
str(FPI_ts) # Display the structure of the data
##  Time-Series [1:208, 1] from 2007 to 2024: 0.52 0.51 0.47 0.49 0.49 0.48 0.49 0.49 0.52 0.53 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr "Food.Price.Index"
# Descriptive Statistics or Summary Statistics of the Food Price Index
describe(FPI)
##                  vars   n mean   sd median trimmed  mad  min  max range skew
## Food.Price.Index    1 208 0.96 0.37   0.81    0.91 0.24 0.47 2.09  1.62 1.06
##                  kurtosis   se
## Food.Price.Index     0.04 0.03
#The minimum value of food price is 0.47 while the maximum value is 2.09. The standard deviation is 0.37. The median of food price is 0.81 while the mean is 0.96. The skewness is 1.06 means the distribution is not close to symmetrical, with a right (positive) skew. The kurtosis is 0.04 is close to zero, which indicates that the distribution is close to normal and it is mesokurtic, that is its tail is similar to the normal distribution.
# Time Plot of Food Price in Nigeria from January 2007 to April 2024
plot(FPI_ts, main="Time Plot of Food Price in Nigeria", ylab="Food Price",col="blue")

#The time plot shows that mean and variance is not constant over time and food price shows clear upward shifts in levels, especially around early 2020 and sharp rise around 2021 to 2024 which shows another noticeable level shift. This implies that there is presence of trend, general upward trend is visible. Therefore, the time series data is not stationary so it requires differencing or trend removal.
# Autocorrelation (ACF) plot and Partial Autocorrelation(PACF) plot
acf(FPI_ts, main = "ACF of Food Price")

#The ACF plot shows a very slow, exponential decay in autocorrelation values and there is no sharp cutoff after a few lags. Also, the first few lags are highly significant, gradually decreasing in height. This means that the ACF pattern is typical of a non-stationary time series, often due to a trend or unit root and the slow decay suggests that the series has strong autocorrelation at many lags which is consistent with a non-stationary process.
pacf(FPI_ts, main = "PACF of Food Price")

#At lag 0, the partial autocorrelation is 1, which is expected since a time series is perfectly correlated with itself at lag 0. For all other lags shown in the plot (from approximately 0.1 to 1.4), the partial autocorrelation coefficients are within the significance bounds (between -0.2 and 0.2). This indicates that there are no significant partial autocorrelations at these lags which makes the data not to be stationary.
# Unit Root Test - Augmented Dickey Fuller (ADF) test for Stationarity with lag 0
adf.test(FPI_ts, k=0) 
## Warning in adf.test(FPI_ts, k = 0): p-value greater than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  FPI_ts
## Dickey-Fuller = 0.54132, Lag order = 0, p-value = 0.99
## alternative hypothesis: stationary
#The Unit Root Test indicates that the Food Price time series data is not stationary because we fail to reject the null hypothesis H0; P-value (0.99) is greater than 0.05).


#Hence, the food price time series data needs transformation by differencing to make the data stationary. This process will be carried out in my next analysis which will discuss Differencing, Test for Stationarity of the transformed food price data, fit an ARIMA model, carry out diagnostics check and forecast for the future food price in Nigeria from 2025 to 2030.