Skip to contents
library(conmat)
library(socialmixr)
#> 
#> Attaching package: 'socialmixr'
#> The following object is masked from 'package:utils':
#> 
#>     cite
library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(mgcv)
#> Loading required package: nlme
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
#> This is mgcv 1.8-41. For overview type 'help("mgcv-package")'.
library(patchwork)

The goal of conmat is to simplify the process of generating synthetic contact matrices for a given age population.

What is a contact matrix?

Contact matrices describe the degree of contact between individuals of given age groups. For example, this matrix describes the number of contacts between individuals.

#>       0-4 5-9 10-14
#> 0-4    10   3     4
#> 5-9     3  11     5
#> 10-14   4   5    13

The rows and columns represent the age groups of the people. On the main diagonal we see that we have a higher number of contacts - showing that people of similar ages tend to interact more with one another. We can use the information in these matrices to model how diseases such as COVID-19 spread in a population through social contact.

Why do we need synthetic contact matrices?

Contact matrices are produced from empirical data resulting from a contact survey, which requires individuals to diary the amount and manner of contact a person has in a day. However, these surveys are highly time-consuming and expensive to run, meaning that only a handful of these empirical datasets exist globally.

We can use statistical methods to create synthetic contact matrices, which are new contact matrices that have been generalised to new countries based on existing surveys.

Why do we need conmat?

Existing methods only provide outputs of the contact matrices for each country, or at best, for urban and rural areas for a given country. We need methods that allow for flexibly creating synthetic contact matrices for a specified age population. This is because the age population distribution of many countries (e.g., Australia), are quite heterogeneous, and assuming it is homogeneous would result in inaccurate representation of community infection in many regions.

Quick example using Australian data

Suppose we want to get a contact matrix for a given region in Australia, let’s say the city of Perth. We can get that from a helper function, abs_age_lga.

perth <- abs_age_lga("Perth (C)")
perth
#> # A tibble: 18 × 4 (conmat_population)
#>  - age: lower.age.limit
#>  - population: population
#>    lga       lower.age.limit  year population
#>    <chr>               <dbl> <dbl>      <dbl>
#>  1 Perth (C)               0  2020       1331
#>  2 Perth (C)               5  2020        834
#>  3 Perth (C)              10  2020        529
#>  4 Perth (C)              15  2020        794
#>  5 Perth (C)              20  2020       3615
#>  6 Perth (C)              25  2020       5324
#>  7 Perth (C)              30  2020       4667
#>  8 Perth (C)              35  2020       3110
#>  9 Perth (C)              40  2020       1650
#> 10 Perth (C)              45  2020       1445
#> 11 Perth (C)              50  2020       1299
#> 12 Perth (C)              55  2020       1344
#> 13 Perth (C)              60  2020       1359
#> 14 Perth (C)              65  2020       1145
#> 15 Perth (C)              70  2020       1004
#> 16 Perth (C)              75  2020        673
#> 17 Perth (C)              80  2020        481
#> 18 Perth (C)              85  2020        367

(You can learn more about the data sources we provide in data-sources.Rmd)

We can get a contact matrix made for perth using the extrapolate_polymod function:

perth_contact <- extrapolate_polymod(
  population = perth
)

perth_contact
#> 
#> ── 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`

We can plot this with autoplot

autoplot(perth_contact)

And you can see each contact matrix in a setting by referring to its name - for example, the “home” setting contact matrix:

