Skip to content

Commit

Permalink
add initial support for brms models
Browse files Browse the repository at this point in the history
- models with multiple dependent variables are not supported yet
  • Loading branch information
mutlusun committed Jan 22, 2021
1 parent 02e031a commit 46f822f
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 2 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Imports:
effectsize (>= 0.4.0),
performance (>= 0.5.0)
Suggests:
brms,
BayesFactor,
dplyr,
httr,
Expand Down Expand Up @@ -69,6 +70,9 @@ Collate:
'report.htest.R'
'report.aov.R'
'report.bayesfactor_models.R'
'report.lme4.R'
'report.stanreg.R'
'report.brmsfit.R'
'report.character.R'
'report.compare_performance.R'
'report.data.frame.R'
Expand All @@ -78,10 +82,8 @@ Collate:
'report.glmmTMB.R'
'report.lavaan.R'
'report.lme.R'
'report.lme4.R'
'report.numeric.R'
'report.sessionInfo.R'
'report.stanreg.R'
'report.survreg.R'
'report.test_performance.R'
'report.zeroinfl.R'
Expand Down
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ S3method(report,aov)
S3method(report,aovlist)
S3method(report,bayesfactor_inclusion)
S3method(report,bayesfactor_models)
S3method(report,brmsfit)
S3method(report,character)
S3method(report,compare_performance)
S3method(report,data.frame)
Expand All @@ -55,6 +56,7 @@ S3method(report_effectsize,MixMod)
S3method(report_effectsize,anova)
S3method(report_effectsize,aov)
S3method(report_effectsize,aovlist)
S3method(report_effectsize,brmsfit)
S3method(report_effectsize,default)
S3method(report_effectsize,glm)
S3method(report_effectsize,glmmTMB)
Expand All @@ -69,6 +71,7 @@ S3method(report_info,MixMod)
S3method(report_info,anova)
S3method(report_info,aov)
S3method(report_info,aovlist)
S3method(report_info,brmsfit)
S3method(report_info,default)
S3method(report_info,glm)
S3method(report_info,glmmTMB)
Expand All @@ -80,6 +83,7 @@ S3method(report_info,stanreg)
S3method(report_info,survreg)
S3method(report_info,zeroinfl)
S3method(report_intercept,MixMod)
S3method(report_intercept,brmsfit)
S3method(report_intercept,default)
S3method(report_intercept,glm)
S3method(report_intercept,glmmTMB)
Expand All @@ -93,6 +97,7 @@ S3method(report_model,MixMod)
S3method(report_model,anova)
S3method(report_model,aov)
S3method(report_model,aovlist)
S3method(report_model,brmsfit)
S3method(report_model,default)
S3method(report_model,glm)
S3method(report_model,glmmTMB)
Expand All @@ -107,6 +112,7 @@ S3method(report_parameters,MixMod)
S3method(report_parameters,anova)
S3method(report_parameters,aov)
S3method(report_parameters,aovlist)
S3method(report_parameters,brmsfit)
S3method(report_parameters,character)
S3method(report_parameters,compare_performance)
S3method(report_parameters,data.frame)
Expand All @@ -127,6 +133,7 @@ S3method(report_parameters,survreg)
S3method(report_parameters,test_performance)
S3method(report_parameters,zeroinfl)
S3method(report_performance,MixMod)
S3method(report_performance,brmsfit)
S3method(report_performance,default)
S3method(report_performance,glm)
S3method(report_performance,glmmTMB)
Expand All @@ -137,9 +144,11 @@ S3method(report_performance,merMod)
S3method(report_performance,stanreg)
S3method(report_performance,survreg)
S3method(report_performance,zeroinfl)
S3method(report_priors,brmsfit)
S3method(report_priors,default)
S3method(report_priors,stanreg)
S3method(report_random,MixMod)
S3method(report_random,brmsfit)
S3method(report_random,default)
S3method(report_random,glmmTMB)
S3method(report_random,lme)
Expand All @@ -149,6 +158,7 @@ S3method(report_statistics,MixMod)
S3method(report_statistics,anova)
S3method(report_statistics,aov)
S3method(report_statistics,aovlist)
S3method(report_statistics,brmsfit)
S3method(report_statistics,character)
S3method(report_statistics,compare_performance)
S3method(report_statistics,data.frame)
Expand All @@ -172,6 +182,7 @@ S3method(report_table,aov)
S3method(report_table,aovlist)
S3method(report_table,bayesfactor_inclusion)
S3method(report_table,bayesfactor_models)
S3method(report_table,brmsfit)
S3method(report_table,character)
S3method(report_table,compare_performance)
S3method(report_table,data.frame)
Expand All @@ -198,6 +209,7 @@ S3method(report_text,aov)
S3method(report_text,aovlist)
S3method(report_text,bayesfactor_inclusion)
S3method(report_text,bayesfactor_models)
S3method(report_text,brmsfit)
S3method(report_text,character)
S3method(report_text,compare_performance)
S3method(report_text,data.frame)
Expand Down
109 changes: 109 additions & 0 deletions R/report.brmsfit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#' Reporting Bayesian Models from brms
#'
#' Create reports for Bayesian models. The description of the parameters
#' follows the Sequential Effect eXistence and sIgnificance Testing framework
#' (see \link[bayestestR:sexit]{SEXIT documentation}).
#'
#' @inheritParams report.lm
#' @inherit report return seealso
#'
#' @examples
#' library(report)
#'
#' # Bayesian models
#' if(require("brms")){
#' model <- brm(mpg ~ qsec + wt, data = mtcars, refresh=0, iter=300)
#' r <- report(model)
#' r
#' summary(r)
#' as.data.frame(r)
#' summary(as.data.frame(r))
#' }
#' @include report.lm.R report.stanreg.R report.lme4.R
#' @export
report.brmsfit <- function(x, ...) {
table <- report_table(x, include_effectsize=FALSE, ...)
text <- report_text(x, table = table, ...)

as.report(text, table = table, ...)
}

