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

Add some model components #5

Merged
merged 107 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 100 commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
6491e64
remove extra files
kaitejohnson Jun 26, 2024
342701a
add a .gitignore with R and data stuff
kaitejohnson Jun 26, 2024
4685a13
add git ignore from cfa-repo-template
kaitejohnson Jun 26, 2024
f66494a
make .github folder mirror cfa-repo-template
kaitejohnson Jun 26, 2024
d19fe05
remove attributes
kaitejohnson Jun 26, 2024
eeb3fe6
add bare bones componets of an R package
kaitejohnson Jun 26, 2024
1338587
update description
kaitejohnson Jun 26, 2024
112ae91
add description of package
kaitejohnson Jun 26, 2024
76d8574
Update README.md
kaitejohnson Jun 26, 2024
fcd6b15
fix pre-commit
kaitejohnson Jun 26, 2024
00fac5b
Merge branch 'format-as-package' of https://github.com/CDCgov/ww-infe…
kaitejohnson Jun 26, 2024
620d604
pre-commit on readme
kaitejohnson Jun 26, 2024
d8167b6
attempt to set up pkgdown
kaitejohnson Jun 26, 2024
178c1cf
correct path to deps
kaitejohnson Jun 26, 2024
9d444e8
remove call to package
kaitejohnson Jun 26, 2024
4f8ff3a
Create CODEOWNERS
kaitejohnson Jun 26, 2024
46f79c4
Create CONTRIBUTING.md
kaitejohnson Jun 26, 2024
45d949f
Create NEWS.md
kaitejohnson Jun 26, 2024
47364bd
Create SUPPORT.md
kaitejohnson Jun 26, 2024
9884e8c
add stan model and start of vignette
kaitejohnson Jun 27, 2024
70c69a5
start of generating simulated data
kaitejohnson Jun 27, 2024
d453696
add parameters, package, more documentation to gen simulated data
kaitejohnson Jun 28, 2024
513587b
add in functions for getting delay distributions
kaitejohnson Jun 28, 2024
fcad57a
add functions needed for generating simulated data
kaitejohnson Jun 28, 2024
e858a03
add simulated data as package data
kaitejohnson Jun 29, 2024
5a91ec8
add some initial pre-processing functions from other packages
kaitejohnson Jun 29, 2024
9afd74d
add functions to preprocess wastewater data
kaitejohnson Jun 30, 2024
f09fee2
add minimal hosp data preprocessing
kaitejohnson Jun 30, 2024
9f8ad5d
add internal package data for covid GI and delay
kaitejohnson Jun 30, 2024
c732701
add functions to create stan data, modify model to be more generally …
kaitejohnson Jun 30, 2024
900a5c7
simplify pre-processing in vignette
kaitejohnson Jul 1, 2024
6ead306
add draft of wrapper function to fit model
kaitejohnson Jul 1, 2024
d04ace0
add initialization functions
kaitejohnson Jul 1, 2024
e222e52
add a wrapper function around the model fitting
kaitejohnson Jul 2, 2024
0736f02
fix bug
kaitejohnson Jul 2, 2024
4f2a2d1
update gitignore to exclude stan binary and .Rproj
kaitejohnson Jul 2, 2024
3961d08
make it s.t. ww data needs a column called exclude, add preprocessing…
kaitejohnson Jul 3, 2024
d9bd1d7
add exclude column in preprocessing, make outlier flag optional step
kaitejohnson Jul 3, 2024
2a94332
add functions that set up model specification and mcmc options
kaitejohnson Jul 3, 2024
2894ae1
add wwinference function
kaitejohnson Jul 3, 2024
e6ef714
add post processing functions, realizing that I have an indexing erro…
kaitejohnson Jul 3, 2024
e8a3dfa
fix the handling of passing in col names, error was breaking LOD calling
kaitejohnson Jul 4, 2024
f5b314f
update documentation
kaitejohnson Jul 4, 2024
f558bd9
fix typo
kaitejohnson Jul 4, 2024
00c0543
adjust postprocess function, add in diagnostics
kaitejohnson Jul 5, 2024
f675dc4
add functions for quickly plotting outputs
kaitejohnson Jul 5, 2024
d272090
revert params back to current modle version
kaitejohnson Jul 5, 2024
728d83d
add plotting and diagnostics to vignette
kaitejohnson Jul 5, 2024
1028c72
fix merge conflicts with main from updating package infreastructure"
kaitejohnson Jul 5, 2024
f663ab6
fix merge conflicts with main from updating package infreastructure"
kaitejohnson Jul 5, 2024
06c8147
try running pre-commit locally
kaitejohnson Jul 5, 2024
64ab128
add dependences to DESCRIPTION
kaitejohnson Jul 5, 2024
fe88a39
add more deps
kaitejohnson Jul 5, 2024
b9c4eb8
try removing print statement
kaitejohnson Jul 5, 2024
56037c4
revert
kaitejohnson Jul 5, 2024
102da37
update vignette data
kaitejohnson Jul 5, 2024
b2d064e
fix typos
kaitejohnson Jul 5, 2024
9b3777f
remove example
kaitejohnson Jul 5, 2024
e770235
try adding bookdown to imports
kaitejohnson Jul 5, 2024
81ef6d8
fix to specify ISO8601 format for dates
kaitejohnson Jul 8, 2024
e7871a0
Update R/wwinference.R
kaitejohnson Jul 9, 2024
448af6f
Update inst/stan/functions/utils.stan
kaitejohnson Jul 9, 2024
4ee29b6
update checkers documentation, remove usesethis from imports
kaitejohnson Jul 9, 2024
10a030d
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 9, 2024
33390d4
remove additional stan model
kaitejohnson Jul 9, 2024
b751780
Exclude data-raw in dependency check
kaitejohnson Jul 9, 2024
933029b
remove print statement
kaitejohnson Jul 9, 2024
60eac88
move bookdown to suggests
kaitejohnson Jul 9, 2024
e59f421
Update R/delay_distribs.R
kaitejohnson Jul 10, 2024
00ce331
remove sgtf call
kaitejohnson Jul 10, 2024
8556a29
use to_simplex on discretized growth adjusted weibull
kaitejohnson Jul 10, 2024
965bc1f
fix description incubation period
kaitejohnson Jul 10, 2024
d28c419
use to simplex again
kaitejohnson Jul 10, 2024
307c966
revise figure description to be more general
kaitejohnson Jul 10, 2024
1265741
Update R/figures.R
kaitejohnson Jul 10, 2024
93c5355
Update R/figures.R
kaitejohnson Jul 10, 2024
64c7ad1
Update R/figures.R
kaitejohnson Jul 10, 2024
5b5d146
fix documentation and set rhat tolerance as fxn arg
kaitejohnson Jul 10, 2024
57399fc
change from p_high rhat to frac high rhat
kaitejohnson Jul 10, 2024
d56203b
rename postprocess to get_draws_df
kaitejohnson Jul 10, 2024
89b6178
Update R/preprocessing.R description
kaitejohnson Jul 10, 2024
6c45004
Update R/preprocessing.R description for hosp data
kaitejohnson Jul 10, 2024
40fa4c9
Update R/utils.R fix typo
kaitejohnson Jul 10, 2024
5919d8c
Update R/utils.R
kaitejohnson Jul 10, 2024
dd881a9
Update R/utils.R
kaitejohnson Jul 10, 2024
9144ec3
Update R/utils.R
kaitejohnson Jul 10, 2024
d2ebb91
Update R/utils.R
kaitejohnson Jul 10, 2024
5323019
Update R/utils.R
kaitejohnson Jul 10, 2024
6bc80d4
Update R/utils.R
kaitejohnson Jul 10, 2024
c408929
make col name for eval data general
kaitejohnson Jul 10, 2024
ba2cec8
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 10, 2024
8668baf
run pre-commit on all files
kaitejohnson Jul 10, 2024
cad3aa4
add col name argument to vignette
kaitejohnson Jul 10, 2024
9f8abb2
adjust R(t) figure title and y-axis label
kaitejohnson Jul 10, 2024
164fe96
fix typos in wwinference.Rmd
kaitejohnson Jul 10, 2024
8942981
Update vignettes/wwinference.Rmd
kaitejohnson Jul 10, 2024
0a61076
rename postprocess to get draws_df
kaitejohnson Jul 10, 2024
99ecb80
remove the functions needed to generate delay distribs from package f…
kaitejohnson Jul 10, 2024
2a4c7ec
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 10, 2024
f9849ba
fix documentation of eval data in figures
kaitejohnson Jul 10, 2024
bf26b3d
add documentation to functions in covid_pmf generation, remove drop f…
kaitejohnson Jul 10, 2024
a2f97a7
remove documentation of drop first and renormalize
kaitejohnson Jul 10, 2024
28f6d2d
Update data-raw/covid_pmfs.R
dylanhmorris Jul 10, 2024
d61400f
fix missed namespace call
kaitejohnson Jul 10, 2024
d0e35ad
add to change log with first PR into main
kaitejohnson Jul 10, 2024
c5b6348
Merge branch 'add-some-model-components' of https://github.com/CDCgov…
kaitejohnson Jul 10, 2024
1f892d4
run pre-commit
kaitejohnson Jul 10, 2024
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
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
^docs$
^pkgdown$
^\.github$
^.*\.Rproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
5 changes: 5 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ jobs:
extra-packages: any::pkgdown local::.
needs: website

