Skip to content

Commit d96fe81

Browse files
Deploying to gh-pages from @ 6cee125 🚀
1 parent ec85c9c commit d96fe81

44 files changed

Lines changed: 765 additions & 247 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev/articles/loo2-elpd.html

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/articles/loo2-elpd.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ matching*](https://mc-stan.org/loo/articles/loo2-moment-matching.html).
1818
Here is the Stan code for fitting a Poisson regression model:
1919

2020
``` r
21+
2122
# Note: some syntax used in this Stan program requires RStan >= 2.26 (or CmdStanR)
2223
# To use an older version of RStan change the line declaring `y` to: int y[N];
2324
stancode <- "
@@ -60,6 +61,7 @@ In addition to **loo**, we load the **rstan** package for fitting the
6061
model. We will also need the **rstanarm** package for the data.
6162

6263
``` r
64+
6365
library("rstan")
6466
library("loo")
6567
seed <- 9547
@@ -76,6 +78,7 @@ is evaluated on the held-out “test” data.
7678
The data is divided between train (80% of the data) and test (20%):
7779

7880
``` r
81+
7982
# Prepare data
8083
data(roaches, package = "rstanarm")
8184
roaches$roach1 <- sqrt(roaches$roach1)
@@ -111,15 +114,16 @@ Next we fit the model to the “test” data in Stan using the **rstan**
111114
package:
112115

113116
``` r
117+
114118
# Compile
115119
stanmodel <- stan_model(model_code = stancode)
116120
```
117121

118122
Trying to compile a simple C file
119123

120-
Running /opt/R/4.5.3/lib/R/bin/R CMD SHLIB foo.c
124+
Running /opt/R/4.6.0/lib/R/bin/R CMD SHLIB foo.c
121125
using C compiler: ‘gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0’
122-
gcc -std=gnu2x -I"/opt/R/4.5.3/lib/R/include" -DNDEBUG -I"/home/runner/work/_temp/Library/Rcpp/include/" -I"/home/runner/work/_temp/Library/RcppEigen/include/" -I"/home/runner/work/_temp/Library/RcppEigen/include/unsupported" -I"/home/runner/work/_temp/Library/BH/include" -I"/home/runner/work/_temp/Library/StanHeaders/include/src/" -I"/home/runner/work/_temp/Library/StanHeaders/include/" -I"/home/runner/work/_temp/Library/RcppParallel/include/" -I"/home/runner/work/_temp/Library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/home/runner/work/_temp/Library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/usr/local/include -fpic -g -O2 -c foo.c -o foo.o
126+
gcc -std=gnu2x -I"/opt/R/4.6.0/lib/R/include" -DNDEBUG -I"/home/runner/work/_temp/Library/Rcpp/include/" -I"/home/runner/work/_temp/Library/RcppEigen/include/" -I"/home/runner/work/_temp/Library/RcppEigen/include/unsupported" -I"/home/runner/work/_temp/Library/BH/include" -I"/home/runner/work/_temp/Library/StanHeaders/include/src/" -I"/home/runner/work/_temp/Library/StanHeaders/include/" -I"/home/runner/work/_temp/Library/RcppParallel/include/" -I"/home/runner/work/_temp/Library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/home/runner/work/_temp/Library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/usr/local/include -fpic -g -O2 -c foo.c -o foo.o
123127
In file included from /home/runner/work/_temp/Library/RcppEigen/include/Eigen/Core:19,
124128
from /home/runner/work/_temp/Library/RcppEigen/include/Eigen/Dense:1,
125129
from /home/runner/work/_temp/Library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
@@ -128,9 +132,10 @@ stanmodel <- stan_model(model_code = stancode)
128132
679 | #include <cmath>
129133
| ^~~~~~~
130134
compilation terminated.
131-
make: *** [/opt/R/4.5.3/lib/R/etc/Makeconf:202: foo.o] Error 1
135+
make: *** [/opt/R/4.6.0/lib/R/etc/Makeconf:190: foo.o] Error 1
132136

133137
``` r
138+
134139
# Fit model
135140
fit <- sampling(stanmodel, data = data_train, seed = seed, refresh = 0)
136141
```
@@ -142,6 +147,7 @@ using independent data, the log predictive density coincides with the
142147
log likelihood of the test data.
143148

144149
``` r
150+
145151
gen_test <- gqs(stanmodel, draws = as.matrix(fit), data= data_test)
146152
log_pd <- extract_log_lik(gen_test)
147153
```
@@ -152,9 +158,11 @@ Now we evaluate the predictive performance of the model on the test data
152158
using [`elpd()`](https://mc-stan.org/loo/dev/reference/elpd.md).
153159

154160
``` r
161+
155162
(elpd_holdout <- elpd(log_pd))
156163
```
157164

165+
158166
Computed from 4000 by 52 log-likelihood matrix using the generic elpd function
159167

160168
Estimate SE
@@ -177,6 +185,7 @@ We use the data that is already pre-processed and we divide it in 10
177185
random folds using `kfold_split_random`
178186

179187
``` r
188+
180189
# Prepare data
181190
roaches$fold <- kfold_split_random(K = 10, N = nrow(roaches))
182191
```
@@ -192,6 +201,7 @@ this loop is a matrix of the log pointwise predictive densities of all
192201
the observations.
193202

194203
``` r
204+
195205
# Prepare a matrix with the number of post-warmup iterations by number of observations:
196206
log_pd_kfold <- matrix(nrow = 4000, ncol = nrow(roaches))
197207
# Loop over the folds
@@ -226,9 +236,11 @@ Now we evaluate the predictive performance of the model on the 10 folds
226236
using [`elpd()`](https://mc-stan.org/loo/dev/reference/elpd.md).
227237

228238
``` r
239+
229240
(elpd_kfold <- elpd(log_pd_kfold))
230241
```
231242

243+
232244
Computed from 4000 by 262 log-likelihood matrix using the generic elpd function
233245

234246
Estimate SE

dev/articles/loo2-example.html

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/articles/loo2-example.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ In addition to the **loo** package, we’ll also be using **rstanarm** and
2828
**bayesplot**:
2929

3030
``` r
31+
3132
library("rstanarm")
3233
library("bayesplot")
3334
library("loo")
@@ -67,6 +68,7 @@ were used is not the same for all apartments in the sample, we use the
6768
the linear predictor.
6869

6970
``` r
71+
7072
# the 'roaches' data frame is included with the rstanarm package
7173
data(roaches)
7274
str(roaches)
@@ -80,6 +82,7 @@ str(roaches)
8082
$ exposure2: num 0.8 0.6 1 1 1.14 ...
8183

8284
``` r
85+
8386
# rescale to units of hundreds of roaches
8487
roaches$roach1 <- roaches$roach1 / 100
8588
```
@@ -90,6 +93,7 @@ We’ll fit a simple Poisson regression model using the `stan_glm`
9093
function from the **rstanarm** package.
9194

9295
``` r
96+
9397
fit1 <-
9498
stan_glm(
9599
formula = y ~ roach1 + treatment + senior,
@@ -127,6 +131,7 @@ We’ll also use the argument `save_psis = TRUE` to save some intermediate
127131
results to be re-used later.
128132

129133
``` r
134+
130135
loo1 <- loo(fit1, save_psis = TRUE)
131136
```
132137

@@ -139,9 +144,11 @@ correct the difference. We can see more details by printing the `loo`
139144
object.
140145

141146
``` r
147+
142148
print(loo1)
143149
```
144150

151+
145152
Computed from 4000 by 262 log-likelihood matrix.
146153

147154
Estimate SE
@@ -186,6 +193,7 @@ to fit the model) with horizontal lines corresponding to the same
186193
categories as in the printed output above.
187194

188195
``` r
196+
189197
plot(loo1)
190198
```
191199

@@ -209,6 +217,7 @@ QQ-plot the LOO-PIT values for our model (y-axi) is compared to standard
209217
uniform distribution (x-axis).
210218

211219
``` r
220+
212221
yrep <- posterior_predict(fit1)
213222

214223
ppc_loo_pit_qq(
@@ -238,19 +247,23 @@ Unlike the Poisson distribution, the negative binomial distribution
238247
allows the conditional mean and variance of \\y\\ to differ.
239248

240249
``` r
250+
241251
fit2 <- update(fit1, family = neg_binomial_2)
242252
```
243253

244254
``` r
255+
245256
loo2 <- loo(fit2, save_psis = TRUE, cores = 2)
246257
```
247258

248259
Warning: Found 1 observation(s) with a pareto_k > 0.7. We recommend calling 'loo' again with argument 'k_threshold = 0.7' in order to calculate the ELPD without the assumption that these observations are negligible. This will refit the model 1 times to compute the ELPDs for the problematic observations directly.
249260

250261
``` r
262+
251263
print(loo2)
252264
```
253265

266+
254267
Computed from 4000 by 262 log-likelihood matrix.
255268

256269
Estimate SE
@@ -269,6 +282,7 @@ print(loo2)
269282
See help('pareto-k-diagnostic') for details.
270283

271284
``` r
285+
272286
plot(loo2, label_points = TRUE)
273287
```
274288

@@ -292,6 +306,7 @@ recombined with the approximate LOO calculations already carried out for
292306
the observations without problematic \\k\\ values:
293307

294308
``` r
309+
295310
if (any(pareto_k_values(loo2) > 0.7)) {
296311
loo2 <- loo(fit2, save_psis = TRUE, k_threshold = 0.7)
297312
}
@@ -300,12 +315,15 @@ if (any(pareto_k_values(loo2) > 0.7)) {
300315
1 problematic observation(s) found.
301316
Model will be refit 1 times.
302317

318+
303319
Fitting model 1 out of 1 (leaving out observation 93)
304320

305321
``` r
322+
306323
print(loo2)
307324
```
308325

326+
309327
Computed from 4000 by 262 log-likelihood matrix.
310328

311329
Estimate SE
@@ -330,6 +348,7 @@ is much better than the `p_loo` estimate for the Poisson model.
330348
For further model checking we again examine the LOO-PIT values.
331349

332350
``` r
351+
333352
yrep <- posterior_predict(fit2)
334353
ppc_loo_pit_qq(roaches$y, yrep, lw = weights(loo2$psis_object))
335354
```
@@ -346,12 +365,18 @@ We can use the `loo_compare` function to compare our two models on
346365
expected log predictive density (ELPD) for new data:
347366

348367
``` r
368+
349369
loo_compare(loo1, loo2)
350370
```
351371

352-
elpd_diff se_diff
353-
fit2 0.0 0.0
354-
fit1 -5352.0 709.2
372+
model elpd_diff se_diff p_worse diag_diff diag_elpd
373+
fit2 0.0 0.0 NA <NA>
374+
fit1 -5352.0 709.2 1.00 17 k_psis > 0.7
375+
376+
377+
Diagnostic flags present.
378+
See ?`loo-glossary` (sections `diag_diff` and `diag_elpd`)
379+
or https://mc-stan.org/loo/reference/loo-glossary.html.
355380

356381
The difference in ELPD is much larger than several times the estimated
357382
standard error of the difference again indicating that the

dev/articles/loo2-large-data.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)