@@ -300,22 +300,16 @@ adjust_gamma <- function(N,
300
300
if (prob > = 1 || prob < = 0 ) {
301
301
abort(" Value of 'prob' must be in (0,1)." )
302
302
}
303
- if (L == 1 ) {
304
- if (interpolate_adj == TRUE ) {
305
- gamma <- 0
306
- } else {
307
- gamma <- adjust_gamma_optimize(N , K , prob )
308
- }
303
+ if (interpolate_adj == TRUE ) {
304
+ gamma <- interpolate_gamma(N , K , prob , L )
305
+ } else if (L == 1 ) {
306
+ gamma <- adjust_gamma_optimize(N , K , prob )
309
307
} else {
310
- if (interpolate_adj == TRUE ) {
311
- gamma <- 0
312
- } else {
313
- gamma <- adjust_gamma_simulate(N ,
314
- L ,
315
- K ,
316
- prob ,
317
- M )
318
- }
308
+ gamma <- adjust_gamma_simulate(N ,
309
+ L ,
310
+ K ,
311
+ prob ,
312
+ M )
319
313
}
320
314
gamma
321
315
}
@@ -370,6 +364,55 @@ adjust_gamma_simulate <- function(N, L, K, prob, M) {
370
364
alpha_quantile(gamma , 1 - prob )
371
365
}
372
366
367
+ interpolate_gamma <- function (N , K , p , L ) {
368
+ vals <- get_interpolation_values(N , K , L , p )
369
+ N_lb <- max(vals [vals $ N < = N , ]$ N )
370
+ N_ub <- min(vals [vals $ N > = N , ]$ N )
371
+ g_lb <- approx(
372
+ x = log(vals [vals $ N == N_lb , ]$ K ),
373
+ y = log(vals [vals $ N == N_lb , ]$ val ),
374
+ xout = log(K )
375
+ )$ y
376
+ g_ub <- approx(
377
+ x = log(vals [vals $ N == N_ub , ]$ K ),
378
+ y = log(vals [vals $ N == N_ub , ]$ val ),
379
+ xout = log(K )
380
+ )$ y
381
+ if (N_ub == N_lb ) {
382
+ g <- exp(g_lb )
383
+ } else {
384
+ g <- exp(approx(x = log(c(N_lb , N_ub )), y = c(g_lb , g_ub ), xout = log(N ))$ y )
385
+ }
386
+ g
387
+ }
388
+
389
+ get_interpolation_values <- function (N , K , L , p ) {
390
+ for (dim in c(" L" , " prob" )) {
391
+ if (all(get(if (dim == " L" ) dim else " p" ) != bayesplot ::: gamma_adj [, dim ])) {
392
+ stop(paste(
393
+ " No precomputed values to interpolate from for '" , dim , " ' = " ,
394
+ get(if (dim == " L" ) dim else " p" ),
395
+ " .\n " ,
396
+ " Values of '" , dim , " ' available for interpolation: " ,
397
+ unique(bayesplot ::: gamma_adj [, dim ]),
398
+ " ." ,
399
+ sep = " "
400
+ ))
401
+ }
402
+ }
403
+ vals <- bayesplot ::: gamma_adj [bayesplot ::: gamma_adj $ L == L & bayesplot ::: gamma_adj $ prob == p , ]
404
+ if (K > max(vals [vals $ N < = N , ]$ K )) {
405
+ stop(paste(
406
+ " No precomputed values available for interpolation for 'K' = " ,
407
+ K ,
408
+ " .\n " ,
409
+ " Try either setting a value of 'K' <= " ,
410
+ max(vals [vals $ N < = N , ]$ K ),
411
+ " or 'interpolate_adj' = FALSE."
412
+ ))
413
+ }
414
+ vals
415
+ }
373
416
# ' A helper function for 'adjust_gamma_optimize' defining the probability that
374
417
# ' an ECDF stays within the supplied bounds between z1 and z2.
375
418
# ' @noRd
0 commit comments