- name: "Install cmdstan via cmdstanr"
uses: epinowcast/actions/install-cmdstan@v1
with:
cmdstan-version: "latest"

- name: Build site
run: "pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)"
shell: Rscript {0}
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#####
# Exclude many data and output file types by default

# Exclude stan compiled model

inst/stan/wwinference

dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
# Data
*.csv
*.tsv
Expand Down Expand Up @@ -235,6 +239,8 @@ cython_debug/
# RStudio files
.Rproj.user/

*.Rproj

# produced vignettes
vignettes/*.html
vignettes/*.pdf
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ repos:
- id: style-files
args: [--style_pkg=styler, --style_fun=tidyverse_style,
--cache-root=styler-perm]
- id: roxygenize
- id: use-tidy-description
- id: lintr
- id: readme-rmd-rendered
Expand All @@ -46,6 +45,7 @@ repos:
- id: no-print-statement
- id: no-debug-statement
- id: deps-in-desc
exclude: data-raw
#####
# Java
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
Expand Down
21 changes: 20 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,24 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Suggests:
testthat (>= 3.0.0)
testthat (>= 3.0.0),
bookdown
Config/testthat/edition: 3
LazyData: true
Imports:
fs,
dplyr,
lubridate,
glue,
RcppTOML,
cli,
tibble,
tidybayes,
tidyr,
purrr,
withr,
cmdstanr (>= 0.8.0),
rlang,
scales
Remotes:
stan-dev/cmdstanr
67 changes: 67 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,70 @@
# Generated by roxygen2: do not edit by hand

export(add_pmfs)
export(add_time_indexing)
export(compile_model)
export(convert_to_logmean)
export(convert_to_logsd)
export(create_dir)
export(drop_first_and_renormalize)
export(flag_ww_outliers)
export(generate_simulated_data)
export(get_count_data_sizes)
export(get_count_indices)
export(get_count_values)
export(get_draws_df)
export(get_ind_m)
export(get_mcmc_options)
export(get_model_diagnostic_flags)
export(get_model_spec)
export(get_params)
export(get_plot_forecasted_counts)
export(get_plot_global_rt)
export(get_plot_subpop_rt)
export(get_plot_ww_conc)
export(get_stan_data)
export(get_subpop_data)
export(get_ww_data_indices)
export(get_ww_data_sizes)
export(get_ww_values)
export(indicate_ww_exclusions)
export(preprocess_hosp_data)
export(preprocess_ww_data)
export(to_simplex)
export(validate_paramlist)
export(wwinference)
importFrom(cmdstanr,cmdstan_model)
importFrom(dplyr,arrange)
importFrom(dplyr,as_tibble)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
importFrom(dplyr,ungroup)
importFrom(ggplot2,aes)
importFrom(ggplot2,facet_grid)
importFrom(ggplot2,facet_wrap)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_hline)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_ribbon)
importFrom(ggplot2,geom_vline)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_colour_discrete)
importFrom(ggplot2,scale_fill_discrete)
importFrom(ggplot2,scale_x_date)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,theme)
importFrom(lubridate,ymd)
importFrom(tidybayes,spread_draws)
importFrom(tidybayes,stat_halfeye)
importFrom(tidybayes,stat_slab)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
45 changes: 45 additions & 0 deletions R/checkers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Check that all dates in dataframe passed in are before a specified date
#'
#' @param df dataframe with `date` column
#' @param max_date string indicating the maximum date in ISO8601 convention
#' e.g. YYYY-MM-DD
#' @param call Calling environment to be passed to the type checker
#'
#' @return NULL, invisibly
check_date <- function(df, max_date, call = rlang::caller_env()) {
if (max(df$date) > max_date) {
cli::cli_abort(
c(
"The data passed in has observations beyond the specified",
"maximum date. Either this is the incorrect vintaged",
"data, or the data needs to be filtered to only contain",
"observations before the maximum date"
),
call = call,
class = "wwinference_input_data_error"
)
}
invisible()
}

#' Check that all elements of a vector are non-negative
#'
#' @param x vector of arguments to check for negativity
#' @param call Calling environment to be passed to the type checker
#'
#' @return NULL, invisibly
check_elements_non_neg <- function(x, arg = "x", call = rlang::caller_env()) {
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
# Greater than or equal to 0 or is NA
is_non_neg <- (x >= 0) | is.na(x)
if (!all(is_non_neg)) {
cli::cli_abort(
c("{.arg {arg}} has negative elements",
"!" = "All elements must be 0 or greater",
"i" = "Elements {.val {which(!is_non_neg)}} are negative"
),
class = "wwinference_input_data_error",
call = call
)
}
invisible()
}
95 changes: 95 additions & 0 deletions R/compile_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#' Compile a stan model while pointing at the package default
#' include directory (`stan`) for #include statements
#'
#' @description
#' This function reads in and optionally compiles a Stan model.
#' It is written to search the installed package `stan` directory
#' for additional source files to include. Within each stan file,
#' use #include {path to your file with the `stan` directory}.stan
#'
#' @details The code for this function has been adapted
#' from code written (under an MIT license) as part of
#' the [`epinowcast`](https://github.com/epinowcast/epinowcast)
#' R package.
#'
#' @param model_filepath path to .stan file defining the model, default is
#' `system.file("stan", "wwinference.stan", package = "wwinference")
#' @param include_paths path(s) to directories to search for files
#' specified in #include statements. Passed to [cmdstanr::cmdstan_model()].
#' Defaults to the `stan` subdirectory of the installed
#' `wwinference` package.
#' @param threads Number of threads to use in model compilation,
#' as an integer. Passed to [cmdstanr::cmdstan_model()].
#' Default `FALSE` (use single-threaded compilation).
#' @param target_dir Directory in which to save the compiled
#' stan model binary. Passed as the `dir` keyword argument to
#' [cmdstanr::cmdstan_model()]. Defaults to a temporary directory
#' for the R session (the output of [tempdir()]).
#' @param stanc_options Options for the stan compiler passed to
#' [cmdstanr::cmdstan_model()], as a list. See that function's
#' documentation for more details. Default `list()` (use default
#' options).
#' @param cpp_options Options for the C++ compiler passed to
#' [cmdstanr::cmdstan_model()], as a list. See that function's
#' documentation for more details. Default `list()` (use default
#' options).
#' @param verbose Write detailed output to the terminal while
#' executing the function? Boolean, default `TRUE`.
#' @param ... Additional keyword arguments passed to
#' [cmdstanr::cmdstan_model()].
#'
#' @return The resulting `cmdstanr` model object, as the output
#' of [cmdstanr::cmdstan_model()].
#' @export
compile_model <- function(model_filepath = system.file("stan",
"wwinference.stan",
package = "wwinference"
),
include_paths = system.file(
"stan",
package = "wwinference"
),
threads = FALSE,
target_dir = tempdir(),
stanc_options = list(),
cpp_options = list(),
verbose = TRUE,
...) {
if (verbose) {
cli::cli_inform(
glue::glue(paste0(
"Using model source file: ",
"{model_filepath}"
))
)
cli::cli_inform(
sprintf(
"Using include paths: %s",
toString(include_paths)
)
)
}

create_dir(target_dir)

model <- cmdstanr::cmdstan_model(
kaitejohnson marked this conversation as resolved.
Show resolved Hide resolved
model_filepath,
include_paths = include_paths,
compile = TRUE,
stanc_options = stanc_options,
cpp_options = cpp_options,
threads = threads,
dir = target_dir,
...
)

if (verbose) {
cli::cli_inform(paste0(
"Model compiled or loaded successfully; ",
"model executable binary located at: ",
"{model$exe_file()}"
))
}

return(model)
}
Loading
Loading