-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapply_draws.R
executable file
·51 lines (28 loc) · 1005 Bytes
/
apply_draws.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
library(posterior)
library(bayesplot)
library(patchwork)
apply_draws <- function(obj, FUN=NULL, MARGIN=NULL, ...) {
save_attr <- attr(obj,"dimnames")
save_dim <- dim(obj)
if(length(MARGIN)<2) {
stop("You must use the transform function over at least two array indices (such as chains and variables), otherwise the result will collapse to a matrix.")
}
obj <- apply(obj,MARGIN=MARGIN,FUN=FUN)
new_dim <- match(save_dim,dim(obj))
# need to re-arrange indices as apply can do that
obj <- aperm(obj,perm=new_dim)
dimnames(obj) <- save_attr
class(obj) <- c("draws_array","draws","array")
return(obj)
}
# example
x <- example_draws()
# by chain and by variable so we can test for convergence
x_sq <- apply_draws(x,FUN=function(c) c^2, MARGIN=c(2:3))
identical(dimnames(x),dimnames(x_sq))
identical(dim(x),dim(x_sq))
identical(x,x_sq)
# check
orig <- mcmc_trace(x,"theta[1]")
transform <- mcmc_trace(x_sq,"theta[1]")
orig + transform