|
| 1 | +#' Create a \code{draws} object from a \code{stanreg} object |
| 2 | +#' |
| 3 | +#' Convert a \code{stanreg} object to a format supported by the \pkg{posterior} |
| 4 | +#' package. To subset iterations, chains, or draws, use |
| 5 | +#' \code{\link[posterior:subset_draws]{subset_draws}} after making the |
| 6 | +#' \code{draws} object. |
| 7 | +#' |
| 8 | +#' @name draws-stanreg |
| 9 | +#' @aliases as_draws as_draws_matrix as_draws_array as_draws_df as_draws_rvars as_draws_list |
| 10 | +#' |
| 11 | +#' @param x A \code{stanreg} object. |
| 12 | +#' @param pars,regex_pars See \code{\link{as.matrix.stanreg}}. |
| 13 | +#' @param ... Arguments passed to individual methods. |
| 14 | +#' |
| 15 | +#' @examples |
| 16 | +#' fit <- stan_glm(mpg ~ wt + cyl, data = mtcars) |
| 17 | +#' head(as_draws_df(fit)) |
| 18 | +#' posterior <- as_draws_array(fit) |
| 19 | +#' posterior::summarize_draws(posterior) |
| 20 | +#' |
| 21 | +#' as_draws_array(fit, variable = c("wt", "sigma")) |
| 22 | +#' |
| 23 | +NULL |
| 24 | + |
| 25 | +#' @rdname draws-stanreg |
| 26 | +#' @importFrom posterior as_draws |
| 27 | +#' @method as_draws stanreg |
| 28 | +#' @export |
| 29 | +#' @export as_draws |
| 30 | +as_draws.stanreg <- function(x, pars = NULL, regex_pars = NULL, ...) { |
| 31 | + as_draws_array(x, pars = pars, regex_pars = regex_pars, ...) |
| 32 | +} |
| 33 | + |
| 34 | +#' @rdname draws-stanreg |
| 35 | +#' @importFrom posterior as_draws_matrix |
| 36 | +#' @method as_draws_matrix stanreg |
| 37 | +#' @export |
| 38 | +#' @export as_draws_matrix |
| 39 | +as_draws_matrix.stanreg <- function(x, pars = NULL, regex_pars = FALSE, ...) { |
| 40 | + posterior::as_draws_matrix( |
| 41 | + as.matrix.stanreg(x, pars = pars, regex_pars = regex_pars, ...) |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +#' @rdname draws-stanreg |
| 46 | +#' @importFrom posterior as_draws_array |
| 47 | +#' @method as_draws_array stanreg |
| 48 | +#' @export |
| 49 | +#' @export as_draws_array |
| 50 | +as_draws_array.stanreg <- function(x, pars = NULL, regex_pars = FALSE, ...) { |
| 51 | + posterior::as_draws_array( |
| 52 | + as.array.stanreg(x, pars = pars, regex_pars = regex_pars, ...) |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +#' @rdname draws-stanreg |
| 57 | +#' @importFrom posterior as_draws_df |
| 58 | +#' @method as_draws_df stanreg |
| 59 | +#' @export |
| 60 | +#' @export as_draws_df |
| 61 | +as_draws_df.stanreg <- function(x, pars = NULL, regex_pars = FALSE, ...) { |
| 62 | + posterior::as_draws_df( |
| 63 | + as.array.stanreg(x, pars = pars, regex_pars = regex_pars, ...) |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +#' @rdname draws-stanreg |
| 68 | +#' @importFrom posterior as_draws_list |
| 69 | +#' @method as_draws_list stanreg |
| 70 | +#' @export |
| 71 | +#' @export as_draws_list |
| 72 | +as_draws_list.stanreg <- function(x, pars = NULL, regex_pars = FALSE, ...) { |
| 73 | + posterior::as_draws_list( |
| 74 | + as.array.stanreg(x, pars = pars, regex_pars = regex_pars, ...) |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +#' @rdname draws-stanreg |
| 79 | +#' @importFrom posterior as_draws_rvars |
| 80 | +#' @method as_draws_rvars stanreg |
| 81 | +#' @export |
| 82 | +#' @export as_draws_rvars |
| 83 | +as_draws_rvars.stanreg <- function(x, pars = NULL, regex_pars = FALSE, ...) { |
| 84 | + posterior::as_draws_rvars( |
| 85 | + as.array.stanreg(x, pars = pars, regex_pars = regex_pars, ...) |
| 86 | + ) |
| 87 | +} |
0 commit comments