Example Pipeline
example-pipeline.Rmd
library(conmat)
This vignette outlines a basic workflow of:
- Create a new synthetic matrix by extrapolating from POLYMOD data to a new age distribution
- Generating a Next Generation Matrix
- Applying Vaccination Rates
- Comparing R0 before and post vaccination rates
Create a new synthetic matrix from all POLYMOD data
We can create a synthetic matrix from all POLYMOD data by using the
extrapolate_polymod
function. First, let’s extract an age
distribution from the ABS data.
fairfield <- abs_age_lga("Fairfield (C)")
fairfield
#> # A tibble: 18 × 4 (conmat_population)
#> - age: lower.age.limit
#> - population: population
#> lga lower.age.limit year population
#> <chr> <dbl> <dbl> <dbl>
#> 1 Fairfield (C) 0 2020 12261
#> 2 Fairfield (C) 5 2020 13093
#> 3 Fairfield (C) 10 2020 13602
#> 4 Fairfield (C) 15 2020 14323
#> 5 Fairfield (C) 20 2020 15932
#> 6 Fairfield (C) 25 2020 16190
#> 7 Fairfield (C) 30 2020 14134
#> 8 Fairfield (C) 35 2020 13034
#> 9 Fairfield (C) 40 2020 12217
#> 10 Fairfield (C) 45 2020 13449
#> 11 Fairfield (C) 50 2020 13419
#> 12 Fairfield (C) 55 2020 13652
#> 13 Fairfield (C) 60 2020 12907
#> 14 Fairfield (C) 65 2020 10541
#> 15 Fairfield (C) 70 2020 8227
#> 16 Fairfield (C) 75 2020 5598
#> 17 Fairfield (C) 80 2020 4006
#> 18 Fairfield (C) 85 2020 4240
Note that this is a conmat_population
object, which is
just a data frame that knows which columns represent the
age
and population
information.
We then extrapolate this to home, work, school, other and all settings, using the full POLYMOD data. This gives us a setting prediction matrix.
synthetic_fairfield_5y <- extrapolate_polymod(
population = fairfield
)
synthetic_fairfield_5y
#>
#> ── Setting Prediction Matrices ─────────────────────────────────────────────────
#> A list of matrices containing the model predicted contact rate between ages in
#> each setting.
#> • home: a 16x16 <matrix>
#> • work: a 16x16 <matrix>
#> • school: a 16x16 <matrix>
#> • other: a 16x16 <matrix>
#> • all: a 16x16 <matrix>
#> ℹ Access each <matrix> with `x$name`
#> ℹ e.g., `x$home`
synthetic_fairfield_5y$home
#> [0,5) [5,10) [10,15) [15,20) [20,25) [25,30)
#> [0,5) 0.51865034 0.42591293 0.21253741 0.14435655 0.20878566 0.39631863
#> [5,10) 0.45037500 0.72809875 0.47946904 0.17810939 0.11957496 0.20321044
#> [10,15) 0.23662636 0.50481805 0.82915369 0.43830202 0.15539542 0.11386444
#> [15,20) 0.17148825 0.20009270 0.46767441 0.75053183 0.38027025 0.14276361
#> [20,25) 0.26656804 0.14437574 0.17820418 0.40869741 0.65624200 0.36266610
#> [25,30) 0.51055254 0.24756458 0.13175172 0.15481591 0.36592787 0.59325487
#> [30,35) 0.64344136 0.50106655 0.23712829 0.12022852 0.14552109 0.32667414
#> [35,40) 0.44712318 0.60788787 0.46033541 0.20794794 0.10962513 0.12752173
#> [40,45) 0.23145177 0.39334181 0.52413078 0.37987407 0.17896223 0.09160188
#> [45,50) 0.15825964 0.20199612 0.33931078 0.43360418 0.32903240 0.15226089
#> [50,55) 0.16864816 0.13900097 0.17609777 0.28162880 0.37542529 0.28253283
#> [55,60) 0.19740388 0.14381353 0.11738659 0.14014249 0.23326665 0.31217361
#> [60,65) 0.17325617 0.15192784 0.10754708 0.08123470 0.09992246 0.16971767
#> [65,70) 0.10634663 0.11819894 0.09758387 0.06235041 0.04690121 0.06058447
#> [70,75) 0.05640688 0.06898038 0.07006069 0.05183176 0.03329360 0.02796858
#> [75,Inf) 0.05435706 0.06613422 0.06978553 0.06493144 0.05721213 0.05042167
#> [30,35) [35,40) [40,45) [45,50) [50,55) [55,60)
#> [0,5) 0.54212027 0.41470167 0.22375820 0.14821211 0.15227026 0.17701604
#> [5,10) 0.44641167 0.59619115 0.40210735 0.20003684 0.13271034 0.13636727
#> [10,15) 0.22243227 0.47534697 0.56413866 0.35378459 0.17701705 0.11719341
#> [15,20) 0.12033503 0.22911900 0.43627065 0.48239729 0.30207059 0.14928791
#> [20,25) 0.15653809 0.12981537 0.22089569 0.39342286 0.43277726 0.26706502
#> [25,30) 0.35456624 0.15236624 0.11408243 0.18369520 0.32862330 0.36061938
#> [30,35) 0.51508971 0.30411138 0.11932991 0.08598598 0.14278678 0.26084942
#> [35,40) 0.27625538 0.43776483 0.24492742 0.09429733 0.07160585 0.12282759
#> [40,45) 0.10399622 0.23497820 0.38613043 0.22136642 0.09093122 0.07055105
#> [45,50) 0.07735721 0.09338874 0.22851604 0.40480620 0.24677248 0.10164315
#> [50,55) 0.13324201 0.07355693 0.09736390 0.25596273 0.47109723 0.28201873
#> [55,60) 0.24508663 0.12704208 0.07606152 0.10615358 0.28395825 0.51946516
#> [60,65) 0.24216940 0.20806274 0.11263345 0.06883538 0.09816925 0.27060460
#> [65,70) 0.11385151 0.17651258 0.15207814 0.08074963 0.05062331 0.07760322
#> [70,75) 0.04117526 0.08243239 0.12565898 0.10619170 0.05889077 0.04008722
#> [75,Inf) 0.04628606 0.05280563 0.08532641 0.13899657 0.17174000 0.15772157
#> [60,65) [65,70) [70,75) [75,Inf)
#> [0,5) 0.16521432 0.11982273 0.08265848 0.05014750
#> [5,10) 0.15319685 0.14082590 0.10688934 0.06451683
#> [10,15) 0.11417878 0.12241123 0.11430297 0.07167810
#> [15,20) 0.09202344 0.08345506 0.09022964 0.07116170
#> [20,25) 0.12165488 0.06746941 0.06229074 0.06738897
#> [25,30) 0.20848845 0.08793722 0.05279851 0.05992479
#> [30,35) 0.27408898 0.15225361 0.07161516 0.05068236
#> [35,40) 0.21391671 0.21442852 0.13024007 0.05252487
#> [40,45) 0.11109844 0.17724085 0.19047169 0.08142511
#> [45,50) 0.07009018 0.09714993 0.16616227 0.13692537
#> [50,55) 0.10368143 0.06317315 0.09558045 0.17548148
#> [55,60) 0.28776451 0.09750756 0.06550952 0.16226599
#> [60,65) 0.49195202 0.26740002 0.09904800 0.11104505
#> [65,70) 0.22631063 0.42771285 0.24379777 0.08078800
#> [70,75) 0.06445392 0.18745190 0.34638031 0.11508892
#> [75,Inf) 0.11477965 0.09866634 0.18280815 0.28973562
By full POLYMOD data, we mean these data:
polymod_setting <- get_polymod_setting_data()
polymod_population <- get_polymod_population()
polymod_setting
#>
#> ── Setting Data ────────────────────────────────────────────────────────────────
#> A list of <data.frame>s containing the number of contacts between ages in each
#> setting.
#> • home: a 8,787x5 <data.frame>
#> • work: a 8,787x5 <data.frame>
#> • school: a 8,787x5 <data.frame>
#> • other: a 8,787x5 <data.frame>
#> ℹ Access each <data.frame> with `x$name`
#> ℹ e.g., `x$home`
polymod_setting$home
#> # A tibble: 8,787 × 5
#> setting age_from age_to contacts participants
#> <chr> <int> <dbl> <int> <int>
#> 1 home 0 0 10 92
#> 2 home 0 1 7 92
#> 3 home 0 2 11 92
#> 4 home 0 3 15 92
#> 5 home 0 4 12 92
#> 6 home 0 5 6 92
#> 7 home 0 6 8 92
#> 8 home 0 7 9 92
#> 9 home 0 8 6 92
#> 10 home 0 9 6 92
#> # … with 8,777 more rows
polymod_population
#> # A tibble: 21 × 2 (conmat_population)
#> - age: lower.age.limit
#> - population: population
#> lower.age.limit population
#> <int> <dbl>
#> 1 0 1852682.
#> 2 5 1968449.
#> 3 10 2138897.
#> 4 15 2312032.
#> 5 20 2407486.
#> 6 25 2423602.
#> 7 30 2585137.
#> 8 35 2969393.
#> 9 40 3041663.
#> 10 45 2809154.
#> # … with 11 more rows
The extrapolate_polymod()
function does the
following:
- Uses an already fit model (
polymod_setting_models
) of the contact rate to the full POLYMOD data above - Predicts it to the provided fairfield population data
It also has options to predict to specified age brackets, defaulting to 5 year age groups up to 75, then 75 and older.
This object, synthetic_fairfield_5y
, contains a matrix
of predictions for each of the settings, home, work, school, other, and
all settings, which is summarised when you print the object to the
console:
synthetic_fairfield_5y
#>
#> ── Setting Prediction Matrices ─────────────────────────────────────────────────
#> A list of matrices containing the model predicted contact rate between ages in
#> each setting.
#> • home: a 16x16 <matrix>
#> • work: a 16x16 <matrix>
#> • school: a 16x16 <matrix>
#> • other: a 16x16 <matrix>
#> • all: a 16x16 <matrix>
#> ℹ Access each <matrix> with `x$name`
#> ℹ e.g., `x$home`
You can see more detail by using str
if you like:
str(synthetic_fairfield_5y)
#> List of 5
#> $ home : 'conmat_prediction_matrix' num [1:16, 1:16] 0.519 0.45 0.237 0.171 0.267 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ work : 'conmat_prediction_matrix' num [1:16, 1:16] 0.00251 0.00376 0.00289 0.00414 0.01044 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ school: 'conmat_prediction_matrix' num [1:16, 1:16] 1.2044 0.335 0.0555 0.0335 0.0522 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ other : 'conmat_prediction_matrix' num [1:16, 1:16] 0.757 0.414 0.157 0.102 0.136 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ all : 'conmat_prediction_matrix' num [1:16, 1:16] 2.482 1.203 0.452 0.311 0.465 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:16] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> - attr(*, "class")= chr [1:2] "conmat_setting_prediction_matrix" "list"
Generating a Next Generation Matrix
Once infected, a person can transmit an infectious disease to another, creating generations of infected individuals. We can define a matrix describing the number of newly infected individuals in given categories, such as age, for consecutive generations. This matrix is called a “next generation matrix” (NGM).
We can generate an NGM for Australian specific data like so (in a future release we will change this to work for any predicted population)
ngm_fairfield <- generate_ngm(
lga_name = "Fairfield (C)",
age_breaks = c(seq(0, 80, by = 5), Inf),
R_target = 1.5
)
The output of this is a matrix for each of the settings, where each value is the number of newly infected individuals
ngm_fairfield$home
#> [0,5) [5,10) [10,15) [15,20) [20,25)
#> [0,5) 0.046932897 0.037728586 0.018246153 0.012332791 0.018264934
#> [5,10) 0.049756482 0.078731552 0.050235931 0.018570170 0.012768204
#> [10,15) 0.030815782 0.064337079 0.102367511 0.053846989 0.019554983
#> [15,20) 0.027244618 0.031102130 0.070397325 0.112413979 0.058355869
#> [20,25) 0.061273402 0.032446592 0.038745636 0.088406009 0.145546028
#> [25,30) 0.141125032 0.066868394 0.034401876 0.040212975 0.097510708
#> [30,35) 0.188523058 0.143424225 0.065594864 0.033082542 0.041088733
#> [35,40) 0.129957651 0.172617112 0.126332051 0.056767734 0.030707760
#> [40,45) 0.064505365 0.107117691 0.137976850 0.099478297 0.048080568
#> [45,50) 0.043327748 0.054040906 0.087758926 0.111561661 0.086846257
#> [50,55) 0.046857610 0.037737837 0.046216312 0.073526113 0.100554748
#> [55,60) 0.056707482 0.040363641 0.031843127 0.037816291 0.064585088
#> [60,65) 0.051593433 0.044196244 0.030231912 0.022714725 0.028672419
#> [65,70) 0.030842806 0.033491395 0.026722784 0.016984451 0.013109419
#> [70,75) 0.014913010 0.017823552 0.017503661 0.012882169 0.008487729
#> [75,80) 0.007533183 0.008748265 0.009167352 0.008282818 0.006525350
#> [80,Inf) 0.005764033 0.007061324 0.006974501 0.006653797 0.006946436
#> [25,30) [30,35) [35,40) [40,45) [45,50) [50,55)
#> [0,5) 0.035553728 0.049909162 0.039086807 0.021678420 0.01475638 0.01569532
#> [5,10) 0.022255286 0.050181751 0.068624590 0.047586123 0.02433257 0.01671722
#> [10,15) 0.014698823 0.029478009 0.064516802 0.078738373 0.05076640 0.02631255
#> [15,20) 0.022480484 0.019458760 0.037954874 0.074344522 0.08454498 0.05486635
#> [20,25) 0.082601637 0.036644860 0.031157311 0.054594180 0.10010748 0.11428886
#> [25,30) 0.162451177 0.099860682 0.044026974 0.033973252 0.05637003 0.10478736
#> [30,35) 0.094808673 0.153799306 0.093187087 0.037697219 0.02800133 0.04834149
#> [35,40) 0.036714913 0.081825642 0.133061939 0.076747746 0.03045758 0.02404324
#> [40,45) 0.025290200 0.029532311 0.068463047 0.115949999 0.06850174 0.02924100
#> [45,50) 0.041296038 0.021578321 0.026725521 0.067392877 0.12301340 0.07791638
#> [50,55) 0.077764616 0.037720732 0.021365120 0.029146092 0.07895949 0.15101375
#> [55,60) 0.088832697 0.071744923 0.038161790 0.023552141 0.03387932 0.09420130
#> [60,65) 0.050060880 0.073496380 0.064807991 0.036172941 0.02279123 0.03379717
#> [65,70) 0.017405106 0.033648813 0.053534811 0.047548500 0.02602388 0.01695985
#> [70,75) 0.007325699 0.011090385 0.022775096 0.035771993 0.03114331 0.01794044
#> [75,80) 0.005010072 0.004931920 0.007622938 0.015129757 0.02368369 0.02227803
#> [80,Inf) 0.007161837 0.006559838 0.005869340 0.007389144 0.01404959 0.02591348
#> [55,60) [60,65) [65,70) [70,75) [75,80) [80,Inf)
#> [0,5) 0.01897212 0.01857165 0.01390062 0.009784473 0.00747701 0.004797897
#> [5,10) 0.01786743 0.02106146 0.01998684 0.015482378 0.01149464 0.007780651
#> [10,15) 0.01812580 0.01853812 0.02052412 0.019563013 0.01549537 0.009885475
#> [15,20) 0.02823008 0.01828066 0.01712911 0.018911113 0.01839126 0.012389290
#> [20,25) 0.07355202 0.03527868 0.02024874 0.019111168 0.02223990 0.019858433
#> [25,30) 0.12010229 0.07326374 0.03202991 0.019680657 0.02038909 0.024453665
#> [30,35) 0.09229790 0.10242125 0.05901192 0.028419862 0.01915451 0.021379047
#> [35,40) 0.04309922 0.07926046 0.08239932 0.051238813 0.02599756 0.016798592
#> [40,45) 0.02369797 0.03937963 0.06512416 0.071626035 0.04592924 0.018825552
#> [45,50) 0.03351660 0.02438291 0.03502699 0.061305142 0.07070070 0.035202618
#> [50,55) 0.09442748 0.03663180 0.02313608 0.035824340 0.06749657 0.065908972
#> [55,60) 0.18006504 0.10530918 0.03700272 0.025448876 0.04247786 0.081343708
#> [60,65) 0.09738588 0.18702620 0.10546509 0.040004103 0.02883126 0.059028899
#> [65,70) 0.02716872 0.08365989 0.16397663 0.095689514 0.03598344 0.028972399
#> [70,75) 0.01274965 0.02161666 0.06513453 0.123132981 0.06799937 0.019718215
#> [75,80) 0.01401630 0.01025147 0.01610998 0.044723013 0.07914963 0.028393860
#> [80,Inf) 0.03196516 0.02498789 0.01544015 0.015436882 0.03379784 0.053720539
str(ngm_fairfield)
#> List of 5
#> $ home : 'conmat_prediction_matrix' num [1:17, 1:17] 0.0469 0.0498 0.0308 0.0272 0.0613 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ school: 'conmat_prediction_matrix' num [1:17, 1:17] 0.02924 0.00987 0.00192 0.0014 0.00308 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ work : 'conmat_prediction_matrix' num [1:17, 1:17] 6.10e-05 1.11e-04 9.98e-05 1.73e-04 6.15e-04 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ other : 'conmat_prediction_matrix' num [1:17, 1:17] 0.01837 0.0122 0.00543 0.00426 0.00801 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> $ all : 'conmat_prediction_matrix' num [1:17, 1:17] 0.0946 0.0719 0.0383 0.0331 0.073 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> .. ..$ : chr [1:17] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#> - attr(*, "class")= chr [1:2] "ngm_setting_matrix" "list"
Applying Vaccination Rates
It is important to understand the effect of vaccination on the next
generation of infections. We can use apply_vaccination()
to
return the percentage reduction in acquisition and transmission in each
age group.
It takes two key arguments:
- The next generation matrix
- The vaccination effect data
The vaccination effect could look like the following:
vaccination_effect_example_data
#> # A tibble: 17 × 4
#> age_band coverage acquisition transmission
#> <chr> <dbl> <dbl> <dbl>
#> 1 0-4 0 0 0
#> 2 5-11 0.782 0.583 0.254
#> 3 12-15 0.997 0.631 0.295
#> 4 16-19 0.965 0.786 0.469
#> 5 20-24 0.861 0.774 0.453
#> 6 25-29 0.997 0.778 0.458
#> 7 30-34 0.998 0.803 0.493
#> 8 35-39 0.998 0.829 0.533
#> 9 40-44 0.999 0.841 0.551
#> 10 45-49 0.993 0.847 0.562
#> 11 50-54 0.999 0.857 0.579
#> 12 55-59 0.996 0.864 0.591
#> 13 60-64 0.998 0.858 0.581
#> 14 65-69 0.999 0.864 0.591
#> 15 70-74 0.999 0.867 0.597
#> 16 75-79 0.999 0.866 0.595
#> 17 80+ 0.999 0.844 0.556
Each row contains information, for each age band:
- Coverage % vaccinated
- Acquisition - probability of acquiring COVID
- Transmission - the probability of transmission
Then you need to specify the columns in the vaccination effect data frame related to covarege, acquisition, and transmission.
# Apply vaccination effect to next generation matrices
ngm_nsw_vacc <- apply_vaccination(
ngm = ngm_fairfield,
data = vaccination_effect_example_data,
coverage_col = coverage,
acquisition_col = acquisition,
transmission_col = transmission
)
ngm_nsw_vacc
#>
#> ── Vaccination Setting Matrices ────────────────────────────────────────────────
#> A list of matrices, each <matrix> containing the adjusted number of newly
#> infected individuals for age groups. These numbers have been adjusted based on
#> proposed vaccination rates in age groups
#> • home: a 17x17 <matrix>
#> • school: a 17x17 <matrix>
#> • work: a 17x17 <matrix>
#> • other: a 17x17 <matrix>
#> • all: a 17x17 <matrix>
#> ℹ Access each <matrix> with `x$name`
#> ℹ e.g., `x$home`