Close Navigation
Learn more about IBKR accounts
R Code: Creating Lagged Xs and Y for Supervised Learning

R Code: Creating Lagged Xs and Y for Supervised Learning

Posted December 7, 2022
Sang-Heon Lee
SHLee AI Financial Model

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.

 class=

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 Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.