Skip to content

Instantly share code, notes, and snippets.

@avallecam
Last active September 20, 2024 10:40
Show Gist options
  • Save avallecam/f492a2e48838ab3d57131060e7d79f1c to your computer and use it in GitHub Desktop.
Save avallecam/f492a2e48838ab3d57131060e7d79f1c to your computer and use it in GitHub Desktop.
filter countries in Africa with social contact survey data
library(socialmixr)
library(countrycode)
library(tidyverse)
# Get all countries names and... regex!!
all_countries <- countrycode::codelist
# Filter countries in the Africa continent
africa_countries <- all_countries %>%
select(continent, country.name.en,country.name.en.regex) %>%
filter(continent == "Africa")
africa_countries
#> # A tibble: 59 × 3
#> continent country.name.en country.name.en.regex
#> <chr> <chr> <chr>
#> 1 Africa Algeria "algeria"
#> 2 Africa Angola "angola"
#> 3 Africa Benin "benin|dahome"
#> 4 Africa Botswana "botswana|bechuana"
#> 5 Africa Burkina Faso "burkina|\\bfaso|upper.?volta"
#> 6 Africa Burundi "burundi"
#> 7 Africa Cameroon "cameroon"
#> 8 Africa Cape Verde "verde"
#> 9 Africa Central African Republic "\\bcentral.african.rep"
#> 10 Africa Chad "\\bchad"
#> # ℹ 49 more rows
# Create a single string reprex
africa_reprex <- africa_countries %>%
# slice_tail(n = 2) %>%
pull(country.name.en.regex) %>%
paste(collapse = "|")
# Filter countries in Africa with social contact survey data
socialmixr::list_surveys() %>%
as_tibble() %>%
mutate(location = str_to_lower(title)) %>%
filter(str_detect(
location,
africa_reprex,
negate = FALSE # change to TRUE to double check excluding by accident
)) %>%
select(title, url)
#> # A tibble: 4 × 2
#> title url
#> <chr> <chr>
#> 1 Social contact data for Zimbabwe https://doi…
#> 2 Social contact data for Zambia and South Africa (CODA dataset) https://doi…
#> 3 Social contact data for IDPs in Somaliland (2019) https://doi…
#> 4 Social contact data for the BHDSS and FWHDSS in the Gambia (2022) https://doi…
# Get one country survey using the URL
gambia_survey <- socialmixr::get_survey(
survey = "https://doi.org/10.5281/zenodo.13101862"
)
#> Using Social contact data for the BHDSS and FWHDSS in the Gambia (2022). To cite this in a publication, use the 'get_citation()' function
# Load contact matrix from survey
socialmixr::contact_matrix(
survey = gambia_survey,
countries = "Gambia",
age.limits = c(0,20,40),
symmetric = TRUE
)
#> Removing participants that have contacts without age information. To change this behaviour, set the 'missing.contact.age' option
#> $matrix
#> contact.age.group
#> [0,20) [20,40) 40+
#> [1,] 9.523684 3.188249 1.598430
#> [2,] 6.356374 4.630573 2.195776
#> [3,] 5.940886 4.093441 3.116197
#>
#> $demography
#> age.group population proportion year
#> <char> <num> <num> <int>
#> 1: [0,20) 1116876 0.5647679 2015
#> 2: [20,40) 560206 0.2832780 2015
#> 3: 40+ 300502 0.1519541 2015
#>
#> $participants
#> age.group participants proportion
#> <char> <int> <num>
#> 1: [0,20) 1140 0.72106262
#> 2: [20,40) 157 0.09930424
#> 3: 40+ 284 0.17963314
<sup>Created on 2024-09-19 with [reprex v2.1.1](https://reprex.tidyverse.org)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment