library(bettertogether)
rm(list = setdiff(setdiff(ls(), "scriptfolder"), "resultsfolder"))
setmytheme()
registerDoParallel(cores = detectCores())
set.seed(16)
# nhpv considered as Y
nhpv <- c(7, 6, 10, 10, 1, 1, 10, 4, 35, 0, 10, 8, 4)
y1 <- matrix(nhpv, nrow = 1)
# Npart is put in the parameters
Npart <- c(111, 71, 162, 188, 145, 215, 166, 37, 173,
143, 229, 696, 93)
J <- 13
# For module 2, ncases considered data
ncases <- c(16, 215, 362, 97, 76, 62, 710, 56, 133,28, 62, 413, 194)
y2 <- matrix(ncases, nrow = 1)
# Npop considered parameters
Npop <- c(26983, 250930, 829348, 157775, 150467, 352445, 553066,
26751, 75815, 150302, 354993, 3683043, 507218)
Npop_normalized <- log(10**(-3) * Npop)
#
posterior_phi_alpha <- 1 + nhpv
posterior_phi_beta <- 1 + Npart - nhpv
# Find parameters given Y2
hyper2 <- list(theta2_mean_prior = 0, theta2_prior_sd = sqrt(1000))
dprior2 <- function(theta2s, hyper2){
return(sapply(1:nrow(theta2s), function(index)
sum(dnorm(theta2s[index,], mean = hyper2$theta2_mean_prior, sd = hyper2$theta2_prior_sd, log = TRUE))))
}
rprior2 <- function(n, hyper2){
theta2s <- matrix(0, nrow = n, ncol = 2)
for (j in 1:2){
theta2s[,j] <- rnorm(n, mean = hyper2$theta2_mean_prior, sd = hyper2$theta2_prior_sd)
}
return(theta2s)
}
target2given1plugin <- list(thetadim = 2, ydim = 1,
rprior = function(n, ...){
return(rprior2(n, hyper2))
},
dprior = function(thetas, ...){
evals <- dprior2(thetas, hyper2)
return(evals)
},
fullloglikelihood = function(thetas, ys, parameters){
return(plummer_module2_conditional(parameters$theta1hat, thetas, ncases, Npop_normalized))
},
conditionallikelihood = function(thetas, ys, idata, parameters){
return(plummer_module2_conditional(parameters$theta1hat, thetas, ncases, Npop_normalized))
},
parameters = list(theta1hat = NULL))
param_algo <- list(nthetas = 2^10, minimum_diversity = 0.8, nmoves = 10, proposal = mixture_rmixmod())
### Posterior in Module 1 given Module 1
filename <- paste0("epidemiology_module1altogether.N1024.K100.rep5.RData")
load(filename)
N1 <- nrow(results1[[1]]$thetas)
# results1[[1]]$thetas %>% head
# results1[[1]]$normw %>% head
rep <- 5
filename <- paste0("epidemiology_module2givenmodule1cut.N", param_algo$nthetas, ".K", param_algo$nmoves, ".M", N1, ".rep", rep, ".RData")
results2given1cut <- list()
scores2given1cut <- list()
for (irep in 1:rep){
print(irep)
thetas1 <- results1[[irep]]$thetas
normw1 <- results1[[irep]]$normw
thetas1reduced <- thetas1[systematic_resampling_n(normw1, N1),,drop=F]
results_cut <- list()
for (i in 1:N1) {
target2given1plugin$parameters$theta1hat <- thetas1reduced[i,]
res <- smcsampler(y2, target2given1plugin, param_algo)
thetas2 = res$thetas_history[[2]]
normw2 = res$normw_history[[2]]
results_cut[[i]] <- list(thetas2 = thetas2,
normw2 = normw2,
theta1 = thetas1reduced[i,], logevidence = res$logevidence)
rm(res, thetas2, normw2)
gc()
}
logevid <- rep(0, N1)
for (j in 1:N1){
logevid[j] <- results_cut[[j]]$logevidence
}
maxlogevid <- max(logevid)
log_score <- maxlogevid + log(mean(exp(logevid - maxlogevid)))
for (j in 1:N1){
results_cut[[j]]$logevidence <- NULL # (to save memory space)
}
scores2given1cut[[irep]] <- log_score
results2given1cut[[irep]] <- results_cut
save(scores2given1cut, results2given1cut, file = filename)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.