diff --git a/R/as_draws_array.R b/R/as_draws_array.R index eb4092b4..a88088bf 100644 --- a/R/as_draws_array.R +++ b/R/as_draws_array.R @@ -126,7 +126,7 @@ draws_array <- function(..., .nchains = 1) { variables <- names(out) out <- unlist(out) out <- array(out, dim = c(niterations, .nchains, length(variables))) - dimnames(out)[[3]] <- variables + dimnames(out) <- list(NULL, NULL, variables) as_draws_array(out) } diff --git a/R/weight_draws.R b/R/weight_draws.R index 47591b4b..495710ba 100644 --- a/R/weight_draws.R +++ b/R/weight_draws.R @@ -24,18 +24,25 @@ #' x <- example_draws() #' #' # sample some random weights for illustration -#' weights <- rexp(ndraws(x)) +#' wts <- rexp(ndraws(x)) +#' head(wts) #' #' # add weights -#' x <- weight_draws(x, weights = weights) +#' x <- weight_draws(x, weights = wts) +#' #' # extract weights -#' head(weights(x)) +#' head(weights(x)) # defaults to normalized weights +#' head(weights(x, normalize=FALSE)) # recover original weights +#' head(weights(x, log=TRUE)) # get normalized log-weights #' #' # add weights which are already on the log scale -#' log_weights <- log(weights) +#' log_wts <- log(wts) +#' head(log_wts) +#' +#' x <- weight_draws(x, weights = log_wts, log = TRUE) #' # extract weights -#' x <- weight_draws(x, weights = weights, log = TRUE) #' head(weights(x)) +#' head(weights(x, log=TRUE, normalize = FALSE)) # recover original log_wts #' #' @export weight_draws <- function(x, weights, ...) { @@ -112,22 +119,7 @@ weight_draws.draws_list <- function(x, weights, log = FALSE, ...) { #' #' @seealso [`weight_draws`], [`resample_draws`] #' -#' @examples -#' x <- example_draws() -#' -#' # sample some random weights for illustration -#' weights <- rexp(ndraws(x)) -#' -#' # add weights -#' x <- weight_draws(x, weights = weights) -#' -#' # return normalized weights -#' weights <- weights(x) -#' head(weights) -#' -#' # return normalized log weights -#' log_weights <- weights(x, log = TRUE) -#' head(log_weights) +#' @inherit weight_draws examples #' #' @export weights.draws <- function(object, log = FALSE, normalize = TRUE, ...) { diff --git a/man/weight_draws.Rd b/man/weight_draws.Rd index 3de60e9c..40421a23 100644 --- a/man/weight_draws.Rd +++ b/man/weight_draws.Rd @@ -47,18 +47,25 @@ from \code{draws} objects. x <- example_draws() # sample some random weights for illustration -weights <- rexp(ndraws(x)) +wts <- rexp(ndraws(x)) +head(wts) # add weights -x <- weight_draws(x, weights = weights) +x <- weight_draws(x, weights = wts) + # extract weights -head(weights(x)) +head(weights(x)) # defaults to normalized weights +head(weights(x, normalize=FALSE)) # recover original weights +head(weights(x, log=TRUE)) # get normalized log-weights # add weights which are already on the log scale -log_weights <- log(weights) +log_wts <- log(wts) +head(log_wts) + +x <- weight_draws(x, weights = log_wts, log = TRUE) # extract weights -x <- weight_draws(x, weights = weights, log = TRUE) head(weights(x)) +head(weights(x, log=TRUE, normalize = FALSE)) # recover original log_wts } \seealso{ diff --git a/man/weights.draws.Rd b/man/weights.draws.Rd index 1756a033..184dacac 100644 --- a/man/weights.draws.Rd +++ b/man/weights.draws.Rd @@ -28,18 +28,25 @@ See \code{\link{weight_draws}} for details how to add weights to \code{\link{dra x <- example_draws() # sample some random weights for illustration -weights <- rexp(ndraws(x)) +wts <- rexp(ndraws(x)) +head(wts) # add weights -x <- weight_draws(x, weights = weights) +x <- weight_draws(x, weights = wts) -# return normalized weights -weights <- weights(x) -head(weights) +# extract weights +head(weights(x)) # defaults to normalized weights +head(weights(x, normalize=FALSE)) # recover original weights +head(weights(x, log=TRUE)) # get normalized log-weights -# return normalized log weights -log_weights <- weights(x, log = TRUE) -head(log_weights) +# add weights which are already on the log scale +log_wts <- log(wts) +head(log_wts) + +x <- weight_draws(x, weights = log_wts, log = TRUE) +# extract weights +head(weights(x)) +head(weights(x, log=TRUE, normalize = FALSE)) # recover original log_wts } \seealso{