Learn R Programming

Social mixing matrices for infectious disease modelling in R

socialmixr is an R package to derive social mixing matrices from survey data.

Installation

The package can be installed using

install.packages("socialmixr")

The current development version can be installed using the remotes package

remotes::install_github("epiforecasts/socialmixr")

Usage

Contact matrices are computed through a small pipeline of composable functions: subsetting the survey, assigning age groups, optionally weighing participants, and computing the matrix. A minimal example using the included POLYMOD data:

library(socialmixr)
data(polymod)

polymod[country == "United Kingdom"] |>
  assign_age_groups(age_limits = c(0, 1, 5, 15)) |>
  compute_matrix()

Post-processing functions symmetrise(), split_matrix() and per_capita() can be piped after compute_matrix() to enforce symmetry, decompose the matrix, or convert to per-capita contact rates.

Documentation

For more on how to use the socialmixr package, see the introduction vignette.

Contributors

All contributions to this project are gratefully acknowledged using the allcontributors package following the allcontributors specification. Contributions of any kind are welcome!

Code

sbfnk, Bisaloo, lwillem, njtierney, alxsrobert, Degoot-AM, pearsonca, jarvisc1, jamesmbaazam, LloydChapman, mariabnd, ukhsa-tt

Issue Authors

bastistician, BlackEdder, Pinzo1, florpi, cchauve, thutran, dlaydon, deusthindwa, krivit, linyang17, cliu822, TimTaylor, NaomiWaterlow, aakhmetz, adamkucharski, chitrams, IsaacStopard, avallecam, maishaoshao, FrancescoBonacina, lucy-gf

Issue Contributors

vikkytom, joshwlambert, bahadzie, coderabbitai

Copy Link

Version

Install

install.packages('socialmixr')

Monthly Downloads

929

Version

0.7.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Sebastian Funk

Last Published

August 22nd, 2026

Functions in socialmixr (0.7.0)

copy_survey

Deep copy a contact survey
contact_age_distribution

Extract the empirical age distribution of contacts from a survey
explicit_grouping

Build a grouping triple from an explicit c(part, cnt) entry
.get_survey

Internal function to get survey data
deprecate_arg

Handle deprecated argument
impute_contact_ages

Impute contact ages
rebin_ages

Rebin a population table to a set of age groups
rebin_ages_numeric

Rebin population data to a set of age limits (numeric)
n_participants_per_group

Count participants per grouping combination
get_citation

Citation for a survey
new_contact_matrix

Create a contact_matrix object
new_contact_survey

Contact survey
n_participants_per_age_group

Count participants per age group
find_unique_key

Find the minimal unique key for a data.table
get_survey

Get a survey, either from its Zenodo repository, a set of files, or a survey variable
clean

Clean contact survey data
limits_to_age_groups

Convert lower age limits to age groups.
split_matrix

Decompose a contact matrix into mean contacts, normalisation and assortativity
grouping_predicates

Predicates classifying a by entry
impute_ages

Impute ages from ranges (generic helper)
joint_population_vector

Resolve a survey population to a vector aligned with the matrix strata
normalise_country_names

Normalise country names
polymod

Social contact data from 8 European countries
flat_level_labels

Build colon-joined tuple labels from a list of level vectors
flatten

Flatten a multi-grouping contact matrix to its T x T form
load_survey

Load a survey from local files
reduce_age_groups

Reduce the number of age groups given a broader set of limits
sample_from_age_distribution

Sample ages from a distribution within [min, max] bands
normalise_weights

Post-stratification weight normalisation
matrix_plot

Draws an image plot of a contact matrix with a legend strip and the numeric values in the cells.
resolve_groupings

Resolve grouping specifications to participant/contact column pairs
validate_age_distribution

Validate an age distribution data.frame
impute_participant_ages

Impute participant ages
pop_age

Change age groups in population data
limits_to_agegroups

Convert lower age limits to age groups (deprecated)
reduce_agegroups

Reduce the number of age groups (deprecated)
warn_multiple_observations

Warn if survey has multiple observations per participant
resolve_one_grouping

Resolve a single by entry to a grouping triple
normalise_weights_to_counts

Normalise a weighted contact array to mean contacts per participant
wpp_age

Get age-specific population data according to the World Population Prospects 2017 edition
is_contact_matrix

Test whether an object is a contact_matrix
stem_grouping

Build a grouping triple from a stem string
weigh

Weigh survey participants
wpp_countries

List all countries and regions for which socialmixr has population data
[.contact_survey

Subset a contact survey
is_doi

Checks if a character string is a DOI
weighted_matrix_array

Cross-tab contact weights over grouping columns
list_surveys

List all surveys available for download
survey

Contact survey (defunct)
per_capita

Convert a contact matrix to per-capita rates
survey_countries

List all countries contained in a survey
symmetrise

Symmetrise a contact matrix
survey_country_population

Get survey country population data
add_age

Add age column from exact age (generic helper)
align_ages

Align a population table to a contact matrix's grouping levels
as_contact_survey

Check contact survey data
assign_age_groups

Assign age groups in survey data
check

Check contact survey data
age_groups_to_limits

Convert age groups to lower age limits
compute_matrix

Compute contact matrix from prepared survey data
contact_matrix

Generate a contact matrix from diary survey data
download_survey

Download a survey from its Zenodo repository
assemble_survey

Assemble a contact survey with new participant/contact data
abort_if_missing

Abort if grouping columns are absent from a list of available columns
check_grouping_columns

Check that a survey contains the columns required by a list of groupings
default_age_groupings

Default grouping spec corresponding to age groups
agegroups_to_limits

Convert age groups to lower age limits (deprecated)
check_part_cnt_dims_match

Abort if participant and contact dims of a multi-grouping matrix differ