Skip to content
Merged
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
7 changes: 4 additions & 3 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ CmdStanFit$set("public", name = "lp", value = lp)
# will be used by a subset of fit objects below
#' @rdname fit-method-lp
lp_approx <- function() {
as.numeric(self$draws()[, "lp_approx__"])
x <- self$draws(variables = "lp_approx__", format = "draws_matrix")
as.numeric(x[, "lp_approx__"])
}


Expand Down Expand Up @@ -1973,8 +1974,8 @@ CmdStanMLE <- R6::R6Class(
#' }
#'
mle <- function(variables = NULL) {
x <- self$draws(variables)
x <- x[, colnames(x) != "lp__"]
x <- self$draws(variables = variables, format = "draws_matrix")
x <- x[, colnames(x) != "lp__", drop = FALSE]
stats::setNames(as.numeric(x), posterior::variables(x))
}
CmdStanMLE$set("public", name = "mle", value = mle)
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-fit-laplace.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ test_that("lp(), lp_approx() methods return vectors (reading csv works)", {
expect_equal(length(lg), length(lp))
})

test_that("lp_approx() ignores non-matrix default draws formats", {
expected <- fit_laplace$draws(
variables = "lp_approx__",
format = "draws_matrix"
)
expected <- as.numeric(expected[, "lp_approx__"])

for (format in c("draws_array", "draws_df")) {
withr::local_options(list(cmdstanr_draws_format = format))
expect_equal(fit_laplace$lp_approx(), expected)
}
})

test_that("time() method works after laplace", {
run_times <- fit_laplace$time()
checkmate::expect_list(run_times, names = "strict", any.missing = FALSE)
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-fit-mle.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ test_that("mle and lp methods work after optimization", {
checkmate::expect_numeric(fit_mle$lp(), len = 1)
})

test_that("mle() ignores non-matrix default draws formats", {
expected <- fit_mle$draws(format = "draws_matrix")
expected <- expected[, colnames(expected) != "lp__", drop = FALSE]
expected <- stats::setNames(as.numeric(expected), posterior::variables(expected))

for (format in c("draws_array", "draws_df")) {
withr::local_options(list(cmdstanr_draws_format = format))
expect_equal(fit_mle$mle(), expected)
}
})

test_that("summary method works after optimization", {
x <- fit_mle$summary()
expect_s3_class(x, "draws_summary")
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-fit-vb.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ test_that("lp(), lp_approx() methods return vectors (reading csv works)", {
expect_equal(length(lg), length(lp))
})

test_that("lp_approx() ignores non-matrix default draws formats", {
expected <- fit_vb$draws(
variables = "lp_approx__",
format = "draws_matrix"
)
expected <- as.numeric(expected[, "lp_approx__"])

for (format in c("draws_array", "draws_df")) {
withr::local_options(list(cmdstanr_draws_format = format))
expect_equal(fit_vb$lp_approx(), expected)
}
})

test_that("vb works with scientific notation args", {
x <- fit_vb_sci_not$summary()
expect_s3_class(x, "draws_summary")
Expand Down
Loading