Skip to content

Commit

Permalink
Issue #431: Remove observation process function (#439)
Browse files Browse the repository at this point in the history
* Remove observe_process

* Update NAMESPACE

* Simplify columns created

* Fix names

* Lint fix

* Need to have obs_time in there also

* Set obs_time for Ebola vignette

* Redoc

* Missing part

* Reabase fix for Ebola vignette

* Rebase

* Linter on imports

* Change to nolint strategy

* Increase tol

* Increase tol again

* Update make_hexsticker.R

---------

Co-authored-by: Sam Abbott <s.e.abbott12@gmail.com>
  • Loading branch information
athowes and seabbs authored Nov 19, 2024
1 parent 153b4cc commit 7205068
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 90 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export(lognormal)
export(new_epidist_latent_model)
export(new_epidist_linelist_data)
export(new_epidist_naive_model)
export(observe_process)
export(predict_delay_parameters)
export(predict_dpar)
export(simulate_exponential_cases)
Expand Down
7 changes: 7 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Generated by roxyglobals: do not edit by hand

utils::globalVariables(c(
".data", # <epidist_diagnostics>
"samples", # <epidist_diagnostics>
".data", # <as_epidist_latent_model.epidist_linelist_data>
"woverlap", # <epidist_stancode.epidist_latent_model>
".data", # <as_epidist_naive_model.epidist_linelist_data>
".data", # <add_mean_sd.lognormal_samples>
".data", # <add_mean_sd.gamma_samples>
"rlnorm", # <simulate_secondary>
".data", # <simulate_secondary>
".data", # <.replace_prior>
"prior_new", # <.replace_prior>
"source_new", # <.replace_prior>
NULL
Expand Down
35 changes: 0 additions & 35 deletions R/observe.R

This file was deleted.

4 changes: 0 additions & 4 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ reference:
desc: Tools for simulating datasets
contents:
- has_concept("simulate")
- title: Observe
desc: Functions for observing data
contents:
- has_concept("observe")
- title: Linelist data
desc: Functions for preparing linelist data
contents:
Expand Down
14 changes: 11 additions & 3 deletions inst/make_hexsticker.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ obs <- outbreak |>
meanlog = secondary_dist$mu[[1]],
sdlog = secondary_dist$sigma[[1]]
) |>
observe_process()
mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
delay_daily = .data$stime_lwr - .data$ptime_lwr
)

obs_time <- 25

truncated_obs <- obs |>
filter(.data$stime_upr <= obs_time) |>
mutate(obs_time = obs_time) |>
filter(.data$stime_upr <= .data$obs_time) |>
slice_sample(n = 200, replace = FALSE)

combined_obs <- bind_rows(
truncated_obs,
mutate(obs, obs_time = max(stime_daily))
mutate(obs, obs_time = max(stime_lwr))
) |>
mutate(obs_time = factor(obs_time))

Expand Down
29 changes: 0 additions & 29 deletions man/observe_process.Rd

This file was deleted.

31 changes: 25 additions & 6 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ sim_obs <- simulate_gillespie() |>
meanlog = meanlog,
sdlog = sdlog
) |>
observe_process() |>
dplyr::filter(.data$stime_upr <= obs_time) |>
dplyr::mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
obs_time = obs_time
) |>
dplyr::filter(.data$stime_upr <= .data$obs_time) |>
dplyr::slice_sample(n = sample_size, replace = FALSE)

# Temporary solution for classing time data
Expand All @@ -41,8 +47,14 @@ sim_obs_gamma <- simulate_gillespie() |>
shape = shape,
rate = rate
) |>
observe_process() |>
dplyr::filter(.data$stime_upr <= obs_time) |>
dplyr::mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
obs_time = obs_time
) |>
dplyr::filter(.data$stime_upr <= .data$obs_time) |>
dplyr::slice_sample(n = sample_size, replace = FALSE)

# Temporary solution for classing time data
Expand Down Expand Up @@ -82,8 +94,14 @@ sim_obs_sex_f <- dplyr::filter(sim_obs_sex, sex == 1) |>
dplyr::select(case, ptime, delay, stime, sex)

sim_obs_sex <- dplyr::bind_rows(sim_obs_sex_m, sim_obs_sex_f) |>
observe_process() |>
dplyr::filter(.data$stime_upr <= obs_time) |>
dplyr::mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
obs_time = obs_time
) |>
dplyr::filter(.data$stime_upr <= .data$obs_time) |>
dplyr::slice_sample(n = sample_size, replace = FALSE)

