Skip to content

Commit 348bf5e

Browse files
authored
Merge pull request #254 from stan-dev/fix-cran-issues
Fix CRAN issues
2 parents cb1b403 + 6fd7872 commit 348bf5e

16 files changed

+253
-156
lines changed

R/ppc-loo.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ ppc_loo_pit_overlay <- function(y,
172172
call. = FALSE
173173
)
174174
}
175-
175+
176176
message(paste("NOTE: The kernel density estimate assumes continuous observations",
177177
"and is not optimal for discrete observations."))
178178

@@ -553,7 +553,7 @@ ppc_loo_ribbon <-
553553
attr(psis_object, "norm_const_log") <- attr(psis_object, "norm_const_log")[subset]
554554
attr(psis_object, "tail_len") <- attr(psis_object, "tail_len")[subset]
555555
attr(psis_object, "r_eff") <- attr(psis_object, "r_eff")[subset]
556-
return(psis_object)
556+
psis_object
557557
}
558558

559559
## Boundary correction based on code by ArViz development team
@@ -603,8 +603,7 @@ ppc_loo_ribbon <-
603603
method = 'convolution',
604604
sides = 2)[(npad + 1):(npad + grid_len)]
605605

606-
bc_pvals <- bc_pvals / (bw * (2 * pi)^0.5)
607-
return(bc_pvals)
606+
bc_pvals / (bw * (2 * pi)^0.5)
608607
}
609608

610609
.kde_correction <- function(x,
@@ -625,12 +624,12 @@ ppc_loo_ribbon <-
625624

626625
# Get relative frequency boundaries and counts for input vector
627626
bins <- seq(from= min(x), to = max(x), length.out = grid_len + 1)
628-
hist_obj <- hist(x, breaks = bins, plot = FALSE)
627+
hist_obj <- graphics::hist(x, breaks = bins, plot = FALSE)
629628
grid_breaks <- hist_obj$breaks
630629
grid_counts <- hist_obj$counts
631630

632631
# Compute bandwidth based on use specification
633-
bw <- density(x, bw = bw)$bw
632+
bw <- stats::density(x, bw = bw)$bw
634633

635634
# 1-D Convolution
636635
bc_pvals <- .linear_convolution(x, bw, grid_counts, grid_breaks, grid_len)
@@ -640,12 +639,12 @@ ppc_loo_ribbon <-
640639

641640
xs <- (grid_breaks[2:n_breaks] + grid_breaks[1:(n_breaks - 1)]) / 2
642641

643-
first_nonNA <- head(which(!is.na(bc_pvals)),1)
644-
last_nonNA <- tail(which(!is.na(bc_pvals)),1)
642+
first_nonNA <- utils::head(which(!is.na(bc_pvals)),1)
643+
last_nonNA <- utils::tail(which(!is.na(bc_pvals)),1)
645644
bc_pvals[1:first_nonNA] <- bc_pvals[first_nonNA]
646645
bc_pvals[last_nonNA:length(bc_pvals)] <- bc_pvals[last_nonNA]
647646

648-
return(list(xs = xs, bc_pvals = bc_pvals))
647+
list(xs = xs, bc_pvals = bc_pvals)
649648
}
650649

651650
# Wrapper function to generate runif reference lines based on
@@ -669,5 +668,5 @@ ppc_loo_ribbon <-
669668
xs[idx[i]:(idx[i+1]-1)] <- bc_list$xs
670669
}
671670

672-
return(list(xs = xs, unifs = bc_mat))
671+
list(xs = xs, unifs = bc_mat)
673672
}

tests/testthat.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ test_check("bayesplot")
88
# pr <- testthat::ProgressReporter$new()
99
# pr$max_fail = 1000
1010
# devtools::test(reporter = pr)
11-

