In this tutorial, I explain how to implement, in a flexible way, the algorithm of Bai, Lumsdaine, and Stock (1998).
Step 1: Lag variables.
This function takes as argument a matrix of time series observations and lags it by an order (q).
Code
compute_lags <- function(Y #time series matrix Y
, q) #lag order q
{
p <- dim(Y)[1] #get the dimensions
n <- dim(Y)[2]
myDates <- rownames(Y)[(q + 1) : p] #optional: keep the rownames dates of the data frame with final matching
Y <- data.