# Temporary solution for classing time data
Expand All @@ -107,6 +125,7 @@ if (not_on_cran()) {
data = prep_obs, seed = 1, chains = 2, cores = 2, silent = 2, refresh = 0,
backend = "cmdstanr"
)

fit_rstan <- epidist(
data = prep_obs, seed = 1, chains = 2, cores = 2, silent = 2, refresh = 0
)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-int-latent_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ test_that("epidist.epidist_latent_model recovers a sex effect", { # nolint: line
skip_on_cran()
set.seed(1)
draws <- posterior::as_draws_df(fit_sex$fit)
expect_equal(mean(draws$b_Intercept), meanlog_m, tolerance = 0.1)
expect_equal(mean(draws$b_Intercept), meanlog_m, tolerance = 0.3)
expect_equal(
mean(draws$b_Intercept + draws$b_sex), meanlog_f,
tolerance = 0.1
tolerance = 0.3
)
expect_equal(mean(exp(draws$b_sigma_Intercept)), sdlog_m, tolerance = 0.1)
expect_equal(mean(exp(draws$b_sigma_Intercept)), sdlog_m, tolerance = 0.3)
expect_equal(
mean(exp(draws$b_sigma_Intercept + draws$b_sigma_sex)),
sdlog_f,
tolerance = 0.1
tolerance = 0.3
)
expect_s3_class(fit_sex, "brmsfit")
expect_s3_class(fit_sex, "epidist_fit")
Expand Down
12 changes: 9 additions & 3 deletions vignettes/approx-inference.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ library(purrr)
library(tidyr)
library(tibble)
library(tidybayes)
library(cmdstanr)
library(cmdstanr) # nolint
```

To access the approximate inference methods used in this vignette we will need to use the `cmdstanr` backend for `brms` (we generally recommend using this backend for fitting models). To do this, we first need to install CmdStan (see the README for more details). We can check we have everything we need as follows:
Expand All @@ -122,8 +122,14 @@ obs_cens_trunc_samp <- simulate_gillespie(seed = 101) |>
meanlog = meanlog,
sdlog = sdlog
) |>
observe_process() |>
filter(.data$stime_upr <= obs_time) |>
mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
obs_time = obs_time
) |>
filter(.data$stime_upr <= .data$obs_time) |>
slice_sample(n = sample_size, replace = FALSE)
```

Expand Down
2 changes: 1 addition & 1 deletion vignettes/ebola.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ library(tidybayes)
library(modelr)
library(patchwork)
library(lubridate)
library(cmdstanr)
library(cmdstanr) # nolint
```

For users new to `epidist`, before reading this article we recommend beginning with the "[Getting started with `epidist`](http://epidist.epinowcast.org/articles/epidist.html)" vignette.
Expand Down
13 changes: 11 additions & 2 deletions vignettes/epidist.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ This means that rather than exact event times, we observe event times within an
Here we suppose that the interval is daily, meaning that only the date of the primary or secondary event, not the exact event time, is reported (Figure \@ref(fig:cens)):

```{r}
obs_cens <- obs |> observe_process()
obs_cens <- obs |>
mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
delay_daily = stime_lwr - ptime_lwr
)
```

(ref:cens) Interval censoring of the primary and secondary event times obscures the delay times. A common example of this is when events are reported as daily aggregates. While daily censoring is most common, `epidist` supports the primary and secondary events having other delay intervals.
Expand All @@ -176,7 +183,9 @@ This is called (right) truncation, and biases the observation process towards sh

```{r}
obs_time <- 25
obs_cens_trunc <- filter(obs_cens, .data$stime_upr <= obs_time)
obs_cens_trunc <- obs_cens |>
mutate(obs_time = obs_time) |>
filter(.data$stime_upr <= .data$obs_time)
```

Finally, in reality, it's not possible to observe every case.
Expand Down
10 changes: 8 additions & 2 deletions vignettes/faq.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ obs_cens_trunc_samp <- simulate_gillespie(seed = 101) |>
meanlog = meanlog,
sdlog = sdlog
) |>
observe_process() |>
filter(.data$stime_upr <= obs_time) |>
mutate(
ptime_lwr = floor(.data$ptime),
ptime_upr = .data$ptime_lwr + 1,
stime_lwr = floor(.data$stime),
stime_upr = .data$stime_lwr + 1,
obs_time = obs_time
) |>
filter(.data$stime_upr <= .data$obs_time) |>
slice_sample(n = sample_size, replace = FALSE)
linelist_data <- as_epidist_linelist_data(
Expand Down

0 comments on commit 7205068

Please sign in to comment.