Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
^vignettes/articles/*_files$
^vignettes/articles$
^codecov\.yml$
^shigella\.Rcheck$
^shigella.*\.tar\.gz$
^shigella.*\.tgz$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ data/
/.quarto/
*.pdf
README_files
shigella.Rcheck/
shigella*.tar.gz
shigella*.tgz
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shigella
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9003
Version: 0.0.0.9004
Authors@R: c(
person("Kwan Ho", "Lee", , "ksjlee@ucdavis.edu", role = c("aut", "cre")),
person("Douglas Ezra", "Morrison", , "demorrison@ucdavis.edu", role = c("aut"),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(calculate_metrics)
export(generate_final_table)
export(postprocess_jags_output)
export(simulate_seroincidence2)
importFrom(dplyr,any_of)
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## Bug fixes

None yet

## Internal changes

* Moved helper functions into `R/` subdirectory (#1)
Expand Down
33 changes: 33 additions & 0 deletions R/calculate_metrics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' Calculate Empirical Standard Error for Incidence Rates
#'
#' This function computes the empirical standard error (SE) of incidence rates
#' from the provided dataset and adds sample size and age group information.
#'
#' @param data A data frame containing a column named \code{incidence.rate}.
#' @param sample_size Optional. Integer representing the sample size. If NULL, retrieved from \code{attr(data, "sample_size")}.

Check warning on line 7 in R/calculate_metrics.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/calculate_metrics.R,line=7,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 127 characters.
#' @param age_group Optional. Character string specifying the age group. If NULL, retrieved from \code{attr(data, "age_group")}.

Check warning on line 8 in R/calculate_metrics.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/calculate_metrics.R,line=8,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 128 characters.
#'
#' @return A data frame with \code{sample_size}, \code{empirical_se}, and \code{Age_Group}.

Check warning on line 10 in R/calculate_metrics.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/calculate_metrics.R,line=10,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 91 characters.
#' @examples
#' \dontrun{
#' data <- data.frame(incidence.rate = c(0.1, 0.2, 0.15, 0.18))
#' attr(data, "sample_size") <- 100
#' attr(data, "age_group") <- "Age 0-2"
#' calculate_metrics(data)
#' }
#' @export
calculate_metrics <- function(data, sample_size = NULL, age_group = NULL) {
if (is.null(sample_size)) {
sample_size <- attr(data, "sample_size")

Check warning on line 21 in R/calculate_metrics.R

View check run for this annotation

Codecov / codecov/patch

R/calculate_metrics.R#L20-L21

Added lines #L20 - L21 were not covered by tests
}
if (is.null(age_group)) {
age_group <- attr(data, "age_group")

Check warning on line 24 in R/calculate_metrics.R

View check run for this annotation

Codecov / codecov/patch

R/calculate_metrics.R#L23-L24

Added lines #L23 - L24 were not covered by tests
}

Check warning on line 26 in R/calculate_metrics.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/calculate_metrics.R,line=26,col=1,[trailing_whitespace_linter] Remove trailing whitespace.
data.frame(
sample_size = sample_size,
empirical_se = sd(data$incidence.rate, na.rm = TRUE),
Age_Group = age_group
)

Check warning on line 31 in R/calculate_metrics.R

View check run for this annotation

Codecov / codecov/patch

R/calculate_metrics.R#L27-L31

Added lines #L27 - L31 were not covered by tests
}

Check warning on line 33 in R/calculate_metrics.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/calculate_metrics.R,line=33,col=1,[trailing_blank_lines_linter] Remove trailing blank lines.
30 changes: 23 additions & 7 deletions R/generate_final_table.R
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for line 2:

  generate_final_table <- function(results_list, sample_size = attr(results_list, "sample_size")) {

See related change to end of simulated_seroincidence2(); metadata such as sample size should stay connected to the corresponding object

the output of generate_final_table() should also have attributes sample_size and age_range

Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
# Define a function to generate final tables
#' Generate Final Table from Simulation Results
#'
#' This function loops through a list of simulation results, extracts the required columns,

Check warning on line 3 in R/generate_final_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/generate_final_table.R,line=3,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 91 characters.
#' and combines them into a single data frame. It also adds the sample size for clarity.

Check warning on line 4 in R/generate_final_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/generate_final_table.R,line=4,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 88 characters.
#'
#' @param results_list A list of simulation results. Each element should contain an element named \code{est1}

Check warning on line 6 in R/generate_final_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/generate_final_table.R,line=6,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 109 characters.
#' which can be summarized.
#' @param sample_size An integer specifying the sample size used in the simulation.

Check warning on line 8 in R/generate_final_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/generate_final_table.R,line=8,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 83 characters.
#'
#' @return A data frame combining the selected columns (\code{incidence.rate}, \code{SE}, \code{CI.lwr}, \code{CI.upr})

Check warning on line 10 in R/generate_final_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/generate_final_table.R,line=10,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 119 characters.
#' from each simulation result along with the sample size and an index for tracking.
#' @examples
#' \dontrun{
#' # Suppose you have a list of simulation results
#' final_table <- generate_final_table(results_list, sample_size = 100)
#' }
#' @export
generate_final_table <- function(results_list, sample_size) {
# Initialize an empty list to store the results
summary_results <- list()

# Loop through each of the 100 results and extract the required columns
for (i in 1:200) {
# Loop through each result and extract the required columns
for (i in 1:length(results_list)) {

Check warning on line 23 in R/generate_final_table.R

View check run for this annotation

Codecov / codecov/patch

R/generate_final_table.R#L23

Added line #L23 was not covered by tests
# Extract the summary for each result
result_summary <- summary(results_list[[i]]$est1)

# Select the required columns
# Select the required columns (ensure that result_summary is a data frame)
extracted_columns <- result_summary %>%
select(incidence.rate, SE, CI.lwr, CI.upr)

# Add a column for the index (optional, for tracking)
extracted_columns <- extracted_columns %>%
mutate(index = i)

# Append to the list
# Append the extracted data to the list
summary_results[[i]] <- extracted_columns
}

# Combine all results into a single data frame
# Combine all results into a single data frame and add the sample size column for clarity
final_table <- bind_rows(summary_results) %>%
mutate(sample_size = sample_size) # Add sample size column for clarity
mutate(sample_size = sample_size)

Check warning on line 41 in R/generate_final_table.R

View check run for this annotation

Codecov / codecov/patch

R/generate_final_table.R#L41

Added line #L41 was not covered by tests

return(final_table)
}
4 changes: 4 additions & 0 deletions R/postprocess_jags_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ postprocess_jags_output <- function(jags_output) {

return(to_return)
}




75 changes: 75 additions & 0 deletions R/simulate_seroincidence2.R
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need to keep the old version ofsimulate_seroincidence.R, or can this new function replace that one?

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' Description of the function here.
#' @param nrep Number of repetitions.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repetitions of what? does this mean number of observations in the dataset (i.e. sample size)?

#' @param n_sim Number of simulations.
#' @param observed Observed incidence rate.
#' @param range Range for simulation.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range of what? age?

#' @return A list of simulated seroincidence results.
#' @export
# Define the simulation function
# Define the simulation function
simulate_seroincidence2 <- function(nrep, n_sim, observed, range = NULL) {
# Set parallel plan inside function to avoid issues with distributed nodes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what kinds of issues did you run into?

plan(multicore) # Use multiple cores for parallel processing (works best on HPC)

Check warning on line 12 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L12

Added line #L12 was not covered by tests

# Parameters
dmcmc <- curve_params_shigella_ipab # Curve parameters
antibodies <- c("IgG") # Antigen-isotypes
lambda <- observed # Simulated incidence rate per person-year

Check warning on line 17 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L15-L17

Added lines #L15 - L17 were not covered by tests

# Biologic noise distribution
dlims <- rbind("IgG" = c(min = 0, max = 0.5))

Check warning on line 20 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L20

Added line #L20 was not covered by tests

# Noise parameters
cond <- tibble(
antigen_iso = c("IgG"),
nu = c(0.5), # Biologic noise (nu)
eps = c(0.25), # Measurement noise (eps)
y.low = c(25), # Low cutoff (llod)
y.high = c(200000) # High cutoff (y.high)
)

Check warning on line 29 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L23-L29

Added lines #L23 - L29 were not covered by tests

# Perform simulations in parallel
results <- future_map(1:n_sim, function(i) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we define this function in a separate step before we apply future_map() to it?
Long inline functions are hard to read.

tryCatch({

Check warning on line 33 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L32-L33

Added lines #L32 - L33 were not covered by tests
# Generate cross-sectional data
csdata <- sim_pop_data(
curve_params = dmcmc,
lambda = lambda,
n.smpl = nrep,
age_range = range,
antigen_isos = antibodies,
n.mc = 0,
renew_params = TRUE, # Use different parameters for each simulation
add.noise = TRUE,
noise_limits = dlims,
format = "long"
)

Check warning on line 46 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L35-L46

Added lines #L35 - L46 were not covered by tests

# Estimate seroincidence
est <- est.incidence(
pop_data = csdata,
curve_params = dmcmc,
noise_params = cond,
lambda_start = 0.1,
build_graph = TRUE,
verbose = FALSE,
print_graph = FALSE,
antigen_isos = antibodies
)

Check warning on line 58 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L49-L58

Added lines #L49 - L58 were not covered by tests

# Return results for this simulation
list(csdata = csdata, est1 = est)
}, error = function(e) {
return(list(error = e$message)) # Capture and store errors instead of stopping execution
})
}, .options = furrr_options(seed = TRUE))

Check warning on line 65 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L61-L65

Added lines #L61 - L65 were not covered by tests

# Ensure sequential processing after function execution
plan(sequential)
results <- results |>
structure(
sample_size = nrep,
age_range = paste(range, collapse = " - ")
)
return(results)

Check warning on line 74 in R/simulate_seroincidence2.R

View check run for this annotation

Codecov / codecov/patch

R/simulate_seroincidence2.R#L68-L74

Added lines #L68 - L74 were not covered by tests
}
7 changes: 7 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ ORCID
Postprocess
Seroresponse
repo
HPC
IgG
IpaB
isotypes
qmd
seroincidence
ses
30 changes: 30 additions & 0 deletions man/calculate_metrics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/generate_final_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/simulate_seroincidence2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion project.Rproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Version: 1.0
ProjectId: 3be2b60e-1279-4ad7-941b-69ed270815d6
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't let RStudio or Posit Cloud delete ProjectId (they might try to do it automatically).
If it happens, just revert the change to that file or don't commit the change to git.
Updating RStudio to the latest release should stop it from trying to delete this line.
More info here: https://forum.posit.co/t/use-for-projectid-in-rproj-file/196345


RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
Loading
Loading