R Code: Creating Lagged Xs and Y for Supervised Learning

This post shows a simple R code to create various lagged time series and concatenate them with the original time series. This can be used frequently when preprocessing time series data for machine/deep learning models.

Creating lagged Xs and y

Time series and its various lagged one are used as input variables for supervised machine or deep learning models. The following R code generates this concatenation of a set of lagged and time-t variables of the time series.

As an output format, Xs (lagged variables) are followed by y since it is relatively easy to access X by using “1:nk” rather than “2:(nk+1)”.

#========================================================#
# Quantitative ALM, Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
#
# https://kiandlee.blogspot.com
#--------------------------------------------------------#
# Create lagged Xs and y for supervised learning
#========================================================#
 
graphics.off(); rm(list = ls())
 
#===============================================
# function for creating lagged Xs and y
#===============================================
 
func_lagged_Xs_y <- function(y, k, nmx = "x"){
 
    nk <- length(k); ny <- length(y)
    df <- as.data.frame(matrix(nrow=ny, ncol=nk))
    colnames(df) <- paste0(nmx,k); df$y = y
    
    for(i in 1:nk) 
        df[(1+k[i]):ny,i] <- y[1:(ny-k[i])]
    
    return(df[(max(k)+1):ny,])
}
 
#-----------------------
# sample data
#-----------------------
y <- c(31.12, 27.95, 30.67, 27.18, 21.89, 19.90, 21.58, 
       18.69, 20.31, 21.89, 19.29, 20.57, 21.57, 22.87, 
       21.01, 18.63, 17.96, 17.68, 17.54, 16.95, 17.33)
 
#-----------------------
# test cases
#-----------------------
Xy1 = func_lagged_Xs_y(y, k=1:10)
Xy1
 
Xy2 = func_lagged_Xs_y(y, k=c(1,3,5), nmx = "XX")
Xy2

As expected, the results of two examples in the above R code are as follows.

Visit SH Fintech Modeling for additional insight on this topic: https://kiandlee.blogspot.com/2022/10/r-code-creating-lagged-xs-and-y-for.html

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SH Fintech Modeling and is being posted with permission from SH Fintech Modeling. The views expressed in this material are solely those of the author and/or SH Fintech Modeling and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.