Search code examples
rregressionlinear-regression

linear regression model with lagging effect/interaction


I want to create a model such as below, with numeric continuous data:

revenue~variable+variable2+variable3

My data has around 1000 observations, with 5 variables altogether which are the above mentioned dependent variable and 3 independent variables, together with the date. Each row represents a single day. I want to build the model such that not only do the independent variables effect the dependent variable (revenue), but also the independent variables from previous days. I want the effect from previous days to decrease over time. So for example, yesterdays independent variables would have a larger effect on the revenue todaythan the day before etc.... all the way to the 100th previous day.

To make it more clear, this is an advertising model looking at how an individual seeing a type of advert (independent variables are types of TV advert) impacts the revenue, with adverts seen longer away being less likely to cause an action and purchase.

In other words, perhaps adding decay terms to the model.


Solution

  • I work in econometrics. I use this

    df <- data.frame(X1=(seq(1,10,by=1))) 
    
    library(dplyr)
    df <- 
      df %>%
      mutate(X1_lag1 = dplyr::lag(X1, n = 1, default = NA))
    df
    
    > df
       X1 X1_lag1
    1   1      NA
    2   2       1
    3   3       2
    4   4       3
    5   5       4
    6   6       5
    7   7       6
    8   8       7
    9   9       8
    10 10       9
    

    whereby df is dataframe, and X1 is the variable you want to lag, and X1_lag1 is the new lagged variable. You can put a decay on the 0.5*X1_lag1 on the lagged variable, create permutations. In SAS I can put a coefficient on the decay factor but it gets very messy. I rather create multiple permutations of decay and see which has best fit.