Time -Series
Dr. Sudipta Nanda, JIM, Indore
Moments of a variable
• Mean
• Median
• Mode
# Create mode() function to calculate mode
mode <- function(x, na.rm = FALSE) {
if(na.rm){ #if na.rm is TRUE, remove NA values from input x
x = x[!is.na(x)]
}
val <- unique(x)
return(val[which.max(tabulate(match(x, val)))])
}
• Kurtosis and skewness- install.packages("moments")
Dr. Sudipta Nanda, JIM, Indore
T-test
• CO2 package
• Install datasets
• Single sample t-test:
t.test(CO2$conc,mu=500)
Dr. Sudipta Nanda, JIM, Indore
Two sample t-test
• Unequal variance - t.test(uptake ~ Treatment, data = CO2)
• Equal variance - t.test(uptake ~ Treatment, data = CO2, var.equal
= TRUE)
• Paired - t.test(uptake ~ Treatment, paired = TRUE, data = CO2)
• t.test(x, y,
alternative = c("two.sided", "less", "greater"),
mu = 0, paired = FALSE, var.equal = FALSE,
Dr. Sudipta Nanda, JIM, Indore
conf.level = 0.95, ...)
Normality
• Histogram – hist()
• SPSS - analyze → descriptive statistics → explore → plots → normality
plots with tests
• qqnorm()
qqline()
Dr. Sudipta Nanda, JIM, Indore
Jarque Bera Test
• The test statistic JB is defined as:
JB =[(n-k+1) / 6] * [S2 + (0.25*(C-3)2)]
where n is the number of observations in the sample, k is the number of regressors (k=1 if
not used in the context of regression), S is the sample skewness, and C is the sample
kurtosis.
#install (if not already installed) and load tseries package
if(!require(tseries)){install.packages('tseries')}
#generate a list of 100 normally distributed random variables
dataset <- rnorm(100)
#conduct Jarque-Bera test
jarque.bera.test(dataset)
The p-value will tell if it will fall in null hypothesis or alternate hypothesis. The null
hypothesis is that the data is normally distributed. Dr. Sudipta Nanda, JIM, Indore
Kolmogorov–Smirnov test
# installing the required package
install.packages("dgof")
# loading the required package
library("dgof")
x1 <- rnorm(100)
ks.test(x1, "pnorm")
The p-value will tell if it will fall in null hypothesis or
alternate hypothesis. The null hypothesis is that the
data is normally distributed.
Dr. Sudipta Nanda, JIM, Indore
Shapiro-Wilk’s Test
#perform Shapiro-Wilk test for normality
shapiro.test(data)
The p-value will tell if it will fall in null hypothesis or
alternate hypothesis. The null hypothesis is that the
data is normally distributed.
Non-normal to normal
• Log transformation
• Square root transformation
• Cube root transformation Dr. Sudipta Nanda, JIM, Indore
Kernel Density
• A kernel density estimate (KDE) plot is a method for visualizing the distribution
of observations in a dataset, analogous to a histogram. KDE represents the
data using a continuous probability density curve in one or more dimensions.
• d<-density(data)
plot(d)
• Online R compilers –
• https://www.programiz.com/r/online-compiler/#google_vignette
• https://www.mycompiler.io/new/r
• https://rdrr.io/snippets/
Dr. Sudipta Nanda, JIM, Indore
Stationarity of Data
• A common assumption in many time series techniques is that the
data are stationary. A stationary process has the property that the
mean, variance and autocorrelation structure do not change over
time.
• Differenced data in R
d1<-diff(gold_price$`Closing Prices`,differences=1)
plot(d1)
Dr. Sudipta Nanda, JIM, Indore
WEAK VS STRONG STATIONARITY
• Weak form of stationarity is when the time-series has constant mean and variance throughout the time. Let's put it simple, practitioners say that the
stationary time-series is the one with no trend - fluctuates around the constant mean and has constant variance.
• Covariance between different lags is constant, it doesn't depend on absolute location in time-series. For example, the covariance between t and t-1 (first
order lag) should always be the same (for the period from 1960-1970 same as for the period from 1965-1975 or any other period).
• In non-stationary processes there is no long-run mean to which the series reverts; so we say that non-stationary time series do not mean revert. In that
case, the variance depends on absolute position in time-series and variance goes to infinity as time goes on. Technically speaking, auto-correlations to
not decay with time, but in small samples they do disappear - although slowly.
• In stationary processes, shocks are temporary and dissipate (lose energy) over time. After a while, they do not contribute to the new time-series values.
For example, something which happened log time ago (long enough) such as World War II, had an impact, but, it the time-series today is the same as if
World War II never happened, we would say that shock lost its energy or dissipated. Stationarity is especially important as many classical econometric
theories are derived under the assumptions of stationarity.
• A strong form of stationarity is when the distribution of a time-series is exactly the same trough time. In other words, the distribution of original time-
series is exactly same as lagged time-series (by any number of lags) or even sub-segments of the time-series. For example, strong form also suggests
that the distribution should be the same even for a sub-segments 1950-1960, 1960-1970 or even overlapping periods such as 1950-1960 and 1950-
1980. This form of stationarity is called strong because it doesn't assume any distribution. It only says the probability distribution should be the same. In
the case of weak stationarity, we defined distribution by its mean and variance. We could do this simplification because implicitly we assumed normal
distribution, and normal distribution is fully defined by its mean and variance or standard deviation.
• Any time series where the datapoints are normally distributed through time with a constant mean and variance is considered a strong stationary time
series since given the mean and standard deviation the normal distribution will always have the same probability distribution curve (the inputs to the
normal equation only depend on the mean and the standard deviation).
Dr. Sudipta Nanda, JIM, Indore
Test for Stationarity or Unit root test
• A unit root process is a data-generating process whose first difference
is stationary. In other words, a unit root process yt has the form. yt =
yt–1 + stationary process.
• Null Hyphothesis (H0) : The time series displays a unit-root. This
means that the time series is nonstationary.
• Alternate Hypothesis (H1) : There is no unit-root in the time series,
meaning that the time series is stationary.
• Required package – tseries
• adf.test(data)
Dr. Sudipta Nanda, JIM, Indore
Autocorrelation
ACF Plot
• library(stats)
• acf(model$residuals, type = "correlation")
Durbin-Watson Test
• It checks the first order autocorrelation. Dwtest is for checking autocorrelation of residuals.
• Ho – autocorrelation does not exist
H1- autocorrelation exists
• The Durbin-Watson test statistic has always a value between 0 and 4, where:
[0-2): means positive autocorrelation
2: means no autocorrelation
(2-4]: mean negative autocorrelation
• As a rule of thumb, we assume that the residuals are not correlated when the Durbin-
Watson test statistic has a value between 1.5 and 2.5. If the statistic is below 1 or above 3,
Dr. Sudipta Nanda, JIM, Indore
then there is definitely autocorrelation among the residuals.
• In R, you can use the DWTEST() function from the lmtest package to
perform the Durbin-Watson test.
library(lmtest)
lmtest::dwtest(model)
Breusch-Godfrey Test
• Ho – autocorrelation does not exist
H1 – autocorrelation exists
• library(lmtest)
bgtest(model, order)
Dr. Sudipta Nanda, JIM, Indore
Box-Pierce and Ljung-Box Tests
• The null Hypothesis H0 is that the residuals are independently
distributed (no autocorrelation or serial correlation). The alternative
hypothesis is that the residuals are not independently distributed and
exhibit a serial correlation.
• Box.test(df.ts, lag = 1, type = "Ljung")
Dr. Sudipta Nanda, JIM, Indore
White noise
• The white noise is a stationary time series or a stationary random process
with zero autocorrelation. White noise is a random collection of variables
that are uncorrelated.
• When the error term is white noise, it indicates that the model's residuals are
random and contain no autocorrelation.
• A time series rt is called a white noise if {rt} is a sequence of independent and
identically distributed random variables with finite mean and variance. In
particular, if rt is normally distributed with mean zero and variance σ2, the
series is called a Gaussian white noise. For a white noise series, all the ACFs
are zero. In practice, if all sample ACFs are close to zero, then the series is a
white noise series.
Dr. Sudipta Nanda, JIM, Indore
SIMPLE AUTOREGRESSIVE MODELS
(AR Modeling)
• AR(1)
• AR(p)
Taking lagged value in R
Lag(variable, k=“lag value”)
Dr. Sudipta Nanda, JIM, Indore Serial correlation
Optimal lag(p) in AR(p) model
• PACF method:
The PACF of a stationary time series is a function of its ACF.
The estimate φˆ1,1 of the first equation is called the lag-1 sample PACF of rt. The estimate φˆ2,2 of the
second equation is the lag-2 sample PACF of rt. The estimate of the third equation is the lag-3 sample
PACF of rt, and so on.
From the definition, the lag-2 PACF φˆ2,2 shows the added contribution of rt−2 to rt over the AR(1)
model rt = φ0 + φ1rt−1 + e1t. The lag-3 PACF shows the added contribution of rt−3 to rt over an AR(2)
model, and so on. Therefore, for an AR(p) model, the lag-p sample PACF should not be zero, but φˆj,j
should be close to zero for all j > p. We make use of this property to determine the order p.
Dr. Sudipta Nanda, JIM, Indore
• pacf(variable)
• Can use ACF and PACF values to see which lag cross the blue dotted lines in both.
• AIC –
install.packages("vars")
library(vars)
VARselect(Reg_Model$SensexClosing)
Check the AIC values and select the lag with the least AIC value.
Dr. Sudipta Nanda, JIM, Indore
Forecasting with AR(p) modeling
data <- Reg_Model$CloseInfy
Fit ARIMA Model:
Use the arima() function to fit an ARIMA model. Specify the order parameter order to include the autoregressive order (p). For
example, if you want to fit an AR(2) model, set order = c(2, 0, 0). It would depend on optimal lag
arima_model <- arima(data, order = c(2, 0, 0))
summary(arima_model)
num_forecast_steps <- 10
forecast <- predict(arima_model, n.ahead = num_forecast_steps)
The forecast object will contain the predicted values and corresponding prediction intervals.
plot(data, type = "l", col = "blue", ylim = range(c(data, forecast$pred)))
lines(forecast$pred, col = "red")
lines(forecast$pred + 1.96 * forecast$se, col = "green", lty = 2)
lines(forecast$pred - 1.96 * forecast$se, col = "green", lty = 2)
This code creates a line plot of the original data in blue and overlays the predicted values in red along with 95% prediction
intervals in green. Dr. Sudipta Nanda, JIM, Indore
Moving Average Model
MA(q) Model:
The purpose of using MA models is to filter out the short-term fluctuations or noise in a time series, making it easier
to identify underlying patterns and trends.
,
Identifying MA order using R:
> lag_orders<-1:10
is the mean of the time series.
> aic_values <- numeric(length(lag_orders))
> ma_models <- vector("list", length(lag_orders)) is the white noise error term at time , representing the
> for (i in seq_along(lag_orders)) { unobserved random shock or disturbance at the
+ order <- c(0, 0, lag_orders[i]) current time point. It captures the unpredictability or
+ ma_model <- arima(data, order = order) randomness in the observed data.
+ ma_models[[i]] <- ma_model is the contribution of past white noise error terms to
+ aic_values[i] <- AIC(ma_model) the current observation. Each term represents the
+} weighted influence of the error term at a lagged time
> optimal_lag <- lag_orders[which.min(aic_values)] point , where is the weight or coefficient associated
> optimal_lag with that lag.
[1] 10
> acf_result <- acf(data, lag.max = 20)
> pacf_result <- pacf(data, lag.max = 20)
Dr. Sudipta Nanda, JIM, Indore
Fitting MA model
- All same except
arima_model <- arima(data, order = c(2, 0, 0))
ARIMA Model
arima(p,d,q)
Dr. Sudipta Nanda, JIM, Indore