#' @export
report_effectsize.brmsfit <- report_effectsize.lm

#' @export
report_table.brmsfit <- report_table.lm

#' @export
report_performance.brmsfit <- report_performance.lm

#' @export
report_statistics.brmsfit <- report_statistics.lm

#' @export
report_random.brmsfit <- report_random.merMod

#' @export
report_model.brmsfit <- report_model.lm

#' @export
report_text.brmsfit <- report_text.lm


# ==================== Specific to Bayes ===================================



# report_priors -----------------------------------------------------------

#' @export
report_priors.brmsfit <- function(x, ...) {
params <- bayestestR::describe_prior(x)
params <- params[params$Parameter != "(Intercept)", ]

# Return empty if no priors info
if (!"Prior_Distribution" %in% names(params) |
nrow(params) == 0 |
all(is.na(params$Prior_Scale))) {
return("")
}

values <- ifelse(params$Prior_Distribution == "normal",
paste0("mean = ", insight::format_value(params$Prior_Location), ", SD = ", insight::format_value(params$Prior_Scale)),
paste0("location = ", insight::format_value(params$Prior_Location), ", scale = ", insight::format_value(params$Prior_Scale))
)

values <- paste0(params$Prior_Distribution, " (", values, ")")

if (length(unique(values)) == 1 & nrow(params) > 1) {
text <- paste0("all set as ", values[1])
} else {
text <- paste0("set as ", format_text(values))
}

text <- paste0("Priors over parameters were ", text, " distributions")
as.report_priors(text)
}


# report_parameters -------------------------------------------------------


#' @export
report_parameters.brmsfit <- report_parameters.stanreg



# report_intercept --------------------------------------------------------


#' @export
report_intercept.brmsfit <- report_intercept.stanreg



# report_info -------------------------------------------------------------


#' @export
report_info.brmsfit <- report_info.stanreg
6 changes: 6 additions & 0 deletions R/report_random.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
#' r
#' summary(r)
#' }
#' if(require("brms")){
#' model <- brm(mpg ~ disp + (1 | cyl), data = mtcars, refresh=0, iter=1000)
#' r <- report_random(model)
#' r
#' summary(r)
#' }
#' @export
report_random <- function(x, ...) {
UseMethod("report_random")
Expand Down
67 changes: 67 additions & 0 deletions man/report.brmsfit.Rd

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

6 changes: 6 additions & 0 deletions man/report_random.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/test-report.brmsfit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
if (require("testthat") && require("brms")) {
model <- brm(mpg ~ qsec + wt, data = mtcars, refresh=0, iter=300)

testthat::test_that("model", {
r <- report(model)
testthat::expect_is(summary(r), "character")
testthat::expect_is(as.data.frame(r), "data.frame")

expect_equal(
as.data.frame(r)$Parameter,
c(
"(Intercept)", "qsec", "wt", NA, "ELPD", "LOOIC", "WAIC", "R2",
"R2 (adj.)", "Sigma"
)
)
expect_equal(
as.data.frame(r)$Median,
c(19.906865, 0.930295, -5.119548, rep(NA, 7)),
tolerance = 1e-1
)
expect_equal(
as.data.frame(r)$pd,
c(rep(1, 3), rep(NA, 7)),
tolerance = 1e-1
)
})
}

0 comments on commit 46f822f

Please sign in to comment.