Skip to content

Commit

Permalink
reorg and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
seabbs committed Sep 4, 2024
1 parent b050933 commit d77ed7f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
^Meta$
^\.vscode$
^CITATION\.cff$
^stan\.Rmd$
^using-stan-tools\.Rmd$
^fitting-dists-with-stan\.Rmd$
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

* Added support for `swindow = 0` to `rprimarycensoreddist` to allow for non-secondary event censored distributions.
* Adapted `rprimarycensoreddist` so that truncation is based on the primary censored distribution before secondary events are censored. This better matches the generative process.
* Added a new Stan interface tool to enable finding which files functions are implemented in the Stan code.

## Documentation

* Added a getting started vignette.
* Added a vignette showcasing how to use the package Stan code in a modeling workflow.
* Added a vignette showcasing how to use the package Stan code with `cmdstanr`.

# primarycensoreddist 0.1.0

Expand Down
6 changes: 4 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ navbar:
articles:
text: Articles
menu:
- text: How to use primarycensoreddist with Stan
href: articles/using-stan-tools.html
- text: Fitting distributions using primarycensorseddist and cmdstan
href: articles/stan.html
href: articles/fitting-dists-with-stan.html
- text: Fitting distributions using primarycensorseddist and fitdistr
href: articles/fitdistr.html
href: articles/fitting-dists-with-fitdistr.html

authors:
Sam Abbott:
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-pcd-stan-tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,31 @@ test_that("pcd_load_stan_functions loads functions from specific files", {
}
expect_false(grepl("expgrowth_pdf", stan_code, fixed = TRUE))
})

test_that("pcd_stan_files returns correct files", {
# Test with no functions specified
all_files <- pcd_stan_files()
expect_type(all_files, "character")
expect_gt(length(all_files), 0)
expect_true(all(grepl("\\.stan$", all_files)))

Check warning on line 123 in tests/testthat/test-pcd-stan-tools.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-pcd-stan-tools.R,line=123,col=25,[string_boundary_linter] Use !is.na(x) & endsWith(x, string) to detect a fixed terminal substring, or, if missingness is not a concern, just endsWith. Doing so is more readable and more efficient.

# Test with specific functions
expgrowth_functions <- c("expgrowth_pdf", "expgrowth_lpdf")
expgrowth_files <- pcd_stan_files(functions = expgrowth_functions)
expect_type(expgrowth_files, "character")
expect_gt(length(expgrowth_files), 0)
expect_true(all(grepl("expgrowth", expgrowth_files)))

Check warning on line 130 in tests/testthat/test-pcd-stan-tools.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-pcd-stan-tools.R,line=130,col=25,[fixed_regex_linter] This regular expression is static, i.e., its matches can be expressed as a fixed substring expression, which is faster to compute. Here, you can use "expgrowth" with fixed = TRUE.

# Test with functions from different files
mixed_functions <- c("expgrowth_pdf", "primary_censored_integrand")
mixed_files <- pcd_stan_files(functions = mixed_functions)
expect_type(mixed_files, "character")
expect_gt(length(mixed_files), 1)
expect_true(any(grepl("expgrowth", mixed_files)))

Check warning on line 137 in tests/testthat/test-pcd-stan-tools.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-pcd-stan-tools.R,line=137,col=25,[fixed_regex_linter] This regular expression is static, i.e., its matches can be expressed as a fixed substring expression, which is faster to compute. Here, you can use "expgrowth" with fixed = TRUE.
expect_true(any(grepl("primary_censored", mixed_files)))

Check warning on line 138 in tests/testthat/test-pcd-stan-tools.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-pcd-stan-tools.R,line=138,col=25,[fixed_regex_linter] This regular expression is static, i.e., its matches can be expressed as a fixed substring expression, which is faster to compute. Here, you can use "primary_censored" with fixed = TRUE.

# Test with non-existent function
non_existent <- pcd_stan_files(functions = "non_existent_function")
expect_type(non_existent, "character")
expect_length(non_existent, 0)
})
File renamed without changes.
18 changes: 18 additions & 0 deletions vignettes/fitting-dists-with-stan.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: "Fitting distributions using primarycensorseddist and cmdstan"
description: "A guide on how to use primarycensoreddist with Stan for Bayesian inference of epidemiological delay distributions."
author: Sam Abbott
output:
bookdown::html_document2:
fig_caption: yes
code_folding: show
pkgdown:
as_is: true
bibliography: library.bib
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/apa-numeric-superscript-brackets.csl
link-citations: true
vignette: >
%\VignetteIndexEntry{Fitting distributions using primarycensorseddist and cmdstan}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
37 changes: 4 additions & 33 deletions vignettes/stan.Rmd → vignettes/using-stan-tools.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Fitting distributions using primarycensorseddist and cmdstan"
description: "A guide on how to use primarycensoreddist with Stan for Bayesian inference."
title: "How to use primarycensoreddist with Stan"
description: "A guide on how to use primarycensoreddist with Stan."
author: Sam Abbott
output:
bookdown::html_document2:
Expand All @@ -12,7 +12,7 @@ bibliography: library.bib
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/apa-numeric-superscript-brackets.csl
link-citations: true
vignette: >
%\VignetteIndexEntry{Fitting distributions using primarycensorseddist and cmdstan}
%\VignetteIndexEntry{How to use primarycensoreddist with Stan}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Expand All @@ -22,13 +22,12 @@ vignette: >

## What are we going to do in this Vignette

In this vignette, we'll explore how to use the `primarycensoreddist` package in conjunction with Stan for Bayesian inference on censored data. We'll cover the following key points:
In this vignette, we'll explore how to use the `primarycensoreddist` package in conjunction with Stan. We'll cover the following key points:

1. Introduction to Stan and its relevance for our analysis
2. Overview of the packages we'll be using
3. How to access and use Stan functions provided by `primarycensoreddist`
4. Methods for integrating these Stan functions into your workflow
5. Practical examples of fitting distributions to censored data using Stan

## What is Stan and why are we using it

Expand Down Expand Up @@ -137,31 +136,3 @@ We can now use the function in R. Note that this may get slightly more complicat
```{r}
expgrowth_rng(0, 1, 0.4)
```

# Fitting a delay estimation model

## Simulating data in R

## Writing a naive model in Stan

## Fitting the model using cmdstan

## Comparing posterior estimates with true values

## Writing a model using primarycensoreddist Stan functions

## Comparing posterior estimates with true values

# Fitting a convolution model

## Simulating data in R

## Writing a model using primarycensoreddist Stan functions

## Fitting the model using cmdstan

## Comparing posterior estimates with true values

# Other options for fitting primarycensoreddist models with Stan

## Future support for other packages

0 comments on commit d77ed7f

Please sign in to comment.