Skip to content

Commit 53679a0

Browse files
committed
Made the code changes for timevarying demographics with stratified infection model. Have not tested, errors likely in helper functions or mcmc
1 parent 876849e commit 53679a0

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

R/helpers.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ add_stratifying_variables <- function(antibody_data, timevarying_demographics=NU
544544
demographics <- timevarying_demographics %>%
545545
dplyr::select(all_of(use_demographic_groups)) %>%
546546
distinct() %>%
547+
arrange(across(everything())) %>%
547548
dplyr::mutate(demographic_group = 1:n())
548549

549550
## Assign to timevarying demographics
@@ -555,7 +556,6 @@ add_stratifying_variables <- function(antibody_data, timevarying_demographics=NU
555556
## Merge into antibody data to get correct demographic groups at sample times
556557
antibody_data <- antibody_data %>% left_join(demographics,by=use_demographic_groups)
557558
}
558-
559559
## Now check for population group (attack rate stratifying variable)
560560
## If nothing specified, set all to 1
561561
if(is.na(population_group_strats) & !("population_group" %in% colnames(antibody_data))){
@@ -571,6 +571,7 @@ add_stratifying_variables <- function(antibody_data, timevarying_demographics=NU
571571
population_groups <- timevarying_demographics %>%
572572
dplyr::select(all_of(population_group_strats))%>%
573573
distinct() %>%
574+
arrange(across(everything())) %>%
574575
dplyr::mutate(population_group = 1:n())
575576
## Merge into timevarying_demographics
576577
timevarying_demographics <- timevarying_demographics %>% left_join(population_groups,by=population_group_strats)
@@ -583,7 +584,6 @@ add_stratifying_variables <- function(antibody_data, timevarying_demographics=NU
583584
}
584585
antibody_data <- antibody_data %>% left_join(population_groups,by=population_group_strats)
585586
}
586-
587587
if(!is.null(timevarying_demographics)){
588588
indiv_group_indices <- timevarying_demographics %>% select(individual, time, demographic_group) %>% distinct() %>% pull(demographic_group)
589589
indiv_pop_group_indices <- timevarying_demographics %>% select(individual, time, population_group) %>% distinct() %>% pull(population_group)
@@ -598,7 +598,7 @@ add_stratifying_variables <- function(antibody_data, timevarying_demographics=NU
598598
select(individual,demographic_group) %>%
599599
ungroup()
600600
demographics_start[birth_demographics$individual] <- birth_demographics$demographic_group
601-
indiv_group_indices <- c(rbind(demographics_start, matrix(indiv_group_indices, ncol = n_indiv)))
601+
indiv_group_indices <- c(rbind(demographics_start, matrix(indiv_group_indices, ncol = length(unique(antibody_data$individual)))))
602602
} else {
603603
indiv_group_indices <- antibody_data %>% select(individual, demographic_group) %>% distinct() %>% pull(demographic_group)
604604
indiv_pop_group_indices <- antibody_data %>% select(individual, population_group) %>% distinct() %>% pull(population_group)
@@ -902,8 +902,9 @@ create_demographic_table <- function(antibody_data, par_tab){
902902
if(any(apply(demographics, 2, function(x) length(unique(x))) < 2)){
903903
message("Error - trying to stratify by variable in par_tab, but <2 levels for this variable in antibody_data")
904904
}
905-
}
906-
905+
}
906+
907+
demographics <- demographics %>% arrange(across(everything()))
907908
return(demographics)
908909
}
909910

R/plot_infection_histories.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ plot_attack_rates_pointrange <- function(infection_histories,
281281
n_groups <- length(unique(antibody_data$population_group))
282282
n_alive_tot <- get_n_alive(antibody_data, possible_exposure_times)
283283
colnames(infection_histories)[1] <- "individual"
284-
infection_histories <- merge(infection_histories, data.table(unique(antibody_data[, c("individual", "population_group")])), by = c("individual"))
284+
if (!by_group) {
285+
infection_histories <- merge(infection_histories, data.table(unique(antibody_data[, c("individual", "population_group")])), by = c("individual","population_group"))
286+
} else {
287+
infection_histories <- merge(infection_histories, data.table(unique(antibody_data[, c("individual", "population_group")])), by = c("individual"))
288+
}
285289
years <- c(possible_exposure_times, max(possible_exposure_times) + 2)
286290
data.table::setkey(infection_histories, "samp_no", "j", "chain_no", "population_group")
287291
tmp <- infection_histories[, list(V1 = sum(x)), by = key(infection_histories)]

R/posteriors.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ create_posterior_func <- function(par_tab,
6060
...) {
6161

6262
check_par_tab(par_tab, TRUE, prior_version,verbose)
63-
63+
antibody_data <- as.data.frame(antibody_data)
6464
## Add a dummy observation type variable if not provided
6565
if (!("biomarker_group" %in% colnames(antibody_data))) {
6666
if(verbose) message(cat("Note: no biomarker_group detected in antibody_data. Assuming all biomarker_group as 1.\n"))

src/proposal.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ List inf_hist_prop_prior_v2_and_v4(
272272
int start_index_in_data;
273273
int end_index_in_data;
274274

275-
int popn_group_id; // Vector of group IDs for each individual
275+
// Vector of group IDs for each individual
276+
int popn_group_id;
277+
int popn_group_id_loc1; // For timevarying population groups
278+
int popn_group_id_loc2;
276279

277280
IntegerVector new_infection_history(number_possible_exposures); // New proposed infection history
278281
IntegerVector infection_history(number_possible_exposures); // Old infection history
@@ -423,7 +426,9 @@ List inf_hist_prop_prior_v2_and_v4(
423426
group = indiv_group_indices[indiv];
424427
}
425428

426-
popn_group_id = popn_group_id_vec(indiv);
429+
if(!timevarying_groups){
430+
popn_group_id = popn_group_id_loc1 = popn_group_id_loc2 = popn_group_id_vec(indiv);
431+
}
427432
old_prob = likelihoods_pre_proposal(indiv);
428433

429434
// Time sampling control
@@ -496,9 +501,16 @@ List inf_hist_prop_prior_v2_and_v4(
496501
lik_changed = true;
497502
proposal_swap(indiv) += 1;
498503
if(!prior_on_total){
504+
505+
// Might be moving groups, so need to shift group IDs
506+
if(timevarying_groups){
507+
popn_group_id_loc1 = popn_group_id_vec((number_possible_exposures)*(indiv) + loc1);
508+
popn_group_id_loc2 = popn_group_id_vec((number_possible_exposures)*(indiv) + loc2);
509+
}
510+
499511
// Number of infections in that group in that time
500-
m_1_old = n_infections(popn_group_id,loc1);
501-
m_2_old = n_infections(popn_group_id,loc2);
512+
m_1_old = n_infections(popn_group_id_loc1,loc1);
513+
m_2_old = n_infections(popn_group_id_loc2,loc2);
502514

503515
// Swap contents
504516
new_infection_history(loc1) = new_infection_history(loc2);
@@ -512,12 +524,12 @@ List inf_hist_prop_prior_v2_and_v4(
512524
m_1_new = m_1_old - loc1_val_old + loc2_val_old;
513525
m_2_new = m_2_old - loc2_val_old + loc1_val_old;
514526

515-
prior_1_old = prior_lookup(m_1_old, loc1, popn_group_id);
516-
prior_2_old = prior_lookup(m_2_old, loc2, popn_group_id);
527+
prior_1_old = prior_lookup(m_1_old, loc1, popn_group_id_loc1);
528+
prior_2_old = prior_lookup(m_2_old, loc2, popn_group_id_loc2);
517529
prior_old = prior_1_old + prior_2_old;
518530

519-
prior_1_new = prior_lookup(m_1_new, loc1, popn_group_id);
520-
prior_2_new = prior_lookup(m_2_new, loc2, popn_group_id);
531+
prior_1_new = prior_lookup(m_1_new, loc1, popn_group_id_loc1);
532+
prior_2_new = prior_lookup(m_2_new, loc2, popn_group_id_loc2);
521533
prior_new = prior_1_new + prior_2_new;
522534

523535
} else {
@@ -541,6 +553,11 @@ List inf_hist_prop_prior_v2_and_v4(
541553
// Get number of individuals that were alive and/or infected in that year,
542554
// less the current individual
543555
// Number of infections in this year, less infection status of this individual in this year
556+
// Might be moving groups, so need to shift group IDs
557+
if(timevarying_groups){
558+
popn_group_id = popn_group_id_vec((number_possible_exposures)*(indiv) + year);
559+
}
560+
544561
m = n_infections(popn_group_id, year) - old_entry;
545562
n = n_alive(popn_group_id, year) - 1;
546563
} else {
@@ -761,6 +778,8 @@ List inf_hist_prop_prior_v2_and_v4(
761778
// Update the entry in the new matrix Z1
762779
old_prob = new_prob;
763780
likelihoods_pre_proposal_tmp(indiv) = new_prob;
781+
782+
764783

765784
// Carry out the swap
766785
if(swap_step_option){
@@ -771,8 +790,8 @@ List inf_hist_prop_prior_v2_and_v4(
771790

772791
// Update number of infections in the two swapped times
773792
if(!prior_on_total){
774-
n_infections(popn_group_id, loc1) = m_1_new;
775-
n_infections(popn_group_id, loc2) = m_2_new;
793+
n_infections(popn_group_id_loc1, loc1) = m_1_new;
794+
n_infections(popn_group_id_loc2, loc2) = m_2_new;
776795

777796
}
778797
// Don't need to update group infections if prior_on_total, as infections

0 commit comments

Comments
 (0)