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

Issue #376: Add epidist_diagnostics to FAQ #377

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ S3method(epidist_stancode,default)
S3method(epidist_stancode,epidist_latent_individual)
S3method(epidist_validate,default)
S3method(epidist_validate,epidist_latent_individual)
export(add_event_vars)
export(add_mean_sd)
export(as_latent_individual)
export(epidist)
Expand Down
1 change: 1 addition & 0 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' `max_treedepth`
#' * `per_at_max_treedepth`: the proportion of samples which attained the
#' `max_treedepth`
#'

Check warning on line 17 in R/diagnostics.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/diagnostics.R,line=17,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' When the fitting algorithm is not `"sampling"` (see [brms::brm()] for other
#' possible algorithms) then diagnostics are yet to be implemented.
#' @param fit A fitted model of class `epidist_fit`
Expand Down
86 changes: 86 additions & 0 deletions R/preprocess.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#' Add columns for interval censoring of primary and secondary events
#'
#' @param linelist ...
#' @param ptime_lwr ...
#' @param ptime_upr ...
#' @param pwindow ...
#' @param stime_lwr ...
#' @param stime_upr ...
#' @param swindow ...
#' @family preprocess
#' @autoglobal
#' @export
add_event_vars <- function(

Check warning on line 13 in R/preprocess.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/preprocess.R,line=13,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 17.
linelist, ptime_lwr = NULL, ptime_upr = NULL, pwindow = NULL,
stime_lwr = NULL, stime_upr = NULL, swindow = NULL
) {
if (!is.null(ptime_lwr)) {
linelist <- dplyr::rename(linelist, ptime_lwr = !!ptime_lwr)
}

if (!is.null(ptime_upr)) {
linelist <- dplyr::rename(linelist, ptime_upr = !!ptime_upr)
}

if (!is.null(pwindow) && is.character(pwindow)) {
linelist <- dplyr::rename(linelist, pwindow = !!pwindow)
}

if (!is.null(stime_lwr)) {
linelist <- dplyr::rename(linelist, stime_lwr = !!stime_lwr)
}

if (!is.null(stime_upr)) {
linelist <- dplyr::rename(linelist, stime_upr = !!stime_upr)
}

if (!is.null(swindow) && is.character(swindow)) {
linelist <- dplyr::rename(linelist, swindow = !!swindow)
}

if (is.numeric(pwindow)) {
# Warn that numeric pwindow overwrites pwindow in data
linelist$pwindow <- pwindow
}

if (is.numeric(swindow)) {
# Warn that numeric swindow overwrites swindow in data
linelist$swindow <- swindow
}

if (is.null(stime_upr)) {
linelist <- mutate(linelist, stime_upr = stime_lwr + swindow)
}

if (is.null(ptime_upr)) {
linelist <- mutate(linelist, ptime_upr = ptime_lwr + pwindow)
}

if (is.null(swindow)) {
linelist <- mutate(linelist, pwindow = stime_upr - stime_lwr)
}

if (is.null(pwindow)) {
linelist <- mutate(linelist, swindow = ptime_upr - ptime_lwr)
}

assert_numeric(linelist$ptime_lwr)
assert_numeric(linelist$ptime_upr)
assert_numeric(linelist$pwindow, lower = 0)
assert_true(
all(linelist$ptime_lwr + linelist$pwindow - linelist$ptime_upr < 1e-6)
)

assert_numeric(linelist$stime_lwr)
assert_numeric(linelist$stime_upr)
assert_numeric(linelist$swindow, lower = 0)
assert_true(
all(linelist$stime_lwr + linelist$swindow - linelist$stime_upr < 1e-6)
)

linelist <- dplyr::relocate(
linelist, ptime_lwr, ptime_upr, pwindow, stime_lwr, stime_upr, swindow
)

return(linelist)
}
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ reference:
desc: Functions for observing data
contents:
- has_concept("observe")
- title: Preprocess
desc: Functions for preprocessing data
contents:
- has_concept("preprocess")
- title: S3 generics
desc: S3 generics for delay modelling
contents:
Expand Down
35 changes: 35 additions & 0 deletions man/add_event_vars.Rd

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

4 changes: 3 additions & 1 deletion man/epidist_diagnostics.Rd

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

36 changes: 36 additions & 0 deletions tests/testthat/test-preprocess.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
test_that("add_event_vars produces equivalent linelists in different ways", { # nolint: line_length_linter.
linelist <- tibble::tibble(
"a" = runif(100),
"b" = 1,
"c" = a + b,
"d" = runif(100, 2, 3),
"e" = 1,
"f" = d + e
)

ll <- linelist |>
add_event_vars(
ptime_lwr = "a", pwindow = "b", ptime_upr = "c",
stime_lwr = "d", swindow = "e", stime_upr = "f"
)

ll2 <- select(linelist, a, c, d, f) |>
add_event_vars(
ptime_lwr = "a", pwindow = 1, ptime_upr = "c",
stime_lwr = "d", swindow = 1, stime_upr = "f"
)

ll3 <- select(linelist, a, b, d, e) |>
add_event_vars(
ptime_lwr = "a", pwindow = "b", stime_lwr = "d", swindow = "e",
)

ll4 <- select(linelist, a, c, d, f) |>
add_event_vars(
ptime_lwr = "a", ptime_upr = "c", stime_lwr = "d", stime_upr = "f",
)

expect_equal(ll, ll2)
expect_equal(ll, ll3)
expect_equal(ll, ll4)
})
6 changes: 6 additions & 0 deletions vignettes/faq.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ library(bayesplot)
mcmc_trace(fit, pars = c("Intercept", "Intercept_sigma"))
```

We also provide a helper function `epidist_diagnostics()` which can be used to obtain common diagnostics used to assess the quality of a fitted model.

```{r message = FALSE}
epidist_diagnostics(fit)
```

## I'd like to run a simulation study

We recommend use of the `purrr` package for running many `epidist` models, for example as a part of a simulation study.
Expand Down
Loading