Skip to content

Commit

Permalink
Version of delay_samples working with hard coded lognormal
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed Jul 31, 2024
1 parent 7f5f730 commit ad14b55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
30 changes: 15 additions & 15 deletions R/delay-samples.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#' @export
delay_samples <- function(fit) {
dpars <- fit$family$dpars
lp_mu <- brms::posterior_linpred(fit, transform = TRUE, dpar = dpars[1])
lp_sigma <- brms::posterior_linpred(fit, transform = TRUE, dpar = dpars[2])

# lp_mu <- brms::posterior_linpred(fit, transform = TRUE, dpar = "mu") |>
# as.table() |>
# as.data.table()
# names(lp_mu) <- c("draw", "index", "mu")
# lp_sigma <- brms::posterior_linpred(fit, transform = TRUE, dpar = "sigma") |>
# as.table() |>
# as.data.table(value.name = "sigma")
# names(lp_sigma) <- c("draw", "index", "sigma")
# lp <- dplyr::left_join(lp_mu, lp_sigma)
# class(lp) <- c(class(lp), "lognormal_samples")
# x <- add_mean_sd(lp)
# Warning: only works at the moment with lognormal!
pp <- brms::prepare_predictions(fit)
lp_mu <- brms::get_dpar(pp, dpar = "mu", inv_link = TRUE)
lp_sigma <- brms::get_dpar(pp, dpar = "sigma", inv_link = TRUE)
# Assumes lp_mu and lp_sigma have same dimensions
df <- expand.grid("index" = 1:nrow(lp_mu), "draw" = 1:ncol(lp_mu))

Check warning on line 8 in R/delay-samples.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/delay-samples.R,line=8,col=31,[seq_linter] 1:nrow(...) is likely to be wrong in the empty edge case. Use seq_len(nrow(...)) instead.

Check warning on line 8 in R/delay-samples.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/delay-samples.R,line=8,col=55,[seq_linter] 1:ncol(...) is likely to be wrong in the empty edge case. Use seq_len(ncol(...)) instead.
df[["mu"]] <- as.vector(lp_mu)
df[["sigma"]] <- as.vector(lp_sigma)
class(df) <- c(class(df), "lognormal_samples")
dt <- as.data.table(df)
dt <- add_mean_sd(dt)
}

# data <- as_latent_individual(sim_obs)

Check warning on line 16 in R/delay-samples.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/delay-samples.R,line=16,col=3,[commented_code_linter] Commented code should be removed.
# fit <- epidist(data)

Check warning on line 17 in R/delay-samples.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/delay-samples.R,line=17,col=3,[commented_code_linter] Commented code should be removed.
20 changes: 5 additions & 15 deletions tests/testthat/test-unit-postprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ test_that("add_mean_sd.lognormal_samples works with posterior samples from the l
set.seed(1)
prep_obs <- as_latent_individual(sim_obs)
fit <- epidist(data = prep_obs, seed = 1)
lp_mu <- brms::posterior_linpred(fit, transform = TRUE, dpar = "mu") |>
as.table() |>
as.data.table()
names(lp_mu) <- c("draw", "index", "mu")
lp_sigma <- brms::posterior_linpred(fit, transform = TRUE, dpar = "sigma") |>
as.table() |>
as.data.table(value.name = "sigma")
names(lp_sigma) <- c("draw", "index", "sigma")
lp <- dplyr::left_join(lp_mu, lp_sigma)
class(lp) <- c(class(lp), "lognormal_samples")
x <- add_mean_sd(lp)
expect_s3_class(x, "data.table")
expect_named(x, c("draw", "index", "mu", "sigma", "mean", "sd"))
expect_true(all(x$mean > 0))
expect_true(all(x$sd > 0))
delay_samples <- delay_samples(fit)
expect_s3_class(delay_samples, "data.table")
expect_named(delay_samples, c("index", "draw", "mu", "sigma", "mean", "sd"))
expect_true(all(delay_samples$mean > 0))
expect_true(all(delay_samples$sd > 0))
})

test_that("add_mean_sd.lognormal_samples works with simulated lognormal distribution parameter data", { # nolint: line_length_linter.
Expand Down

0 comments on commit ad14b55

Please sign in to comment.