Skip to content

Commit 3f9f11b

Browse files
committed
minor code updates and modernised documentation to use more markdown styles
1 parent 7ae301d commit 3f9f11b

34 files changed

+393
-327
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Description: Calculate Bayesian marginal effects, average marginal effects, and
1919
License: GPL (>= 3)
2020
Encoding: UTF-8
2121
Roxygen: list(markdown = TRUE)
22-
RoxygenNote: 7.3.2
22+
RoxygenNote: 7.3.3
2323
Depends:
2424
R (>= 4.0.0)
2525
Imports:

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export(bsummary)
55
export(integratemvn)
66
export(integratere)
77
export(lmcpp)
8+
export(lmcpp_alt)
89
export(marginalcoef)
910
export(prediction)
1011
export(rowBootMeans)

R/RcppExports.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ lmcpp <- function(X, y) {
7777
.Call(`_brmsmargins_lmcpp`, X, y)
7878
}
7979

80+
#' Fast(er?) Linear Regression
81+
#'
82+
#' Used to get marginal coefficients off of a generalized linear mixed model.
83+
#'
84+
#' @param X A numeric model matrix. If intercept is desired, it must already have been added as a column.
85+
#' @param y A numeric matrix. A single column if one response variable or multiple columns
86+
#' where each column is a different response, such as a for marginal coefficients where
87+
#' each column is a different MCMC sample.
88+
#' @return A numeric matrix with the coefficient.
89+
#' @export
90+
#' @examples
91+
#' lmcpp_alt(cbind(1, mtcars$hp, mtcars$am), as.matrix(mtcars[, c("mpg", "qsec")]))
92+
lmcpp_alt <- function(X, y) {
93+
.Call(`_brmsmargins_lmcpp_alt`, X, y)
94+
}
95+
8096
#' Bootstrap Row Means
8197
#'
8298
#' This takes a numeric matrix, bootstrap resamples each row, and then

R/assert.R

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
#' @title Check Assertions about a \code{brmsfit} Model Object
1+
#' @title Check Assertions about a [brms::brmsfit-class] class Model Object
22
#'
33
#' @description
44
#' These are a set of internal utility functions.
55
#' They are not intended for general use.
66
#' Instead, they are intended to be called in circumstances
7-
#' where the expected result is \code{TRUE}.
7+
#' where the expected result is `TRUE`.
88
#' All of them are designed to try to give informative error
99
#' messages if the assertion is not met.
10-
#' All of them result in a \code{stop()} error if the assertion is not met.
10+
#' All of them result in a `stop` error if the assertion is not met.
1111
#'
1212
#' @details
13-
#' \itemize{
14-
#' \item{\code{.assertbrmsfit}}{asserts that the object should be of class \code{brmsfit}.}
15-
#' \item{\code{.assertgaussian}}{asserts that all random effects are Gaussian.}
16-
#' \item{\code{.assertfamily}}{asserts that the distribution (family) of the outcome is a currently supported family. Only applies when integrating out random effects.}
17-
#' \item{\code{.assertlink}}{asserts that the link function is a currently supported link function. Only applies when integrating out random effects.}
18-
#' }
13+
#' - `.assertbrmsfit`: asserts that the object should be [brms::brmsfit-class].
14+
#' - `.assertgaussian`: asserts that all random effects are Gaussian.
15+
#' - `.assertfamily`: asserts that the distribution (family) of the outcome is a currently supported family. Only applies when integrating out random effects.
16+
#' - `.assertlink`: asserts that the link function is a currently supported link function. Only applies when integrating out random effects.
1917
#'
20-
#' @param object A \code{brmsfit} model object to be evaluated.
21-
#' @param dpar Required for \code{.assertdpar} which checks this is valid.
22-
#' Optional for \code{.assertlink} which will use \code{NULL} if not
23-
#' specified. If specified, this should be \code{NULL} or
18+
#' @param object A [brms::brmsfit-class] model object to be evaluated.
19+
#' @param dpar Required for [.assertdpar()] which checks this is valid.
20+
#' Optional for [.assertlink()] which will use `NULL` if not
21+
#' specified. If specified, this should be `NULL` or
2422
#' a character string.
2523
#'
26-
#' @return An invisible, logical \code{TRUE} if the assertion is met.
24+
#' @return An invisible, logical `TRUE` if the assertion is met.
2725
#' An (informative) error message if the assertion is not met.
2826
#' @keywords internal
2927
#' @name assertall

