-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathREADME.Rmd
149 lines (114 loc) · 4 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
ggplot2::theme_set(bayesplot::theme_default(base_family = "sans"))
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# priorsense
<!-- badges: start -->
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-green.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![CRAN status](https://www.r-pkg.org/badges/version/priorsense)](https://CRAN.R-project.org/package=priorsense)
[![R-CMD-check](https://github.com/n-kall/priorsense/workflows/R-CMD-check/badge.svg)](https://github.com/n-kall/priorsense/actions)
<!-- badges: end -->
## Overview
priorsense provides tools for prior diagnostics and sensitivity
analysis.
It currently includes functions for performing power-scaling
sensitivity analysis on Stan models. This is a way to check how
sensitive a posterior is to perturbations of the prior and likelihood
and diagnose the cause of sensitivity. For efficient computation,
power-scaling sensitivity analysis relies on Pareto smoothed
importance sampling (Vehtari et al., 2024) and importance weighted
moment matching (Paananen et al., 2021).
Power-scaling sensitivity analysis and priorsense are described in
Kallioinen et al. (2023).
## Installation
Download the stable version from CRAN with:
```{r, eval = F}
install.packages("priorsense")
```
Download the development version from [GitHub](https://github.com/) with:
```{r, eval = F}
# install.packages("remotes")
remotes::install_github("n-kall/priorsense", ref = "development")
```
## Usage
priorsense works with models created with rstan, cmdstanr or brms, or
with draws objects from the posterior package.
### Example
Consider a simple univariate model with unknown mu and sigma fit to
some data y (available
via`example_powerscale_model("univariate_normal")`):
```stan
data {
int<lower=1> N;
array[N] real y;
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
// priors
target += normal_lpdf(mu | 0, 1);
target += normal_lpdf(sigma | 0, 2.5);
// likelihood
target += normal_lpdf(y | mu, sigma);
}
generated quantities {
vector[N] log_lik;
real lprior;
// log likelihood
for (n in 1:N) log_lik[n] = normal_lpdf(y[n] | mu, sigma);
// joint log prior
lprior = normal_lpdf(mu | 0, 1) +
normal_lpdf(sigma | 0, 2.5);
```
We first fit the model using Stan:
```{r, eval = T, results = F, message = F, warning = F}
library(priorsense)
normal_model <- example_powerscale_model("univariate_normal")
fit <- rstan::stan(
model_code = normal_model$model_code,
data = normal_model$data,
refresh = FALSE,
seed = 123
)
```
Once fit, sensitivity can be checked as follows:
```{r, eval = T}
powerscale_sensitivity(fit)
```
To visually inspect changes to the posterior, use one of the
diagnostic plot functions. Estimates with high Pareto-k values may be
inaccurate and are indicated.
```{r dens_plot, eval = T, out.width = '70%'}
powerscale_plot_dens(fit)
```
```{r ecdf_plot, eval = T, out.width = '70%'}
powerscale_plot_ecdf(fit)
```
```{r quants_plot, eval = T, out.width = '70%'}
powerscale_plot_quantities(fit)
```
In some cases, setting `moment_match = TRUE` will improve the
unreliable estimates at the cost of some further computation. This
requires the [`iwmm` package](https://github.com/topipa/iwmm).
## References
Noa Kallioinen, Topi Paananen, Paul-Christian Bürkner, Aki Vehtari
(2023). Detecting and diagnosing prior and likelihood sensitivity
with power-scaling. Statistics and Computing. 34, 57.
https://doi.org/10.1007/s11222-023-10366-5
Topi Paananen, Juho Piironen, Paul-Christian Bürkner, Aki Vehtari (2021).
Implicitly adaptive importance sampling. Statistics and Computing
31, 16. https://doi.org/10.1007/s11222-020-09982-2
Aki Vehtari, Daniel Simpson, Andrew Gelman, Yuling Yao, Jonah Gabry (2024).
Pareto smoothed importance sampling. Journal of
Machine Learning Research. 25, 72. https://jmlr.org/papers/v25/19-556.html