Skip to content

Commit

Permalink
Convert output to contact_matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo committed Dec 5, 2024
1 parent faf81fa commit 2c74287
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 104 deletions.
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Description: Data package for the supplementary data in Prem et al. (2017)
License: MIT + file LICENSE
Depends:
R (>= 3.5.0)
Imports:
contactmatrix
Suggests:
countrycode,
ggplot2,
Expand All @@ -35,6 +37,8 @@ Suggests:
spelling,
testthat(>= 3.0.0),
covr
Remotes:
socialcontactdata/contactmatrix
URL: https://hugogruson.fr/contactdata/, https://github.com/bisaloo/contactdata
BugReports: https://github.com/bisaloo/contactdata/issues
Encoding: UTF-8
Expand Down
11 changes: 10 additions & 1 deletion R/contact_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,14 @@ contact_matrix <- function(
)
}

return(matrix_country)
res <- contactmatrix::new_contactmatrix(
from = rep(rownames(matrix_country),
each = length(colnames(matrix_country))),
to = rep(colnames(matrix_country),
length(rownames(matrix_country))),
value = c(t(matrix_country)),
symmetric = FALSE
)

return(res)
}
234 changes: 132 additions & 102 deletions tests/testthat/_snaps/contact_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,143 @@

Code
res
Message
-- Asymmetric Contact matrix --
-- Groupings
Output
[00,05) [05,10) [10,15) [15,20) [20,25) [25,30) [30,35) [35,40) [40,45)
[00,05) 2.82093 0.92170 0.48046 0.02891 0.03428 0.42758 0.65892 0.14161 0.08598
[05,10) 0.17441 4.25239 0.32984 0.02286 0.00000 0.12828 0.16429 0.17113 0.28740
[10,15) 0.10457 0.17891 7.33501 0.53665 0.22516 0.14806 0.02504 0.29424 0.14234
[15,20) 0.35473 0.26124 0.32568 8.49234 0.65227 0.22267 0.00000 0.38819 0.20268
[20,25) 0.00000 0.00000 0.00000 0.32447 0.51299 0.06472 0.00000 0.00000 0.02545
[25,30) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.01296 0.00515 0.00000
[30,35) 0.02412 0.03038 0.00000 0.00000 0.00335 0.00430 0.00418 0.01719 0.02419
[35,40) 0.09168 0.00000 0.03160 0.00000 0.01454 0.07513 0.04337 0.02606 0.03884
[40,45) 0.00000 0.00000 0.00000 0.05836 0.00000 0.00000 0.00000 0.00000 0.05427
[45,50) 0.00000 0.00000 0.00000 0.01057 0.00000 0.00000 0.00000 0.02775 0.00000
[50,55) 0.00000 0.00000 0.02082 0.00747 0.00000 0.00000 0.00000 0.00793 0.00778
[55,60) 0.01693 0.01664 0.00000 0.00000 0.00000 0.00000 0.04766 0.01915 0.00000
[60,65) 0.00000 0.00000 0.01085 0.00000 0.00000 0.00000 0.00000 0.05103 0.01057
[65,70) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00047 0.00046
[70,75) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[75,80) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[45,50) [50,55) [55,60) [60,65) [65,70) [70,75) [75,80)
[00,05) 0.00000 0.14394 0.00000 0.00000 0.00000 0.00000 0.00000
[05,10) 0.24403 0.12990 0.05876 0.00000 0.02149 0.02046 0.00000
[10,15) 0.26037 0.10748 0.05924 0.02357 0.00000 0.00000 0.02104
[15,20) 0.42162 0.20215 0.10576 0.00000 0.00000 0.00000 0.00000
[20,25) 0.00981 0.00000 0.00000 0.00000 0.00916 0.00000 0.00000
[25,30) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[30,35) 0.00380 0.01012 0.00000 0.00387 0.00000 0.00000 0.00000
[35,40) 0.00901 0.00352 0.00894 0.00000 0.00000 0.00000 0.00000
[40,45) 0.05050 0.05626 0.00000 0.00000 0.00000 0.00000 0.00000
[45,50) 0.00000 0.00000 0.00000 0.01010 0.00000 0.00000 0.00000
[50,55) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[55,60) 0.01683 0.04592 0.00000 0.01731 0.00000 0.00000 0.00000
[60,65) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[65,70) 0.00217 0.00129 0.00133 0.00048 0.00047 0.00049 0.00132
[70,75) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[75,80) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[1] "[00,05)" "[05,10)" "[10,15)" "[15,20)" "[20,25)" "[25,30)" "[30,35)"
[8] "[35,40)" "[40,45)" "[45,50)" "[50,55)" "[55,60)" "[60,65)" "[65,70)"
[15] "[70,75)" "[75,80)"
Message
-- Contact rates
Output
_to
_from [00,05) [05,10) [10,15) [15,20) [20,25) [25,30) [30,35) [35,40)
[00,05) 2.82093 0.92170 0.48046 0.02891 0.03428 0.42758 0.65892 0.14161
[05,10) 0.17441 4.25239 0.32984 0.02286 0.00000 0.12828 0.16429 0.17113
[10,15) 0.10457 0.17891 7.33501 0.53665 0.22516 0.14806 0.02504 0.29424
[15,20) 0.35473 0.26124 0.32568 8.49234 0.65227 0.22267 0.00000 0.38819
[20,25) 0.00000 0.00000 0.00000 0.32447 0.51299 0.06472 0.00000 0.00000
[25,30) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.01296 0.00515
[30,35) 0.02412 0.03038 0.00000 0.00000 0.00335 0.00430 0.00418 0.01719
[35,40) 0.09168 0.00000 0.03160 0.00000 0.01454 0.07513 0.04337 0.02606
[40,45) 0.00000 0.00000 0.00000 0.05836 0.00000 0.00000 0.00000 0.00000
[45,50) 0.00000 0.00000 0.00000 0.01057 0.00000 0.00000 0.00000 0.02775
[50,55) 0.00000 0.00000 0.02082 0.00747 0.00000 0.00000 0.00000 0.00793
[55,60) 0.01693 0.01664 0.00000 0.00000 0.00000 0.00000 0.04766 0.01915
[60,65) 0.00000 0.00000 0.01085 0.00000 0.00000 0.00000 0.00000 0.05103
[65,70) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00047
[70,75) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[75,80) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
_to
_from [40,45) [45,50) [50,55) [55,60) [60,65) [65,70) [70,75) [75,80)
[00,05) 0.08598 0.00000 0.14394 0.00000 0.00000 0.00000 0.00000 0.00000
[05,10) 0.28740 0.24403 0.12990 0.05876 0.00000 0.02149 0.02046 0.00000
[10,15) 0.14234 0.26037 0.10748 0.05924 0.02357 0.00000 0.00000 0.02104
[15,20) 0.20268 0.42162 0.20215 0.10576 0.00000 0.00000 0.00000 0.00000
[20,25) 0.02545 0.00981 0.00000 0.00000 0.00000 0.00916 0.00000 0.00000
[25,30) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[30,35) 0.02419 0.00380 0.01012 0.00000 0.00387 0.00000 0.00000 0.00000
[35,40) 0.03884 0.00901 0.00352 0.00894 0.00000 0.00000 0.00000 0.00000
[40,45) 0.05427 0.05050 0.05626 0.00000 0.00000 0.00000 0.00000 0.00000
[45,50) 0.00000 0.00000 0.00000 0.00000 0.01010 0.00000 0.00000 0.00000
[50,55) 0.00778 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[55,60) 0.00000 0.01683 0.04592 0.00000 0.01731 0.00000 0.00000 0.00000
[60,65) 0.01057 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[65,70) 0.00046 0.00217 0.00129 0.00133 0.00048 0.00047 0.00049 0.00132
[70,75) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
[75,80) 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000