R/averageposterior.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#' draws are on different rows.
1111
#' @param resample An integer indicating the number of
1212
#' bootstrap resamples of the posterior predictions to
13-
#' use when calculating summaries. Defaults to \code{0L}.
14-
#' See the details section for more informations as its implementation
13+
#' use when calculating summaries. Defaults to `0L`.
14+
#' See the details section for more information as its implementation
1515
#' is experimental and it may not operate as one would expect.
16-
#' @param seed A seed for random number generation. Defaults to \code{FALSE},
16+
#' @param seed A seed for random number generation. Defaults to `FALSE`,
1717
#' which means no seed is set.
18-
#' Only used if \code{resample} is a positive, non-zero integer.
18+
#' Only used if `resample` is a positive, non-zero integer.
1919
#' @return A vector of the averaged posterior.
2020
#' @keywords internal
2121
.averagePosterior <- function(posterior, resample = 0L, seed = FALSE) {

R/builders.R

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@
55
#' They are not intended for general use.
66
#'
77
#' @details
8-
#' \itemize{
9-
#' \item{\code{.namesL}}{Generate names of an L matrix from \code{brms}. Create the variable names for the Cholesky decomposition of the random effects correlation matrix in \code{brms}. Note that \code{brms} returns the lower triangular matrix and we want the upper triangular matrix, so the names are transposed. The results can then be passed to the \code{tab2mat} function to convert the row vector into a matrix.}
10-
#' \item{\code{.buildL}}{Returns the L matrix object. Rows are posterior draws.}
11-
#' \item{\code{.namesSD}}{Create the names of random effect standard deviation estimates.}
12-
#' \item{\code{.buildSD}}{Return matrix of random effect standard deviation estimates. Rows are posterior draws.}
13-
#' \item{\code{.namesZ}}{Create the names of random effects data for predictions.}
14-
#' \item{\code{.buildZ}}{Return matrix of data for random effect predictions.}
15-
#' }
8+
#' - `.namesL`: Generate names of an L matrix from \pkg{brms}.
9+
#' Create the variable names for the Cholesky decomposition of the random effects
10+
#' correlation matrix in [brms::brm()]. Note that [brms::brm()] returns the lower
11+
#' triangular matrix and we want the upper triangular matrix, so the names are
12+
#' transposed. The results can then be passed to [tab2mat()]
13+
#' to convert the row vector into a matrix.
14+
#' - `.buildL`: Returns the L matrix object. Rows are posterior draws.
15+
#' - `.namesSD`: Create the names of random effect standard deviation estimates.
16+
#' - `.buildSD`: Return matrix of random effect standard deviation estimates. Rows are posterior draws.
17+
#' - `.namesZ`: Create the names of random effects data for predictions.
18+
#' - `.buildZ`: Return matrix of data for random effect predictions.
1619
#'
17-
#' @param data A data object. For example the result of [make_standata()]
20+
#' @param data A data object. For example the result of [brms::make_standata()]
1821
#' for [.buildZ()], which is a list,
19-
#' or a dataset of the posterior draws such as from [as_draws_df()]
22+
#' or a dataset of the posterior draws such as from [brms::as_draws_df()]
2023
#' for [.buildL()] and [.buildSD()].
2124
#' @param ranef A data set with information about the model object random effects.
22-
#' Only used for \code{.namesSD} and \code{.buildSD}.
25+
#' Only used for [.namesSD()] and [.buildSD()].
2326
#' @param block Which random effect block to use. An integer.
2427
#' @param number The number of elements in that random effect block. An integer.
2528
#' @param dpar Which dpar to use. Does not apply to the L matrix.
26-
#' @return A character vector for all \code{.names} functions or a matrix
27-
#' for all \code{.build} functions.
29+
#' @return A character vector for all `.names` functions or a matrix
30+
#' for all `.build` functions.
2831
#' @keywords internal
2932
#' @name builders
3033
NULL

R/marginalcoef.R

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
#' Marginal Coefficients from a 'brms' Model
22
#'
3-
#' Calculate marginal coefficients from a \code{brms}
3+
#' Calculate marginal coefficients from a `brms`
44
#' generalized linear mixed model using the method proposed by Hedeker (2018).
55
#'
6-
#' @param object A fitted brms model object that includes random effects. Required.
6+
#' @param object A fitted `brms` model object that includes random effects. Required.
77
#' @param summarize A logical value, whether or not to
88
#' calculate summaries of the posterior predictions.
9-
#' Defaults to \code{TRUE}.
9+
#' Defaults to `TRUE`.
1010
#' @param posterior A logical value whether or not to
1111
#' save and return the posterior samples. Defaults
12-
#' to \code{FALSE} as the assumption is a typical
12+
#' to `FALSE` as the assumption is a typical
1313
#' use case is to return the summaries only.
1414
#' @param index An optional integer vector, giving the posterior draws
1515
#' to be used in the calculations. If omitted, defaults to all
1616
#' posterior draws.
1717
#' @param backtrans A character string indicating the type of
1818
#' back transformation to be applied. Can be one of
19-
#' \dQuote{response} meaning to use the response scale,
20-
#' \dQuote{linear} or \dQuote{identity} meaning to use the linear predictor scale,
19+
#' "response" meaning to use the response scale,
20+
#' "linear" or "identity" meaning to use the linear predictor scale,
2121
#' or a specific back transformation desired, from a possible list of
22-
#' \dQuote{invlogit}, \dQuote{exp}, \dQuote{square}, or \dQuote{inverse}.
22+
#' "invlogit", "exp", "square", or "inverse".
2323
#' Custom back transformations should only be needed if, for example,
2424
#' the outcome variable was transformed prior to fitting the model.
2525
#' @param k An integer providing the number of random draws to use for
26-
#' integrating out the random effects. Only relevant when \code{effects}
27-
#' is \dQuote{integrateoutRE}.
26+
#' integrating out the random effects. Only relevant when `effects = "integrateoutRE"`.
2827
#' @param seed An \emph{optional} argument that controls whether (and if so what) random seed
2928
#' to use. This can help with reproducibility of results.
3029
#' It is missing by default.
31-
#' @param ... Additional arguments passed to \code{bsummary()},
32-
#' and only relevant if \code{summarize} is \code{TRUE}.
33-
#' @return A list with \code{Summary} and \code{Posterior}.
34-
#' Some of these may be \code{NULL} depending on the arguments used.
30+
#' @param ... Additional arguments passed to [bsummary()],
31+
#' and only relevant if `summarize` is `TRUE`.
32+
#' @return A list with `Summary` and `Posterior`.
33+
#' Some of these may be `NULL` depending on the arguments used.
3534
#' @references
3635
#' Hedeker, D., du Toit, S. H., Demirtas, H. & Gibbons, R. D. (2018)
37-
#' \doi{10.1111/biom.12707}
36+
#' [DOI: 10.1111/biom.12707](https://doi.org/10.1111/biom.12707).
3837
#' \dQuote{A note on marginalization of regression parameters from mixed models of binary outcomes}
3938
#' @importFrom data.table as.data.table
4039
#' @importFrom stats formula

0 commit comments

Comments
 (0)