allows to apply a monthly moving sliding window transformation on a data set.

sliding_window(data, inicio, pliegues, variables)

Arguments

data

dataframe that contains historical counts of different events in monthly time frames. Each row is a unique observation and the columns corresponding to the different months of study. The variables must have keywords to be able to select them together. To see an example of the structure of the data, the dataset such contained in this package can be used.

inicio

initial month, integer numeric format.

pliegues

vector that starts at 1 and ends in the number of periods to be traversed.

variables

a word or vector that allows you to select the variables together and implement the function for each group.

Value

a data frame with the ID of the observations and the different counting time slots calculated by variables.

Details

the operation is as follows, the intermediate month "t" of the entire study period is selected, then the number of events that occurred for each observation in the previous month is calculated, in the last 3 months, 6 months, 12 months and the same month of the previous year.

The procedure described above is replicated in a mobile manner, that is, rolling the time window from t + 1 to n, where n is the last month of study. To see a real use case, visit Crime analysis with tidymodels

See also

Examples

pliegues = 1:13 names(pliegues) = pliegues variables = c("delitos", "temperatura", "mm_agua", "lluvia", "viento") names(variables) = variables data("data_longer_crime") sliding_window(data = data_longer_crime %>% dplyr::select(-c(long,lat)), inicio = 13, pliegues = pliegues, variables = variables)
#> # A tibble: 26,299 x 32 #> id pliegue delitos_last_ye… delitos_last_12 delitos_last_6 delitos_last_3 #> <chr> <int> <dbl> <dbl> <dbl> <dbl> #> 1 esqui… 1 1 73 41 20 #> 2 esqui… 1 5 106 47 19 #> 3 esqui… 1 4 25 11 4 #> 4 esqui… 1 0 4 1 1 #> 5 esqui… 1 0 31 16 7 #> 6 esqui… 1 1 11 6 3 #> 7 esqui… 1 1 16 11 4 #> 8 esqui… 1 1 19 8 5 #> 9 esqui… 1 0 9 6 4 #> 10 esqui… 1 3 27 14 10 #> # … with 26,289 more rows, and 26 more variables: delitos_last_1 <dbl>, #> # delitos <dbl>, temperatura_last_year <dbl>, temperatura_last_12 <dbl>, #> # temperatura_last_6 <dbl>, temperatura_last_3 <dbl>, #> # temperatura_last_1 <dbl>, temperatura <dbl>, mm_last_year <dbl>, #> # mm_last_12 <dbl>, mm_last_6 <dbl>, mm_last_3 <dbl>, mm_last_1 <dbl>, #> # mm <dbl>, dias_last_year <dbl>, dias_last_12 <dbl>, dias_last_6 <dbl>, #> # dias_last_3 <dbl>, dias_last_1 <dbl>, dias <dbl>, veloc_last_year <dbl>, #> # veloc_last_12 <dbl>, veloc_last_6 <dbl>, veloc_last_3 <dbl>, #> # veloc_last_1 <dbl>, veloc <dbl>