Skip to content

Commit 7a5af8d

Browse files
committed
v1.1 ready
1 parent 1cc35d0 commit 7a5af8d

32 files changed

+309
-88
lines changed

DESCRIPTION

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: adjustr
22
Encoding: UTF-8
33
Type: Package
44
Title: Stan Model Adjustments and Sensitivity Analyses using Importance Sampling
5-
Version: 0.1.0
5+
Version: 0.1.1
66
Authors@R: person("Cory", "McCartan", email = "[email protected]",
77
role = c("aut", "cre"))
88
Description: Functions to help assess the sensitivity of a Bayesian model
@@ -11,18 +11,17 @@ Description: Functions to help assess the sensitivity of a Bayesian model
1111
package uses Pareto-smoothed importance sampling to estimate posterior
1212
quantities of interest under each specification.
1313
License: BSD_3_clause + file LICENSE
14-
Depends: R (>= 3.6.0)
14+
Depends: R (>= 3.6.0),
15+
dplyr (>= 1.0.0)
1516
Imports:
16-
tibble,
1717
tidyselect,
18-
dplyr (>= 1.0.0),
1918
purrr,
2019
stringr,
2120
methods,
2221
utils,
2322
stats,
24-
rlang,
2523
rstan,
24+
rlang,
2625
ggplot2,
2726
loo
2827
Suggests:
@@ -33,6 +32,7 @@ Suggests:
3332
knitr,
3433
rmarkdown
3534
URL: https://corymccartan.github.io/adjustr/
35+
BugReports: https://github.com/CoryMcCartan/adjustr/issues
3636
LazyData: true
3737
RoxygenNote: 7.1.1
3838
VignetteBuilder: knitr

NEWS.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# adjustr 0.1.1
2+
3+
* Improved documentation and additional references.
4+
5+
* Fix bug in `extract_samp_stmts()` which prevented `brmsfit` objects from being used directly.
6+
7+
18
# adjustr 0.1.0
29

310
* Initial release.

