archive/process_stan.R

#' fpem_process_stan
#'
#' @param samples
#' @param country_data
#' @param time_frame
#'
#' @return posterior samples with format 1 x chain*iterations x nyears x nprops
#'
fpem_process_stan <- function(samples, core_data){
  PRZ <- rstan::extract(samples, pars = c("P", "R", "Z"),
                        inc_warmup=FALSE, permuted=TRUE) %>%
    lapply(function(A) {
      dimnames(A) = list(iteration = 1:dim(A)[1],
                         country = core_data$units$name,
                         year = core_data$time_frame$sequence())
      return(A)}
    )

  props <- c("mod", "trad", "unmet")
  all_vec <- fpemreporting::calculate_3proportions_vec(PRZ$P %>% unlist() %>% as.vector(),
                                                       PRZ$R %>% unlist() %>% as.vector(),
                                                       PRZ$Z %>% unlist() %>% as.vector())
  posterior_samples <- array(data = all_vec, #get rid of hard code for iterations and years following comment
                             dim = c(1, dim(PRZ$P)[1], dim(PRZ$P)[3], length(props)),
                             dimnames = list(chain = 1, iteration = 1:dim(PRZ$P)[1], unit_time = 1:dim(PRZ$P)[3], props = props))
  return(posterior_samples)
}
FPRgroup/fpemreporting documentation built on March 14, 2020, 7:58 a.m.