@@ -659,202 +659,3 @@ test_that("simulate_serosurvey handles invalid model inputs", {
659659 expect_error(simulate_serosurvey(" invalid_model" , foi_df , survey_features ),
660660 " model must be one of 'age', 'time', or 'age-time'." )
661661})
662-
663- test_that(" prob_seroprev_gen_by_age reduces to time-varying model under
664- appropriate limits" , {
665-
666- # simple time-varying FoI model
667- construct_A <- function (t , tau , lambda ) {
668- A <- matrix (0 , ncol = 2 , nrow = 2 )
669- A [1 , 1 ] <- - lambda [t ]
670- A [2 , 1 ] <- lambda [t ]
671- A
672- }
673-
674- # determines the sum of seropositive compartments of those still alive
675- calculate_seroprev_fun <- function (Y ) {
676- Y [2 ]
677- }
678-
679- # initial conditions in 12D state vector
680- initial_conditions <- rep(0 , 2 )
681- initial_conditions [1 ] <- 1
682-
683- # random FOIs
684- lambda <- runif(70 , 0 , 0.01 )
685-
686- # solve linear system of ODEs
687- seropositive_linear_system <- prob_seroprev_gen_by_age(
688- construct_A ,
689- calculate_seroprev_fun ,
690- initial_conditions ,
691- max_age = length(lambda ),
692- lambda
693- )
694-
695- foi_df <- data.frame (
696- year = seq_along(lambda ),
697- foi = lambda
698- )
699-
700- seropositive_true <- prob_seroprev_by_age(
701- model = " time" ,
702- foi = foi_df ,
703- seroreversion_rate = 0
704- )
705-
706- expect_equal(seropositive_true , seropositive_linear_system )
707-
708- })
709-
710- test_that(" prob_seroprev_gen_by_age reduces to age-varying model under
711- appropriate limits" , {
712-
713- # simple age-varying FoI model
714- construct_A <- function (t , tau , lambda ) {
715- A <- matrix (0 , ncol = 2 , nrow = 2 )
716- A [1 , 1 ] <- - lambda [t - tau ]
717- A [2 , 1 ] <- lambda [t - tau ]
718- A
719- }
720-
721- # determines the sum of seropositive compartments of those still alive
722- calculate_seroprev_fun <- function (Y ) {
723- Y [2 ]
724- }
725-
726- # initial conditions in 12D state vector
727- initial_conditions <- rep(0 , 2 )
728- initial_conditions [1 ] <- 1
729-
730- # random FOIs
731- lambda <- runif(70 , 0 , 0.01 )
732-
733- # solve linear system of ODEs
734- seropositive_linear_system <- prob_seroprev_gen_by_age(
735- construct_A ,
736- calculate_seroprev_fun ,
737- initial_conditions ,
738- max_age = length(lambda ),
739- lambda
740- )
741-
742- foi_df <- data.frame (
743- age = seq_along(lambda ),
744- foi = lambda
745- )
746-
747- seropositive_true <- prob_seroprev_by_age(
748- model = " age" ,
749- foi = foi_df ,
750- seroreversion_rate = 0
751- )
752-
753- expect_equal(seropositive_true , seropositive_linear_system )
754-
755- })
756-
757- test_that(" prob_seroprev_gen_by_age reduces to age- and time-varying model under
758- appropriate limits" , {
759-
760- # age- and time-varying FoI model
761- construct_A <- function (t , tau , u , v ) {
762- A <- matrix (0 , ncol = 2 , nrow = 2 )
763- u_bar <- u [t - tau ]
764- v_bar <- v [t ]
765-
766- A [1 , 1 ] <- - u_bar * v_bar
767- A [2 , 1 ] <- u_bar * v_bar
768- A
769- }
770-
771- # determines the sum of seropositive compartments of those still alive
772- calculate_seroprev_fun <- function (Y ) {
773- Y [2 ]
774- }
775-
776- # initial conditions in 12D state vector
777- initial_conditions <- rep(0 , 2 )
778- initial_conditions [1 ] <- 1
779-
780- # age and time-varying FOIs
781- ages <- seq(1 , 70 , 1 )
782- foi_age <- 2 * dlnorm(
783- ages , meanlog = 3.5 , sdlog = 0.5 )
784-
785- foi_df_age <- data.frame (
786- age = ages ,
787- foi = foi_age
788- )
789-
790- foi_time <- c(rep(0 , 30 ), rep(1 , 40 ))
791- foi_df_time <- data.frame (
792- year = seq(1956 , 2025 , 1 ),
793- foi = foi_time
794- )
795-
796- u <- foi_df_age $ foi
797- v <- foi_df_time $ foi
798-
799- # solve linear system of ODEs
800- seropositive_linear_system <- prob_seroprev_gen_by_age(
801- construct_A ,
802- calculate_seroprev_fun ,
803- initial_conditions ,
804- max_age = nrow(foi_df_time ),
805- u ,
806- v
807- )
808-
809- foi_df <- expand.grid(
810- year = foi_df_time $ year ,
811- age = foi_df_age $ age
812- ) | >
813- dplyr :: left_join(foi_df_age , by = " age" ) | >
814- dplyr :: rename(foi_age = foi ) | >
815- dplyr :: left_join(foi_df_time , by = " year" ) | >
816- dplyr :: rename(foi_time = foi ) | >
817- dplyr :: mutate(foi = foi_age * foi_time ) | >
818- dplyr :: select(- c(" foi_age" , " foi_time" ))
819-
820- seropositive_true <- prob_seroprev_by_age(
821- model = " age-time" ,
822- foi = foi_df ,
823- seroreversion_rate = 0
824- )
825-
826- expect_equal(seropositive_true , seropositive_linear_system )
827-
828- # simulate survey from age and time FoI
829- survey_features <- data.frame (
830- age_min = seq(1 , 70 , 10 ),
831- age_max = seq(10 , 70 , 10 ),
832- n_sample = 1e9 )
833-
834- serosurvey <- simulate_serosurvey_general(
835- construct_A ,
836- calculate_seroprev_fun ,
837- initial_conditions ,
838- survey_features ,
839- u ,
840- v
841- ) | >
842- dplyr :: mutate(
843- seropositivity = n_seropositive / n_sample
844- )
845-
846- seropositive_true_age_group <- base :: colSums(matrix (
847- seropositive_linear_system $ seropositivity , 10
848- )) / 10
849-
850- expect_true(
851- all(
852- dplyr :: near(
853- serosurvey $ seropositivity ,
854- seropositive_true_age_group ,
855- tol = 1e-4
856- )
857- )
858- )
859-
860- })
0 commit comments