Skip to content

Commit b48f93a

Browse files
committed
A few fixes for data type
1 parent 97372d8 commit b48f93a

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

R/mcmc.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ run_MCMC <- function(par_tab,
476476
}
477477
## Gibbs sampler version, integrate out phi
478478
} else if (hist_proposal == 2) {
479+
479480
## Swap entire contents or propose new
480481
if (inf_swap_prob > hist_switch_prob) {
481482
#print(paste0("Sum infection histories before: ", sum(infection_histories)))

R/plots.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ get_titre_predictions <- function(chain, infection_histories, titre_dat,
5858
mu_indices = NULL,
5959
measurement_indices_by_time = NULL,
6060
for_res_plot = FALSE, expand_titredat = FALSE,
61-
titre_before_infection=FALSE, titres_for_regression=FALSE){
61+
titre_before_infection=FALSE, titres_for_regression=FALSE,data_type=1){
6262
## Need to align the iterations of the two MCMC chains
6363
## and choose some random samples
6464
samps <- intersect(unique(infection_histories$sampno), unique(chain$sampno))
@@ -124,7 +124,7 @@ get_titre_predictions <- function(chain, infection_histories, titre_dat,
124124
tmp_inf_hist <- infection_histories[infection_histories$sampno == index, ]
125125
tmp_inf_hist <- as.matrix(Matrix::sparseMatrix(i = tmp_inf_hist$i, j = tmp_inf_hist$j, x = tmp_inf_hist$x, dims = c(n_indiv, nstrain)))
126126
predicted_titres[, i] <- model_func(pars, tmp_inf_hist)
127-
observed_predicted_titres[,i] <- add_noise(predicted_titres[,i], pars, NULL, NULL)
127+
observed_predicted_titres[,i] <- add_noise(predicted_titres[,i], pars, NULL, NULL,data_type)
128128
inf_hist_all[[i]] <- tmp_inf_hist
129129
## Get residuals between observations and predictions
130130
residuals[, i] <- titre_dat1$titre - floor(predicted_titres[, i])
@@ -227,15 +227,16 @@ plot_infection_histories_long <- function(chain, infection_histories, titre_dat,
227227
strain_isolation_times=NULL, par_tab,
228228
nsamp = 100,
229229
mu_indices = NULL,
230-
measurement_indices_by_time = NULL) {
230+
measurement_indices_by_time = NULL,
231+
data_type=1) {
231232
individuals <- individuals[order(individuals)]
232233
## Generate titre predictions
233234
titre_preds <- get_titre_predictions(
234235
chain, infection_histories, titre_dat, individuals,
235236
antigenic_map, strain_isolation_times,
236237
par_tab, nsamp, FALSE, mu_indices,
237238
measurement_indices_by_time,
238-
expand_titredat=TRUE
239+
expand_titredat=TRUE,data_type=data_type
239240
)
240241

241242
## Use these titre predictions and summary statistics on infection histories
@@ -307,17 +308,18 @@ plot_infection_histories <- function(chain, infection_histories, titre_dat,
307308
nsamp = 100,
308309
mu_indices = NULL,
309310
measurement_indices_by_time = NULL,
310-
p_ncol=length(individuals)/2) {
311+
p_ncol=length(individuals)/2,
312+
data_type=1) {
311313
individuals <- individuals[order(individuals)]
312314
## Generate titre predictions
313315
titre_preds <- get_titre_predictions(
314316
chain, infection_histories, titre_dat, individuals,
315317
antigenic_map, strain_isolation_times,
316318
par_tab, nsamp, FALSE, mu_indices,
317319
measurement_indices_by_time,
318-
expand_titredat=TRUE
320+
expand_titredat=TRUE,data_type=data_type
319321
)
320-
322+
321323
## Use these titre predictions and summary statistics on infection histories
322324
to_use <- titre_preds$predicted_observations
323325
model_preds <- titre_preds$predictions
@@ -332,7 +334,6 @@ plot_infection_histories <- function(chain, infection_histories, titre_dat,
332334

333335
max_x <- max(inf_hist_densities$variable) + 5
334336
time_range <- range(inf_hist_densities$variable)
335-
336337
titre_pred_p <- ggplot(to_use) +
337338
geom_rect(data=inf_hist_densities,
338339
aes(xmin=xmin,xmax=xmax,fill=value),ymin=min_titre-1,ymax=max_titre+2)+

R/simulate_data.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,21 @@ simulate_individual <- function(theta,
414414
#' y <- runif(100)
415415
#' noisy_y <- add_noise(y, pars)
416416
#' }
417-
add_noise <- function(y, theta, measurement_bias = NULL, indices = NULL) {
417+
add_noise <- function(y, theta, measurement_bias = NULL, indices = NULL, data_type=1) {
418418
## Draw from normal
419+
if(data_type == 1){
419420
if (!is.null(measurement_bias)) {
420421
noise_y <- floor(rnorm(length(y), mean = y + measurement_bias[indices], sd = theta["error"]))
421422
} else {
422423
noise_y <- floor(rnorm(length(y), mean = y, sd = theta["error"]))
423424
}
425+
} else {
426+
if (!is.null(measurement_bias)) {
427+
noise_y <- (rnorm(length(y), mean = y + measurement_bias[indices], sd = theta["error"]))
428+
} else {
429+
noise_y <- (rnorm(length(y), mean = y, sd = theta["error"]))
430+
}
431+
}
424432

425433
## If outside of bounds, truncate
426434
noise_y[noise_y < 0] <- 0

0 commit comments

Comments
 (0)