Skip to contents

Unpack state. You will see state come out of dust2 systems in several places, for example dust_system_state, but it will typically be an unstructured vector with no names; this is not very useful! However, your model knows what each element, or group of elements "means". You can unpack your state from this unstructured array into a named list using this function. The same idea applies to the higher-dimensional arrays that you might get if your system has multiple particles, multiple parameter groups or has been run for multiple time steps.

Usage

dust_unpack_state(obj, state)

dust_unpack_index(obj)

Arguments

obj

A dust_system object (from dust_system_create) or dust_likelihood object (from dust_filter_create or dust_unfilter_create).

state

A state vector, matrix or array. This might have come from dust_system_state, dust_likelihood_last_trajectories, or dust_likelihood_last_state.

Value

A named list, where each element corresponds to a logical compartment.

See also

monty::monty_packer, and within that especially documentation for $unpack(), which powers this function.

Examples

sir <- dust_example("sir")
sys <- dust_system_create(sir, list(), n_particles = 10, dt = 0.25)
dust_system_set_state_initial(sys)
t <- seq(0, 100, by = 5)
y <- dust_system_simulate(sys, t)
# The result here is a 5 x 10 x 21 matrix: 5 states by 10 particles by
# 21 times.
dim(y)
#> [1]  5 10 21

# The 10 particles and 21 times (following t) are simple enough, but
# what are our 5 compartments?

