Takes all of the input data/parameters and returns a function pointer. This function finds the posterior for a given set of input parameters (theta) and infection histories without needing to pass the data set back and forth. No example is provided for function_type=2, as this should only be called within serosolver

create_posterior_func(
  par_tab,
  antibody_data,
  antigenic_map = NULL,
  possible_exposure_times = NULL,
  prior_version = 2,
  solve_likelihood = TRUE,
  age_mask = NULL,
  measurement_bias = NULL,
  n_alive = NULL,
  function_type = 1,
  antibody_level_before_infection = FALSE,
  data_type = 1,
  biomarker_groups_weights = 1,
  start_level = "other",
  start_level_randomize = FALSE,
  demographics = NULL,
  demographic_groups = NULL,
  fixed_inf_hists = NULL,
  verbose = FALSE,
  exponential_waning = FALSE,
  ...
)

Arguments

par_tab

the parameter table controlling information such as bounds, initial values etc. See example_par_tab

antibody_data

the data frame of data to be fitted. Must have columns: individual (integer ID of individual); sample_time (numeric time of sample taken); biomarker_id (numeric ID of the biomarker); measurement (antibody measurement); and birth (birth time). `biomarker_group`, `repeat_number`, and `population_group` are optional. `population_group` identifies groups with different infection rates or attack rates; if it is absent, all individuals are assigned to group 1. See example_antibody_data

antigenic_map

(optional) a data frame of antigenic x and y coordinates. Must have column names: x_coord; y_coord; inf_times. See example_antigenic_map

possible_exposure_times

(optional) if no antigenic map is specified, this argument gives the vector of times at which individuals can be infected

prior_version

which infection history assumption version to use? See describe_priors for options. Can be 1, 2, 3 or 4

solve_likelihood

usually set to TRUE. If FALSE, does not solve the likelihood and instead just samples/solves based on the model prior

age_mask

see create_age_mask - a vector with one entry for each individual specifying the first epoch of circulation in which an individual could have been exposed

measurement_bias

if not NULL, then use these indices to specify which measurement bias parameter index corresponds to which time. See the [advanced features vignette](https://seroanalytics.github.io/serosolver/articles/advanced_features.html) for this optional workflow.

n_alive

if not NULL, uses this as the number alive in a given year rather than calculating from the ages. This is needed if the number of alive individuals is known, but individual birth dates are not

function_type

integer specifying which version of this function to use. Specify 1 to give a posterior solving function; 2 to give the gibbs sampler for infection history proposals; otherwise just solves the antibody model and returns predicted antibody levels. Note that this is not the same as the attack rate prior argument, `prior_version`.

antibody_level_before_infection

TRUE/FALSE value. If TRUE, solves antibody level predictions, but gives the predicted antibody level at a given time point BEFORE any infection during that time occurs.

data_type

numeric or text value, with an entry for each biomarker group in `antibody_data`: `1` or `"discrete"` for discrete data (e.g., fold dilution), `2` or `"continuous"` for continuous data (e.g., ELISA optical density), or `3` or `"false_positive"` for continuous data with the false-positive observation model. A single value is used for all groups.

biomarker_groups_weights

integer or vector, giving a factor to multiply the log-likelihood contribution of this data type towards the overall likelihood.

start_level

character, to tell the model how to treat initial antibody levels. This uses the observed data to either select starting values for each unique `individual`, `biomarker_id` and `biomarker_group` combination. See create_start_level_data. One of "min", "max", "mean", "median", or "full_random". Any other entry assumes all antibody starting levels are set to 0. Can also pass a tibble or data frame of starting levels matching the output of create_start_level_data. See the [advanced features vignette](https://seroanalytics.github.io/serosolver/articles/advanced_features.html) for this optional workflow.

start_level_randomize

if TRUE, and data is discretized, then sets the starting antibody level to a random value between floor(x) and floor(x) + 1. Does nothing if using continuous data.

demographics

if not NULL, then a tibble for each individual (1:n_indiv) giving demographic variable entries. Most importantly must include "birth" as the birth time. This is used if, for example, you have a stratification grouping in `par_tab`. See the [demographics and covariates vignette](https://seroanalytics.github.io/serosolver/articles/demographics_covariates.html) for this optional workflow.

demographic_groups

optional data frame of demographic-group combinations. If NULL, these are created from `demographics` or `antibody_data` and `par_tab`.

fixed_inf_hists

optional data frame with columns `individual`, `time`, and `value`, giving infection states that should be fixed during the MCMC run. See the [advanced features vignette](https://seroanalytics.github.io/serosolver/articles/advanced_features.html) for this optional workflow.

verbose

if TRUE, prints warning messages

exponential_waning

Deprecated compatibility argument. The preferred setting is a fixed `exponential_waning` row in `par_tab`, with `values = 1` and `par_type = 0`. See the [advanced features vignette](https://seroanalytics.github.io/serosolver/articles/advanced_features.html).

...

other arguments to pass to the posterior solving function

Value

a single function pointer that takes only pars and infection_histories as unnamed arguments. This function goes on to return a vector of posterior values for each individual

Examples

if (FALSE) { # \dontrun{
data(example_par_tab)
data(example_antibody_data)
data(example_antigenic_map)
data(example_inf_hist)

## Simple model solving code. Output matches entries of example_antibody_data
model_func <- create_posterior_func(example_par_tab, example_antibody_data, example_antigenic_map, function_type = 3)
y <- model_func(example_par_tab$values, example_inf_hist)

## Solve likelihood
par_tab <- example_par_tab[example_par_tab$names != "phi",]
likelihood_func <- create_posterior_func(par_tab, example_antibody_data, example_antigenic_map, function_type = 1, prior_version = 2)
liks <- likelihood_func(par_tab$values, example_inf_hist)
} # }