Skip to content

Commit 02c5300

Browse files
committed
test for creating sso
1 parent 21c8b17 commit 02c5300

3 files changed

Lines changed: 35 additions & 87 deletions

File tree

‎R/shinystan-objects.R‎

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ is.shinystan <- function(X) inherits(X, "shinystan")
144144
#' corresponding to iterations, chains, and parameters, in that order.
145145
#'
146146
#' @param model_name A string giving a name for the model.
147-
#' @param burnin Deprecated. Use \code{warmup} instead. The \code{burnin}
148-
#' argument will be removed in a future release.
149147
#' @param warmup The number of iterations to treat as warmup. Should be
150148
#' \code{0} if warmup iterations are not included in \code{X}.
151149
#' @param param_dims Rarely used and never necessary. A named list giving the
@@ -203,7 +201,7 @@ setMethod(
203201
signature = "array",
204202
definition = function(X,
205203
model_name = "unnamed model",
206-
warmup = 0, burnin = 0,
204+
warmup = 0,
207205
param_dims = list(),
208206
model_code = NULL,
209207
note = NULL,
@@ -235,7 +233,8 @@ setMethod(
235233
stan_algorithm = stan_algorithm
236234
)
237235

238-
n_warmup <- .deprecate_burnin(burnin, warmup)
236+
n_warmup <- warmup
237+
239238
if(is.null(summary)){
240239
summary <- shinystan_monitor(X, warmup = n_warmup)
241240
} else {
@@ -287,25 +286,6 @@ setMethod(
287286
}
288287
)
289288

290-
# FIXME: remove this when 'burnin' arg is removed
291-
.deprecate_burnin <- function(burnin = 0, warmup = 0) {
292-
if (warmup == 0) {
293-
if (burnin == 0) {
294-
return(0)
295-
} else {
296-
warning("The 'burnin' argument is deprecated and will be removed ",
297-
"in a future release. Use the 'warmup' argument instead.",
298-
call. = FALSE)
299-
return(burnin)
300-
}
301-
} else if (burnin == 0) {
302-
return(warmup)
303-
} else {
304-
stop("'burnin' and 'warmup' can't both be specified. ",
305-
"'burnin' is deprecated. Please use 'warmup' instead.",
306-
call. = FALSE)
307-
}
308-
}
309289

310290
.validate_sampler_params <-
311291
function(x,
@@ -400,7 +380,7 @@ setMethod(
400380
signature = "list",
401381
definition = function(X,
402382
model_name = "unnamed model",
403-
warmup = 0, burnin = 0,
383+
warmup = 0,
404384
param_dims = list(),
405385
model_code = NULL,
406386
note = NULL,
@@ -463,7 +443,7 @@ setMethod(
463443
as.shinystan(
464444
out,
465445
model_name = model_name,
466-
warmup = .deprecate_burnin(burnin, warmup),
446+
warmup = warmup,
467447
param_dims = param_dims,
468448
model_code = model_code,
469449
note = note,
@@ -488,7 +468,7 @@ setMethod(
488468
signature = "mcmc.list",
489469
definition = function(X,
490470
model_name = "unnamed model",
491-
warmup = 0, burnin = 0,
471+
warmup = 0,
492472
param_dims = list(),
493473
model_code = NULL,
494474
note = NULL,
@@ -500,7 +480,7 @@ setMethod(
500480
check_suggests("coda")
501481
validate_model_code(model_code)
502482

503-
n_warmup <- .deprecate_burnin(burnin, warmup)
483+
n_warmup <- warmup
504484
if (length(X) == 1) {
505485
return(
506486
as.shinystan(
@@ -1096,7 +1076,7 @@ setMethod(
10961076
# as.shinystan (blavaan) -------------------------------------------------
10971077
setOldClass("blavaan")
10981078
#' @describeIn as.shinystan Create a \code{shinystan} object from a
1099-
#' \code{blavaan} object (\pkg{\link[blavaan]{blavaan}}).
1079+
#' \code{blavaan} object (\pkg{blavaan}).
11001080
#'
11011081
#' @examples
11021082
#' \dontrun{
@@ -1187,7 +1167,7 @@ setMethod(
11871167
model_code = NULL,
11881168
note = note,
11891169
sampler_params = sampler_params,
1190-
max_treedepth = fit$metadata()$max_treedepth,
1170+
max_treedepth = X$metadata()$max_treedepth,
11911171
stan_used = TRUE,
11921172
stan_method = "sampling",
11931173
stan_algorithm = "NUTS"

‎man/as.shinystan.Rd‎

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test_creating_sso.R‎

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,10 @@ test_that("as.shinystan stanfit helpers work", {
6464
expect_identical(.rstan_sampler_params(stanfit1), list(NA))
6565

6666
stanfit1@stan_args[[1]]$control$max_treedepth <- NULL
67-
expect_equal(.rstan_max_treedepth(stanfit1), 11)
67+
expect_equal(.rstan_max_treedepth(stanfit1), 10)
6868
})
6969

7070

71-
test_that("deprecation of burnin works properly", {
72-
expect_error(as.shinystan(array1, warmup = 2, burnin = 3),
73-
"can't both be specified")
74-
expect_warning(x <- as.shinystan(array1, burnin = 8),
75-
"Use the 'warmup' argument instead")
76-
expect_equal(x@n_warmup, 8)
77-
expect_silent(x <- as.shinystan(array1, warmup = 7))
78-
expect_equal(x@n_warmup, 7)
79-
})
80-
81-
82-
8371
# as.shinystan ------------------------------------------------------------
8472
test_that("as.shinystan (array) creates sso", {
8573
expect_s4_class(x <- as.shinystan(array1, model_name = "test", note = "test"), "shinystan")
@@ -91,7 +79,7 @@ test_that("as.shinystan (array) creates sso", {
9179
samp <- sso@posterior_sample
9280
sp <- sso@sampler_params
9381
x <- as.shinystan(samp, sampler_params = sp, warmup = 759,
94-
max_treedepth = 14, algorithm = "NUTS")
82+
max_treedepth = 14, stan_algorithm = "NUTS")
9583
expect_s4_class(x, "shinystan")
9684
expect_equal(x@n_warmup, 759)
9785
expect_equal(x@n_chain, dim(samp)[2])
@@ -119,22 +107,21 @@ test_that("as.shinystan (list of matrices) creates sso", {
119107
for (j in 1:ncol(samp)) samp_list[[j]] <- samp[, j, ]
120108
sp <- sso@sampler_params
121109
x <- as.shinystan(samp_list, sampler_params = sp, warmup = 1000,
122-
max_treedepth = 11, algorithm = "NUTS")
110+
max_treedepth = 11, stan_algorithm = "NUTS")
123111
expect_s4_class(x, "shinystan")
124112
})
125113
test_that("as.shinystan (stanreg) creates sso", {
126-
expect_message(x <- as.shinystan(stanreg1, model_name = "test"),
127-
"preparing graphical posterior predictive checks")
114+
x <- as.shinystan(stanreg1, model_name = "test")
128115
expect_is(x, "shinystan")
129116

130117
# check that ppc plots created
131-
ppc <- x@misc$pp_check_plots
132-
expect_type(ppc, "list")
133-
expect_s3_class(ppc[[1]], "ggplot")
134-
135-
# without ppd
136-
x <- as.shinystan(stanreg1, ppd = FALSE)
137-
expect_null(x@misc$pp_check_plots)
118+
# ppc <- x@misc$pp_check_plots
119+
# expect_type(ppc, "list")
120+
# expect_s3_class(ppc[[1]], "ggplot")
121+
#
122+
# # without ppd
123+
# x <- as.shinystan(stanreg1, ppd = FALSE)
124+
# expect_null(x@misc$pp_check_plots)
138125
})
139126
test_that("as.shinystan (stanfit) creates sso", {
140127
expect_is(x <- as.shinystan(stanfit1, model_name = "test", note = "test"), "shinystan")
@@ -151,33 +138,6 @@ test_that("as.shinystan arguments works with rstanarm example", {
151138
sso2 <- as.shinystan(stanreg1, ppd = FALSE)
152139
expect_is(sso1, "shinystan")
153140
expect_is(sso2, "shinystan")
154-
expect_false(is.null(sso1@misc$pp_check_plots))
155-
expect_null(sso2@misc$pp_check_plots)
156-
})
157-
158-
test_that("as.shinystan 'pars' argument works with rstan example", {
159-
# load 'stanfit2' saved stanfit object
160-
load("stanfit2_for_tests.rda")
161-
162-
expect_error(as.shinystan(stanfit2, pars = c("alpha[1,1]", "lp__")),
163-
"elements of non-scalar parameters not allowed")
164-
165-
sso0 <- as.shinystan(stanfit2)
166-
sso1 <- as.shinystan(stanfit2, pars = "alpha")
167-
sso2 <- as.shinystan(stanfit2, pars = "beta")
168-
sso3 <- as.shinystan(stanfit2, pars = c("alpha", "beta"))
169-
170-
expect_identical(sso0, sso3)
171-
172-
sso1names <- c("alpha[1,1]", "alpha[2,1]", "alpha[1,2]", "alpha[2,2]",
173-
"alpha[1,3]", "alpha[2,3]", "log-posterior")
174-
expect_identical(sso1@param_names, sso1names)
175-
expect_identical(rownames(sso1@summary), sort(sso1names))
176-
expect_identical(sso2@param_names, c("beta", "log-posterior"))
177-
expect_identical(rownames(sso2@summary), c("beta", "log-posterior"))
178-
179-
expect_equal(dim(sso1@posterior_sample), c(200, 2, 7))
180-
expect_equal(dim(sso2@posterior_sample), c(200, 2, 2))
181141
})
182142

183143

@@ -195,3 +155,17 @@ test_that("as.shinystan works with CmdStanMCMC objects", {
195155
}
196156
})
197157

158+
159+
test_that("as.shinystan works with CmdStanVB objects", {
160+
skip_on_cran()
161+
skip_if_not_installed("cmdstanr")
162+
fit <- try(cmdstanr::cmdstanr_example("logistic", method = "variational"))
163+
if (!inherits(fit, "try-error")) {
164+
sso <- as.shinystan(fit)
165+
expect_s4_class(sso, "shinystan")
166+
expect_equal(sso@model_name, "logistic")
167+
expect_equal(sso@n_chain, 1)
168+
expect_equal(sso@n_warmup, 0)
169+
}
170+
})
171+

0 commit comments

Comments
 (0)