Skip to content

Commit

Permalink
TMB and coda to Suggests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Sep 25, 2020
1 parent 87d4e19 commit 89e8b9a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 37 deletions.
38 changes: 19 additions & 19 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,37 @@ Description: Convert fitted objects from various R mixed-model packages
provides a one-row summary of model-level statistics.
Imports:
broom,
cubelyr,
dplyr,
tidyr,
purrr,
tibble,
nlme,
methods,
nlme,
purrr,
stringr,
coda,
TMB,
cubelyr
tibble,
tidyr
Suggests:
brms,
coda,
dotwhisker,
knitr,
testthat,
ggplot2,
Matrix,
MCMCglmm,
lme4,
brms,
mgcv,
gamlss,
gamlss.data,
lmerTest,
pbkrtest,
ggplot2,
GLMMadaptive,
glmmADMB,
glmmTMB,
dotwhisker,
lmerTest,
lme4,
Matrix,
MCMCglmm,
mgcv,
pander,
R2jags,
GLMMadaptive,
pbkrtest,
rstan,
rstanarm
rstanarm,
R2jags,
TMB
URL: http://github.com/bbolker/broom.mixed
BugReports: http://github.com/bbolker/broom.mixed/issues
License: GPL-3
Expand Down
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,10 @@ export(tidy)
export(tidyMCMC)
if(getRversion()>='3.3.0') importFrom(stats, sigma) else importFrom(lme4,sigma)
import(dplyr)
importFrom(TMB,sdreport)
importFrom(TMB,tmbprofile)
importFrom(TMB,tmbroot)
importFrom(broom,augment)
importFrom(broom,augment_columns)
importFrom(broom,glance)
importFrom(broom,tidy)
importFrom(coda,HPDinterval)
importFrom(coda,as.mcmc)
importFrom(coda,mcmc)
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(dplyr,mutate)
Expand Down
10 changes: 6 additions & 4 deletions R/MCMCglmm_prediction.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@
## boxplot(M ~ Level, data = longsum)
## }
#' @importFrom stats pnorm plogis
#' @importFrom coda mcmc
#' @importFrom nlme fixef ranef
predict2.MCMCglmm <- function(object, X, Z, use = c("all", "mean"),
type = c("lp", "response"), ...) {

assert_dependency("coda")

use <- match.arg(use)
type <- match.arg(type)

Expand Down Expand Up @@ -132,9 +134,9 @@ predict2.MCMCglmm <- function(object, X, Z, use = c("all", "mean"),

q <- vector("list", length(CP) - 2)
for (i in 2:(length(CP) - 1)) {
q[[i - 1]] <- mcmc(CP[[i + 1]] - CP[[i]])
q[[i - 1]] <- coda::mcmc(CP[[i + 1]] - CP[[i]])
}
q <- c(list(mcmc(1 - Reduce(`+`, q[1:(i - 1)]))), q)
q <- c(list(coda::mcmc(1 - Reduce(`+`, q[1:(i - 1)]))), q)
class(q) <- c("list", "MCMCglmmPredictedProbs")
res <- q
} else if (all(object$family %in% c("categorical", "multinomial"))) {
Expand All @@ -149,7 +151,7 @@ predict2.MCMCglmm <- function(object, X, Z, use = c("all", "mean"),
stop("Function does not support response type for families beside ordinal")
}
} else if (type == "lp") {
res <- as.mcmc(res)
res <- coda::as.mcmc(res)
class(res) <- c("mcmc", "MCMCglmmPredictedLP")
}

Expand Down
10 changes: 6 additions & 4 deletions R/TMB_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
##' @param effect which effects should be returned?
##' @param conf.method method for computing confidence intervals
##' @param ... additional arguments passed to confint function (tmbroot, tmbprofile)
##' @importFrom TMB sdreport tmbroot tmbprofile
##' @importFrom stats approx predict
##' @importFrom splines backSpline interpSpline
## FIXME: retrieving stored objects doesn't work well ...
Expand All @@ -26,9 +25,12 @@ tidy.TMB <- function(x, effect = c("fixed", "random"),
conf.int = FALSE,
conf.level = 0.95,
conf.method = c("wald", "uniroot", "profile"), ...) {

assert_dependency("TMB")

## R CMD check/global variables
branch <- v <- param <- value <- zeta <- Estimate <- estimate <- std.error <- NULL
sdr <- sdreport(x)
sdr <- TMB::sdreport(x)
retlist <- list()
if ("fixed" %in% effect) {
ss <- summary(sdr, select = "fixed") %>%
Expand All @@ -51,7 +53,7 @@ tidy.TMB <- function(x, effect = c("fixed", "random"),
tt <- do.call(
rbind,
lapply(seq(nrow(ss)),
tmbroot,
TMB::tmbroot,
obj = x,
...
)
Expand All @@ -66,7 +68,7 @@ tidy.TMB <- function(x, effect = c("fixed", "random"),
all_vars <- all_vars[-rnd]
}
prof0 <- purrr::map_dfr(seq_along(all_vars),
~ setNames(tmbprofile(x,name=.,trace=FALSE),c("focal","value")),
~ setNames(TMB::tmbprofile(x,name=.,trace=FALSE),c("focal","value")),
.id="param")
prof1 <- (prof0
%>% group_by(param)
Expand Down
3 changes: 3 additions & 0 deletions R/brms_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ tidy.brmsfit <- function(x, parameters = NA,
out$estimate <- apply(samples, 2, pointfun)
out$std.error <- apply(samples, 2, stdfun)
if (conf.int) {

assert_dependency("coda")

stopifnot(length(conf.level) == 1L)
probs <- c((1 - conf.level) / 2, 1 - (1 - conf.level) / 2)
if (conf.method == "HPDinterval") {
Expand Down
15 changes: 11 additions & 4 deletions R/mcmc_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@

#'
#' @importFrom stats median sd
#' @importFrom coda HPDinterval as.mcmc
#' @export
tidyMCMC <- function(x,
pars,
Expand All @@ -82,6 +81,9 @@ tidyMCMC <- function(x,
ess = FALSE,
index = FALSE,
...) {

assert_dependency("coda")

conf.method <- match.arg(conf.method)

stan <- inherits(x, "stanfit")
Expand Down Expand Up @@ -119,7 +121,7 @@ tidyMCMC <- function(x,

ci <- switch(conf.method,
quantile = t(apply(ss, 2, stats::quantile, levs)),
HPDinterval(as.mcmc(ss), prob = conf.level)
coda::HPDinterval(coda::as.mcmc(ss), prob = conf.level)
) %>%
as.data.frame()

Expand All @@ -146,15 +148,17 @@ tidyMCMC <- function(x,


##' @rdname mcmc_tidiers
##' @importFrom coda as.mcmc
##' @export
tidy.rjags <- function(x,
robust = FALSE,
conf.int = FALSE,
conf.level = 0.95,
conf.method = "quantile",
...) {
tidyMCMC(as.mcmc(x$BUGS),

assert_dependency("coda")

tidyMCMC(coda::as.mcmc(x$BUGS),
robust = robust,
conf.int = conf.int,
conf.level = conf.level,
Expand All @@ -178,6 +182,9 @@ tidy.mcmc.list <- tidyMCMC

## copied from emdbook ...
as.mcmc.bugs <- function (x) {

assert_dependency("coda")

if (x$n.chains > 1) {
z <- list()
for (i in 1:x$n.chains) {
Expand Down
3 changes: 3 additions & 0 deletions R/rstanarm_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ tidy.stanreg <- function(x,
}

if (conf.int) {

assert_dependency("coda")

cifix <- switch(conf.method,
HPDinterval= {
m <- as.matrix(x$stanfit)
Expand Down
11 changes: 11 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
## most of these are unexported (small) functions from broom;
## could be removed if these were exported


#' check if a package is available and return informative message otherwise
#'
#' @keywords internal
assert_dependency <- function(library_name) {
if (!requireNamespace(library_name, quietly = TRUE)) {
stop(sprintf("Please install the %s package.", library_name))
}
}


## https://github.com/klutometis/roxygen/issues/409
#' @importFrom broom tidy glance augment
#' @export
Expand Down
12 changes: 12 additions & 0 deletions man/assert_dependency.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 89e8b9a

Please sign in to comment.