timeseries: logit depending on the past period r

This is an example of my timeseries data:

df<-tibble::tribble(
  ~ID, ~Month1, ~Month2, ~Month3, ~Month4, ~Month5, ~Month6, ~Month7, ~Month7,
   1L,      1L,      1L,      1L,      0L,      0L,      1L,      1L,      1L,
   2L,      1L,      1L,      0L,      1L,      0L,      0L,      0L,      1L,
   3L,      1L,      1L,      1L,      1L,      0L,      1L,      0L,      0L,
   4L,      1L,      0L,      0L,      0L,      1L,      0L,      1L,      1L,
   5L,      1L,      0L,      1L,      0L,      1L,      1L,      0L,      0L,
   6L,      0L,      0L,      0L,      0L,      1L,      0L,      1L,      1L
  )

or in long format

 df %>% 
      pivot_longer(-ID,names_to = "Months",values_to = "Sales")

Where ID is a customer, and 1 in month 1 means that the client bought in that month, 0 if not, 1 in month2 mean that the customer bought in that month.. and so on.

I need to create a logit where the dependent variable (y) is 1 if the client bought in the month t (0 if not), and the independent variable (x) is 1 if the client bought in the month t-1, and 0 if not.

Like this: glm(y~x,data=df),family="binomial")
It doesnt depend on the ID, only on past behavior.

After that, also I have to try this but with month t-2 and t-3.
Does anyone know how to solve it? Thanks!

Is this what you are looking for? You can then fit Sale.curr ~ Sale.prev

library(tibble)
library(tidyr)
library(dplyr)

df<-tibble::tribble(
  ~ID, ~Month1, ~Month2, ~Month3, ~Month4, ~Month5, ~Month6, ~Month7, ~Month8,
  1L,      1L,      1L,      1L,      0L,      0L,      1L,      1L,      1L,
  2L,      1L,      1L,      0L,      1L,      0L,      0L,      0L,      1L,
  3L,      1L,      1L,      1L,      1L,      0L,      1L,      0L,      0L,
  4L,      1L,      0L,      0L,      0L,      1L,      0L,      1L,      1L,
  5L,      1L,      0L,      1L,      0L,      1L,      1L,      0L,      0L,
  6L,      0L,      0L,      0L,      0L,      1L,      0L,      1L,      1L
)
dfLng <- df %>% 
  gather(key = "Month",value = "Sale", 2:9)
dfLng <- dfLng %>%  mutate(MonthNum = as.numeric(substr(Month, 6,6)), 
                           MonthNumNext = as.numeric(substr(Month, 6,6)) + 1)
dfLag <- inner_join(dfLng, dfLng, by = c(MonthNumNext = "MonthNum", ID = "ID"), 
                    suffix = c(".prev", ".curr")) %>% 
  select(-MonthNum, -MonthNumNext, - MonthNumNext.curr)
dfLag
#> # A tibble: 42 x 5
#>       ID Month.prev Sale.prev Month.curr Sale.curr
#>    <int> <chr>          <int> <chr>          <int>
#>  1     1 Month1             1 Month2             1
#>  2     2 Month1             1 Month2             1
#>  3     3 Month1             1 Month2             1
#>  4     4 Month1             1 Month2             0
#>  5     5 Month1             1 Month2             0
#>  6     6 Month1             0 Month2             0
#>  7     1 Month2             1 Month3             1
#>  8     2 Month2             1 Month3             0
#>  9     3 Month2             1 Month3             1
#> 10     4 Month2             0 Month3             0
#> # … with 32 more rows

Created on 2020-06-14 by the reprex package (v0.2.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

thanks for your solution ! it really helped me!