---

Code
res_2017
Message
-- Asymmetric Contact matrix --
-- Groupings
Output
[1] "[00,05)" "[05,10)" "[10,15)" "[15,20)" "[20,25)" "[25,30)" "[30,35)"
[8] "[35,40)" "[40,45)" "[45,50)" "[50,55)" "[55,60)" "[60,65)" "[65,70)"
[15] "[70,75)" "[75,80)"
Message
-- Contact rates
Output
[00,05) [05,10) [10,15) [15,20) [20,25)
[00,05) 2.231562e+00 2.188485e-01 3.900767e-01 2.688331e-02 1.032931e-02
[05,10) 2.472377e-01 3.117592e+00 2.057124e-01 1.607709e-02 9.661761e-19
[10,15) 7.631087e-02 1.573915e-01 4.195039e+00 1.005322e-01 6.057941e-02
[15,20) 1.667880e-01 1.188428e-01 5.047092e-01 4.047474e+00 6.130302e-02
[20,25) 3.797864e-27 3.165459e-30 1.110084e-64 2.179770e-01 2.186010e-01
[25,30) 5.860132e-52 3.296817e-44 1.510324e-61 8.547018e-39 9.412904e-34
[30,35) 5.880712e-02 1.027104e-01 1.469999e-45 5.640773e-23 5.685250e-03
[35,40) 4.086717e-01 1.705787e-16 1.546249e-01 1.873899e-51 3.260699e-02
[40,45) 1.414248e-01 7.960342e-45 2.448759e-57 3.152303e-02 7.128404e-43
[45,50) 5.537966e-63 9.859117e-104 1.576919e-49 4.952548e-02 1.134950e-38
[50,55) 3.252134e-53 8.848808e-134 3.248688e-33 5.037806e-85 2.965335e-101
[55,60) 2.937304e-02 2.816686e-02 7.384065e-02 2.832956e-02 9.790374e-127
[60,65) 6.079235e-69 1.214646e-47 3.733487e-02 7.817605e-75 4.152412e-105
[65,70) 1.740252e-141 9.780961e-109 2.514760e-99 5.718298e-89 2.861488e-147
[70,75) 5.395875e-124 5.013824e-125 9.620625e-104 5.056621e-123 4.011300e-115
[75,80) 1.641101e-116 6.770031e-123 1.936422e-116 1.190831e-119 4.543822e-119
[25,30) [30,35) [35,40) [40,45) [45,50)
[00,05) 1.738315e-01 2.889025e-01 7.389466e-02 4.453167e-02 4.535417e-14
[05,10) 6.256750e-02 7.893416e-02 7.008973e-02 1.198746e-01 7.160435e-02
[10,15) 5.682804e-02 8.798125e-03 7.864829e-02 6.385997e-02 1.542912e-01
[15,20) 5.795102e-02 1.697930e-25 1.325180e-01 3.960846e-02 1.105025e-01
[20,25) 1.046959e-02 2.013059e-28 3.447825e-22 2.661493e-02 1.153428e-02
[25,30) 3.074052e-01 2.812532e-44 1.864332e-36 3.672584e-54 5.624769e-62
[30,35) 8.176267e-03 3.597535e-02 5.285380e-02 5.494858e-02 8.736286e-03
[35,40) 1.851403e-01 8.048927e-02 6.714555e-02 8.853332e-02 3.042403e-02
[40,45) 4.827482e-02 8.277887e-02 1.984801e-02 9.134568e-02 2.011418e-02
[45,50) 4.636087e-65 1.108271e-25 8.792324e-02 6.012818e-51 1.431795e-46
[50,55) 5.525322e-133 8.965046e-43 2.650957e-02 2.715616e-02 2.235838e-43
[55,60) 2.047690e-126 5.069021e-02 1.980646e-02 5.330642e-34 1.999003e-02
[60,65) 1.724260e-81 1.789528e-78 1.359651e-01 3.079255e-02 2.328519e-35
[65,70) 1.387484e-166 4.157155e-51 8.389592e-62 4.998088e-43 5.506727e-02
[70,75) 2.139350e-97 7.074939e-87 1.661226e-02 1.674010e-02 4.565433e-02
[75,80) 4.235386e-126 3.939033e-76 1.222147e-94 1.245041e-86 2.734155e-67
[50,55) [55,60) [60,65) [65,70) [70,75)
[00,05) 6.800606e-02 5.251640e-48 1.133523e-90 3.079103e-104 2.003412e-75
[05,10) 4.988977e-02 1.683643e-02 6.127720e-99 2.056286e-03 1.979552e-03
[10,15) 4.046869e-02 1.982978e-02 3.923117e-03 9.768538e-53 8.221526e-43
[15,20) 5.163183e-02 2.154750e-02 1.101372e-102 5.472303e-80 7.378277e-103
[20,25) 1.562146e-40 2.238559e-79 7.644916e-43 2.398178e-139 2.483573e-108
[25,30) 7.031231e-84 3.013535e-61 5.939161e-126 4.614686e-02 1.654861e-93
[30,35) 2.153146e-02 3.187020e-61 4.323460e-03 1.080235e-99 2.525521e-70
[35,40) 4.654946e-31 8.013485e-03 8.639725e-79 1.496899e-107 9.645247e-135
[40,45) 5.053899e-02 1.479312e-02 7.719070e-43 2.760307e-88 5.801369e-138
[45,50) 1.009745e-60 1.585206e-93 1.558346e-02 5.605592e-134 2.724760e-108
[50,55) 1.530534e-81 2.273165e-42 7.685372e-96 6.001953e-62 1.796864e-131
[55,60) 4.992229e-02 4.560906e-84 1.106032e-02 6.447958e-87 8.829809e-111
[60,65) 2.091794e-41 1.744472e-67 1.310905e-115 1.327183e-114 1.677496e-139
[65,70) 5.154972e-51 1.628736e-87 1.082476e-89 2.923436e-83 1.683342e-64
[70,75) 4.590242e-02 4.587703e-02 1.664027e-02 1.659724e-02 1.654680e-02
[75,80) 2.462743e-65 3.462799e-68 1.218998e-76 8.387174e-76 1.051333e-68
[75,80)
[00,05) 9.377263e-107
[05,10) 2.368802e-85
[10,15) 2.315574e-03
[15,20) 3.734503e-127
[20,25) 5.385756e-97
[25,30) 1.219396e-96
[30,35) 1.878419e-137
[35,40) 3.375347e-129
[40,45) 9.617777e-117
[45,50) 1.262355e-111
[50,55) 4.112569e-130
[55,60) 5.214644e-120
[60,65) 6.424454e-111
[65,70) 5.775052e-74
[70,75) 4.622517e-02
[75,80) 1.014157e-96
_to
_from [00,05) [05,10) [10,15) [15,20) [20,25)
[00,05) 2.231562e+00 2.188485e-01 3.900767e-01 2.688331e-02 1.032931e-02
[05,10) 2.472377e-01 3.117592e+00 2.057124e-01 1.607709e-02 9.661761e-19
[10,15) 7.631087e-02 1.573915e-01 4.195039e+00 1.005322e-01 6.057941e-02
[15,20) 1.667880e-01 1.188428e-01 5.047092e-01 4.047474e+00 6.130302e-02
[20,25) 3.797864e-27 3.165459e-30 1.110084e-64 2.179770e-01 2.186010e-01
[25,30) 5.860132e-52 3.296817e-44 1.510324e-61 8.547018e-39 9.412904e-34
[30,35) 5.880712e-02 1.027104e-01 1.469999e-45 5.640773e-23 5.685250e-03
[35,40) 4.086717e-01 1.705787e-16 1.546249e-01 1.873899e-51 3.260699e-02
[40,45) 1.414248e-01 7.960342e-45 2.448759e-57 3.152303e-02 7.128404e-43
[45,50) 5.537966e-63 9.859117e-104 1.576919e-49 4.952548e-02 1.134950e-38
[50,55) 3.252134e-53 8.848808e-134 3.248688e-33 5.037806e-85 2.965335e-101
[55,60) 2.937304e-02 2.816686e-02 7.384065e-02 2.832956e-02 9.790374e-127
[60,65) 6.079235e-69 1.214646e-47 3.733487e-02 7.817605e-75 4.152412e-105
[65,70) 1.740252e-141 9.780961e-109 2.514760e-99 5.718298e-89 2.861488e-147
[70,75) 5.395875e-124 5.013824e-125 9.620625e-104 5.056621e-123 4.011300e-115
[75,80) 1.641101e-116 6.770031e-123 1.936422e-116 1.190831e-119 4.543822e-119
_to
_from [25,30) [30,35) [35,40) [40,45) [45,50)
[00,05) 1.738315e-01 2.889025e-01 7.389466e-02 4.453167e-02 4.535417e-14
[05,10) 6.256750e-02 7.893416e-02 7.008973e-02 1.198746e-01 7.160435e-02
[10,15) 5.682804e-02 8.798125e-03 7.864829e-02 6.385997e-02 1.542912e-01
[15,20) 5.795102e-02 1.697930e-25 1.325180e-01 3.960846e-02 1.105025e-01
[20,25) 1.046959e-02 2.013059e-28 3.447825e-22 2.661493e-02 1.153428e-02
[25,30) 3.074052e-01 2.812532e-44 1.864332e-36 3.672584e-54 5.624769e-62
[30,35) 8.176267e-03 3.597535e-02 5.285380e-02 5.494858e-02 8.736286e-03
[35,40) 1.851403e-01 8.048927e-02 6.714555e-02 8.853332e-02 3.042403e-02
[40,45) 4.827482e-02 8.277887e-02 1.984801e-02 9.134568e-02 2.011418e-02
[45,50) 4.636087e-65 1.108271e-25 8.792324e-02 6.012818e-51 1.431795e-46
[50,55) 5.525322e-133 8.965046e-43 2.650957e-02 2.715616e-02 2.235838e-43
[55,60) 2.047690e-126 5.069021e-02 1.980646e-02 5.330642e-34 1.999003e-02
[60,65) 1.724260e-81 1.789528e-78 1.359651e-01 3.079255e-02 2.328519e-35
[65,70) 1.387484e-166 4.157155e-51 8.389592e-62 4.998088e-43 5.506727e-02
[70,75) 2.139350e-97 7.074939e-87 1.661226e-02 1.674010e-02 4.565433e-02
[75,80) 4.235386e-126 3.939033e-76 1.222147e-94 1.245041e-86 2.734155e-67
_to
_from [50,55) [55,60) [60,65) [65,70) [70,75)
[00,05) 6.800606e-02 5.251640e-48 1.133523e-90 3.079103e-104 2.003412e-75
[05,10) 4.988977e-02 1.683643e-02 6.127720e-99 2.056286e-03 1.979552e-03
[10,15) 4.046869e-02 1.982978e-02 3.923117e-03 9.768538e-53 8.221526e-43
[15,20) 5.163183e-02 2.154750e-02 1.101372e-102 5.472303e-80 7.378277e-103
[20,25) 1.562146e-40 2.238559e-79 7.644916e-43 2.398178e-139 2.483573e-108
[25,30) 7.031231e-84 3.013535e-61 5.939161e-126 4.614686e-02 1.654861e-93
[30,35) 2.153146e-02 3.187020e-61 4.323460e-03 1.080235e-99 2.525521e-70
[35,40) 4.654946e-31 8.013485e-03 8.639725e-79 1.496899e-107 9.645247e-135
[40,45) 5.053899e-02 1.479312e-02 7.719070e-43 2.760307e-88 5.801369e-138
[45,50) 1.009745e-60 1.585206e-93 1.558346e-02 5.605592e-134 2.724760e-108
[50,55) 1.530534e-81 2.273165e-42 7.685372e-96 6.001953e-62 1.796864e-131
[55,60) 4.992229e-02 4.560906e-84 1.106032e-02 6.447958e-87 8.829809e-111
[60,65) 2.091794e-41 1.744472e-67 1.310905e-115 1.327183e-114 1.677496e-139
[65,70) 5.154972e-51 1.628736e-87 1.082476e-89 2.923436e-83 1.683342e-64
[70,75) 4.590242e-02 4.587703e-02 1.664027e-02 1.659724e-02 1.654680e-02
[75,80) 2.462743e-65 3.462799e-68 1.218998e-76 8.387174e-76 1.051333e-68
_to
_from [75,80)
[00,05) 9.377263e-107
[05,10) 2.368802e-85
[10,15) 2.315574e-03
[15,20) 3.734503e-127
[20,25) 5.385756e-97
[25,30) 1.219396e-96
[30,35) 1.878419e-137
[35,40) 3.375347e-129
[40,45) 9.617777e-117
[45,50) 1.262355e-111
[50,55) 4.112569e-130
[55,60) 5.214644e-120
[60,65) 6.424454e-111
[65,70) 5.775052e-74
[70,75) 4.622517e-02
[75,80) 1.014157e-96

2 changes: 1 addition & 1 deletion tests/testthat/test-contact_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("contact_matrix()", {
res <- contact_matrix("Belgium", location = "school")

expect_type(res, "double")
expect_identical(dim(res), c(16L, 16L))
expect_identical(dim(res), c(16L, 16L), ignore_attr = TRUE)
expect_identical(
rownames(res),
sprintf("[%02i,%02i)", seq(0, 75, 5), seq(5, 80, 5))
Expand Down

0 comments on commit 2c74287

Please sign in to comment.