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.
Arguments
- obj
A
dust_system
object (from dust_system_create) ordust_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.
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 976 964 937 898 856 810 759 686 606 528 460 390
#> [2,] 990 971 934 869 793 693 602 496 418 365 318 290 264
#> [3,] 990 980 965 948 918 875 833 771 688 602 514 435 375
#> [4,] 990 977 940 913 849 772 679 577 461 388 333 287 245
#> [5,] 990 974 930 888 819 754 678 599 512 441 392 358 330
#> [6,] 990 982 966 938 901 840 769 687 610 515 452 395 346
#> [7,] 990 973 946 893 841 782 710 644 586 509 449 408 367
#> [8,] 990 974 955 929 867 774 690 599 496 423 356 305 268
#> [9,] 990 977 964 935 899 856 809 732 617 515 436 360 317
#> [10,] 990 980 963 933 892 832 765 680 575 493 429 372 331
#> [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 347 307 276 254 232 220 212 200
#> [2,] 250 240 229 222 218 214 213 210
#> [3,] 328 291 267 251 240 232 228 223
#> [4,] 226 214 200 191 183 182 180 178
#> [5,] 301 276 258 248 242 237 227 223
#> [6,] 328 295 270 248 236 227 222 217
#> [7,] 331 306 279 260 249 237 232 230
#> [8,] 241 225 217 203 202 196 195 189
#> [9,] 283 257 243 232 221 211 206 203
#> [10,] 301 279 262 248 240 232 226 224
#>
#> $I
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] 10 18 23 39 53 67 74 81 107 128 141 144 141
#> [2,] 10 23 39 74 115 139 156 174 171 144 126 91 70
#> [3,] 10 14 22 24 30 54 63 91 118 138 159 171 140
#> [4,] 10 17 35 46 76 108 144 161 207 186 172 128 110
#> [5,] 10 20 41 59 89 107 109 135 152 137 117 96 76
#> [6,] 10 11 19 36 46 75 115 132 158 161 139 136 122
#> [7,] 10 19 34 59 71 86 102 107 99 132 127 106 96
#> [8,] 10 20 26 37 76 115 133 157 173 162 162 146 112
#> [9,] 10 16 22 34 46 53 74 114 160 170 179 165 131
#> [10,] 10 13 24 38 64 85 107 139 174 166 151 124 114
#> [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 113 107 89 65 51 43 34 32
#> [2,] 49 39 32 23 17 14 7 5
#> [3,] 114 97 78 52 41 31 24 20
#> [4,] 78 56 36 28 25 16 11 9
#> [5,] 69 64 56 43 34 27 25 18
#> [6,] 89 81 72 65 46 36 25 22
#> [7,] 92 75 76 65 45 36 28 20
#> [8,] 91 63 43 37 23 19 16 16
#> [9,] 102 82 74 55 48 36 25 14
#> [10,] 97 78 65 49 32 25 19 9
#>
#> $R
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] 0 6 13 24 49 77 116 160 207 266 331 396 469
#> [2,] 0 6 27 57 92 168 242 330 411 491 556 619 666
#> [3,] 0 6 13 28 52 71 104 138 194 260 327 394 485
#> [4,] 0 6 25 41 75 120 177 262 332 426 495 585 645
#> [5,] 0 6 29 53 92 139 213 266 336 422 491 546 594
#> [6,] 0 7 15 26 53 85 116 181 232 324 409 469 532
#> [7,] 0 8 20 48 88 132 188 249 315 359 424 486 537
#> [8,] 0 6 19 34 57 111 177 244 331 415 482 549 620
#> [9,] 0 7 14 31 55 91 117 154 223 315 385 475 552
#> [10,] 0 7 13 29 44 83 128 181 251 341 420 504 555
#> [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 540 586 635 681 717 737 754 768
#> [2,] 701 721 739 755 765 772 780 785
#> [3,] 558 612 655 697 719 737 748 757
#> [4,] 696 730 764 781 792 802 809 813
#> [5,] 630 660 686 709 724 736 748 759
#> [6,] 583 624 658 687 718 737 753 761
#> [7,] 577 619 645 675 706 727 740 750
#> [8,] 668 712 740 760 775 785 789 795
#> [9,] 615 661 683 713 731 753 769 783
#> [10,] 602 643 673 703 728 743 755 767
#>
#> $cases_cumul
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] 0 14 26 53 92 134 180 231 304 384 462 530 600
#> [2,] 0 19 56 121 197 297 388 494 572 625 672 700 726
#> [3,] 0 10 25 42 72 115 157 219 302 388 476 555 615
#> [4,] 0 13 50 77 141 218 311 413 529 602 657 703 745
#> [5,] 0 16 60 102 171 236 312 391 478 549 598 632 660
#> [6,] 0 8 24 52 89 150 221 303 380 475 538 595 644
#> [7,] 0 17 44 97 149 208 280 346 404 481 541 582 623
#> [8,] 0 16 35 61 123 216 300 391 494 567 634 685 722
#> [9,] 0 13 26 55 91 134 181 258 373 475 554 630 673
#> [10,] 0 10 27 57 98 158 225 310 415 497 561 618 659
#> [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 643 683 714 736 758 770 778 790
#> [2,] 740 750 761 768 772 776 777 780
#> [3,] 662 699 723 739 750 758 762 767
#> [4,] 764 776 790 799 807 808 810 812
#> [5,] 689 714 732 742 748 753 763 767
#> [6,] 662 695 720 742 754 763 768 773
#> [7,] 659 684 711 730 741 753 758 760
#> [8,] 749 765 773 787 788 794 795 801
#> [9,] 707 733 747 758 769 779 784 787
#> [10,] 689 711 728 742 750 758 764 766
#>
#> $cases_inc
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] 0 4 3 6 8 7 13 12 12 17 12 16 12
#> [2,] 0 3 9 17 16 18 10 21 11 5 7 5 2
#> [3,] 0 2 4 5 4 13 8 13 16 23 19 17 10
#> [4,] 0 5 7 7 15 20 22 23 22 13 8 4 9
#> [5,] 0 6 9 12 12 18 9 15 15 9 11 5 6
#> [6,] 0 2 2 7 6 13 21 18 21 21 13 7 10
#> [7,] 0 6 4 7 8 13 18 13 7 14 6 3 9
#> [8,] 0 2 4 5 7 21 22 14 21 12 10 15 4
#> [9,] 0 5 2 6 4 7 14 17 20 18 20 11 6
#> [10,] 0 1 4 5 5 11 18 12 24 20 16 10 9
#> [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 9 4 4 4 1 1 2 3
#> [2,] 3 2 1 3 0 0 0 0
#> [3,] 3 8 4 3 1 2 1 2
#> [4,] 2 0 2 3 4 0 1 0
#> [5,] 3 4 1 3 1 2 1 1
#> [6,] 2 7 6 4 1 1 0 1
#> [7,] 8 3 9 3 4 1 1 0
#> [8,] 3 1 2 6 0 0 0 2
#> [9,] 2 3 2 1 1 1 0 2
#> [10,] 6 6 4 3 0 2 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] 200 210 223 178 223 217 230 189 203 224
#>
#> $I
#> [1] 32 5 20 9 18 22 20 16 14 9
#>
#> $R
#> [1] 768 785 757 813 759 761 750 795 783 767
#>
#> $cases_cumul
#> [1] 790 780 767 812 767 773 760 801 787 766
#>
#> $cases_inc
#> [1] 3 0 2 0 1 1 0 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
#>