Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
e168016
update psis paper reference
avehtari Mar 1, 2024
206f2cc
Merge pull request #344 from stan-dev/update-psis-paper-reference
paul-buerkner Mar 1, 2024
204fbbe
do not allow missing values in weights
Mar 5, 2024
0a5a29c
Merge branch 'stan-dev:master' into validate_weights-fix
n-kall Mar 6, 2024
5403ae5
Merge pull request #350 from n-kall/validate_weights-fix
paul-buerkner Mar 6, 2024
e8ec435
Fix #345 only count non-constant tail for pareto_k if tail = "both"
Mar 6, 2024
6872b50
ensure both tails are smoothed if tail = "both"
Mar 6, 2024
f1e25b1
improve tests for pareto_smooth tail argument
Mar 6, 2024
23d3e9a
Merge branch 'stan-dev:master' into pareto_k_tail_fix
n-kall Mar 6, 2024
d0deed3
Merge pull request #351 from n-kall/pareto_k_tail_fix
paul-buerkner Mar 6, 2024
16285fa
expose 'scalar' variable selection option
paul-buerkner Mar 12, 2024
9de9857
rerun doc generation
paul-buerkner Mar 12, 2024
084946e
export helper functions
Apr 11, 2024
226a4fa
update psis paper reference
Apr 11, 2024
91baeea
rerun doc gen
Apr 11, 2024
f7d07d6
update psis ref journal info
avehtari Apr 12, 2024
609028d
Merge pull request #362 from stan-dev/update-psis-ref
paul-buerkner Apr 12, 2024
39c9204
do not export gpdfit
Apr 16, 2024
d95b029
adjust variable names in exported functions
Apr 16, 2024
42d042d
Merge branch 'master' into export-helpers
n-kall Apr 16, 2024
72fbd6d
fixes to weighted mcse
Apr 16, 2024
0964ad6
fixes to documentation
Apr 16, 2024
beadac1
Merge branch 'export-helpers' of github.com:n-kall/posterior into exp…
Apr 16, 2024
15dc777
type check assertions for rvar draws, closes #363
mjskay May 7, 2024
fd72161
Merge pull request #364 from stan-dev/rvar-type-check
mjskay May 7, 2024
b788e4f
update news
May 10, 2024
050ce3e
add thin_draws updates to news
May 10, 2024
f500de1
add individual pareto diagnostics to news
May 10, 2024
a77b178
minor tweak to news
May 10, 2024
0cd1433
tweak news formatting
May 10, 2024
c89ed34
Merge pull request #366 from n-kall/master
paul-buerkner May 13, 2024
8a5f677
set r_eff default back to NULL, fixes #368
May 14, 2024
5112b65
Merge branch 'master' of github.com:n-kall/posterior
May 14, 2024
6c121b8
update docs
May 14, 2024
419a1a3
Merge pull request #369 from n-kall/master
paul-buerkner May 14, 2024
ee9d36f
Merge branch 'master' into export-helpers
n-kall May 15, 2024
86c8fba
Merge pull request #361 from n-kall/export-helpers
paul-buerkner May 15, 2024
a102f61
fix eof newline issue in doc template
May 16, 2024
8aea0c1
pareto_* functions now return unnamed scalars rather than named
May 16, 2024
9d59c18
render docs
May 16, 2024
173f7d7
don't pass dots from pareto_khat
May 16, 2024
4d3ccf1
Merge pull request #370 from n-kall/pareto_diags_names
paul-buerkner May 16, 2024
0c6620e
Improve input checking and warnings for pareto diags. Fixes #315.
Jun 4, 2024
44fc43e
Merge pull request #371 from n-kall/pareto_k_tail_fix
paul-buerkner Jun 4, 2024
b3fa9b0
new Pareto-k diagnostics vignette
avehtari Jun 27, 2024
d7f2f2f
Apply part of suggestions from Noa's code review
avehtari Jun 28, 2024
bab7b3f
vignette fixes based on Noa's review
avehtari Jun 28, 2024
a462b6c
fix references in the posterior vignette
avehtari Jun 28, 2024
128cf09
Apply suggestions from Noa
avehtari Jun 28, 2024
470075c
Merge pull request #372 from stan-dev/pareto_diags_vignette
avehtari Jun 28, 2024
758dac5
prepare new cran release
paul-buerkner Jun 28, 2024
93dfae1
fix minor cran check notes
paul-buerkner Jun 28, 2024
a5c3f6c
update website
paul-buerkner Jul 5, 2024
bc05324
Also skip if missing 'gbm'
MichaelChirico Jul 12, 2024
18c915e
Merge pull request #373 from MichaelChirico/patch-1
paul-buerkner Jul 14, 2024
d6389f6
update reference for nested r-hat to published version
Aug 5, 2024
fc295b0
fix typo in R-hat reference
Aug 5, 2024
88b95de
render docs
Aug 6, 2024
20b6d3a
Merge pull request #374 from n-kall/master
paul-buerkner Aug 6, 2024
b7bdbb2
fix autocorrelation() not printing
jgabry Sep 27, 2024
25b5322
fix rdname for ess_mean
n-kall Oct 3, 2024
ff9104a
Merge pull request #379 from n-kall/ess_mean
paul-buerkner Oct 3, 2024
90a07cc
more diagnostic doc fixes
paul-buerkner Oct 3, 2024
dd9f71e
fix use of ceiling in ps_tail_length
avehtari Nov 7, 2024
39f3bbd
add more citations
avehtari Nov 7, 2024
4166286
Merge pull request #383 from stan-dev/fix_pareto_khat
paul-buerkner Nov 7, 2024
ed24471
Merge pull request #384 from stan-dev/add_more_citations
paul-buerkner Nov 7, 2024
cb5b2b1
Update misc.R
jgabry Nov 11, 2024
d0f8b76
Merge pull request #376 from stan-dev/fix-print-autocorrelation
jgabry Nov 11, 2024
bf7ac8c
default method for PIT and LOO-PIT
TeemuSailynoja Nov 12, 2024
79d4521
Convert lists of matrices to `draws_array` objects
paul-buerkner Dec 2, 2024
a6ab390
Revert "Convert lists of matrices to `draws_array` objects"
jgabry Dec 17, 2024
e0610a4
fix as_array_matrix_list
paul-buerkner Dec 17, 2024
55e18ab
Merge branch 'master' into convert-list-of-matrices-to-draws_array
jgabry Dec 17, 2024
bf68d98
Fix merge conflict
jgabry Dec 17, 2024
aeb26ba
Merge pull request #387 from stan-dev/convert-list-of-matrices-to-dra…
jgabry Dec 17, 2024
b962b63
use the same equation in ess_sd as in mcse_sd
avehtari Dec 20, 2024
20e3aa1
Merge pull request #388 from stan-dev/update_ess_sd
paul-buerkner Dec 21, 2024
6bf2a07
don't allow variables to contain NA values
n-kall Jan 7, 2025
797ab20
Merge pull request #389 from n-kall/master
paul-buerkner Jan 7, 2025
c405519
fix documentation of u_scale()
avehtari Jan 29, 2025
e96fe22
Merge pull request #390 from stan-dev/fix_u_scale_doc
paul-buerkner Jan 30, 2025
8d5fb87
fix iteration_ids for zero length draws_lists
paul-buerkner Feb 23, 2025
b2b3ac2
rerun readme
paul-buerkner Feb 27, 2025
54cb1a4
fix test for pareto_khat
n-kall Feb 27, 2025
6c0e335
Merge pull request #393 from n-kall/pareto-test-fix
paul-buerkner Feb 27, 2025
6a54bbb
prepare for release of version 1.6.1
paul-buerkner Feb 27, 2025
d9b02be
Add validation for loo_weights.
TeemuSailynoja Mar 6, 2025
4b372be
Update README to include a link to CONTRIBUTING.md
TeemuSailynoja Mar 6, 2025
d2e2341
Correctly reference to the CONTRIBUTING.md in posterior.
TeemuSailynoja Mar 6, 2025
307260e
Merge pull request #394 from TeemuSailynoja/link-to-contribution-guide
paul-buerkner Mar 6, 2025
359f6b1
Check PIT bounds only once.
TeemuSailynoja Mar 14, 2025
72a3a30
Normalize loo_weights in pit.default. Add tests for internals and pit…
TeemuSailynoja Mar 14, 2025
75d2592
Handle PIT = 0 without warnings. Add documentation for input parameters.
TeemuSailynoja Mar 14, 2025
efc37d6
default method for PIT and LOO-PIT
TeemuSailynoja Nov 12, 2024
0258555
Add validation for loo_weights.
TeemuSailynoja Mar 6, 2025
48dc3a6
Normalize loo_weights in pit.default. Add tests for internals and pit…
TeemuSailynoja Mar 14, 2025
f43023e
Handle PIT = 0 without warnings. Add documentation for input parameters.
TeemuSailynoja Mar 14, 2025
b2ffb7e
Merge remote-tracking branch 'refs/remotes/origin/add_pit_functions' …
TeemuSailynoja Mar 14, 2025
8f2c361
Implement pit for draws and rvar. Add unit tests.
TeemuSailynoja Mar 19, 2025
ab21007
Make PIT description short and add myself as contributor.
TeemuSailynoja Mar 19, 2025
c00b326
typo1 R/pit.R
TeemuSailynoja Mar 19, 2025
2537986
change \(x) to function(x) for backewards compatibility.
TeemuSailynoja Mar 19, 2025
b392bd4
Update cache version in covr.yml
TeemuSailynoja Mar 19, 2025
47be6e3
Update cache version in rcmdcheck.yml
TeemuSailynoja Mar 19, 2025
26a57f2
upload-artivact version
TeemuSailynoja Mar 19, 2025
4e1cc8a
Merge pull request #397 from TeemuSailynoja/bump-workflow-cache-version
paul-buerkner Mar 19, 2025
f58e347
Merge branch 'stan-dev:master' into add_pit_functions
TeemuSailynoja Mar 19, 2025
7acb46f
fix typo in test-pit.R
TeemuSailynoja Mar 19, 2025
985a6c5
Update documentation of pit(), use validate_weights, log = FALSE by d…
TeemuSailynoja Mar 27, 2025
203fdb3
make x optional in validate_y
TeemuSailynoja Mar 27, 2025
9787124
fix rvar and draws checking
TeemuSailynoja Mar 27, 2025
4a4f212
Update log_sum_exp
TeemuSailynoja Mar 27, 2025
69e3560
Add tolerance to warning of pit over 1. Always clip pit at 1.
TeemuSailynoja Mar 27, 2025
af8f26a
remove test for warning when pit over tolerance. Can't reproduce.
TeemuSailynoja Mar 27, 2025
f33a216
fix example in pit
TeemuSailynoja Mar 27, 2025
fcec220
Test log_sum_exp
TeemuSailynoja Mar 27, 2025
4b7e0bd
Merge pull request #400 from TeemuSailynoja/update-log_sum_exp
paul-buerkner Mar 28, 2025
f8a9894
Update NEWS.md
TeemuSailynoja Mar 28, 2025
006ef16
Update NEWS.md
TeemuSailynoja Mar 28, 2025
c5f1c5a
properly take into account r_eff when deciding tail length
n-kall Mar 28, 2025
aba4bb8
Merge pull request #395 from TeemuSailynoja/add_pit_functions
paul-buerkner Mar 28, 2025
302a2dd
change ceiling to floor in ps_tail_length to match paper
n-kall Mar 31, 2025
6cee79a
correct tail length calc
n-kall Mar 31, 2025
f17844f
Merge pull request #402 from n-kall/tail-length-fix
avehtari Mar 31, 2025
253ce18
Merge branch 'stan-dev:rvar_weights' into rvar_weights
n-kall May 6, 2025
0fc1519
Merge branch 'rvar_weights' into rvar_weights_merge
n-kall May 6, 2025
51df13b
fix merge conflicts
n-kall May 6, 2025
acaa738
fixes to docs for weighted rvars and diagnostics
n-kall May 6, 2025
ea582b4
Merge pull request #3 from n-kall/rvar_weights_merge
n-kall May 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/covr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rcmdcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:

- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
uses: actions/upload-artifact@v4
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: posterior
Title: Tools for Working with Posterior Distributions
Version: 1.5.0.9000
Date: 2023-10-31
Version: 1.6.1
Date: 2025-02-27
Authors@R: c(person("Paul-Christian", "Bürkner", email = "paul.buerkner@gmail.com", role = c("aut", "cre")),
person("Jonah", "Gabry", email = "jsg2201@columbia.edu", role = c("aut")),
person("Matthew", "Kay", email = "mjskay@northwestern.edu", role = c("aut")),
Expand All @@ -11,7 +11,8 @@ Authors@R: c(person("Paul-Christian", "Bürkner", email = "paul.buerkner@gmail.c
person("Ben", "Lambert", role = c("ctb")),
person("Ozan", "Adıgüzel", role = c("ctb")),
person("Jacob", "Socolar", role = c("ctb")),
person("Noa", "Kallioinen", role = c("ctb")))
person("Noa", "Kallioinen", role = c("ctb")),
person("Teemu", "Säilynoja", role = c("ctb")))
Description: Provides useful tools for both users and developers of packages
for fitting Bayesian models or working with output from Bayesian models.
The primary goals of the package are to:
Expand Down Expand Up @@ -56,5 +57,5 @@ LazyData: false
URL: https://mc-stan.org/posterior/, https://discourse.mc-stan.org/
BugReports: https://github.com/stan-dev/posterior/issues
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.2
VignetteBuilder: knitr
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ S3method(pareto_min_ss,rvar)
S3method(pareto_smooth,default)
S3method(pareto_smooth,rvar)
S3method(pillar_shaft,rvar)
S3method(pit,default)
S3method(pit,draws_matrix)
S3method(pit,rvar)
S3method(print,draws_array)
S3method(print,draws_df)
S3method(print,draws_list)
Expand Down Expand Up @@ -480,6 +483,11 @@ export(pareto_khat)
export(pareto_khat_threshold)
export(pareto_min_ss)
export(pareto_smooth)
export(pit)
export(ps_convergence_rate)
export(ps_khat_threshold)
export(ps_min_ss)
export(ps_tail_length)
export(quantile2)
export(r_scale)
export(rdo)
Expand Down
28 changes: 25 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
# posterior (development version)
# posterior 1.6.1

### Bug Fixes

* Fix a test issue that led to an R CMD check failure on R devel.

### Enhancements

* Add `pareto_smooth` option to `weight_draws`, to Pareto smooth
weights before adding to a draws object.
* Convert lists of matrices to `draws_array` objects.
* Improve the documentation in various places.
* Implement `pit()` for draws and rvar objects. LOO-PIT can be computed using `weights`.
* Add support for applying weights to individual `rvar` objects.
* Add `log_weights()` function for easy access to raw internal weights.
* Add support for weights in `ess` and `mcse` functions.

# posterior 1.6.0

### Enhancements

* Add `exclude` option to `subset_draws()`, which can be used to exclude
the matched selection.
* Add `are_log_weights` option to `pareto_smooth()`, which is necessary
for correct Pareto smoothing computation if the input vector
consists of log weights.
* Add `pareto_smooth` option to `weight_draws()`, to Pareto smooth
weights before adding to a draws object.
* Add individual Pareto diagnostic functions (`pareto_khat()`,
`pareto_khat_threshold()`, `pareto_min_ss()`, `pareto_convergence_rate()`)
* `thin_draws()` now automatically thins draws based on ESS by default,
and non-integer thinning is possible.
* Matrix multiplication of `rvar`s can now be done with the base matrix
multiplication operator (`%*%`) instead of `%**%` in R >= 4.3.
* `variables()`, `variables<-()`, `set_variables()`, and `nvariables()` now
Expand Down
9 changes: 5 additions & 4 deletions R/as_draws.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ closest_draws_format <- function(x) {
out <- "rvars"
} else if (is_draws_list_like(x)) {
out <- "list"
}
else {
stop_no_call("Don't know how to transform an object of class ",
"'", class(x)[1L], "' to any supported draws format.")
} else {
stop_no_call(
"Don't know how to transform an object of class '",
class(x)[1L], "' to any supported draws format."
)
}
paste0("draws_", out)
}
Expand Down
27 changes: 15 additions & 12 deletions R/as_draws_array.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ as_draws_array.mcmc.list <- function(x, ...) {

# try to convert any R object into a 'draws_array' object
.as_draws_array <- function(x) {
x <- as.array(x)
if (is_matrix_list_like(x)) {
x <- as_array_matrix_list(x)
} else {
x <- as.array(x)
}
new_dimnames <- list(iteration = NULL, chain = NULL, variable = NULL)
if (!is.null(dimnames(x)[[3]])) {
new_dimnames[[3]] <- dimnames(x)[[3]]
Expand Down Expand Up @@ -178,7 +182,14 @@ is_draws_array <- function(x) {

# is an object looking like a 'draws_array' object?
is_draws_array_like <- function(x) {
is.array(x) && length(dim(x)) == 3L
is.array(x) && length(dim(x)) == 3L ||
is_matrix_list_like(x)
}

# is an object likely a list of matrices?
# such an object can be easily converted to a draws_array
is_matrix_list_like <- function(x) {
is.list(x) && length(dim(x[[1]])) == 2L
}

#' Extract parts of a `draws_array` object
Expand Down Expand Up @@ -217,15 +228,8 @@ variance.draws_array <- function(x, ...) {
# convert a list of matrices to an array
as_array_matrix_list <- function(x) {
stopifnot(is.list(x))
if (length(x) == 1) {
tmp <- dimnames(x[[1]])
x <- x[[1]]
dim(x) <- c(dim(x), 1)
dimnames(x) <- tmp
} else {
x <- abind::abind(x, along = 3L)
}
x <- aperm(x, c(1, 3, 2))
x <- abind::abind(x, along = 3L)
aperm(x, c(1, 3, 2))
}

# create an empty draws_array object
Expand All @@ -246,4 +250,3 @@ empty_draws_array <- function(variables = character(0), nchains = 0,
class(out) <- class_draws_array()
out
}

46 changes: 32 additions & 14 deletions R/convergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#' | [ess_basic()] | Basic version of effective sample size |
#' | [ess_bulk()] | Bulk effective sample size |
#' | [ess_tail()] | Tail effective sample size |
#' | [ess_mean()] | Effective sample sizes for the mean |
#' | [ess_median()] | Effective sample sizes for the median |
#' | [ess_quantile()] | Effective sample sizes for quantiles |
#' | [ess_sd()] | Effective sample sizes for standard deviations |
#' | [ess_sd()] | Effective sample sizes for the standard deviation |
#' | [mcse_mean()] | Monte Carlo standard error for the mean |
#' | [mcse_median()] | Monte Carlo standard error for the median |
#' | [mcse_quantile()] | Monte Carlo standard error for quantiles |
#' | [mcse_sd()] | Monte Carlo standard error for standard deviations |
#' | [mcse_sd()] | Monte Carlo standard error for the standard deviation |
#' | [pareto_khat()] | Pareto khat diagnostic for tail(s) |
#' | [pareto_diags()] | Additional diagnostics related to Pareto khat |
#' | [rhat_basic()] | Basic version of Rhat |
Expand Down Expand Up @@ -88,6 +91,7 @@ rhat_basic.rvar <- function(x, split = TRUE, ...) {
#' @family diagnostics
#' @template args-conv
#' @template args-conv-split
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-gelman-bda-2013
Expand Down Expand Up @@ -178,6 +182,7 @@ rhat.rvar <- function(x, ...) {
#'
#' @family diagnostics
#' @template args-conv
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-vehtari-rhat-2021
Expand Down Expand Up @@ -231,6 +236,7 @@ ess_bulk.rvar <- function(x, ...) {
#'
#' @family diagnostics
#' @template args-conv
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-vehtari-rhat-2021
Expand Down Expand Up @@ -270,6 +276,7 @@ ess_tail.rvar <- function(x, ...) {
#' @family diagnostics
#' @template args-conv
#' @template args-conv-quantile
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv-quantile
#' @template ref-vehtari-rhat-2021
Expand Down Expand Up @@ -360,6 +367,7 @@ ess_median <- function(x, ...) {
#' estimate of a single variable.
#'
#' @template args-conv
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-gelman-bda-2013
Expand All @@ -374,7 +382,7 @@ ess_median <- function(x, ...) {
#' @export
ess_mean <- function(x, ...) UseMethod("ess_mean")

#' @rdname ess_quantile
#' @rdname ess_mean
#' @export
ess_mean.default <- function(x, weights = NULL, ...) {

Expand Down Expand Up @@ -410,6 +418,7 @@ ess_mean.rvar <- function(x, ...) {
#'
#' @family diagnostics
#' @template args-conv
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-vehtari-rhat-2021
Expand All @@ -428,6 +437,9 @@ ess_sd <- function(x, ...) UseMethod("ess_sd")
#' @export
ess_sd.default <- function(x, weights = NULL, ...) {
if (is.null(weights)) {
# var/sd are not a simple expectation of g(X), e.g. variance
# has (X-E[X])^2. The following ESS is based on a relevant quantity
# in the computation and is empirically a good choice.
.ess(.split_chains(abs(x - mean(x))))
} else {

Expand All @@ -449,7 +461,7 @@ ess_sd.rvar <- function(x, ...) {
summarise_rvar_by_element_with_chains(x, ess_sd, weights = weights, ...)
}

# TODO: ess_weights
# TODO: ess_weights function

#' Monte Carlo standard error for quantiles
#'
Expand All @@ -460,6 +472,7 @@ ess_sd.rvar <- function(x, ...) {
#' @family diagnostics
#' @template args-conv
#' @template args-conv-quantile
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv-quantile
#' @template ref-vehtari-rhat-2021
Expand Down Expand Up @@ -555,6 +568,7 @@ mcse_median <- function(x, ...) {
#'
#' @family diagnostics
#' @template args-conv
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-gelman-bda-2013
Expand Down Expand Up @@ -583,7 +597,7 @@ mcse_mean.default <- function(x, weights = NULL, ...) {
x <- as.matrix(x)

r_eff <- .ess(.split_chains(x)) / (nrow(x) * ncol(x))
.mcse_weighted(x, weights, r_eff, ...)
.mcse_weighted(x, weights = weights, r_eff = r_eff, ...)
}
}

Expand All @@ -602,6 +616,7 @@ mcse_mean.rvar <- function(x, ...) {
#'
#' @family diagnostics
#' @template args-conv
#' @template args-conv-weights
#' @template args-methods-dots
#' @template return-conv
#' @template ref-vehtari-rhat-2021
Expand Down Expand Up @@ -632,7 +647,6 @@ mcse_sd.default <- function(x, weights = NULL, ...) {
# which doesn't assume normality of sims.
Evar <- mean(sims_c^2)
varvar <- (mean(sims_c^4) - Evar^2) / ess # (Equation 6.20)

# The first order Taylor series approximation of variance of sd.
# Kenney and Keeping (1951, p. 141) write "...since fluctuations of
# any moment are of order N^{-1/2}, squares and higher powers of
Expand All @@ -656,7 +670,7 @@ mcse_sd.default <- function(x, weights = NULL, ...) {
second_moment_weighted <- weighted.mean(x_centered^2, w = weights)
fourth_moment_weighted <- weighted.mean(x_centered^4, w = weights)

r_eff <- .ess(x_centered^2) / (nrow(x) * ncol(x))
r_eff <- .ess(.split_chains(x_centered^2)) / (nrow(x) * ncol(x))
weighted_ess <- .ess_weighted(x_centered^2, weights = weights, r_eff = r_eff)

# Kenney and Keeping (1951, eq 6.20)
Expand All @@ -681,6 +695,7 @@ mcse_sd.rvar <- function(x, ...) {
#' with other summary functions in the \pkg{posterior} package.
#'
#' @template args-conv
#' @template args-conv-weights
#' @template args-conv-quantile
#' @param na.rm (logical) Should `NA` and `NaN` values be removed from `x` prior
#' to computing quantiles? The default is `FALSE`.
Expand All @@ -697,19 +712,19 @@ mcse_sd.rvar <- function(x, ...) {
#' quantile2(mu)
#'
#' @export
quantile2 <- function(x, probs = c(0.05, 0.95), na.rm = FALSE, ...) {
quantile2 <- function(x, ...) {
UseMethod("quantile2")
}

#' @rdname quantile2
#' @export
quantile2.default <- function(
x, probs = c(0.05, 0.95), na.rm = FALSE, names = TRUE, ...
x, probs = c(0.05, 0.95), na.rm = FALSE, names = TRUE, weights = NULL, ...
) {
names <- as_one_logical(names)
na.rm <- as_one_logical(na.rm)

out <- weighted_quantile(x, probs = probs, na.rm = na.rm, ...)
out <- weighted_quantile(x, probs = probs, na.rm = na.rm, weights = weights, ...)

if (names) {
names(out) <- paste0("q", probs * 100)
Expand All @@ -726,8 +741,8 @@ quantile2.rvar <- function(
) {
weights <- weights(x)
summarise_rvar_by_element(x, function(draws) {
quantile2(
draws, probs = probs, weights = weights, na.rm = na.rm, names = names, ...
quantile2.default(
draws, probs = probs, na.rm = na.rm, names = names, weights = weights, ...
)
})
}
Expand Down Expand Up @@ -777,6 +792,7 @@ autocovariance <- function(x) {
autocorrelation <- function(x) {
ac <- autocovariance(x)
ac <- ac / ac[1]
ac
}

#' Rank normalization
Expand Down Expand Up @@ -807,8 +823,10 @@ z_scale <- function(x, c = 3/8) {
#'
#' Compute rank uniformization for a numeric array. First replace each value by
#' its rank. Average rank for ties are used to conserve the number of unique
#' values of discrete quantities. Second, uniformize ranks to the scale
#' `[1/(2S), 1-1/(2S)]`, where `S` is the number of values.
#' values of discrete quantities. Second, uniformize ranks using formula
#' `(r - c) / (S - 2 * c + 1)`, where `r` is a rank, `S` is the number of
#' values, and `c` is a fractional offset which defaults to c = 3/8 as
#' recommend by Blom (1958).
#'
#' @template args-scale
#' @template args-frac-offset
Expand Down
4 changes: 4 additions & 0 deletions R/draws-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ iteration_ids.draws_df <- function(x) {

#' @export
iteration_ids.draws_list <- function(x) {
if (!length(x) || !length(x[[1]])) {
# no chains or no variables within chains
return(integer(0))
}
seq_along(x[[1]][[1]])
}

Expand Down
Loading
Loading