# You can use dust_unpack_state() to reshape your output as a
# list:
dust_unpack_state(sys, y)
#> $S
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]  990  972  938  888  826  751  658  576  472   402   350   304   270
#>  [2,]  990  979  956  923  873  798  713  623  530   423   359   311   275
#>  [3,]  990  985  965  938  871  794  720  647  549   470   390   328   287
#>  [4,]  990  984  961  928  882  817  732  640  552   476   415   373   335
#>  [5,]  990  983  967  949  936  913  880  840  791   733   681   621   571
#>  [6,]  990  979  958  935  897  817  718  605  515   433   364   319   276
#>  [7,]  990  979  951  924  872  802  705  597  503   417   350   286   253
#>  [8,]  990  981  966  943  911  869  836  784  700   626   546   471   420
#>  [9,]  990  978  961  936  890  815  731  644  552   460   391   344   309
#> [10,]  990  974  935  881  818  744  662  560  477   422   370   313   288
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#>  [1,]   249   236   230   221   219   218   217   216
#>  [2,]   245   227   217   212   209   205   202   198
#>  [3,]   256   238   216   204   195   188   187   186
#>  [4,]   306   294   274   259   253   242   240   238
#>  [5,]   507   441   402   367   351   334   315   286
#>  [6,]   244   212   197   190   174   168   166   163
#>  [7,]   230   203   189   175   164   160   159   155
#>  [8,]   378   350   316   289   259   245   230   220
#>  [9,]   275   260   250   243   236   228   218   210
#> [10,]   262   243   228   221   211   208   207   207
#> 
#> $I
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]   10   24   37   62   86  119  155  170  195   172   133   121   103
#>  [2,]   10   13   26   48   77  100  126  157  170   186   158   134   108
#>  [3,]   10    9   25   36   77  114  113  136  162   158   156   148   129
#>  [4,]   10   10   28   41   67   95  121  140  165   155   133   120   107
#>  [5,]   10    8   21   26   19   30   46   52   67    84    99   102   102
#>  [6,]   10   16   22   33   53   98  152  176  190   180   166   140   118
#>  [7,]   10   14   37   47   76  104  147  190  182   184   162   148   113
#>  [8,]   10   12   22   30   37   59   62   79  118   124   132   145   129
#>  [9,]   10   15   26   36   62  106  134  153  165   184   149   132   103
#> [10,]   10   25   46   69   89  115  140  167  161   149   130   121    95
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#>  [1,]    74    61    35    29    19    12     7     4
#>  [2,]    90    60    47    26    18    12     7     6
#>  [3,]   110    93    72    53    36    26    12     8
#>  [4,]    78    55    47    40    27    26    16    10
#>  [5,]   117   113    99    84    59    52    49    57
#>  [6,]   104    91    67    47    40    28    17    12
#>  [7,]    92    77    61    52    42    30    22    15
#>  [8,]   114    93    93    82    76    63    56    43
#>  [9,]    80    63    44    24    23    26    29    21
#> [10,]    79    64    51    40    24    15     8     3
#> 
#> $R
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    0    4   25   50   88  130  187  254  333   426   517   575   627
#>  [2,]    0    8   18   29   50  102  161  220  300   391   483   555   617
#>  [3,]    0    6   10   26   52   92  167  217  289   372   454   524   584
#>  [4,]    0    6   11   31   51   88  147  220  283   369   452   507   558
#>  [5,]    0    9   12   25   45   57   74  108  142   183   220   277   327
#>  [6,]    0    5   20   32   50   85  130  219  295   387   470   541   606
#>  [7,]    0    7   12   29   52   94  148  213  315   399   488   566   634
#>  [8,]    0    7   12   27   52   72  102  137  182   250   322   384   451
#>  [9,]    0    7   13   28   48   79  135  203  283   356   460   524   588
#> [10,]    0    1   19   50   93  141  198  273  362   429   500   566   617
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#>  [1,]   677   703   735   750   762   770   776   780
#>  [2,]   665   713   736   762   773   783   791   796
#>  [3,]   634   669   712   743   769   786   801   806
#>  [4,]   616   651   679   701   720   732   744   752
#>  [5,]   376   446   499   549   590   614   636   657
#>  [6,]   652   697   736   763   786   804   817   825
#>  [7,]   678   720   750   773   794   810   819   830
#>  [8,]   508   557   591   629   665   692   714   737
#>  [9,]   645   677   706   733   741   746   753   769
#> [10,]   659   693   721   739   765   777   785   790
#> 
#> $cases_cumul
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    0   18   52  102  164  239  332  414  518   588   640   686   720
#>  [2,]    0   11   34   67  117  192  277  367  460   567   631   679   715
#>  [3,]    0    5   25   52  119  196  270  343  441   520   600   662   703
#>  [4,]    0    6   29   62  108  173  258  350  438   514   575   617   655
#>  [5,]    0    7   23   41   54   77  110  150  199   257   309   369   419
#>  [6,]    0   11   32   55   93  173  272  385  475   557   626   671   714
#>  [7,]    0   11   39   66  118  188  285  393  487   573   640   704   737
#>  [8,]    0    9   24   47   79  121  154  206  290   364   444   519   570
#>  [9,]    0   12   29   54  100  175  259  346  438   530   599   646   681
#> [10,]    0   16   55  109  172  246  328  430  513   568   620   677   702
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#>  [1,]   741   754   760   769   771   772   773   774
#>  [2,]   745   763   773   778   781   785   788   792
#>  [3,]   734   752   774   786   795   802   803   804
#>  [4,]   684   696   716   731   737   748   750   752
#>  [5,]   483   549   588   623   639   656   675   704
#>  [6,]   746   778   793   800   816   822   824   827
#>  [7,]   760   787   801   815   826   830   831   835
#>  [8,]   612   640   674   701   731   745   760   770
#>  [9,]   715   730   740   747   754   762   772   780
#> [10,]   728   747   762   769   779   782   783   783
#> 
#> $cases_inc
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    0    4   11   12   15   19   23   21   17    11     8     9     5
#>  [2,]    0    2    6    9   14   13   22   26   26    19    10     6     9
#>  [3,]    0    2    6    5   12   20   12   19   21    10    17    12     6
#>  [4,]    0    1    4    9    9   18   18   21   25    14     9     5     6
#>  [5,]    0    2    4    7    2    3    5    6    9    11    12    11    13
#>  [6,]    0    3    6    8   12   18   25   17   23    13     8     6    10
#>  [7,]    0    4   11    9   11   19   24   26   16    12    10    13     6
#>  [8,]    0    1    4    7    6    9    9   12   19    16    13    17     6
#>  [9,]    0    2    3    6   11   19   17   14   17    19     8    13     5
#> [10,]    0    2   14   12   18   18   12   16   16    16    11     6     4
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#>  [1,]     4     3     2     2     1     0     0     0
#>  [2,]     7     2     2     2     0     2     0     0
#>  [3,]     6     3     6     0     1     1     0     0
#>  [4,]     3     2     4     2     1     3     0     0
#>  [5,]    19    13    10     7     3     1     2     4
#>  [6,]     8     7     1     4     1     2     0     0
#>  [7,]     1     6     2     2     3     1     0     1
#>  [8,]    10     7     9     5     7     3     1     2
#>  [9,]     7     1     3     1     2     4     3     2
#> [10,]     3     4     5     2     1     0     0     0
#> 

# Here, the list is named following the compartments (S, I, R,
# etc) and is a 10 x 21 matrix (i.e., the remaining dimensions
# from y)

# We could apply this to the final state, which converts a 5 x 10
# matrix of state into a 5 element list of vectors, each with
# length 10:
s <- dust_system_state(sys)
dim(s)
#> [1]  5 10
dust_unpack_state(sys, s)
#> $S
#>  [1] 216 198 186 238 286 163 155 220 210 207
#> 
#> $I
#>  [1]  4  6  8 10 57 12 15 43 21  3
#> 
#> $R
#>  [1] 780 796 806 752 657 825 830 737 769 790
#> 
#> $cases_cumul
#>  [1] 774 792 804 752 704 827 835 770 780 783
#> 
#> $cases_inc
#>  [1] 0 0 0 0 4 0 1 2 2 0
#> 

# If you need more control, you can use 'dust_unpack_index' to map
# names to positions within the state dimension of this array
dust_unpack_index(sys)
#> $S
#> [1] 1
#> 
#> $I
#> [1] 2
#> 
#> $R
#> [1] 3
#> 
#> $cases_cumul
#> [1] 4
#> 
#> $cases_inc
#> [1] 5
#>