Skip to content

Commit 93300eb

Browse files
committed
Added TRANSVIR Case Study 2, changed some internals and added sero check functions
1 parent 1d1978c commit 93300eb

File tree

81 files changed

+2829
-509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2829
-509
lines changed

NAMESPACE

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
export(addAbkineticsModel)
44
export(addCopModel)
55
export(addObservationalModel)
6-
export(add_par_df)
6+
export(addPrior)
7+
export(check_exposures_times)
8+
export(check_input_data)
9+
export(check_sero_no_single_entries)
10+
export(check_sero_timings)
711
export(createSeroJumpModel)
812
export(data_known_exposures_ex1)
913
export(data_titre_ex)

R/input_checks.R

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ check_time <- function(data_titre) {
2626
}
2727

2828

29-
addExposurePrior_checkempirical <- function(exp_prior, data_t) {
29+
addExposurePrior_checkempirical <- function(exp_prior, T) {
3030
columns <- c("time", "prob")
3131
existing_columns <- names(exp_prior)
3232

@@ -39,91 +39,90 @@ addExposurePrior_checkempirical <- function(exp_prior, data_t) {
3939
stop(paste("Error: Column(s)", paste(names(column_types[!column_types %in% c("numeric", "integer")]), collapse = ", "), "are not numeric or integers in the exposure prior dataframe"))
4040
}
4141

42-
if (data_t$T != nrow(exp_prior) ) {
42+
if (T != nrow(exp_prior) ) {
4343
warning("Warning: The number of rows in the exposure prior dataframe does not match the number of time points in the study\n")
4444
}
4545
}
4646

4747

48-
check_inputs <- function(data_sero, data_known, modeldefinition) {
48+
check_inputs <- function(data_sero, data_known, biomarkers, exposureTypes, exposureFitted, observationalModel, abkineticsModel, exposurePrior, exposurePriorType) {
4949
# CHECK inputs of modeldefinition are present
50-
if(is.null(modeldefinition$biomarkers)) {
51-
stop("Please define the `biomarkers` variable in `modeldefinition`")
52-
}
53-
if(is.null(modeldefinition$exposureTypes)) {
54-
stop("Please define the `exposureTypes` variable in `modeldefinition`")
55-
}
56-
if(is.null(modeldefinition$exposureFitted)) {
57-
stop("Please define the `exposureFitted` variable in `modeldefinition`")
58-
}
59-
if(is.null(modeldefinition$observationalModel)) {
60-
stop("Please define the `observationalModel` structure in `modeldefinition`")
61-
}
62-
if(is.null(modeldefinition$abkineticsModel)) {
63-
stop("Please define the `abkineticsModel` structure in `modeldefinition`")
64-
}
50+
# if(is.null(modeldefinition$biomarkers)) {
51+
# stop("Please define the `biomarkers`")
52+
# }
53+
# if(is.null(modeldefinition$exposureTypes)) {
54+
# stop("Please define the `exposureTypes`")
55+
# }
56+
# if(is.null(modeldefinition$exposureFitted)) {
57+
# stop("Please define the `exposureFitted`")
58+
# }
59+
# if(is.null(modeldefinition$observationalModel)) {
60+
# stop("Please define the `observationalModel``")
61+
# }
62+
## if(is.null(modeldefinition$abkineticsModel)) {
63+
# stop("Please define the `abkineticsModel`")
64+
# }
6565

6666

6767
# CHECK BIOMARKERS ARE WELL DEFINED
6868
# Check columns of data_sero match model definition
6969
data_sero_name <- data_sero %>% names
70-
biomarkers_md <- modeldefinition$biomarkers
71-
biomarkers_obs <- modeldefinition$observationalModel$model %>% map(~.x$biomarker) %>% unlist
72-
biomarkers_abkin <- modeldefinition$abkineticsModel$model %>% map(~.x$biomarker) %>% unlist %>% unique
70+
biomarkers_md <- biomarkers
71+
biomarkers_obs <- observationalModel$model %>% map(~.x$biomarker) %>% unlist
72+
biomarkers_abkin <- abkineticsModel$model %>% map(~.x$biomarker) %>% unlist %>% unique
7373

7474
for(b in biomarkers_md) {
7575
if(!b %in% data_sero_name) {
76-
stop("Biomarker, ", b, ", in `modeldefinition$biomarkers` is not a column of serological data; `",
76+
stop("Biomarker, ", b, ", in `biomarkers` is not a column of serological data; `",
7777
paste(data_sero_name, collapse = ", "), "`")
7878
}
7979
}
8080
if(!identical(biomarkers_md, biomarkers_obs) ) {
81-
stop("Biomarkers in observationalModel (", paste(biomarkers_obs, collapse = ", "), ") do not match biomarkers in `modeldefinition$biomarkers` (", paste(biomarkers_md, collapse = ", "), ")")
81+
stop("Biomarkers in observationalModel (", paste(biomarkers_obs, collapse = ", "), ") do not match biomarkers in `biomarkers` (", paste(biomarkers_md, collapse = ", "), ")")
8282
}
8383
if(!identical(biomarkers_md, biomarkers_abkin) ) {
84-
stop("Biomarkers in abkineticsModel (", paste(biomarkers_abkin, collapse = ", "), ") do not match biomarkers in `modeldefinition$biomarkers` (", paste(biomarkers_md, collapse = ", "), ")")
84+
stop("Biomarkers in abkineticsModel (", paste(biomarkers_abkin, collapse = ", "), ") do not match biomarkers in `biomarkers` (", paste(biomarkers_md, collapse = ", "), ")")
8585
}
8686

87-
exposures_md <- modeldefinition$exposureTypes
88-
exposures_obs <- modeldefinition$abkineticsModel$model %>% map(~.x$exposureType) %>% unlist %>% unique
87+
exposures_md <- exposureTypes
88+
exposures_obs <- abkineticsModel$model %>% map(~.x$exposureType) %>% unlist %>% unique
8989

9090
# CHECK EXPSURETYPES ARE WELL DEFINED
9191
if (!is.null(data_known)) {
9292
exposure_type_names <- data_known$exposure_type %>% unique
9393
for(e in exposure_type_names) {
9494
if(!e %in% exposures_md) {
9595
stop("Exposure type, ", e, ", in known exposure data.frame column 'exposure_type' (", paste(exposure_type_names, collapse = ", "),
96-
"is not defined in `modeldefinition$exposureTypes` (", paste(exposures_md, collapse = ", "), ")")
96+
"is not defined in `exposureTypes` (", paste(exposures_md, collapse = ", "), ")")
9797
}
9898
}
9999
}
100100
if(!identical(exposures_md, exposures_obs) ) {
101-
stop("Exposure types in abkineticsModel (", paste(exposures_obs, collapse = ", "), ") do not match exposure types in `modeldefinition$exposureTypes` (", paste(exposures_md, collapse = ", "), ")")
101+
stop("Exposure types in abkineticsModel (", paste(exposures_obs, collapse = ", "), ") do not match exposure types in `exposureTypes` (", paste(exposures_md, collapse = ", "), ")")
102102
}
103-
if(is.null(modeldefinition$exposureFitted)) {
104-
stop("`modeldefinition$exposureFitted` is NULL, please define a biomarker to fit.")
103+
if(is.null(exposureFitted)) {
104+
stop("`exposureFitted` is NULL, please define a biomarker to fit.")
105105
}
106106

107-
exposure_fitted <- modeldefinition$exposureFitted
107+
exposure_fitted <- exposureFitted
108108
if(!exposure_fitted %in% exposures_md) {
109-
stop("The fitted exposure type, ", exposure_fitted, ", is not defined in, `modeldefinition$exposureTypes`: ", paste(exposures_md, collapse = ", "))
109+
stop("The fitted exposure type, ", exposure_fitted, ", is not defined in, `exposureTypes`: ", paste(exposures_md, collapse = ", "))
110110
}
111111

112-
names_obs <- modeldefinition$observationalModel$model %>% map(~.x$name) %>% unlist
113-
names_abkin <- modeldefinition$abkineticsModel$model %>% map(~.x$name) %>% unlist %>% unique
112+
names_obs <- observationalModel$model %>% map(~.x$name) %>% unlist
113+
names_abkin <-abkineticsModel$model %>% map(~.x$name) %>% unlist %>% unique
114114
# Read out into console
115115
cat("There are ", length(biomarkers_md), " measured biomarkers: ", paste(biomarkers_md, collapse = ", "), "\n")
116116
cat("There are ", length(exposures_md), " exposure types in the study period: ", paste(exposures_md, collapse = ", "), "\n")
117-
cat("The fitted exposure type is ", modeldefinition$exposureFitted, "\n")
117+
cat("The fitted exposure type is ", exposureFitted, "\n")
118118
}
119119

120120

121121

122-
check_priors <- function(modeldefinition) {
122+
check_priors <- function(observationalModel, abkineticsModel) {
123123
priors <- bind_rows(
124-
modeldefinition$observationalModel$prior,
125-
modeldefinition$abkineticsModel$prior,
126-
modeldefinition$copModel$prior
124+
observationalModel$prior,
125+
abkineticsModel$prior,
127126
)
128127
if(any(duplicated(priors$par_name))) {
129128
stop("Priors: ", paste0(priors$par_name[duplicated(priors$par_name)], collapse = ", "), " are duplicated, please assign original names to each prior")
@@ -139,7 +138,7 @@ check_priors <- function(modeldefinition) {
139138
}
140139

141140
cat("PRIOR DISTRIBUTIONS", "\n")
142-
cat("Prior parameters of observationalModel are: ", paste(modeldefinition$observationalModel$prior$par_name, collapse = ", "), "\n")
143-
cat("Prior parameters of abkineticsModel are: ", paste(modeldefinition$abkineticsModel$prior$par_name, collapse = ", "), "\n")
141+
cat("Prior parameters of observationalModel are: ", paste(observationalModel$prior$par_name, collapse = ", "), "\n")
142+
cat("Prior parameters of abkineticsModel are: ", paste(abkineticsModel$prior$par_name, collapse = ", "), "\n")
144143

145144
}

R/postprocess.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ postprocess_fit <- function(model_fit) {
5353

5454
n_post <- post$mcmc[[1]] %>% nrow
5555
post_exp_combine <- post$jump
56-
post_inf_combine <- post$inf
5756

5857
post_infexp <- 1:n_chains %>% map_df(
5958
function(i) {

R/utils_data.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#' \describe{
66
#' \item{id}{Unique identifier for each biomarker.}
77
#' \item{time}{Description of the model using the \code{makeModel} and \code{addObservationalModel} functions.}
8-
#' \item{titre}{Description of the prior distribution using the \code{add_par_df} function.}
9-
#' \item{biomarker}{Description of the prior distribution using the \code{add_par_df} function.}
8+
#' \item{titre}{Description of the prior distribution using the \code{addPrior} function.}
9+
#' \item{biomarker}{Description of the prior distribution using the \code{addPrior} function.}
1010
#' }
1111
#'
1212
#' @details Add more data about this model here.
@@ -85,12 +85,12 @@ data_known_exposures_ex1 <- data.frame(
8585
#' \describe{
8686
#' \item{names}{Unique identifier for each exposure type.}
8787
#' \item{model}{Description of the model using the \code{makeModel} and \code{addAbkineticsModel} functions.}
88-
#' \item{prior}{Description of the prior distribution using the \code{add_par_df} function.}
88+
#' \item{prior}{Description of the prior distribution using the \code{addPrior} function.}
8989
#' }
9090
#'
9191
#' @details Add more data about this model here.
9292
#'
93-
#' @seealso \code{\link{makeModel}}, \code{\link{addAbkineticsModel}}, \code{\link{add_par_df}}, for related functions.
93+
#' @seealso \code{\link{makeModel}}, \code{\link{addAbkineticsModel}}, for related functions.
9494
#'
9595
#' @examples
9696
#' # Example usage. This describes the antibody kinetics for a SARS-CoV-2 delta wave using IgG data. First define the antibody kinetics function:
@@ -115,9 +115,9 @@ data_known_exposures_ex1 <- data.frame(
115115
#' addAbkineticsModel("IgG", "delta", TRUE, c("a_d", "b_d", "c_d"), infSerumKinetics)
116116
#' ),
117117
#' prior = dplyr::bind_rows(
118-
#' add_par_df("a_d", -2, 2, "norm", 0, 1), # ab kinetics
119-
#' add_par_df("b_d", 0, 1, "norm", 0.3, 0.05), # ab kinetics
120-
#' add_par_df("c_d", 0, 4, "unif", 0, 4) # ab kinetics
118+
#' addPrior("a_d", -2, 2, "norm", 0, 1), # ab kinetics
119+
#' addPrior("b_d", 0, 1, "norm", 0.3, 0.05), # ab kinetics
120+
#' addPrior("c_d", 0, 4, "unif", 0, 4) # ab kinetics
121121
#' )
122122
#' )
123123
#'
@@ -134,12 +134,12 @@ NULL
134134
#' \describe{
135135
#' \item{names}{Unique identifier for each biomarker.}
136136
#' \item{model}{Description of the model using the \code{makeModel} and \code{addObservationalModel} functions.}
137-
#' \item{prior}{Description of the prior distribution using the \code{add_par_df} function.}
137+
#' \item{prior}{Description of the prior distribution using the \code{addPrior} function.}
138138
#' }
139139
#'
140140
#' @details Add more data about this model here.
141141
#'
142-
#' @seealso \code{\link{makeModel}}, \code{\link{addObservationalModel}}, \code{\link{add_par_df}}, for related functions.
142+
#' @seealso \code{\link{makeModel}}, \code{\link{addObservationalModel}}, \code{\link{addPrior}}, for related functions.
143143
#'
144144
#' @examples
145145
#' # Example usage. This describes the observation model for a SARS-CoV-2 delta wave using IgG data. First define the log likelihood function, which is cauchy, with a LOD at a titre value of log10(40):
@@ -157,7 +157,7 @@ NULL
157157
#' observationalModel <- list(
158158
#' names = c("IgG"),
159159
#' model = makeModel(addObservationalModel("IgG", c("sigma"), obsFunction)),
160-
#' prior = add_par_df("sigma", 0.0001, 4, "unif", 0.0001, 4) # observational model,
160+
#' prior = addPrior("sigma", 0.0001, 4, "unif", 0.0001, 4) # observational model,
161161
#' )
162162
#'
163163
#'

0 commit comments

Comments
 (0)