Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to fix R CMD check issues #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
chore: styler::styler_pkg
  • Loading branch information
jemus42 committed Nov 9, 2024
commit 13e28ab6849b7a83abd8dc2959a8ce67792db35f
38 changes: 19 additions & 19 deletions R/apply-function-to-observed-timepoints.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#' Apply Function to Subset Drug History
#'
#' Observations might not be present for certain patients, since they entered
#' the data set later time = 1 or before the end. This function applies, for
#' example, a risk model only to the observed time points.
#'
#' @param drug_history The drug history. Unobserved time points are denoted by
#'
#' Observations might not be present for certain patients, since they entered
#' the data set later time = 1 or before the end. This function applies, for
#' example, a risk model only to the observed time points.
#'
#' @param drug_history The drug history. Unobserved time points are denoted by
#' \code{NA}
#' @param fn A function (Default \code{\link{risk_model_current_use}}).
#'
#' @param fn A function (Default \code{\link{risk_model_current_use}}).
#'
#' @return Vector of the same length as \code{drug_history}. None observed time points
#' are denoted by \code{NA}
#' @export
apply_function_to_observed_timepoints <- function(drug_history,
fn = expard::risk_model_current_use()) {

#' @export
apply_function_to_observed_timepoints <- function(
drug_history,
fn = expard::risk_model_current_use()) {
simulation_time <- length(drug_history)

# determine the indices that are not NA
indices_not_NA <- which(!is.na(drug_history))

# get only the observed drug history given these indices
observed_drug_history <- drug_history[indices_not_NA]

# determine the values for only this part of the drug history
values_fn <- fn(observed_drug_history)

# initialize the risk vector with the same length as the original drug history
values <- rep(NA, simulation_time)

# fill in the observed risks
values[indices_not_NA] <- values_fn

return(values)
}
}
33 changes: 16 additions & 17 deletions R/create2x2table.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,23 @@
#' create2x2table(cohort, method = "drug-era")
#' create2x2table(cohort, method = "patient")
#' @export
create2x2table <- function(drug_ADR_pair, method = c("time-point",
"drug-era",
"patient")) {

create2x2table <- function(drug_ADR_pair, method = c(
"time-point",
"drug-era",
"patient"
)) {
if (!(method[1] %in% c("time-point", "drug-era", "patient"))) {
stop(sprintf("method should be either '%s', '%s' or '%s'",
"time-point", "drug-era", "patient"))
stop(sprintf(
"method should be either '%s', '%s' or '%s'",
"time-point", "drug-era", "patient"
))
}

# initialize tables
table <- list(a = 0, b = 0, c = 0, d = 0, method = method[1])
class(table) <- "cont_table"

if (method[1] == "time-point") {

drug <- drug_ADR_pair$drug_history == 1
ADR <- drug_ADR_pair$adr_history == 1

Expand All @@ -118,11 +120,7 @@ create2x2table <- function(drug_ADR_pair, method = c("time-point",
}

if (method[1] == "drug-era") {



sapply(1:cohort$n_patients, function(k) {

# remove any none observed time points. They are represented by NAs
indices_observed_time_points_drug <- which(!is.na(drug_ADR_pair$drug_history[k, ]))
indices_observed_time_points_adr <- which(!is.na(drug_ADR_pair$adr_history[k, ]))
Expand All @@ -137,9 +135,9 @@ create2x2table <- function(drug_ADR_pair, method = c("time-point",
# in which era we (drug or non-drug) and whether
# the ADR occured during this era
in_drug_era <-
drug_history_patient[1] == 1 # are we currently in a drug era?
drug_history_patient[1] == 1 # are we currently in a drug era?
ADR_happened <-
drug_history_patient[1] == 1 # did the ADR occur during this era?
drug_history_patient[1] == 1 # did the ADR occur during this era?

sapply(2:(simulation_time_patient - 1), function(t) {
if (in_drug_era) {
Expand Down Expand Up @@ -176,7 +174,6 @@ create2x2table <- function(drug_ADR_pair, method = c("time-point",
table$d <<- table$d + 1
}
}

}
ADR_happened <- adr_history_patient[t] == 1
})
Expand All @@ -201,9 +198,11 @@ print.cont_table <- function(x, ...) {
cat(sprintf(" drug |\t%d\t|\t%d\t| %d\n", x$a, x$c, x$a + x$c))
cat(sprintf("not drug |\t%d\t|\t%d\t| %d\n", x$b, x$d, x$b + x$d))
cat("------------------------------------------------\n")
cat(sprintf(" total |\t%d\t|\t%d\t| %d\n", x$a + x$b,
x$c + x$d,
x$a + x$b + x$c + x$d))
cat(sprintf(
" total |\t%d\t|\t%d\t| %d\n", x$a + x$b,
x$c + x$d,
x$a + x$b + x$c + x$d
))

if ((x$a + x$c) == (x$a + x$b + x$c + x$d)) {
cat(crayon::magenta(sprintf("\nwarning: since the number of patients that were prescribed \nthe drug and the total number of patients is the same,\nit might be that the cohort was created like this on purpose")))
Expand Down
124 changes: 63 additions & 61 deletions R/create2x2tables.R
Original file line number Diff line number Diff line change
@@ -1,118 +1,120 @@
#' \eqn{2 \times 2} Tables
#'
#' Creates the \eqn{2 \times 2} contingency tables for *all*
#'
#' Creates the \eqn{2 \times 2} contingency tables for *all*
#' drug-ADR pairs in a specific cohort. The function is basically
#' a wrapper for \code{\link{create2x2table}}
#'
#'
#' \cr\cr
#' A table is structured in the following form:
#' A table is structured in the following form:
#' \tabular{lcc}{
#' \tab ADR \tab not ADR\cr
#' drug \tab \code{a} \tab \code{c}\cr
#' not drug \tab \code{b} \tab \code{d}
#' }
#'
#' # Ways to construct the table
#' The counts can be constructed in three different ways. See for
#' two of them Zorych et al. (2013).
#'
#' ## By individual time-points (\code{time-point})
#' Each time-point is counted separately. The counts are the
#'
#' # Ways to construct the table
#' The counts can be constructed in three different ways. See for
#' two of them Zorych et al. (2013).
#'
#' ## By individual time-points (\code{time-point})
#' Each time-point is counted separately. The counts are the
#' \emph{number of time points} that
#' \itemize{
#' \itemize{
#' \item{\code{a} - the drug was prescribed and the ADR occurred}
#' \item{\code{b} - the drug was not prescribed but the ADR occurred}
#' \item{\code{c} - the drug was prescribed but the ADR did not occur}
#' \item{\code{d} - the drug was not prescribed and the ADR did not occur}
#' }
#' Note that in this case the total count \code{n = a + b + c + d} is
#' the same as the total number of time points observed, i.e., the total
#' number of patients times the number of time points observed:
#' \code{n_patients * simulation_time}.
#'
#' Note that in this case the total count \code{n = a + b + c + d} is
#' the same as the total number of time points observed, i.e., the total
#' number of patients times the number of time points observed:
#' \code{n_patients * simulation_time}.
#'
#' ## By individual patients (\code{patient})
#' In this case, individual patients are counted. The counts are the \emph{number
#' of patients} that
#' In this case, individual patients are counted. The counts are the \emph{number
#' of patients} that
#' \itemize{
#' \item{\code{a} - were prescribed the drug and did experience the ADR }
#' \item{\code{b} - were never prescribed the drug and did experience the ADR}
#' \item{\code{c} - were prescribed the drug and never experienced the ADR}
#' \item{\code{d} - were never prescribed the drug and never experienced the ADR}
#' }
#' In this case, the total count \code{n = a + b + c +d} is the same as
#' In this case, the total count \code{n = a + b + c +d} is the same as
#' the number of patients, \code{n_patients}.
#'
#'
#' ## By individual patients (\code{drug-era})
#' In this case we look at \emph{drug-eras}, i.e., periods in which the
#' patients was prescribed or not prescribed the drug for a longer time.
#' For example, if the patient was prescribed the drug from time point 3 to
#' 6, then that period is called a drug-era.
#' The counts are the \emph{number of drug- and non-drug eras} in which
#' \itemize{
#' In this case we look at \emph{drug-eras}, i.e., periods in which the
#' patients was prescribed or not prescribed the drug for a longer time.
#' For example, if the patient was prescribed the drug from time point 3 to
#' 6, then that period is called a drug-era.
#' The counts are the \emph{number of drug- and non-drug eras} in which
#' \itemize{
#' \item{\code{a} - the drug was prescribed and the ADR occurred}
#' \item{\code{b} - the drug was not prescribed but the ADR occurred}
#' \item{\code{c} - the drug was prescribed but the ADR did not occur}
#' \item{\code{d} - the drug was not prescribed and the ADR did not occur}
#' }
#' In this case, the total count \code{n} is the total number of drug- and
#' In this case, the total count \code{n} is the total number of drug- and
#' non-drug eras.
#'
#' @param cohort A cohort; see \code{\link{generate_cohort}}
#' @param method Method used to construct the table; either
#' \code{time-point}, \code{drug-era} and \code{patient}.
#' See the description for more information (Default:
#'
#' @param cohort A cohort; see \code{\link{generate_cohort}}
#' @param method Method used to construct the table; either
#' \code{time-point}, \code{drug-era} and \code{patient}.
#' See the description for more information (Default:
#' \code{time-point})
#'
#' @return A list of \code{cont_table} objects
#'
#' @references
#' Zorych, I., Madigan, D., Ryan, P., & Bate, A. (2013). Disproportionality methods for
#' pharmacovigilance in longitudinal observational databases.
#' Statistical Methods in Medical Research, 22(1), 39–56.
#' https://doi.org/10.1177/0962280211403602
#' @references
#' Zorych, I., Madigan, D., Ryan, P., & Bate, A. (2013). Disproportionality methods for
#' pharmacovigilance in longitudinal observational databases.
#' Statistical Methods in Medical Research, 22(1), 39–56.
#' https://doi.org/10.1177/0962280211403602
#' @seealso \code{\link{create2x2table}}
#' @examples
#' @examples
#' set.seed(1)
#' cohort <- generate_cohort(n_patients = 200)
#'
#' # create the 2x2 contingency table per time-point,
#' # drug-era and patient:
#' cohort <- generate_cohort(n_patients = 200)
#'
#' # create the 2x2 contingency table per time-point,
#' # drug-era and patient:
#' create2x2table(cohort, method = "time-point")
#' create2x2table(cohort, method = "drug-era")
#' create2x2table(cohort, method = "patient")
#' @export
create2x2tables <- function(cohort,
method = c("time-point",
"drug-era",
"patient"),
method = c(
"time-point",
"drug-era",
"patient"
),
verbose = TRUE) {


if (!(method[1] %in% c("time-point", "drug-era", "patient"))) {
stop(sprintf("method should be either '%s', '%s' or '%s'",
"time-point", "drug-era", "patient"))
if (!(method[1] %in% c("time-point", "drug-era", "patient"))) {
stop(sprintf(
"method should be either '%s', '%s' or '%s'",
"time-point", "drug-era", "patient"
))
}
if (verbose) {

if (verbose) {
cat("Generating 2x2 tables...\n")
pb <- txtProgressBar(min = 0, max = cohort$n_drug_ADR_pairs, style = 3)
pb <- txtProgressBar(min = 0, max = cohort$n_drug_ADR_pairs, style = 3)
}

# initialize tables
tables <- lapply(1:cohort$n_drug_ADR_pairs, function(i) {
tables <- lapply(1:cohort$n_drug_ADR_pairs, function(i) {
table <- create2x2table(cohort[[i]], method)

if (verbose) {
setTxtProgressBar(pb, i)
}

return(table)
})

if (verbose) {
close(pb)
close(pb)
cat("DONE generating tables...\n")
}

return(tables)
}
43 changes: 23 additions & 20 deletions R/determine-first-prescription.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
#' First Prescription
#'
#'
#' \code{determine_first_perscription} returns a time point
#' for the first prescription of the drug of interest
#' for the first prescription of the drug of interest
#' given the time frame of the patient (\code{simulation_time})
#' and the chance for it be prescribed is \code{min_chance_drug}.
#' The patient, in this case, is always prescribed the drug at least
#' once. The time point follows a \eqn{\Gamma} distribution.
#'
#' \emph{Note:} we assume here that the probability of
#' the drug being prescribed does not depend on any previous prescriptions.
#'
#' once. The time point follows a \eqn{\Gamma} distribution.
#'
#' \emph{Note:} we assume here that the probability of
#' the drug being prescribed does not depend on any previous prescriptions.
#'
#' @param n_patients Number of patients (Default: 1)
#' @param simulation_time The total number of time points for this patient
#' @param min_chance_drug The probability of the drug being prescribed
#' @param min_chance_drug The probability of the drug being prescribed
#' at any given time point
#'
#'
#' @return A time point between \code{1} and \code{simulation_time}
#' @examples
#' @examples
#' set.seed(1)
#' determine_first_prescription(n_patients = 4, simulation_time = 10, min_chance_drug = 0.1)
#' # -> [1] 2 3 5 9
#' # -> [1] 2 3 5 9
#' @export
determine_first_prescription <- function(n_patients, simulation_time, min_chance_drug) {

determine_first_prescription <- function(n_patients, simulation_time, min_chance_drug) {
# check correctness input --------
if (n_patients < 1) { stop("n_patients should be >= 1") }
if (simulation_time < 1) { stop("simulation_time should be >= 1") }
if (min_chance_drug <= 0 || min_chance_drug > 1) {
stop("min_chance_drug should be in the interval (0,1]")
if (n_patients < 1) {
stop("n_patients should be >= 1")
}

if (simulation_time < 1) {
stop("simulation_time should be >= 1")
}
if (min_chance_drug <= 0 || min_chance_drug > 1) {
stop("min_chance_drug should be in the interval (0,1]")
}

# determining the probabilities that the drug is prescribed at each
# time point (1, 2, ..., simulation_time). Follows a Gamma distribution
probs <- sapply(1:simulation_time, function(t) (1 - min_chance_drug)^(t - 1))
probs <- probs * min_chance_drug / sum(probs) # normalization

# sample a random time point given the probabilities
return(
sample(x = 1:simulation_time, n_patients, replace = TRUE, prob = probs)
)
}
}
Loading