-
Notifications
You must be signed in to change notification settings - Fork 0
Modify and add some .qmd and .R files. #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
045e53f
823768a
af1cafb
a027066
8a795c5
47ea147
eb24065
1427d5a
c52a509
91aa23a
9ed291e
49965de
db22307
3399623
900f362
cf10e67
c499389
37716a6
c70e4cd
eea461a
6fac38e
6f651b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,6 @@ data/ | |
| /.quarto/ | ||
| README_files | ||
| shigella.Rcheck/ | ||
| shigella*.tar.gz | ||
| shigella*.tgz | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
|
|
||
| ## Bug fixes | ||
|
|
||
| None yet | ||
|
|
||
| ## Internal changes | ||
|
|
||
| * Moved helper functions into `R/` subdirectory (#1) | ||
|
|
||
| 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")}. | ||
| #' @param age_group Optional. Character string specifying the age group. If NULL, retrieved from \code{attr(data, "age_group")}. | ||
| #' | ||
| #' @return A data frame with \code{sample_size}, \code{empirical_se}, and \code{Age_Group}. | ||
| #' @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") | ||
| } | ||
| if (is.null(age_group)) { | ||
| age_group <- attr(data, "age_group") | ||
| } | ||
|
|
||
| data.frame( | ||
| sample_size = sample_size, | ||
| empirical_se = sd(data$incidence.rate, na.rm = TRUE), | ||
| Age_Group = age_group | ||
| ) | ||
| } | ||
|
|
||
| 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, | ||
| #' and combines them into a single data frame. It also adds the sample size for clarity. | ||
| #' | ||
| #' @param results_list A list of simulation results. Each element should contain an element named \code{est1} | ||
| #' which can be summarized. | ||
| #' @param sample_size An integer specifying the sample size used in the simulation. | ||
| #' | ||
| #' @return A data frame combining the selected columns (\code{incidence.rate}, \code{SE}, \code{CI.lwr}, \code{CI.upr}) | ||
| #' 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)) { | ||
| # 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) | ||
|
|
||
| return(final_table) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,3 +45,7 @@ postprocess_jags_output <- function(jags_output) { | |
|
|
||
| return(to_return) | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need to keep the old version of |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #' Description of the function here. | ||
| #' @param nrep Number of repetitions. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
| # Parameters | ||
| dmcmc <- curve_params_shigella_ipab # Curve parameters | ||
| antibodies <- c("IgG") # Antigen-isotypes | ||
| lambda <- observed # Simulated incidence rate per person-year | ||
|
|
||
| # Biologic noise distribution | ||
| dlims <- rbind("IgG" = c(min = 0, max = 0.5)) | ||
|
|
||
| # 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) | ||
| ) | ||
|
|
||
| # Perform simulations in parallel | ||
| results <- future_map(1:n_sim, function(i) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we define this function in a separate step before we apply |
||
| tryCatch({ | ||
| # 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" | ||
| ) | ||
|
|
||
| # 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 | ||
| ) | ||
|
|
||
| # 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)) | ||
|
|
||
| # Ensure sequential processing after function execution | ||
| plan(sequential) | ||
| results <- results |> | ||
| structure( | ||
| sample_size = nrep, | ||
| age_range = paste(range, collapse = " - ") | ||
| ) | ||
| return(results) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,10 @@ ORCID | |
| Postprocess | ||
| Seroresponse | ||
| repo | ||
| HPC | ||
| IgG | ||
| IpaB | ||
| isotypes | ||
| qmd | ||
| seroincidence | ||
| ses | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| Version: 1.0 | ||
| ProjectId: 3be2b60e-1279-4ad7-941b-69ed270815d6 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please don't let RStudio or Posit Cloud delete |
||
|
|
||
| RestoreWorkspace: Default | ||
| SaveWorkspace: Default | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion for line 2:
See related change to end of
simulated_seroincidence2(); metadata such as sample size should stay connected to the corresponding objectthe output of
generate_final_table()should also have attributessample_sizeandage_range