ShanaScogin / BayesPostEst

An R package to generate and plot postestimation quantities after estimating Bayesian regression models using MCMC
https://shanascogin.github.io/BayesPostEst/
GNU General Public License v3.0
12 stars 2 forks source link

clean up r2jags in testthat #86

Closed ShanaScogin closed 2 years ago

ShanaScogin commented 3 years ago

Right now R2jags is loaded separately from tests, but it makes it hard to go in and find code that depends on this. Might be easier to go in and call each function that uses R2jags from the namespace, ie R2jags::function().

ShanaScogin commented 2 years ago

Going to expand this to include general clean up for the testthat files. Need to add skip_if_not_installed() such as the following:

test_that("Simple model runs with mcmcTab", {
  testthat::skip_if_not_installed("rjags")
  data("jags_logit")
  object <- mcmcTab(jags_logit, 
                    ci = c(0.025, 0.975), 
                    pars = NULL, 
                    Pr = FALSE,
                    ROPE = NULL)
  value <- object[2, 2]
  check_against <- c(0.527)
  expect_equal(round(as.numeric(value), 2), round(check_against, 2))
  })

Files include

ShanaScogin commented 2 years ago

I don't think the tests in test_mcmcRocPrc.R are working due to an error thrown in the line pardraws <- as.matrix(coda::as.mcmc(object)) in the chunk below. Need to sort this before can add the skips:

# pull out a predicted probability matrix and yvec, for simple testing
data("jags_logit")
object   <- jags_logit
mdl_data <- object$model$data()
xdata <- as.matrix(cbind(X0 = 1L, as.data.frame(mdl_data[c("X1", "X2")])))
yvec  <- mdl_data[["Y"]]
pardraws <- as.matrix(coda::as.mcmc(object))
betadraws <- pardraws[, c(sprintf("b[%s]", 1:ncol(xdata - 1)))]
pred_prob <- plogis(xdata %*% t(betadraws)) 
ShanaScogin commented 2 years ago

R2jags moved to depends to fix the error in #88 since it's needed for some mcmc functions. This means loading the package in the helper file is fine. The rest of the files have been refitted with the skip_if_not_installed() lines with their various dependencies in commit f8f8e23