forked from neelsoumya/rlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanfunc.R
1550 lines (1414 loc) · 52.2 KB
/
stanfunc.R
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# stanfunc.R
# Note re data.table:
# ... trailing [] to prevent "doesn't print first time" bug:
# https://stackoverflow.com/questions/32988099/data-table-objects-not-printed-after-returned-from-function
# https://github.com/Rdatatable/data.table/blob/master/NEWS.md#bug-fixes-5
library(bridgesampling)
library(coda)
library(ggplot2)
library(matrixStats)
library(parallel)
library(reshape)
library(rstan)
library(stringr)
#==============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
#==============================================================================
stanfunc = new.env()
#==============================================================================
# Core functions for e.g. rstan 2.16.2:
#==============================================================================
stanfunc$load_or_run_stan <- function(
data,
model_code,
fit_filename,
model_name,
save_code_filename = NULL,
forcerun = FALSE,
chains = 8,
iter = 2000,
init = "0", # the default, "random", uses the range -2 to +2
seed = 1234, # for consistency across runs
cache_filetype=c("rds", "rda"),
...)
{
# Other potential common parameters:
# control = list(
# adapt_delta = 0.99
# # http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
# # https://www.rdocumentation.org/packages/rstanarm/versions/2.14.1/topics/adapt_delta
# )
cache_filetype <- match.arg(cache_filetype)
if (!forcerun && file.exists(fit_filename)) {
if (cache_filetype == "rds") {
# .Rds
cat("Loading Stan model fit from RDS file: ",
fit_filename, "...\n", sep="")
fit <- readRDS(fit_filename)
} else {
# .Rda, .Rdata
cat("Loading Stan model fit from RDA file: ",
fit_filename, "...\n", sep="")
fit <- NULL # so we can detect the change when we load
load(fit_filename) # assumes it will be called 'fit'
if (class(fit) != "stanfit") {
stop(paste("No stanfit object called 'fit' in file",
fit_filename))
}
}
cat("... loaded\n")
} else {
n_cores_stan <- options("mc.cores")
n_cores_available <- parallel::detectCores()
if (n_cores_stan < n_cores_available) {
warning(paste(
"Stan is not set to use all available CPU cores; using ",
n_cores_stan, " when ", n_cores_available,
" are available; retry after issuing the command\n",
" options(mc.cores = parallel::detectCores())",
sep=""))
}
if (n_cores_stan == 1) {
warning("Running with a single CPU core; Stan may be slow")
}
cat(paste("--- Running Stan, starting at", Sys.time(), "...\n"))
# Stan now supports parallel operation directly
fit <- rstan::stan(
model_name = model_name,
model_code = model_code,
data = data,
chains = chains,
iter = iter,
init = init,
seed = seed,
...
)
cat(paste("... Finished Stan run at", Sys.time(), "\n"))
if (cache_filetype == "rds") {
# .Rds
cat("--- Saving Stan model fit to RDS file: ",
fit_filename, "...\n", sep="")
saveRDS(fit, file=fit_filename) # load with readRDS()
} else {
# .Rda, .Rdata
cat("--- Saving Stan model fit to RDA file: ",
fit_filename, "...\n", sep="")
save(list = c("fit"), file=fit_filename)
}
cat("... saved\n")
}
if (!is.null(save_code_filename) &&
(forcerun || !file.exists(save_code_filename))) {
cat("--- Generating C++ code to save...\n")
stanc_result <- rstan::stanc(model_code = model_code)
cpp_code <- stanc_result$cppcode
cat("--- Saving C++ code to file: ",
save_code_filename, "...\n", sep="")
cppfile <- file(save_code_filename)
writeLines(cpp_code, cppfile)
close(cppfile)
cat("... saved\n")
}
return(fit)
}
stanfunc$load_or_run_bridge_sampler <- function(
stanfit,
filename,
assume_stanfit_from_this_R_session = FALSE,
model_code = NULL,
data = NULL,
cores = parallel::detectCores(),
forcerun = FALSE,
...)
{
if (!forcerun && file.exists(filename)) {
cat("Loading bridge_sampler() fit from RDS file: ",
filename, "...\n", sep="")
b <- readRDS(filename)
cat("... loaded\n")
} else {
# POTENTIAL PROBLEM:
# Error in .local(object, ...) :
# the model object is not created or not valid
# This is a message from rstan::log_prob(stanfit).
# https://groups.google.com/forum/#!topic/stan-users/uu1p9oGIMhU
# The FIX is to specify a new stanfit_model, like this.
if (assume_stanfit_from_this_R_session) {
cat("Using existing Stan fit as stanfit_model; will crash if the",
"Stan model was created within a different R session\n")
stanfit_model <- stanfit
} else {
cat("Creating dummy compiled Stan model...\n")
if (is.null(model_code)) {
stop("model_code not specified")
}
if (is.null(data)) {
stop("data not specified")
}
stanfit_model <- rstan::stan(
model_code=model_code,
data=data, # if you use data=list(), it segfaults
chains=1,
iter=1 # despite the bridgesampling help, iter=0 causes an error
)
cat("... done\n")
}
cat(paste("--- Running bridge_sampler, starting at",
Sys.time(), "...\n"))
b <- bridgesampling::bridge_sampler(
samples=stanfit,
stanfit_model=stanfit_model,
cores=cores,
...
)
cat(paste("... Finished bridge_sampler run at", Sys.time(), "\n"))
cat("--- Saving bridge_sampler() fit to RDS file: ",
filename, "...\n", sep="")
saveRDS(b, file=filename) # load with readRDS()
cat("... saved\n")
}
return(b)
}
stanfunc$compare_model_evidence <- function(bridgesample_list_list,
priors = NULL,
detail = FALSE,
rhat_warning_threshold = 1.1)
{
# bridgesample_list_list
# A list of lists. Each item is a list with names:
# name: the model name
# bridgesample: the output from the
# bridgesampling::bridge_sampler() function (an item of
# class bridge_list)
# stanfit (optional): a corresponding Stan fit
# ... useful to show e.g. maximum R-hat summaries
# (R note: if x is a list, then if x *doesn't* have item y, x$y == NULL.)
#
# priors:
# optional, but can be a vector containing prior probabilities for
# each model
#
# detail:
# keep the details used for intermediate calculations
#
# stanfit_list:
# NULL, or a list of stanfits whose order must match bridgesample_list;
# if present, summary statistics (such as maximum R-hat) are included
#
# Note:
# - "marginal likelihood" is the same as "evidence" (e.g. Kruschke 2011
# p57-58)
# https://stackoverflow.com/questions/9950144/access-lapply-index-names-inside-fun
# https://stackoverflow.com/questions/4227223/r-list-to-data-frame
d <- data.table(
t(
vapply(
X=seq_along(bridgesample_list_list),
FUN=function(y, i) {
item <- y[[i]]
if (is.null(item$stanfit)) {
max_rhat <- NA_real_
} else {
fit <- item$stanfit
max_rhat <- stanfunc$max_rhat(fit)
}
return(c(
i, # index
item$name, # model_name
item$bridgesample$logml, # log_marginal_likelihood
max_rhat # max_rhat
))
},
FUN.VALUE=c("index"=NA_integer_,
"model_name"=NA_character_,
"log_marginal_likelihood"=NA_real_,
"max_rhat"=NA_real_),
y=bridgesample_list_list
)
)
)
d[, index := as.numeric(index)]
d[, log_marginal_likelihood := as.numeric(log_marginal_likelihood)]
d[, rhat_warning := ifelse(
is.na(max_rhat),
NA_character_,
ifelse(max_rhat > rhat_warning_threshold, "WARNING: HIGH R-HAT", "OK")
)]
d[, model_rank := frank(-log_marginal_likelihood,
ties.method="min")] # "sports method"
# ... bigger (less negative) is better
# ... and rank() ranks from smallest (-> 1) to biggest, so want the reverse
# ... and data.table::frank is quicker than rank (not that we care here!)
n_models <- nrow(d)
if (is.null(priors)) {
# Flat priors
d[, prior_p_model := 1/n_models]
} else {
# User-specified priors
if (length(priors) != n_models) {
stop("priors: wrong length")
}
if (sum(priors) != 1) {
warning("priors sum to ", sum(priors), ", not 1")
}
d[, prior_p_model := priors]
}
# Work with logs or everything will overflow.
d[, log_prior_p_model := log(prior_p_model)]
# e.g. Grounau 2017 eq 2:
# marginal_likelihood[i] * prior[i]
# posterior_p_model[i] = ---------------------------------------------------
# sum_over_all_j( marginal_likelihood[j] * prior[j] )
#
# Taking logs:
#
# log(posterior_p_model[i]) = log(marginal_likelihood[i]) + log(prior[i]) -
# log(sum_over_all_j( marginal_likelihood[j] * prior[j] ))
#
# and note the helpful R function matrixStats::logSumExp, where
#
# logSumExp(lx) == log(sum(exp(lx))
#
# which, for lx == log(x), means
#
# logSumExp(lx) == log(sum(x))
#
# so we will use
#
# log(marginal_likelihood[j] * prior[j]) = log(marginal_likelihood[j]) +
# log(prior[j])
d[, log_prior_times_lik :=
log_marginal_likelihood + log_prior_p_model]
d[, log_sum_prior_times_lik_all_models :=
matrixStats::logSumExp(d$log_prior_times_lik)]
d[, log_posterior_p_model :=
log_prior_times_lik - log_sum_prior_times_lik_all_models]
d[, posterior_p_model := exp(log_posterior_p_model)][]
if (!detail) {
# Remove working unless the user wants it
d[, log_prior_p_model := NULL]
d[, log_prior_times_lik := NULL]
d[, log_sum_prior_times_lik_all_models := NULL]
# d[, log_posterior_p_model := NULL][]
}
# print(d)
return(d)
}
stanfunc$sampled_values_from_stanfit <- function(
fit,
parname,
method = c("extract", "manual", "as.matrix"))
{
method <- match.arg(method)
if (method == "manual") {
# 1. Laborious hand-crafted way.
n_chains <- slot(fit, "sim")$chains
n_warmup <- slot(fit, "sim")$warmup
sampled_values <- NULL
for (c in 1:n_chains) {
n_save <- slot(fit, "sim")$n_save[c]
new_values <- slot(fit, "sim")$samples[[c]][parname][[1]][(n_warmup+1):n_save]
sampled_values <- c(sampled_values, new_values)
}
} else if (method == "extract") {
# 2. The way it's meant to be done.
ex <- rstan::extract(fit, permuted=TRUE)
# Now, slightly tricky. For a plain-text parameter like "xyz", this
# is simple. For something like "subject_k[1]", it isn't so simple,
# because rstan::extract gives us proper structure.
# Can also be e.g. parname[1,1], etc.
# Grep with capture: https://stackoverflow.com/questions/952275/regex-group-capture-in-r-with-multiple-capture-groups
PARAM_WITH_INDEX_REGEX = "^(\\w+)\\[((?:\\d+,)*\\d+)\\]$" # e.g. "somevar[3]", "blah[1,2]"
# matches <- stringr::str_match("blah", PARAM_WITH_INDEX_REGEX)
# matches <- stringr::str_match("blah[1]", PARAM_WITH_INDEX_REGEX)
# matches <- stringr::str_match("blah[2,3]", PARAM_WITH_INDEX_REGEX)
matches <- stringr::str_match(parname, PARAM_WITH_INDEX_REGEX)
if (!is.na(matches[1])) {
# parameter with index/indices e.g. "subject_k[3]", "blah[1,1]"
parname_par <- matches[2]
index_csv_numbers <- matches[3]
indices <- as.integer(unlist(strsplit(index_csv_numbers, ",")))
if (!(parname_par %in% names(ex))) {
stop("No such parameter: ", parname)
}
sampled_array <- ex[[parname_par]]
# ... for one index, sampled_array has indices [samplenum, parnum]
# so one can use sampled_values <- sampled_array[, parname_num]
# ... but for two, dim(sampled_array) is e.g. c(8000, 3, 3); this
# means 8000 samples of a 3x3 array.
# To retrieve them... see http://r.789695.n4.nabble.com/array-slice-notation-td902486.html
arraydims <- dim(sampled_array)
if (length(indices) != length(arraydims) - 1) {
stop("Bad indices for parameter: ", parname,
". Indices were: ", indices,
" and dimensions were: ", arraydims)
}
n_samples <- arraydims[1]
slicelist <- c(list(1:n_samples), as.list(indices))
# e.g. for param[3, 3], slicelist should be a list whose first
# element is 1:8000 (for 8000 samples), whose second element is 3,
# and whose third element is 3.
sampled_values <- do.call("[", c(list(sampled_array), slicelist))
} else {
# e.g. "somevar"
if (!(parname %in% names(ex))) {
stop("No such parameter: ", parname)
}
sampled_values <- ex[[parname]]
}
} else if (method == "as.matrix") {
# 3. Another...
m <- as.matrix(fit)
if (!(parname %in% colnames(m))) {
stop("No such parameter: ", parname)
}
sampled_values <- m[,parname]
} else {
stop("Bad method")
}
return(sampled_values)
}
stanfunc$summary_data_table <- function(fit, ...)
{
# help("summary,stanfit-method")
s <- rstan::summary(fit, ...)
# This summary object, s, has members:
# summary = overall summary
# c_summary = per-chain summary
ss <- s$summary
parnames <- rownames(ss)
ss <- data.table(ss)
ss$parameter <- parnames
# Move the "parameters" column so it's first:
setcolorder(ss, c(ncol(ss), 1:(ncol(ss) - 1))) # make last move to first
return(ss)
}
stanfunc$params_with_high_rhat <- function(fit, threshold = 1.1)
{
s <- stanfunc$summary_data_table(fit)
return(s[Rhat >= threshold])
}
stanfunc$max_rhat <- function(fit)
{
s <- stanfunc$summary_data_table(fit)
return(max(s$Rhat))
}
stanfunc$summary_by_par_regex <- function(fit, pars=NULL, par_regex=NULL, ...)
{
if (is.null(pars)) {
s <- stanfunc$summary_data_table(fit, ...)
} else {
s <- stanfunc$summary_data_table(fit, pars=pars, ...)
}
# Optionally, filter on a regex
if (!is.null(par_regex)) {
s <- s[grepl(par_regex, s$parameter), ]
}
return(s)
}
stanfunc$annotated_parameters <- function(
fit,
pars = NULL,
ci = c(0.025, 0.975), # for the "nonzero" column
probs = c(0.025, 0.25, 0.50, 0.75, 0.975),
par_regex = NULL,
annotate = TRUE,
nonzero_as_hdi = TRUE,
hdi_proportion = 0.95,
hdi_method = "kruschke_mcmc",
...
)
{
if (length(ci) != 2) {
stop("Bad ci parameter")
}
if (!ci[1] %in% probs || !ci[2] %in% probs) {
stop("Elements of ci must be in probs")
}
initial_probs <- probs
if (annotate) {
probs <- c(probs, c(0.0005, 0.9995, # p < 0.001 two-tailed
0.005, 0.995, # for "**", p < 0.01 two-tailed
0.025, 0.975, # for "*", p < 0.05 two-tailed
0.05, 0.95)) # for ".", p < 0.1 two-tailed
} else {
probs <- initial_probs
}
probs <- sort(unique(probs))
s <- stanfunc$summary_by_par_regex(fit, pars=pars, probs=probs,
par_regex=par_regex)
# Find nonzero parameters (credible interval excludes zero)
get_colname <- function(prob) {
return(paste(prob * 100, "%", sep=""))
}
nonzero_at <- function(lower, upper) {
lower_name = get_colname(lower)
upper_name = get_colname(upper)
return(
0 < s[, lower_name, with=FALSE] | s[, upper_name, with=FALSE] < 0
)
}
if (annotate) {
p_001 <- nonzero_at(0.0005, 0.9995)
p_01 <- nonzero_at(0.005, 0.995)
p_05 <- nonzero_at(0.025, 0.975)
p_1 <- nonzero_at(0.05, 0.95)
s[
,
annotation := ifelse(p_001, "***",
ifelse(p_01, "**",
ifelse(p_05, "*",
ifelse(p_1, ".", ""))))
]
}
if (nonzero_as_hdi) {
s[, hdi_lower := NA_real_]
s[, hdi_upper := NA_real_]
for (rownum in 1:nrow(s)) {
parname <- s[rownum, parameter]
values <- stanfunc$sampled_values_from_stanfit(fit, parname)
hdi_pair <- hdi(values, hdi_proportion=hdi_proportion,
method=hdi_method)
s[rownum, hdi_lower := hdi_pair[1]]
s[rownum, hdi_upper := hdi_pair[2]]
}
s[, nonzero := (0 < hdi_lower | hdi_upper < 0)]
} else {
s[, nonzero := nonzero_at(ci[1], ci[2])]
}
hidden_probs <- setdiff(probs, initial_probs) # in former, not latter
for (remove_prob in hidden_probs) {
remove_colname <- get_colname(remove_prob)
# cat(paste("Removing column", remove_colname, "\n"))
s[, (remove_colname) := NULL]
}
s <- s[] # fix nonprinting bug; see above
return(s)
}
stanfunc$nonzero_parameters <- function(fit, annotate=FALSE, ...)
{
s <- stanfunc$annotated_parameters(fit=fit, annotate=annotate, ...)
s <- s[nonzero == TRUE][] # restrict
return(s)
}
#==============================================================================
# Running in parallel
#==============================================================================
stanfunc$parallel_stan <- function(
file = NULL,
code = "",
data,
cores = parallel:detectCores(),
chains = 8,
iter = 2000,
warmup = floor(iter/2),
seed = 1234)
{
warning("stanfunc$parallel_stan: DEPRECATED; superseded by developments to rstan")
cat("parallel_stan: cores=", cores,
", chains=", chains,
", iter=", iter,
", seed=", seed,
"\n",
sep="")
cat("--- Step 1: compile the model (and run it once, very briefly, ignoring its output)\n")
f1 <- stan(file = file,
model_code = code,
data = data,
chains = 1,
iter = 1,
seed = seed,
chain_id = 1)
# sflist1 = list(f1)
cat("--- Step 2: run more chains in parallel\n")
sflist2 <- mclapply(
1:chains,
mc.cores = cores,
function(i) {
stan(fit = f1,
data = data,
chains = 1,
iter = iter,
warmup = warmup,
seed = seed,
chain_id = i)
}
# , refresh = -1
)
# ... passing the same seed to all chains follows example(sflist2stanfit)
# ... important to use the same seed but different chain_id when executing in parallel: http://stackoverflow.com/questions/12848168/will-rstan-run-on-a-supercomputer
sflist <- sflist2
cat("--- Finished.\n")
return(sflist2stanfit(sflist))
}
stanfunc$load_or_run_stan_old <- function(data, code, file, forcerun = FALSE)
{
warning("stanfunc$load_or_run_stan_old: DEPRECATED; superseded by developments to rstan")
if (!forcerun && file.exists(file)) {
cat("Loading Stan model from file:", file, "\n")
load(file)
} else {
cat("Running Stan model\n")
fit = stanfunc$parallel_stan(code, data)
cat("--- Saving Stan model to file:", file, "\n")
save(fit, file=file)
}
return(fit)
}
stanfunc$parallel_stan_reuse_fit <- function(f1, data,
cores = detectCores(),
chains = 8,
iter = 2000, seed = 1234)
{
warning("stanfunc$parallel_stan_reuse_fit: DEPRECATED; superseded by developments to rstan")
cat("parallel_stan_reuse_fit: cores=", cores,
", chains=", chains,
", iter=", iter,
", seed=", seed,
"\n",
sep="")
cat("--- Reusing existing model\n")
sflist2 <- mclapply(
1:chains,
mc.cores = cores,
function(i) {
stan(fit = f1, data = data,
iter = iter, seed = seed, chains = 1, chain_id = i)
}
)
sflist <- sflist2
cat("--- Finished.\n")
return(sflist2stanfit(sflist))
}
#==============================================================================
# LOOKING AT OUTPUT
#==============================================================================
stanfunc$save_plots_from_stanfit <- function(
fit,
parfile = "teststan_parameters.pdf",
tracefile = "teststan_trace.pdf",
pairfile = "teststan_pairs.pdf")
{
cat("Plotting parameters to", parfile, "\n")
pdf(file = parfile)
plot(fit)
dev.off()
cat("Plotting trace to", tracefile, "\n")
pdf(file = tracefile)
traceplot(fit) # options include: pars, inc_warmup
dev.off()
cat("Plotting pairs to", pairfile, "\n")
pdf(file = pairfile)
pairs(fit)
dev.off()
}
stanfunc$quick_summary_stanfit <- function(
fit, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
{
print(fit, digits_summary = 5, probs = probs)
}
stanfunc$calculate_mode <- function(sampled_values)
{
my_density <- density(sampled_values)
max_density <- max(my_density$y)
my_density$x[which(my_density$y == max_density)]
}
stanfunc$density_at_sub <- function(my_density, value)
{
# Known exactly?
if (value %in% my_density$x) {
# cat("density_at: exact\n")
return( my_density$y[ my_density$x == value ] )
}
# Out of range?
if (value < min(my_density$x) || value > max(my_density$x) ) {
cat("density_at: out of range\n")
return(NA)
}
# Otherwise, interpolate:
# cat("density_at: interpolating\n")
lower_x <- max(my_density$x[my_density$x < value])
upper_x <- min(my_density$x[my_density$x > value])
lower_d <- my_density$y[my_density$x == lower_x]
upper_d <- my_density$y[my_density$x == upper_x]
proportion <- (value - lower_x) / (upper_x - lower_x)
return(lower_d + proportion * (upper_d - lower_d))
}
stanfunc$density_at <- function(sampled_values, values)
{
my_density <- density(sampled_values)
result <- NULL
for (v in values) {
result <- c(result, density_at_sub(my_density, v))
}
return(result)
}
stanfunc$cum_density_between_two_values <- function(sampled_values,
lower, upper)
{
my_ecdf <- ecdf(sampled_values)
my_ecdf(upper) - my_ecdf(lower)
}
stanfunc$find_value_giving_density <- function(sampled_values, target_density)
{
dens <- density(sampled_values)
finder <- function(x) {
density_at_sub(dens, x) - target_density
}
uniroot()
}
stanfunc$find_value_giving_cum_density <- function(sampled_values, cum_density)
{
cdf <- ecdf(sampled_values)
find_root <- function(x) {
cdf(x) - cum_density
}
search_range <- c(min(sampled_values), max(sampled_values))
value <- uniroot(find_root, interval = search_range)$root
}
stanfunc$JUNK1 = "
calculate_hdi_from_sample_interpolating <- function(x, hdi_proportion = 0.95)
{
# INCOMPLETE
# x contains sampled values
dens = density(x)
cdf = ecdf(x)
finder_density_between_values_equals <- function(lower, upper, target_cum_density) {
# 0 when CDF(upper) - CDF(lower) = target_cum_density
cdf(upper) - cdf(lower) - target_cum_density
}
finder_density_equal_at_upper_and_lower <- function(lower, upper) {
density_at_upper = density_at_sub(dens, upper)
density_at_lower = density_at_sub(dens, lower)
density_at_upper - density_at_lower
}
# https://stat.ethz.ch/pipermail/r-help/2007-November/146688.html
density_diff <- function(lower, level=0.95) {
plower = density_at_sub(dens, lower)
pupper = plower + level
# ...
}
# ...
}
"
stanfunc$calculate_hdi_from_sample_piecewise <- function(x, hdi_proportion = 0.95)
{
# WORKS, BUT USE coda::HPDinterval instead
# x contains sampled values
# ... the shortest interval for which the difference in the empirical cumulative density function values of the endpoints is the nominal probability
# http://stats.stackexchange.com/questions/18533/find-probability-density-intervals
x <- sort(x)
# http://www.sumsar.net/best_online/js/js_mcmc.js
n <- length(x)
ci_nbr_of_points <- floor(n * hdi_proportion) # want this many samples in the HDI
min_width_ci <- c(min(x), max(x)) # initialize
for (i in 1:(n - ci_nbr_of_points)) {
ci_width <- x[i + ci_nbr_of_points] - x[i]
if (ci_width < min_width_ci[2] - min_width_ci[1]) {
min_width_ci <- c( x[i], x[i + ci_nbr_of_points] )
}
}
return(min_width_ci)
# We want an HDI such that
# CDF(upper) - CDF(lower) = hdi_proportion
# and PDF(upper) = PDF(lower)
}
stanfunc$HDIofMCMC <- function(sampleVec, credMass = 0.95)
{
# Krushke, p628, HDIofMCMC.R
# Computes highest density interval from a sample of representative values,
# estimated as shortest credible interval.
# Arguments:
# sampleVec
# is a vector of representative values from a probability distribution.
# credMass
# is a scalar between 0 and 1, indicating the mass within the credible
# interval that is to be estimated.
# Value:
# HDIlim is a vector containing the limits of the HDI
sortedPts <- sort( sampleVec )
ciIdxInc <- floor( credMass * length( sortedPts ) )
nCIs <- length( sortedPts ) - ciIdxInc
ciWidth <- rep( 0 , nCIs )
for (i in 1:nCIs) {
ciWidth[i] <- sortedPts[i + ciIdxInc] - sortedPts[i]
}
HDImin <- sortedPts[which.min(ciWidth)]
HDImax <- sortedPts[which.min(ciWidth) + ciIdxInc]
HDIlim <- c(HDImin, HDImax)
return(HDIlim)
}
TEST_HDI_OF_MCMC <- '
# See Kruschke (2011) p41, p628
set.seed(1234)
n = 20000
symmetric_y <- rnorm(n, mean=0, sd=1)
symmetric_d <- density(symmetric_y)
symmetric_hdi <- stanfunc$HDIofMCMC(symmetric_y) # -1.975671 1.904936
plot(symmetric_d)
abline(v=symmetric_hdi[1])
abline(v=symmetric_hdi[2])
asymmetric_y <- rgamma(n, shape=2, scale=2)
asymmetric_d <- density(asymmetric_y)
asymmetric_hdi <- stanfunc$HDIofMCMC(asymmetric_y) #
plot(asymmetric_d)
abline(v=asymmetric_hdi[1])
abline(v=asymmetric_hdi[2])
'
stanfunc$hdi_via_coda <- function(sampled_values, hdi_proportion = 0.95)
{
hdi_limits_matrix <- coda::HPDinterval(as.mcmc(sampled_values),
prob = hdi_proportion)
# ... Sometimes crashes with
# "Error in dimnames(x)[[2]] : subscript out of bounds"
# for perfectly valid-looking data that works with other methods.
return(c(hdi_limits_matrix[1, "lower"], hdi_limits_matrix[1, "upper"]))
}
stanfunc$hdi_via_lme4 <- function(sampled_values, hdi_proportion = 0.95)
{
hdi_limits_matrix <- lme4::HPDinterval(as.matrix(sampled_values),
prob = hdi_proportion)
return(c( hdi_limits_matrix[1, "lower"], hdi_limits_matrix[1, "upper"]))
}
stanfunc$compare_hdi_methods <- function(sampled_values, hdi_proportion)
{
cat("Bååth:\n")
print(calculate_hdi_from_sample_piecewise(sampled_values, hdi_proportion))
cat("Kruschke:\n")
print(HDIofMCMC(sampled_values, hdi_proportion))
cat("coda:\n")
print(hdi_via_coda(sampled_values, hdi_proportion))
cat("lme4:\n")
print(hdi_via_lme4(sampled_values, hdi_proportion))
}
stanfunc$hdi <- function(sampled_values, hdi_proportion = 0.95,
method=c("kruschke_mcmc",
"coda",
"baath",
"lme4"))
{
# Method chooser!
method <- match.arg(method)
if (method == "kruschke_mcmc") {
return(HDIofMCMC(sampled_values, hdi_proportion))
} else if (method == "coda") {
return(hdi_via_coda(sampled_values, hdi_proportion))
} else if (method == "baath") {
return(calculate_hdi_from_sample_piecewise(sampled_values, hdi_proportion))
} else if (method == "lme4") {
return(hdi_via_lme4(sampled_values, hdi_proportion))
} else {
stop("Bad method")
}
}
stanfunc$interval_includes <- function(interval, testval,
lower_inclusive = TRUE,
upper_inclusive = TRUE)
{
# Ensure ordered from low to high:
if (interval[2] < interval[1]) {
interval <- c(interval[2], interval[1])
}
lowertest <- ifelse(lower_inclusive,
interval[1] <= testval,
interval[1] < testval)
uppertest <- ifelse(upper_inclusive,
testval <= interval[2],
testval < interval[2])
return(lowertest && uppertest)
}
stanfunc$interval_excludes <- function(interval, testval,
lower_inclusive = TRUE,
upper_inclusive = TRUE)
{
!stanfunc$interval_includes(interval, testval,
lower_inclusive = lower_inclusive,
upper_inclusive = upper_inclusive)
}
stanfunc$hdi_proportion_excluding_test_value <- function(
x, test_value = 0, largest_such_interval = TRUE, debug = FALSE)
{
# cruddy method!
# NOTE ALSO: neither the lower bound nor the upper bound of an HDI
# move monotonically as the HDI proportion is changed (because the
# distribution can be asymmetrical).
width_accuracy <- 0.001 # 0.1%
if (largest_such_interval) {
startval <- 1
endval <- 0
width_accuracy <- -width_accuracy
stoptest <- interval_excludes
}
else {
startval <- 0
endval <- 1
stoptest <- interval_includes
}
prev_width <- startval
for (width in seq(startval, endval, width_accuracy)) {
if (width == 1) next # or HDI will be invalid (infinite)
interval <- hdi(x, width)
current_interval_fails <- stoptest(interval, test_value)
if (debug) cat("testing proportion ", width,
", interval: ", interval,
", fails? ", current_interval_fails,
"\n", sep="")
if (current_interval_fails) {
# current interval fails, so return the previous
return(prev_width)
}
prev_width <- width
}
return(endval)
}
stanfunc$plot_density_function <- function(
sampled_values,
parname,
test_value = 0,
quantile_probs = c(0.025, 0.5, 0.975),
hdi_proportion = 0.95,
histogram_breaks = 50,
digits = 3,
colour_quantiles = "gray",
colour_mean = "black",
colour_mode = "lightgrey",
colour_hdi = "darkgreen",
lty_quantiles = 3,
lty_mean = 1,
lty_mode = 1,
lty_hdi = 1,
colour_density = "blue",
show_hdi_proportion_excluding_test_value = FALSE,
show_quantiles = TRUE,
show_mean = TRUE,
show_mode = TRUE,
show_hdi = TRUE,
ypos_quantiles = 1.15,
ypos_mean = 0.6,
ypos_mode = 0.4)
{
my_density <- density(sampled_values)
max_density <- max(my_density$y)
q <- quantile(sampled_values, probs=quantile_probs)
my_mean <- mean(sampled_values)
my_mode <- calculate_mode(sampled_values)
my_ecdf <- ecdf(sampled_values)
hdi_percent <- hdi_proportion * 100
#debug_quantity(sampled_values)
hdi_limits <- hdi(sampled_values, hdi_proportion)
central_proportion_excluding_test_value <- 1 - 2 * my_ecdf(test_value)
cat("\nParameter:", parname, "\n")
cat("Mean:", my_mean, "\n")
cat("Mode:", my_mode, "\n")
cat("Quantiles:\n")
print(q)
cat("Central proportion excluding test value of", test_value, ":",