Skip to content

Commit

Permalink
preserve attributes from mh sampler; test use of index
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinsimpson committed Mar 6, 2024
1 parent cbd444f commit f297e5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions R/samplers.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@
b = model, ns = n * thin,
burn = burnin, thin = thin, t.df = t_df, rw.scale = rw_scale
))
rw_acceptance <- betas[["rw.accept"]]
fixed_acceptance <- betas[["accept"]]
rw_acceptance <- attr(betas, "rw_acceptance")
fixed_acceptance <- attr(betas, "fixed_acceptance")
betas <- betas[["bs"]]
if (!is.null(index)) {
betas <- betas[, index, drop = FALSE]
Expand Down Expand Up @@ -250,7 +250,13 @@

# if index provided, subset the draws
if (!is.null(index)) {
fx_a <- attr(draws, "fixed_acceptance")
rw_a <- attr(draws, "rw_acceptance")
draws <- draws[, index, drop = FALSE]
if (!is.null(fx_a)) {
attr(draws, "fixed_acceptance") <- fx_a
attr(draws, "rw_acceptance") <- rw_a
}
}

# return the user-supplied draws
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-samplers.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ test_that("generate_draws() works for a GAM with MH", {
expect_true(is.matrix(drws1))
expect_identical(dim(drws1), c(20L, 37L))
expect_identical(drws1, drws2)

expect_silent(drws3 <- generate_draws(m_gam, n = 20, method = "mh",
burnin = 100, thin = 2, t_df = 4, rw_scale = 0.3, seed = 2, index = 2:10))
expect_type(drws3, "double")
expect_true(is.matrix(drws3))
expect_identical(dim(drws3), c(20L, 9L))
expect_identical(drws3, drws1[, 2:10])
})

test_that("generate_draws() fails for INLA", {
Expand All @@ -82,11 +89,25 @@ test_that("generate_draws() works for a GAM with mgcv mvn()", {
test_that("user_draws() works for a GAM", {
expect_silent(drws1 <- generate_draws(m_gam, n = 20, method = "gaussian",
seed = 2))
expect_silent(drws2 <- generate_draws(m_gam, n = 20, method = "mh",
seed = 2))
expect_silent(udrws <- user_draws(m_gam, draws = drws1))
expect_type(udrws, "double")
expect_true(is.matrix(udrws))
expect_identical(dim(udrws), c(20L, 37L))
expect_identical(udrws, drws1)

expect_silent(udrws <- user_draws(m_gam, draws = drws1, index = 2:10))
expect_type(udrws, "double")
expect_true(is.matrix(udrws))
expect_identical(dim(udrws), c(20L, 9L))
expect_identical(udrws, drws1[, 2:10])

expect_silent(udrws <- user_draws(m_gam, draws = drws2, index = 2:10))
expect_type(udrws, "double")
expect_true(is.matrix(udrws))
expect_identical(dim(udrws), c(20L, 9L))
expect_identical(udrws, drws2[, 2:10])
})

test_that("user_draws() fails for incorrect matrix of draws", {
Expand All @@ -96,4 +117,6 @@ test_that("user_draws() fails for incorrect matrix of draws", {
"Supplied 'draws' doesn't match number of model coefficients.
Number of model coefs: 37
Number of columns in 'draws': 10")
expect_error(user_draws(m_gam, draws = 1:10),
"Supplied 'draws' is not a matrix of coefficients.")
})

0 comments on commit f297e5c

Please sign in to comment.