R/adjust_weights.R

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
#' data frame or list, compute Pareto-smoothed importance weights and attach
66
#' them to the specification object, for further calculation and plotting.
77
#'
8+
#' This function does the bulk of the sensitivity analysis work. It operates
9+
#' by parsing the model code from the provided Stan object, extracting the
10+
#' parameters and their sampling statements. It then uses R
11+
#' metaprogramming/tidy evaluation tools to flexibly evaluate the log density
12+
#' for each draw and each sampling statement, under the original and alternative
13+
#' specifications. From these, the function computes the overall importance
14+
#' weight for each draw and performs Pareto-smoothed importance sampling. All
15+
#' of the work is performed in R, without recompiling or refitting the Stan
16+
#' model.
17+
#'
818
#' @param spec An object of class \code{adjustr_spec}, probably produced by
919
#' \code{\link{make_spec}}, containing the new sampling sampling statements
1020
#' to replace their counterparts in the original Stan model, and the data,
@@ -17,7 +27,7 @@
1727
#' alternate specifications which deviate too much from the original
1828
#' posterior, and which as a result cannot be reliably estimated using
1929
#' importance sampling (i.e., if the Pareto shape parameter is larger than
20-
#' 0.7), have their weights discarded.
30+
#' 0.7), have their weights discarded—weights are set to \code{NA_real_}.
2131
#' @param incl_orig When \code{TRUE}, include a row for the original
2232
#' model specification, with all weights equal. Can facilitate comaprison
2333
#' and plotting later.
@@ -32,6 +42,12 @@
3242
#' \code{\link{pull.adjustr_weighted}} method. The returned object also
3343
#' includes the model sample draws, in the \code{draws} attribute.
3444
#'
45+
#' @references
46+
#' Vehtari, A., Simpson, D., Gelman, A., Yao, Y., & Gabry, J. (2015).
47+
#' Pareto smoothed importance sampling. \href{https://arxiv.org/abs/1507.02646}{arXiv preprint arXiv:1507.02646}.
48+
#'
49+
#' @seealso \code{\link{make_spec}}, \code{\link{summarize.adjustr_weighted}}, \code{\link{spec_plot}}
50+
#'
3551
#' @examples \dontrun{
3652
#' model_data = list(
3753
#' J = 8,
@@ -52,6 +68,8 @@
5268
#' @export
5369
adjust_weights = function(spec, object, data=NULL, keep_bad=FALSE, incl_orig=TRUE) {
5470
# CHECK ARGUMENTS
71+
if (is.null(data) & is(object, "brmsfit"))
72+
data = object$data
5573
object = get_fit_obj(object)
5674
model_code = object@stanmodel@model_code
5775
stopifnot(is.adjustr_spec(spec))
@@ -120,9 +138,11 @@ is.adjustr_weighted = function(x) inherits(x, "adjustr_weighted")
120138
#' @param var A variable, as in \code{\link[dplyr]{pull}}. The default returns
121139
#' the \code{.weights} column, and if there is only one row, it returns the
122140
#' first element of that column
141+
#' @param name Ignored
142+
#' @param ... Ignored
123143
#'
124144
#' @export
125-
pull.adjustr_weighted = function(.data, var=".weights") {
145+
pull.adjustr_weighted = function(.data, var=".weights", name=NULL, ...) {
126146
var = tidyselect::vars_pull(names(.data), !!enquo(var))
127147
if (nrow(.data) == 1 && var == ".weights") {
128148
.data$.weights[[1]]
@@ -149,7 +169,8 @@ pull.adjustr_weighted = function(.data, var=".weights") {
149169
#' }
150170
#' @export
151171
extract_samp_stmts = function(object) {
152-
model_code = get_fit_obj(object)@stanmodel@model_code
172+
object = get_fit_obj(object)
173+
model_code = object@stanmodel@model_code
153174

154175
parsed = parse_model(model_code)
155176

R/make_spec.R

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#' \code{\link[dplyr]{rename}}, and \code{\link[dplyr]{slice}}) are
4343
#' supported and operate on the underlying table of specification parameters.
4444
#'
45+
#' @seealso \code{\link{adjust_weights}}, \code{\link{summarize.adjustr_weighted}}, \code{\link{spec_plot}}
46+
#'
4547
#' @examples
4648
#' make_spec(eta ~ cauchy(0, 1))
4749
#'

R/use_weights.R

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ get_resampling_idxs = function(x, frac=1, replace=T) {
7575
#' @return An \code{adjustr_weighted} object, wth the new columns specified in
7676
#' \code{...} added.
7777
#'
78+
#' @seealso \code{\link{adjust_weights}}, \code{\link{spec_plot}}
79+
#'
7880
#' @examples \dontrun{
7981
#' model_data = list(
8082
#' J = 8,
@@ -227,6 +229,8 @@ funs_env = new_environment(list(
227229
#' @return A \code{\link[ggplot2]{ggplot}} object which can be further
228230
#' customized with the \strong{ggplot2} package.
229231
#'
232+
#' @seealso \code{\link{adjust_weights}}, \code{\link{summarize.adjustr_weighted}}
233+
#'
230234
#' @examples \dontrun{
231235
#' spec = make_spec(eta ~ student_t(df, 0, scale),
232236
#' df=1:10, scale=seq(2, 1, -1/9))

README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,34 @@ and [`spec_plot`](https://corymccartan.github.io/adjustr/reference/spec_plot.htm
4545
to examine posterior quantities of interest for each alternative specification,
4646
in order to assess the sensitivity of the underlying model.
4747

48+
To illustrate, the package lets us do the following:
49+
```r
50+
extract_samp_stmts(eightschools_m)
51+
#> Sampling statements for model 2c8d1d8a30137533422c438f23b83428:
52+
#> parameter eta ~ std_normal()
53+
#> data y ~ normal(theta, sigma)
54+
55+
make_spec(eta ~ student_t(0, 1, df), df=1:10) %>%
56+
adjust_weights(eightschools_m) %>%
57+
summarize(wasserstein(mu))
58+
#> # A tibble: 11 x 5
59+
#> df .samp .weights .pareto_k `wasserstein(mu)`
60+
#> <int> <chr> <list> <dbl> <dbl>
61+
#> 1 1 eta ~ student_t(df, 0, 1) <dbl [1,000]> 1.02 0.928
62+
#> 2 2 eta ~ student_t(df, 0, 1) <dbl [1,000]> 1.03 0.736
63+
#> 3 3 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.915 0.534
64+
#> 4 4 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.856 0.411
65+
#> 5 5 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.826 0.341
66+
#> 6 6 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.803 0.275
67+
#> 7 7 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.782 0.234
68+
#> 8 8 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.753 0.195
69+
#> 9 9 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.736 0.166
70+
#> 10 10 eta ~ student_t(df, 0, 1) <dbl [1,000]> 0.721 0.151
71+
#> 11 NA <original model> <dbl [1,000]> -Inf 0
72+
```
73+
4874
The tutorial [vignette](https://corymccartan.github.io/adjustr/articles/eight-schools.html)
49-
walks through a full sensitivity analysis for the classic 8-schools example.
75+
walks through a full sensitivity analysis for this 8-schools example.
5076
Smaller examples are also included in the package
5177
[documentation](https://corymccartan.github.io/adjustr/reference/index.html).
5278

@@ -60,3 +86,9 @@ if (!require("devtools")) {
6086
}
6187
devtools::install_github("corymccartan/adjustr@*release")
6288
```
89+
90+
## References
91+
92+
Vehtari, A., Simpson, D., Gelman, A., Yao, Y., & Gabry, J. (2015).
93+
Pareto smoothed importance sampling.
94+
_[arXiv preprint arXiv:1507.02646](https://arxiv.org/abs/1507.02646)_.

_pkgdown.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ navbar:
1717
href: articles/index.html
1818
- text: "Functions"
1919
href: reference/index.html
20+
- text: "News"
21+
href: news/index.html
2022
- text: "Other Packages"
2123
menu:
2224
- text: "rstan"
@@ -55,12 +57,8 @@ home:
5557
interest under each specification. The package also provides functions to
5658
summarize and plot how these quantities change across specifications.
5759
links:
58-
- text: Report a bug
59-
href: https://github.com/corymccartan/adjustr/issues/
6060
- text: Ask a question
6161
href: https://discourse.mc-stan.org/
62-
- text: Browse source code
63-
href: https://github.com/corymccartan/adjustr/
6462

6563
authors:
6664
Cory McCartan:

docs/404.html

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE-text.html

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)