diff --git a/vignettes/conmat-population.Rmd b/vignettes/conmat-population.Rmd index 96a2a997..50a1a1e2 100644 --- a/vignettes/conmat-population.Rmd +++ b/vignettes/conmat-population.Rmd @@ -8,9 +8,22 @@ vignette: > --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) ``` @@ -212,3 +225,8 @@ If you want to use `conmat_population` within your R package, then please get in # Conclusion That's how we can use the conmat population information! Please go ahead and use and enjoy! + +```{r} +all_times +``` + diff --git a/vignettes/data-sources.Rmd b/vignettes/data-sources.Rmd index 6c85fcee..b5b20302 100644 --- a/vignettes/data-sources.Rmd +++ b/vignettes/data-sources.Rmd @@ -8,9 +8,22 @@ vignette: > --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) ``` @@ -203,3 +216,8 @@ eyre_transmission_probabilities %>% axis.text = element_text(angle = 45, hjust = 1) ) ``` + +```{r} +all_times +``` + diff --git a/vignettes/example-pipeline.Rmd b/vignettes/example-pipeline.Rmd index 402dc243..7edc448a 100644 --- a/vignettes/example-pipeline.Rmd +++ b/vignettes/example-pipeline.Rmd @@ -8,9 +8,22 @@ vignette: > --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) ``` @@ -268,3 +281,8 @@ This is a bit briefer than the two step process, and might be preferable to crea + +```{r} +all_times +``` + diff --git a/vignettes/getting-started.Rmd b/vignettes/getting-started.Rmd index ec7f9aa7..c3eee958 100644 --- a/vignettes/getting-started.Rmd +++ b/vignettes/getting-started.Rmd @@ -8,9 +8,22 @@ vignette: > --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) ``` @@ -310,3 +323,8 @@ autoplot( title = "Setting-specific synthetic contact matrices (fairfield 2020 projected)" ) ``` + +```{r} +all_times +``` + diff --git a/vignettes/other-data-sources.Rmd b/vignettes/other-data-sources.Rmd index 51b92503..623a9c5e 100644 --- a/vignettes/other-data-sources.Rmd +++ b/vignettes/other-data-sources.Rmd @@ -8,9 +8,22 @@ vignette: > --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) ``` @@ -99,3 +112,8 @@ ngm_italy_vacc <- apply_vaccination( ngm_italy_vacc ``` + +```{r} +all_times +``` + diff --git a/vignettes/parallel-computing.Rmd b/vignettes/parallel-computing.Rmd index eadf9013..849c5d2f 100644 --- a/vignettes/parallel-computing.Rmd +++ b/vignettes/parallel-computing.Rmd @@ -8,9 +8,22 @@ vignette: > --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) ``` @@ -70,3 +83,8 @@ synthetic_settings_5y_perth <- predict_setting_contacts( age_breaks = c(seq(0, 85, by = 5), Inf) ) ``` + +```{r} +all_times +``` + diff --git a/vignettes/sir-model.Rmd b/vignettes/sir-model.Rmd index da0ef35c..36949898 100644 --- a/vignettes/sir-model.Rmd +++ b/vignettes/sir-model.Rmd @@ -7,10 +7,24 @@ vignette: > %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + time_it = TRUE ) + ``` ```{r setup} @@ -44,10 +58,30 @@ people at a given time point. We can model how these numbers change at each time We start with a complicated version of a relatively simple model: an age-stratified SIR Model, but with all age groups acting exactly the same. +For the sake of speed, we will use just 3 age bands. We will use 17 age groups, each in 5 year age bands, and turn these into a `conmat_population` object. This is an object that knows which columns represent age and population, which is used by other functions within `conmat`. ```{r} #| label: create-population +age_bands <- seq(0, 80, length.out = 3) +n_age_bands <- length(age_bands) +homogeneous_population <- data.frame( + age = age_bands, + population = rep(100, times = n_age_bands) +) |> + as_conmat_population( + age = age, + population = population + ) + +homogeneous_population +``` + +As an exercise, we recommend that the user explores using many age bands, e.g., with + +```{r} +#| label: create-population-many +#| eval: false homogeneous_population <- data.frame( age = seq(0, 80, by = 10), population = rep(100, times = 9) @@ -64,8 +98,8 @@ Then, we extrapolate these into a set of contact matrices, which we can construc ```{r} #| label: homogenous-contact -age_breaks_0_80_plus <- c(seq(0, 80, by = 10), Inf) -mat_ones <- matrix(1, nrow = 9, ncol = 9) +age_breaks_0_80_plus <- c(age_bands, Inf) +mat_ones <- matrix(1, nrow = n_age_bands, ncol = n_age_bands) # Relative number of contacts between individuals in 2 age categories # Think of as P(contact) @@ -84,7 +118,7 @@ Similarly, we construct a set of transmission matrices, which provide the probab ```{r} #| label: homogenous-transmission -mat_05 <- matrix(0.05, nrow = 9, ncol = 9) +mat_05 <- matrix(0.05, nrow = n_age_bands, ncol = n_age_bands) transmission_matrix <- transmission_probability_matrix( home = mat_05, work = mat_05, @@ -100,13 +134,13 @@ We also need to set up our population structures. We'll have all the S states, t ```{r} #| label: initial-condition -S0 <- rep(999, times = 9) -I0 <- rep(1, times = 9) -R0 <- rep(0, times = 9) +S0 <- rep(999, times = n_age_bands) +I0 <- rep(1, times = n_age_bands) +R0 <- rep(0, times = n_age_bands) initial_condition <- c(S0, I0, R0) names(initial_condition) <- paste( - rep(c("S0", "I0", "R0"), each = 9), - age_breaks_0_80_plus[1:9], + rep(c("S0", "I0", "R0"), each = n_age_bands), + age_breaks_0_80_plus[1:n_age_bands], sep = "_" ) @@ -122,9 +156,9 @@ parameters <- list( "transmission_matrix" = transmission_matrix, "homogeneous_contact" = homogeneous_contact, "gamma" = 1, - "s_indexes" = 1:9, - "i_indexes" = 10:18, - "r_indexes" = 19:27 + "s_indexes" = 1, + "i_indexes" = 2, + "r_indexes" = 3 ) parameters @@ -218,7 +252,7 @@ Now, let's compare this to an SIR model with no stratification - as in, no age g ```{r} #| label: standard-sir parameters_sir <- c("beta" = 1.8, "gamma" = 1) -initial_condition_sir <- c("S" = 8991, "I" = 9, "R" = 0) +initial_condition_sir <- c("S" = 8991, "I" = n_age_bands, "R" = 0) sir <- function(time, state, parameters) { N <- sum(state) @@ -564,3 +598,8 @@ conmat_prem_soln %>% So now we have as fair of a comparison of the two matrices as we will get, and yet, there are significant differences in the dynamics of the two models. + +```{r} +all_times +``` + diff --git a/vignettes/visualising-conmat.Rmd b/vignettes/visualising-conmat.Rmd index 5c144efd..b6277845 100644 --- a/vignettes/visualising-conmat.Rmd +++ b/vignettes/visualising-conmat.Rmd @@ -7,16 +7,30 @@ vignette: > %\VignetteEncoding{UTF-8} --- -```{r, include = FALSE} +```{r} +all_times <- list() # store the time for each chunk +knitr::knit_hooks$set(time_it = local({ + now <- NULL + function(before, options) { + if (before) { + now <<- Sys.time() + } else { + res <- difftime(Sys.time(), now) + all_times[[options$label]] <<- res + } + } +})) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", + time_it = TRUE, fig.width = 8, fig.height = 8, dev = "png" ) ``` + ```{r setup} library(conmat) ```