perth_contact$home
#>               [0,5)     [5,10)    [10,15)    [15,20)    [20,25)    [25,30)
#> [0,5)    0.39743272 0.32858156 0.16266890 0.11549597 0.17737726 0.32337782
#> [5,10)   0.19517061 0.31631444 0.20405822 0.07308499 0.05366436 0.09450085
#> [10,15)  0.07340049 0.15501627 0.26262315 0.13052669 0.04629618 0.03647250
#> [15,20)  0.08129556 0.08660778 0.20361271 0.34617980 0.17117933 0.06733926
#> [20,25)  0.33520290 0.17073579 0.19389231 0.45958005 0.75033980 0.43371301
#> [25,30)  1.12422936 0.55310701 0.28100613 0.33259296 0.79787952 1.25287546
#> [30,35)  1.46981373 1.16733001 0.53958673 0.26907742 0.34942848 0.77286620
#> [35,40)  0.75228654 1.03184699 0.76692996 0.32500119 0.17969761 0.22452945
#> [40,45)  0.24714099 0.42175817 0.56434159 0.38442657 0.17519410 0.09664137
#> [45,50)  0.12258162 0.15477531 0.26467697 0.33499218 0.24021233 0.11346046
#> [50,55)  0.11634877 0.09453224 0.11980486 0.19681878 0.25441570 0.18843147
#> [55,60)  0.13583681 0.09907539 0.07972905 0.09715725 0.16291241 0.21212824
#> [60,65)  0.12409809 0.11019331 0.07728851 0.05785383 0.07334905 0.12295444
#> [65,70)  0.08047604 0.09043208 0.07481086 0.04645367 0.03568280 0.04680601
#> [70,75)  0.04563800 0.05602698 0.05732976 0.04134836 0.02651310 0.02292755
#> [75,Inf) 0.04426322 0.05402899 0.05735758 0.05301371 0.04629475 0.04067223
#>             [30,35)    [35,40)    [40,45)    [45,50)    [50,55)    [55,60)
#> [0,5)    0.42309386 0.32249828 0.17482882 0.11643874 0.12111968 0.14046086
#> [5,10)   0.19959024 0.26274269 0.17721602 0.08732629 0.05845263 0.06085201
#> [10,15)  0.07008581 0.14835228 0.18013774 0.11344430 0.05627581 0.03720051
#> [15,20)  0.05451947 0.09806839 0.19141749 0.22397873 0.14421799 0.07071525
#> [20,25)  0.19008268 0.14557817 0.23420575 0.43119851 0.50050295 0.31834779
#> [25,30)  0.77343430 0.33462789 0.23767091 0.37468074 0.68194766 0.76257233
#> [30,35)  1.18337183 0.71235629 0.27862083 0.19726013 0.32950674 0.60259651
#> [35,40)  0.47832995 0.74598235 0.41641836 0.15812553 0.12221004 0.21381367
#> [40,45)  0.11337564 0.25235141 0.41451479 0.23314619 0.09520851 0.07604271
#> [45,50)  0.05977803 0.07136314 0.17362987 0.31201559 0.18803938 0.07732844
#> [50,55)  0.09111415 0.05032666 0.06469804 0.17158054 0.31976171 0.19028456
#> [55,60)  0.16775025 0.08864244 0.05202211 0.07103519 0.19156607 0.35290686
#> [60,65)  0.17318176 0.15183317 0.08300011 0.04953457 0.06990436 0.19371084
#> [65,70)  0.08608739 0.13403123 0.11850338 0.06277680 0.03878107 0.05928730
#> [70,75)  0.03335757 0.06575363 0.10223365 0.08777977 0.04847661 0.03283896
#> [75,Inf) 0.03731198 0.04258510 0.06921557 0.11451449 0.14108751 0.12662847
#>             [60,65)    [65,70)    [70,75)   [75,Inf)
#> [0,5)    0.12923192 0.09276131 0.06398890 0.03949403
#> [5,10)   0.06816025 0.06191473 0.04666018 0.02863430
#> [10,15)  0.03631734 0.03890983 0.03627042 0.02309264
#> [15,20)  0.04240693 0.03768949 0.04080715 0.03329481
#> [20,25)  0.14434743 0.07772649 0.07025032 0.07806026
#> [25,30)  0.44513693 0.18756273 0.11175844 0.12616273
#> [30,35)  0.62651639 0.34471913 0.16247928 0.11565443
#> [35,40)  0.36883088 0.36038124 0.21505704 0.08863433
#> [40,45)  0.12218423 0.19309132 0.20262997 0.08730195
#> [45,50)  0.05430515 0.07617760 0.12956877 0.10756643
#> [50,55)  0.06992880 0.04294051 0.06529161 0.12092723
#> [55,60)  0.19508361 0.06608822 0.04452763 0.10926521
#> [60,65)  0.35290261 0.19172258 0.07114219 0.07726893
#> [65,70)  0.17321190 0.32753478 0.18703148 0.06145177
#> [70,75)  0.05283892 0.15375773 0.28357109 0.09684605
#> [75,Inf) 0.09018228 0.07938642 0.15218488 0.24365406

These contact matrices could then be used in subsequent modelling, such as the input in a SIR (Suscepible, Infected, Recovered) model.

Quick example using world data

Similarly to above, we can access some world data from another data source - we have some helpers to pull data from the world population:

world_data <- socialmixr::wpp_age()

head(world_data)
#>   country lower.age.limit year population
#> 1  AFRICA               0 1950   38705049
#> 2  AFRICA               0 1955   44304214
#> 3  AFRICA               0 1960   50491493
#> 4  AFRICA               0 1965   57690110
#> 5  AFRICA               0 1970   65452837
#> 6  AFRICA               0 1975   75017430

