Prepare data for use with the $set_data()
method. This is not
required for use but tries to simplify the most common use case
where you have a data.frame with some column indicating "dust
time step" (name_time
), and other columns that might be use in
your data_compare
function. Each row will be turned into a named
R list, which your dust_data
function can then work with to get
this time-steps values. See Details for use with multi-pars
objects.
dust_data(object, name_time = "time", multi = NULL)
An object, at this point must be a data.frame
The name of the data column within object
; this
column must be integer-like and every integer must be
nonnegative and unique
Control how to interpret data for multi-parameter dust object; see Details
A list of dust time/data pairs that will be used for the compare function in a compiled model. Each element is a list of length two or more where the first element is the time step and the subsequent elements are data for that time step.
Note that here "dust time step" (name_time
) refers to the dust
time step (which will be a non-negative integer) and not the
rescaled value of time that you probably use within the model. See
dust_generator for more information.
The data object as accepted by data_set
must be a list and
each element must itself be a list with two elements; the dust
time
at which the data applies and any R object that corresponds
to data at that point. We expect that most of the time this second
element will be a key-value list with scalar keys, but more
flexibility may be required.
For multi-data objects, the final format is a bit more awkward;
each time step we have a list with elements time
, data_1
,
data_2
, ..., data_n
for n
parameters. There are two ways of
creating this that might be useful: sharing the data across all
parameters and using some column as a grouping value.
The behaviour here is driven by the multi
argument;
NULL
: (the default) do nothing; this creates an object that
is suitable for use with a pars_multi = FALSE
dust
object.
<integer>
(e.g., multi = 3); share the data across 3 sets of
parameters. This number must match the number of parameter sets
that your dust object is created with
<column_name>
(e.g., multi = "country"); the name of a column
within your data to split the data at. This column must be a
factor, and that factor must have levels that map to integers 1,
2, ..., n (e.g., unique(as.integer(object[[multi]]))
returns
the integers 1:n
).
d <- data.frame(time = seq(0, 50, by = 10), a = runif(6), b = runif(6))
dust::dust_data(d)
#> [[1]]
#> [[1]][[1]]
#> [1] 0
#>
#> [[1]][[2]]
#> [[1]][[2]]$time
#> [1] 0
#>
#> [[1]][[2]]$a
#> [1] 0.9970691
#>
#> [[1]][[2]]$b
#> [1] 0.5470434
#>
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] 10
#>
#> [[2]][[2]]
#> [[2]][[2]]$time
#> [1] 10
#>
#> [[2]][[2]]$a
#> [1] 0.1490355
#>
#> [[2]][[2]]$b
#> [1] 0.8348018
#>
#>
#>
#> [[3]]
#> [[3]][[1]]
#> [1] 20
#>
#> [[3]][[2]]
#> [[3]][[2]]$time
#> [1] 20
#>
#> [[3]][[2]]$a
#> [1] 0.5185566
#>
#> [[3]][[2]]$b
#> [1] 0.02795603
#>
#>
#>
#> [[4]]
#> [[4]][[1]]
#> [1] 30
#>
#> [[4]][[2]]
#> [[4]][[2]]$time
#> [1] 30
#>
#> [[4]][[2]]$a
#> [1] 0.8461201
#>
#> [[4]][[2]]$b
#> [1] 0.4693843
#>
#>
#>
#> [[5]]
#> [[5]][[1]]
#> [1] 40
#>
#> [[5]][[2]]
#> [[5]][[2]]$time
#> [1] 40
#>
#> [[5]][[2]]$a
#> [1] 0.7182697
#>
#> [[5]][[2]]$b
#> [1] 0.80568
#>
#>
#>
#> [[6]]
#> [[6]][[1]]
#> [1] 50
#>
#> [[6]][[2]]
#> [[6]][[2]]$time
#> [1] 50
#>
#> [[6]][[2]]$a
#> [1] 0.241314
#>
#> [[6]][[2]]$b
#> [1] 0.8140513
#>
#>
#>