tests/testthat/test-convenience-functions.R

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,39 @@ context("Convenience functions (for ggplot objects)")
77
test_that("abline_01 returns the correct object", {
88
expect_equal(
99
abline_01(color = "green", linetype = 2),
10-
geom_abline(intercept = 0, slope = 1, color = "green", linetype = 2, na.rm = TRUE)
10+
geom_abline(intercept = 0, slope = 1, color = "green", linetype = 2, na.rm = TRUE),
11+
check.environment = FALSE
1112
)
1213
})
1314
test_that("vline_* and hline_* return correct objects", {
1415
expect_equal(
1516
vline_0(color = "red"),
16-
geom_vline(xintercept = 0, color = "red", na.rm = TRUE)
17+
geom_vline(xintercept = 0, color = "red", na.rm = TRUE),
18+
check.environment = FALSE
1719
)
1820
expect_equal(
1921
hline_0(size = 2, linetype = 3),
20-
geom_hline(yintercept = 0, size = 2, linetype = 3, na.rm = TRUE)
22+
geom_hline(yintercept = 0, size = 2, linetype = 3, na.rm = TRUE),
23+
check.environment = FALSE
2124
)
2225
expect_equal(
2326
vline_at(c(3,4), na.rm = FALSE),
24-
geom_vline(xintercept = c(3,4))
27+
geom_vline(xintercept = c(3,4)),
28+
check.environment = FALSE
2529
)
2630
expect_equal(
2731
hline_at(c(3,4), na.rm = FALSE),
28-
geom_hline(yintercept = c(3,4))
32+
geom_hline(yintercept = c(3,4)),
33+
check.environment = FALSE
2934
)
3035
})
3136
test_that("vline_at with 'fun' works", {
3237
x <- example_mcmc_draws(chains = 1)
33-
expect_equal(vline_at(x, colMeans),
34-
geom_vline(xintercept = colMeans(x), na.rm = TRUE))
38+
expect_equal(
39+
vline_at(x, colMeans),
40+
geom_vline(xintercept = colMeans(x), na.rm = TRUE),
41+
check.environment = FALSE
42+
)
3543
})
3644
test_that("calc_v (internal function) works", {
3745
a <- 1:4
@@ -176,8 +184,7 @@ test_that("overlay_function returns the correct object", {
176184
expect_error(overlay_function(), 'argument "fun" is missing')
177185
expect_equal(
178186
overlay_function(fun = "dnorm"),
179-
stat_function(fun = "dnorm", inherit.aes = FALSE)
187+
stat_function(fun = "dnorm", inherit.aes = FALSE),
188+
check.environment = FALSE
180189
)
181190
})
182-
183-

tests/testthat/test-extractors.R

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
library(bayesplot)
2-
suppressPackageStartupMessages(library(rstanarm))
32
context("Extractors")
43

5-
ITER <- 1000
6-
CHAINS <- 3
7-
fit <- stan_glm(mpg ~ wt + am, data = mtcars,
8-
iter = ITER, chains = CHAINS,
9-
refresh = 0)
10-
4+
if (requireNamespace("rstanarm", quietly = TRUE)) {
5+
ITER <- 1000
6+
CHAINS <- 3
7+
fit <- rstanarm::stan_glm(mpg ~ wt + am, data = mtcars,
8+
iter = ITER, chains = CHAINS,
9+
refresh = 0)
10+
}
1111
x <- list(cbind(a = 1:3, b = rnorm(3)), cbind(a = 1:3, b = rnorm(3)))
1212

1313
# nuts_params and log_posterior methods -----------------------------------
@@ -30,6 +30,8 @@ test_that("nuts_params.list parameter selection ok", {
3030
})
3131

3232
test_that("all nuts_params methods identical", {
33+
skip_if_not_installed("rstanarm")
34+
skip_if_not_installed("rstan")
3335
expect_identical(
3436
nuts_params(fit),
3537
nuts_params(fit$stanfit)
@@ -41,6 +43,8 @@ test_that("all nuts_params methods identical", {
4143
})
4244

4345
test_that("nuts_params.stanreg returns correct structure", {
46+
skip_if_not_installed("rstanarm")
47+
4448
np <- nuts_params(fit)
4549
expect_identical(colnames(np), c("Chain", "Iteration", "Parameter", "Value"))
4650

@@ -53,13 +57,17 @@ test_that("nuts_params.stanreg returns correct structure", {
5357
})
5458

5559
test_that("log_posterior.stanreg returns correct structure", {
60+
skip_if_not_installed("rstanarm")
61+
5662
lp <- log_posterior(fit)
5763
expect_identical(colnames(lp), c("Chain", "Iteration", "Value"))
5864
expect_equal(length(unique(lp$Iteration)), floor(ITER / 2))
5965
expect_equal(length(unique(lp$Chain)), CHAINS)
6066
})
6167

6268
test_that("rhat.stanreg returns correct structure", {
69+
skip_if_not_installed("rstanarm")
70+
6371
r <- rhat(fit)
6472
expect_named(r)
6573
expect_equal(r, summary(fit)[1:length(r), "Rhat"])
@@ -69,6 +77,8 @@ test_that("rhat.stanreg returns correct structure", {
6977
})
7078

7179
test_that("neff_ratio.stanreg returns correct structure", {
80+
skip_if_not_installed("rstanarm")
81+
7282
expect_named(neff_ratio(fit, pars = c("wt", "am")), c("wt", "am"))
7383

7484
ratio <- neff_ratio(fit)
@@ -78,6 +88,8 @@ test_that("neff_ratio.stanreg returns correct structure", {
7888
})
7989

8090
test_that("rhat.stanfit returns correct structure", {
91+
skip_if_not_installed("rstanarm")
92+
8193
r <- rhat(fit$stanfit)
8294
expect_named(r)
8395
expect_equal(r, summary(fit)[, "Rhat"])
@@ -88,6 +100,8 @@ test_that("rhat.stanfit returns correct structure", {
88100
})
89101

90102
test_that("neff_ratio.stanreg returns correct structure", {
103+
skip_if_not_installed("rstanarm")
104+
91105
denom <- floor(ITER / 2) * CHAINS
92106

93107
ratio <- neff_ratio(fit$stanfit)

tests/testthat/test-helpers-mcmc.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,14 @@ test_that("prepare_mcmc_array processes non-array input types correctly", {
241241
expect_equal(parameter_names(a3), colnames(chainlist[[1]]))
242242

243243
# object with acceptable as.array method
244-
suppressPackageStartupMessages(library(rstanarm))
245-
fit <- suppressWarnings(stan_glm(mpg ~ wt, data = mtcars, chains = 2, iter = 500, refresh = 0))
246-
a4 <- prepare_mcmc_array(fit)
247-
expect_s3_class(a4, "mcmc_array")
248-
expect_equal(a4, prepare_mcmc_array(as.array(fit)))
249-
expect_equal(dim(a4), c(250, 2, 3))
250-
expect_equal(parameter_names(a4), c("(Intercept)", "wt", "sigma"))
244+
if (requireNamespace("rstanarm", quietly = TRUE)) {
245+
fit <- suppressWarnings(rstanarm::stan_glm(mpg ~ wt, data = mtcars, chains = 2, iter = 500, refresh = 0))
246+
a4 <- prepare_mcmc_array(fit)
247+
expect_s3_class(a4, "mcmc_array")
248+
expect_equal(a4, prepare_mcmc_array(as.array(fit)))
249+
expect_equal(dim(a4), c(250, 2, 3))
250+
expect_equal(parameter_names(a4), c("(Intercept)", "wt", "sigma"))
251+
}
251252

252253
# object with unacceptable as.array method
253254
fit2 <- lm(mpg ~ wt, data = mtcars)

tests/testthat/test-mcmc-nuts.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
library(bayesplot)
2-
suppressPackageStartupMessages(library(rstanarm))
32
context("MCMC: nuts")
43

5-
ITER <- 1000
6-
CHAINS <- 3
7-
fit <- stan_glm(mpg ~ wt + am, data = mtcars,
8-
iter = ITER, chains = CHAINS,
9-
refresh = 0)
10-
np <- nuts_params(fit)
11-
lp <- log_posterior(fit)
4+
if (requireNamespace("rstanarm", quietly = TRUE)) {
5+
ITER <- 1000
6+
CHAINS <- 3
7+
fit <- rstanarm::stan_glm(mpg ~ wt + am, data = mtcars,
8+
iter = ITER, chains = CHAINS,
9+
refresh = 0)
10+
np <- nuts_params(fit)
11+
lp <- log_posterior(fit)
12+
}
1213

1314
test_that("all mcmc_nuts_* (except energy) return gtable objects", {
15+
skip_if_not_installed("rstanarm")
1416
expect_gtable(mcmc_nuts_acceptance(np, lp))
1517
expect_gtable(mcmc_nuts_acceptance(np, lp, chain = CHAINS))
1618

@@ -24,7 +26,9 @@ test_that("all mcmc_nuts_* (except energy) return gtable objects", {
2426
expect_gtable(mcmc_nuts_divergence(np, lp))
2527
expect_gtable(mcmc_nuts_divergence(np, lp, chain = CHAINS))
2628
})
29+
2730
test_that("all mcmc_nuts_* (except energy) error if chain argument is bad", {
31+
skip_if_not_installed("rstanarm")
2832
funs <- c("acceptance", "divergence", "treedepth", "stepsize")
2933
for (f in paste0("mcmc_nuts_", funs)) {
3034
expect_error(do.call(f, list(x=np, lp=lp, chain = CHAINS + 1)),
@@ -37,6 +41,8 @@ test_that("all mcmc_nuts_* (except energy) error if chain argument is bad", {
3741
})
3842

3943
test_that("mcmc_nuts_energy returns a ggplot object", {
44+
skip_if_not_installed("rstanarm")
45+
4046
p <- mcmc_nuts_energy(np)
4147
expect_gg(p)
4248
expect_s3_class(p$facet, "FacetWrap")
@@ -46,12 +52,15 @@ test_that("mcmc_nuts_energy returns a ggplot object", {
4652
expect_gg(p)
4753
expect_s3_class(p$facet, "FacetNull")
4854
})
55+
4956
test_that("mcmc_nuts_energy throws correct warnings", {
57+
skip_if_not_installed("rstanarm")
5058
expect_warning(mcmc_nuts_energy(np, chain = 1), "ignored: chain")
5159
})
5260

5361

5462
test_that("validate_nuts_data_frame throws errors", {
63+
skip_if_not_installed("rstanarm")
5564
expect_error(
5665
validate_nuts_data_frame(list(Iteration = 1, Chain = 1)),
5766
"NUTS parameters should be in a data frame"

tests/testthat/test-mcmc-recover.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
library(bayesplot)
2-
suppressPackageStartupMessages(library(rstanarm))
32
context("MCMC: recover")
43

5-
alpha <- 1; beta <- c(-.5, .5); sigma <- 2
6-
X <- matrix(rnorm(200), 100, 2)
7-
y <- rnorm(100, mean = c(alpha + X %*% beta), sd = sigma)
8-
fit <- stan_glm(y ~ ., data = data.frame(y, X), refresh = 0, iter = 750, chains = 2, seed = 8420)
9-
draws <- as.matrix(fit)
10-
true <- c(alpha, beta, sigma)
4+
if (requireNamespace("rstanarm", quietly = TRUE)) {
5+
alpha <- 1; beta <- c(-.5, .5); sigma <- 2
6+
X <- matrix(rnorm(200), 100, 2)
7+
y <- rnorm(100, mean = c(alpha + X %*% beta), sd = sigma)
8+
fit <- rstanarm::stan_glm(y ~ ., data = data.frame(y, X), refresh = 0, iter = 750, chains = 2, seed = 8420)
9+
draws <- as.matrix(fit)
10+
true <- c(alpha, beta, sigma)
11+
}
1112

1213
test_that("mcmc_recover_intervals throws correct errors", {
14+
skip_if_not_installed("rstanarm")
15+
1316
expect_error(
1417
mcmc_recover_intervals(draws, letters[1:ncol(draws)]),
1518
"is.numeric(true) is not TRUE",
@@ -43,6 +46,8 @@ test_that("mcmc_recover_intervals throws correct errors", {
4346
})
4447

4548
test_that("mcmc_recover_intervals returns a ggplot object", {
49+
skip_if_not_installed("rstanarm")
50+
4651
expect_gg(mcmc_recover_intervals(draws, true))
4752
expect_gg(mcmc_recover_intervals(draws, true, batch = c(1, 2, 2, 1),
4853
point_est = "mean"))
@@ -52,13 +57,17 @@ test_that("mcmc_recover_intervals returns a ggplot object", {
5257
})
5358

5459
test_that("mcmc_recover_intervals works when point_est = 'none'", {
60+
skip_if_not_installed("rstanarm")
61+
5562
a <- mcmc_recover_intervals(draws, true, batch = 1:4, point_est = "none")
5663
expect_gg(a)
5764
expect_equal(a$data$Point, rep(NA, ncol(draws)))
5865
})
5966

6067

6168
test_that("mcmc_recover_scatter returns a ggplot object", {
69+
skip_if_not_installed("rstanarm")
70+
6271
expect_gg(
6372
mcmc_recover_scatter(draws, true)
6473
)
@@ -98,6 +107,8 @@ test_that("mcmc_recover_scatter returns a ggplot object", {
98107

99108

100109
test_that("mcmc_recover_hist returns a ggplot object", {
110+
skip_if_not_installed("rstanarm")
111+
101112
expect_gg(mcmc_recover_hist(draws, true))
102113
expect_gg(mcmc_recover_hist(draws, true, binwidth = .1,
103114
facet_args = list(nrow = 1)))

0 commit comments

Comments
 (0)