We can tidy the data up, filtering down to a specified location and year with the age_population function:

nz_2015 <- age_population(
  data = world_data,
  location_col = country,
  location = "New Zealand",
  age_col = lower.age.limit,
  year_col = year,
  year = 2015
)

nz_2015
#> # A tibble: 21 × 5 (conmat_population)
#>  - age: lower.age.limit
#>  - population: population
#>    country      year population lower.age.limit upper.age.limit
#>    <chr>       <int>      <dbl>           <dbl>           <dbl>
#>  1 New Zealand  2015     308681               0               4
#>  2 New Zealand  2015     315632               5               9
#>  3 New Zealand  2015     297247              10              14
#>  4 New Zealand  2015     318932              15              19
#>  5 New Zealand  2015     339362              20              24
#>  6 New Zealand  2015     315914              25              29
#>  7 New Zealand  2015     287537              30              34
#>  8 New Zealand  2015     273539              35              39
#>  9 New Zealand  2015     312245              40              44
#> 10 New Zealand  2015     313312              45              49
#> # … with 11 more rows

Then we could create a contact matrix for NZ population data for 2015 like so:

nz_contact <- extrapolate_polymod(
  population = nz_2015
)
autoplot(nz_contact)

nz_contact$home
#>               [0,5)     [5,10)    [10,15)    [15,20)    [20,25)    [25,30)
#> [0,5)    0.59372117 0.48807593 0.24377912 0.16584841 0.23975317 0.45548642
#> [5,10)   0.48735618 0.78767181 0.51898987 0.19288384 0.12966106 0.22071700
#> [10,15)  0.24394805 0.52011652 0.85521627 0.45218987 0.16056629 0.11749698
#> [15,20)  0.17066896 0.19878333 0.46501097 0.74634166 0.37886666 0.14176243
#> [20,25)  0.25176429 0.13635788 0.16849356 0.38660988 0.62017139 0.34139229
#> [25,30)  0.46211055 0.22425759 0.11912318 0.13976177 0.32983320 0.53602158
#> [30,35)  0.59258512 0.46404285 0.21988147 0.11105734 0.13338957 0.29953916
#> [35,40)  0.44053422 0.60229377 0.45916574 0.20800220 0.10911204 0.12575089
#> [40,45)  0.24813611 0.42156753 0.56415008 0.41092584 0.19420400 0.09880733
#> [45,50)  0.17232679 0.21969047 0.36922131 0.47226512 0.35903885 0.16576368
#> [50,55)  0.17668769 0.14562290 0.18461788 0.29536175 0.39353569 0.29546690
#> [55,60)  0.19201905 0.13985880 0.11424333 0.13662510 0.22732424 0.30388208
#> [60,65)  0.15926284 0.13975180 0.09892502 0.07471056 0.09179506 0.15608975
#> [65,70)  0.09790455 0.10895449 0.09008584 0.05757998 0.04323414 0.05575062
#> [70,75)  0.05302412 0.06486384 0.06597804 0.04888336 0.03141857 0.02632443
#> [75,Inf) 0.05322248 0.06526955 0.06920200 0.06451199 0.05707800 0.05077948
#>             [30,35)    [35,40)    [40,45)    [45,50)    [50,55)    [55,60)
#> [0,5)    0.62178022 0.47273161 0.25546533 0.17009553 0.17482914 0.20323908
#> [5,10)   0.48618700 0.64536061 0.43337937 0.21652618 0.14387863 0.14781271
#> [10,15)  0.23087433 0.49306630 0.58121593 0.36469323 0.18280251 0.12100258
#> [15,20)  0.11991586 0.22969215 0.43536014 0.47969941 0.30074955 0.14881154
#> [20,25)  0.14697309 0.12295252 0.20995680 0.37214425 0.40890406 0.25266113
#> [25,30)  0.31886747 0.13690412 0.10320521 0.16599688 0.29661069 0.32631605
#> [30,35)  0.47434704 0.27926066 0.10983187 0.07908944 0.13066467 0.23913330
#> [35,40)  0.27306178 0.43376575 0.24335324 0.09408603 0.07081651 0.12061829
#> [40,45)  0.11193671 0.25364723 0.41604280 0.23986879 0.09857511 0.07585060
#> [45,50)  0.08407456 0.10228690 0.25019328 0.44061859 0.26925538 0.11099486
#> [50,55)  0.13855966 0.07680014 0.10256560 0.26859440 0.49295805 0.29562369
#> [55,60)  0.23706275 0.12228840 0.07377991 0.10350946 0.27636552 0.50468265
#> [60,65)  0.22277113 0.19040752 0.10305079 0.06326389 0.09002586 0.24753763
#> [65,70)  0.10512420 0.16287373 0.14000115 0.07461175 0.04669411 0.07105616
#> [70,75)  0.03882415 0.07788079 0.11835254 0.10017738 0.05570677 0.03776983
#> [75,Inf) 0.04727707 0.05392646 0.08536482 0.13703371 0.16875709 0.15660524
#>             [60,65)    [65,70)    [70,75)   [75,Inf)
#> [0,5)    0.18962996 0.13726099 0.09466968 0.05459774
#> [5,10)   0.16615331 0.15252761 0.11563763 0.06685737
#> [10,15)  0.11786897 0.12638678 0.11787934 0.07103937
#> [15,20)  0.09154143 0.08307282 0.08981351 0.06810253
#> [20,25)  0.11477348 0.06365035 0.05890519 0.06148627
#> [25,30)  0.18855468 0.07929840 0.04768337 0.05284919
#> [30,35)  0.25279312 0.14046263 0.06606222 0.04622150
#> [35,40)  0.21127183 0.21279445 0.12957846 0.05155211
#> [40,45)  0.11917957 0.19064869 0.20524521 0.08505827
#> [45,50)  0.07631472 0.10597694 0.18120369 0.14241867
#> [50,55)  0.10833087 0.06616051 0.10051663 0.17495812
#> [55,60)  0.27846513 0.09412025 0.06371175 0.15178295
#> [60,65)  0.45192467 0.24477051 0.09071207 0.10291735
#> [65,70)  0.20787743 0.39438792 0.22467643 0.07728059
#> [70,75)  0.06049509 0.17642670 0.32608811 0.10587527
#> [75,Inf) 0.11945422 0.10561731 0.18426904 0.27097932

