Skip to content

Commit

Permalink
Fixes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed May 21, 2024
1 parent 6be59e9 commit 62da2d1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,3 @@ NeedsCompilation: yes
URL: https://mc-stan.org/rstanarm/, https://discourse.mc-stan.org
BugReports: https://github.com/stan-dev/rstanarm/issues
RoxygenNote: 7.3.1
Config/testthat/parallel: true
8 changes: 4 additions & 4 deletions tests/testthat/test_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ library(lme4)
library(MASS)
SEED <- 12345
set.seed(SEED)
ITER <- 10
ITER <- 100
CHAINS <- 2
REFRESH <- 0

Expand Down Expand Up @@ -719,7 +719,7 @@ test_that("prior_summary returns correctly named list", {
# predictive_error,predictive_interval ------------------------------------
context("predictive error and interval methods")
test_that("predictive_error works", {
expect_error(predictive_error(stan_glm1, draws = 100),
expect_error(predictive_error(stan_glm1, draws = 500),
"'draws' should be <= posterior sample size")
expect_error(predictive_error(stan_polr1),
"not currently available for stan_polr")
Expand Down Expand Up @@ -748,7 +748,7 @@ test_that("predictive_error works", {
expect_equal(dim(err3), c(5, 2))
})
test_that("predictive_interval works", {
expect_error(predictive_interval(stan_glm1, draws = 100),
expect_error(predictive_interval(stan_glm1, draws = 500),
"'draws' should be <= posterior sample size")
expect_error(predictive_interval(stan_glm1, prob = c(0.25, 0.76)),
"'prob' should be a single number greater than 0 and less than 1")
Expand Down Expand Up @@ -786,7 +786,7 @@ test_that("predictive_error stanreg and ppd methods return the same thing", {
preds <- posterior_predict(stan_betareg1, seed = 123)
expect_equal(
predictive_error(stan_betareg1, seed = 123),
predictive_error(preds, y = stan_betareg1$y)
predictive_error(preds, y = stan_betareg2$y)
)
})
test_that("predictive_interval stanreg and ppd methods return the same thing", {
Expand Down
28 changes: 14 additions & 14 deletions tests/testthat/test_stan_betareg.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,39 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
# sparse currently not used in stan_betareg
test_that("sparse = TRUE errors", {
expect_error(
stan_betareg(y ~ x, link = "logit", seed = SEED, sparse = TRUE, data = dat),
stan_betareg(y ~ x, link = "logit", sparse = TRUE, data = dat),
"unknown arguments: sparse"
)
})

# test QR
test_that("QR errors when number of x and/or z predictors is <= 1", {
expect_error(
stan_betareg(y ~ x, link = "logit", seed = SEED, QR = TRUE, data = dat),
stan_betareg(y ~ x, link = "logit", QR = TRUE, data = dat),
"'QR' can only be specified when there are multiple predictors"
)
expect_error(
stan_betareg(y ~ x | z, link = "logit", seed = SEED, QR = TRUE, data = dat),
stan_betareg(y ~ x | z, link = "logit", QR = TRUE, data = dat),
"'QR' can only be specified when there are multiple predictors"
)
})

test_that("QR works when number of x and/or z predictors is >= 1", {
SW(fit1 <- stan_betareg(y ~ x + z, link = "logit", seed = SEED, QR = TRUE,
SW(fit1 <- stan_betareg(y ~ x + z, link = "logit", QR = TRUE,
prior = NULL, prior_intercept = NULL, refresh = 0,
data = dat, algorithm = "optimizing"))
expect_stanreg(fit1)
expect_output(print(prior_summary(fit1)), "Q-space")

SW(fit2 <- stan_betareg(y ~ x + z | z, link = "logit", seed = SEED, QR = TRUE,
SW(fit2 <- stan_betareg(y ~ x + z | z, link = "logit", QR = TRUE,
prior = NULL, prior_intercept = NULL, refresh = 0,
data = dat, algorithm = "optimizing"))
expect_stanreg(fit2)
})

test_that("stan_betareg returns expected result when modeling x and dispersion", {
for (i in 1:length(link1)) {
SW(fit <- stan_betareg(y ~ x, link = link1[i], seed = SEED,
SW(fit <- stan_betareg(y ~ x, link = link1[i],
prior = NULL, prior_intercept = NULL,
prior_phi = NULL, refresh = 0,
data = dat, algorithm = "optimizing"))
Expand All @@ -85,7 +85,7 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
})

test_that("stan_betareg works with QR = TRUE and algorithm = 'optimizing'", {
SW(fit <- stan_betareg(y ~ x + z, link = "logit", seed = SEED, QR = TRUE,
SW(fit <- stan_betareg(y ~ x + z, link = "logit", QR = TRUE,
prior = NULL, prior_intercept = NULL,
prior_phi = NULL, refresh = 0,
data = dat, algorithm = "optimizing"))
Expand Down Expand Up @@ -185,7 +185,7 @@ if (.Platform$OS.type != "windows" && require(betareg)) {

SW(fit <- stan_betareg(y ~ x | 1, link = link1[i], link.phi = "sqrt",
data = dat, chains = 1, iter = 1, refresh = 0,
algorithm = "sampling", seed = SEED))
algorithm = "sampling"))
expect_stanreg(fit)
}
})
Expand All @@ -199,7 +199,7 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
mu <- binomial(link="logit")$linkinv(1+0.2*dat$x)
phi <- 20
dat$y <- rbeta(N, mu * phi, (1 - mu) * phi)
SW(fit <- stan_betareg(y ~ x, link = "logit", seed = SEED,
SW(fit <- stan_betareg(y ~ x, link = "logit",
prior = NULL, prior_intercept = NULL, prior_phi = NULL,
data = dat, weights = weights, offset = offset,
algorithm = "optimizing", iter = 2000, refresh = 0))
Expand Down Expand Up @@ -240,7 +240,7 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
test_that("compatible with stan_betareg with z", {
data("GasolineYield", package = "betareg")
SW(fit <- stan_betareg(yield ~ pressure + temp | temp, data = GasolineYield,
iter = ITER*5, chains = 2*CHAINS, seed = SEED,
iter = ITER*5, chains = 2*CHAINS,
refresh = 0))
check_for_pp_errors(fit)
# expect_linpred_equal(fit)
Expand All @@ -249,7 +249,7 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
test_that("compatible with stan_betareg without z", {
data("GasolineYield", package = "betareg")
SW(fit <- stan_betareg(yield ~ temp, data = GasolineYield,
iter = ITER, chains = CHAINS, seed = SEED, refresh = 0))
iter = ITER, chains = CHAINS, refresh = 0))
check_for_pp_errors(fit)
# expect_linpred_equal(fit)
})
Expand All @@ -258,9 +258,9 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
GasolineYield2 <- GasolineYield
GasolineYield2$offs <- runif(nrow(GasolineYield2))
SW(fit <- stan_betareg(yield ~ temp, data = GasolineYield2, offset = offs,
iter = ITER*5, chains = CHAINS, seed = SEED, refresh = 0))
iter = ITER*5, chains = CHAINS, refresh = 0))
SW(fit2 <- stan_betareg(yield ~ temp + offset(offs), data = GasolineYield2,
iter = ITER*5, chains = CHAINS, seed = SEED, refresh = 0))
iter = ITER*5, chains = CHAINS, refresh = 0))

expect_warning(posterior_predict(fit, newdata = GasolineYield),
"offset")
Expand All @@ -284,7 +284,7 @@ if (.Platform$OS.type != "windows" && require(betareg)) {
betaregfit <- betareg(y ~ x | z, data = dat)
SW(capture.output(
stanfit <- stan_betareg(y ~ x | z, data = dat, chains = CHAINS,
iter = ITER, seed = SEED, refresh = 0)
iter = ITER, refresh = 0)
))

pb <- predict(betaregfit, type = "response")
Expand Down
12 changes: 5 additions & 7 deletions tests/testthat/test_stan_clogit.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
suppressPackageStartupMessages(library(rstanarm))

SEED <- 123
ITER <- 100
ITER <- 500
CHAINS <- 2
CORES <- 1
REFRESH <- 0
Expand All @@ -30,9 +30,8 @@ threshold <- 0.03
context("stan_clogit")

SW(fit <- stan_clogit(case ~ spontaneous + induced, strata = stratum, prior = NULL,
data = infert[order(infert$stratum), ],
QR = TRUE, init_r = 0.5,
chains = CHAINS, iter = ITER, seed = SEED, refresh = 0))
data = infert[order(infert$stratum), ],
chains = CHAINS, iter = ITER, refresh = 0))

test_that("stan_clogit is similar to survival::clogit", {
expect_equal(c(spontaneous = 1.985876, induced = 1.409012), coef(fit), tol = threshold)
Expand All @@ -47,9 +46,8 @@ test_that("stan_clogit works when y is a factor", {
d$case <- factor(d$case, labels = c("A", "B"))
SW(fit_factor <- stan_clogit(case ~ spontaneous + induced, strata = stratum, prior = NULL,
data = infert[order(infert$stratum), ],
QR = TRUE, init_r = 0.5,
chains = CHAINS, iter = ITER, seed = SEED, refresh = 0))
expect_equal(coef(fit_factor), coef(fit))
chains = CHAINS, iter = ITER, refresh = 0))
expect_equal(coef(fit_factor), coef(fit), tolerance = threshold)
})

test_that("stan_clogit throws error if data are not sorted", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_stan_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
Sys.setenv(USE_CXX17 = 1)
set.seed(12345)

MODELS_HOME <- "../../inst/stan"
INCLUDE_DIR <- "../../inst/include"
MODELS_HOME <- system.file("stan", package = "rstanarm", mustWork = TRUE)
INCLUDE_DIR <- system.file("include", package = "rstanarm", mustWork = TRUE)

context("setup")
test_that("Stan programs are available", {
Expand Down

0 comments on commit 62da2d1

Please sign in to comment.