Prepare data for use with the particle_filter. This function is required to use the particle filter as helps arrange data and be explicit about the off-by-one errors that can occur. It takes as input your data to compare against a model, including some measure of "time". We need to convert this time into model steps.

particle_filter_data(data, time, rate, initial_time = NULL, population = NULL)

## Arguments

data

A data.frame() of data

time

The name of a column within data that represents your measure of time. This column must be integer-like

rate

The number of model "steps" that occur between each time point (in time). This must also be integer-like

initial_time

Optionally, an initial time to start the model from. Provide this if you need to burn the model in, or if there is a long period with no data at the beginning of the simulation. If provided, it must be a non-negative integer and must be at most equal to the first value of the time column, minus 1 (i.e., data[[time]] - 1).

population

Optionally, the name of a column within data that represents different populations. Must be a factor.

## Value

If population is NULL, a data.frame with new columns step_start and step_end (required by particle_filter), along side all previous data except for the time variable, which is replaced by new <time>_start and <time>_endcolumns. If population is not NULL then a named list of data.frames as described above where each element represents populations in the order specified in the data.

## Details

We require that the time variable increments in unit steps; this may be relaxed in future to even steps, or possibly irregular steps, but for now this assumption is required. We assume that the data in the first column is recorded at the end of a period of 1 time unit. So if you have in the first column time = 10, data = 100 we assume that the model steps from time 9 to to time 10 and at that period the data has value 100.

## Examples

d <- data.frame(day = 5:20, y = runif(16))
mcstate::particle_filter_data(d, "day", 4)
#>    day_start day_end step_start step_end         y
#> 1          4       5         16       20 0.4227366
#> 2          5       6         20       24 0.5724237
#> 3          6       7         24       28 0.4448950
#> 4          7       8         28       32 0.8541564
#> 5          8       9         32       36 0.8020746
#> 6          9      10         36       40 0.3864496
#> 7         10      11         40       44 0.6633784
#> 8         11      12         44       48 0.6008278
#> 9         12      13         48       52 0.1260871
#> 10        13      14         52       56 0.6262046
#> 11        14      15         56       60 0.7566720
#> 12        15      16         60       64 0.6125216
#> 13        16      17         64       68 0.2149079
#> 14        17      18         68       72 0.4762703
#> 15        18      19         72       76 0.9975840
#> 16        19      20         76       80 0.2478263

# If providing an initial day, then the first epoch of simulation
# will be longer (see the first row)
mcstate::particle_filter_data(d, "day", 4, 0)
#>    day_start day_end step_start step_end         y
#> 1          0       5          0       20 0.4227366
#> 2          5       6         20       24 0.5724237
#> 3          6       7         24       28 0.4448950
#> 4          7       8         28       32 0.8541564
#> 5          8       9         32       36 0.8020746
#> 6          9      10         36       40 0.3864496
#> 7         10      11         40       44 0.6633784
#> 8         11      12         44       48 0.6008278
#> 9         12      13         48       52 0.1260871
#> 10        13      14         52       56 0.6262046
#> 11        14      15         56       60 0.7566720
#> 12        15      16         60       64 0.6125216
#> 13        16      17         64       68 0.2149079
#> 14        17      18         68       72 0.4762703
#> 15        18      19         72       76 0.9975840
#> 16        19      20         76       80 0.2478263

# If including populations:
d <- data.frame(day = 5:20, y = runif(16),
population = factor(rep(letters[1:2], each = 16)))
mcstate::particle_filter_data(d, "day", 4, 0, "population")
#>    day_start day_end step_start step_end         y population
#> 1          0       5          0       20 0.2776957          a
#> 2          5       6         20       24 0.7709844          a
#> 3          6       7         24       28 0.8433513          a
#> 4          7       8         28       32 0.7186267          a
#> 5          8       9         32       36 0.7591982          a
#> 6          9      10         36       40 0.5648261          a
#> 7         10      11         40       44 0.1787645          a
#> 8         11      12         44       48 0.8043700          a
#> 9         12      13         48       52 0.1671991          a
#> 10        13      14         52       56 0.6295686          a
#> 11        14      15         56       60 0.7021172          a
#> 12        15      16         60       64 0.3351547          a
#> 13        16      17         64       68 0.3127220          a
#> 14        17      18         68       72 0.1493619          a
#> 15        18      19         72       76 0.9177476          a
#> 16        19      20         76       80 0.7546986          a
#> 17         0       5          0       20 0.2776957          b
#> 18         5       6         20       24 0.7709844          b
#> 19         6       7         24       28 0.8433513          b
#> 20         7       8         28       32 0.7186267          b
#> 21         8       9         32       36 0.7591982          b
#> 22         9      10         36       40 0.5648261          b
#> 23        10      11         40       44 0.1787645          b
#> 24        11      12         44       48 0.8043700          b
#> 25        12      13         48       52 0.1671991          b
#> 26        13      14         52       56 0.6295686          b
#> 27        14      15         56       60 0.7021172          b
#> 28        15      16         60       64 0.3351547          b
#> 29        16      17         64       68 0.3127220          b
#> 30        17      18         68       72 0.1493619          b
#> 31        18      19         72       76 0.9177476          b
#> 32        19      20         76       80 0.7546986          b