What next?

From here you might want to:

  • Create a next generation matrix (NGM)
  • Apply vaccination to an NGM

See the vignette, “example pipeline” for more details.

A More in depth example

The above example showed how we might extract a contact matrix based on the polymod data - this example now shows all the steps that can be taken for full flexibility, and provides amore detail on the initial datasets that could be used.

First we want to fit the model to the POLYMOD data, which contains various survey and population data.

library(conmat)
polymod_contact_data_home <- get_polymod_contact_data(setting = "home")
polymod_survey_data <- get_polymod_population()

The contact data is a data frame containing the age from and to, and the number of contacts for each of the specified settings, “home”, “work”, “school”, “other”, or “all” as well as the number of participants. By default, polymod_contact_data contains data from “all”, but we’re going to use the “work” set of data, as it produces an interesting looking dataset. Each row contains survey information of the number of contacts. Specifically, the number of contacts from one age group to another age group, and then the number of participants in that age group.

The survey data, polymod_survey_data contains the lower age limit and the population in that age group.

polymod_survey_data
#> # 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

We also provide control over the POLYMOD data retrieved from get_polymod_contact_data() via the arguments, setting, country, and ages. These allow you to specify the data to be only from certain settings or countries or ages. See ?get_polymod_contact_data for more details. Below is a brief example of this:

polymod_contact_data_belgium_0_10 <- get_polymod_contact_data(
  setting = "work",
  countries = "Belgium",
  ages = c(0, 5, 10)
)

polymod_contact_data_belgium_0_10
#> # A tibble: 3,447 × 5
#>    setting age_from age_to contacts participants
#>    <chr>      <int>  <dbl>    <int>        <int>
#>  1 work           0      0        0            5
#>  2 work           0      1        0            2
#>  3 work           0      2        0            2
#>  4 work           0      3        0            2
#>  5 work           0      4        0            1
#>  6 work           0      5        0            5
#>  7 work           0      7        0            1
#>  8 work           0     10        0            5
#>  9 work           0     11        0            1
#> 10 work           0     12        0            1
#> # … with 3,437 more rows

Similarly, you can control the population data, retrieving it only for certain countries:

get_polymod_population(countries = "Belgium")
#> # A tibble: 21 × 2 (conmat_population)
#>  - age: lower.age.limit
#>  - population: population
#>    lower.age.limit population
#>              <int>      <dbl>
#>  1               0     583492
#>  2               5     593148
#>  3              10     632157
#>  4              15     626921
#>  5              20     649588
#>  6              25     663176
#>  7              30     705878
#>  8              35     773177
#>  9              40     823305
#> 10              45     779436
#> # … with 11 more rows
get_polymod_population(countries = "Finland")
#> # A tibble: 21 × 2 (conmat_population)
#>  - age: lower.age.limit
#>  - population: population
#>    lower.age.limit population
#>              <int>      <dbl>
#>  1               0     284683
#>  2               5     296753
#>  3              10     330425
#>  4              15     319947
#>  5              20     334180
#>  6              25     331510
#>  7              30     308108
#>  8              35     352962
#>  9              40     379090
#> 10              45     381713
#> # … with 11 more rows

You can see the available countries in the helpfile with ?get_polymod_population.

Predicting the contact rate

We can create a model of the contact rate with the function fit_single_contact_model. We’re first going to use some smaller sets of the data, to save on computation time.

set.seed(2022 - 10 - 04)
polymod_contact_data_home_small <- polymod_contact_data_home %>%
  filter(
    age_from <= 30,
    age_to <= 30
  )

polymod_survey_data_small <- polymod_survey_data %>%
  filter(lower.age.limit <= 30)

contact_model <- fit_single_contact_model(
  contact_data = polymod_contact_data_home_small,
  population = polymod_survey_data_small
)

This fits a generalised additive model (GAM), predicting the contact rate, based on a series of prediction terms that describe various features of the contact rates.

contact_model
#> 
#> Family: poisson 
#> Link function: log 
#> 
#> Formula:
#> contacts ~ s(gam_age_offdiag) + s(gam_age_offdiag_2) + s(gam_age_diag_prod) + 
#>     s(gam_age_diag_sum) + s(gam_age_pmax) + s(gam_age_pmin) + 
#>     school_probability + work_probability + offset(log_contactable_population)
#> 
#> Estimated degrees of freedom:
#> 7.14 1.00 5.23 1.00 5.62 4.50  total = 27.48 
#> 
#> fREML score: 1643.134     rank: 55/57

We can use this contact model to then predict the contact rate in a new population.

As a demonstration, let’s take an age population from a given LGA in Australia (this was the initial motivation for the package, so there are some helper functions for Australian specific data).

fairfield_age_pop <- abs_age_lga("Fairfield (C)")
fairfield_age_pop
#> # 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

We can then pass the contact model through to predict_contacts, along with the fairfield age population data, and some age breaks that we want to predict to. Note that these age breaks could be any size, we just ahve them set to 5 year age brackets in most of the examples, but these could be 1 year, 2 year, or even sub year.

set.seed(2022 - 10 - 04)
synthetic_contact_fairfield <- predict_contacts(
  model = contact_model,
  population = fairfield_age_pop,
  age_breaks = c(seq(0, 30, by = 5), Inf)
)

synthetic_contact_fairfield
#> # A tibble: 49 × 3
#>    age_group_from age_group_to contacts
#>    <fct>          <fct>           <dbl>
#>  1 [0,5)          [0,5)        1.79e- 1
#>  2 [0,5)          [5,10)       1.62e- 1
#>  3 [0,5)          [10,15)      8.58e- 2
#>  4 [0,5)          [15,20)      5.62e- 2
#>  5 [0,5)          [20,25)      8.60e- 2
#>  6 [0,5)          [25,30)      1.81e- 1
#>  7 [0,5)          [30,Inf)     2.33e+21
#>  8 [5,10)         [0,5)        1.53e- 1
#>  9 [5,10)         [5,10)       2.51e- 1
#> 10 [5,10)         [10,15)      1.89e- 1
#> # … with 39 more rows

Plotting

Let’s visualise the matrix to get a sense of the predictions with autoplot. First we need to transform the predictions to a matrix:

synthetic_contact_fairfield %>%
  predictions_to_matrix() %>%
  autoplot()

Note

It is worth noting that the contact matrices created using this package are transposed when compared to the contact matrices discussed by Prem and Mossong. That is, the rows are “age group to”, and the columns are “age group from”.

Applying the model across all settings.

Our experience has been that we would be fitting the same models to each setting when doing data analysis when using conmat. Accordingly, you can also fit a model for all of the settings all at once with the functions, fit_setting_contacts(), and predict_setting_contacts(). This means we can do the above, but for each setting, “home”, “work”, “school”, “other”, and “all”, at once. If we want to use all of the POLYMOD data, we can also use the extrapolate_polymod() function.

Fit to all settings

We can create a model for each of the settings with fit_setting_contacts().

set.seed(2021 - 09 - 24)

polymod_setting_data <- get_polymod_setting_data()

polymod_setting_data_small <- polymod_setting_data %>%
  lapply(FUN = function(x) x %>% filter(age_from <= 20, age_to <= 20))

setting_models <- fit_setting_contacts(
  contact_data_list = polymod_setting_data_small,
  population = polymod_survey_data
)

This contains a list of models, one for each setting. We can look at one, and get summary information out:

names(setting_models)
#> [1] "home"   "work"   "school" "other"
setting_models$home
#> 
#> Family: poisson 
#> Link function: log 
#> 
#> Formula:
#> contacts ~ s(gam_age_offdiag) + s(gam_age_offdiag_2) + s(gam_age_diag_prod) + 
#>     s(gam_age_diag_sum) + s(gam_age_pmax) + s(gam_age_pmin) + 
#>     school_probability + work_probability + offset(log_contactable_population)
#> 
#> Estimated degrees of freedom:
#> 6.239 1.000 4.863 1.000 0.698 4.394  total = 21.19 
#> 
#> fREML score: 789.9536     rank: 55/57

So this gives us our baseline model of a contact model. We have fit this model to the existing contact survey data. We can now predict to another age population, to create our “synthetic” contact matrix.

Predict to all settings

Then we take the model we had earlier, and extrapolate to the fairfield data with predict_setting_contacts, also providing some age breaks we want to predict to

set.seed(2021 - 10 - 04)
synthetic_settings_5y_fairfield <- predict_setting_contacts(
  population = fairfield_age_pop,
  contact_model = setting_models,
  age_breaks = c(seq(0, 20, by = 5), Inf)
)

This then returns a list of synthetic matrices, “home”, “work”, “school”, “other”, and “all”, which is the sum of all matrices.

str(synthetic_settings_5y_fairfield)
#> List of 5
#>  $ home  : 'conmat_prediction_matrix' num [1:5, 1:5] 1.16e-01 1.04e-01 5.74e-02 3.52e-02 1.77e+34 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>  $ work  : 'conmat_prediction_matrix' num [1:5, 1:5] 0.000244 0.00015 0.000143 0.00217 0.000367 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>  $ school: 'conmat_prediction_matrix' num [1:5, 1:5] 0.76853 0.1457 0.0087 0.00949 Inf ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>  $ other : 'conmat_prediction_matrix' num [1:5, 1:5] 1.64e-01 9.77e-02 3.89e-02 2.61e-02 2.68e+38 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>  $ all   : 'conmat_prediction_matrix' num [1:5, 1:5] 1.0486 0.3475 0.1051 0.0729 Inf ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>   .. ..$ : chr [1:5] "[0,5)" "[5,10)" "[10,15)" "[15,20)" ...
#>  - attr(*, "class")= chr [1:2] "conmat_setting_prediction_matrix" "list"
synthetic_settings_5y_fairfield$home
#>                 [0,5)       [5,10)      [10,15)      [15,20)     [20,Inf)
#> [0,5)    1.160003e-01 9.830709e-02 5.153552e-02 2.958917e-02 1.377237e+33
#> [5,10)   1.039533e-01 1.570231e-01 1.137220e-01 4.114760e-02 4.499173e+26
#> [10,15)  5.737655e-02 1.197343e-01 1.888259e-01 9.939819e-02 4.376313e+21
#> [15,20)  3.515043e-02 4.622629e-02 1.060593e-01 1.663848e-01 9.740524e+16
#> [20,Inf) 1.770787e+34 5.470627e+27 5.054038e+22 1.054247e+18 1.929846e+13
synthetic_settings_5y_fairfield$all
#>               [0,5)    [5,10)    [10,15)    [15,20) [20,Inf)
#> [0,5)    1.04858409 0.3296462 0.09438879 0.06134469      Inf
#> [5,10)   0.34752254 4.6872902 0.52828926 0.09026246      Inf
#> [10,15)  0.10508680 0.5540780 6.73445049 0.57429654      Inf
#> [15,20)  0.07287437 0.1014032 0.60944139 4.93719307      Inf
#> [20,Inf)        Inf       Inf        Inf        Inf      Inf

We can use autoplot to plot all at once

# this code is erroring for the moment - something to do with rendering a large plot I think.
autoplot(
  synthetic_settings_5y_fairfield,
  title = "Setting-specific synthetic contact matrices (fairfield 2020 projected)"
)