-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjags_utils.R
76 lines (63 loc) · 2.04 KB
/
jags_utils.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
##
## JAGS utilities
##
## Author: Pawel Gajer
## July 28, 2012
##
## return the mean and 95% CI
## modification of coda summary.mcmc()
## from output.R in coda/R
ci.mcmc <- function (object,short=TRUE,qs=NA, with.se=FALSE, n=NA)
{
x <- mcmc.list(object)
if ( length(qs)==1 && is.na(qs) ){
if ( short && !with.se ){
qs <- c(0.5, 0.025, 0.975)
statnames <- c("Mean", "Median", "2.5%", "97.5%")
} else if (short && with.se) {
qs <- c(0.5, 0.025, 0.975)
statnames <- c("Mean", "S.E.", "Median", "2.5%", "97.5%")
} else if (!short && !with.se) {
qs <- c(0.5, 0.025, 0.975, 0.25, 0.75)
statnames <- c("Mean", "Median", "2.5%", "97.5%", "25%", "75%")
} else {
qs <- c(0.5, 0.025, 0.975, 0.25, 0.75)
statnames <- c("Mean", "S.E.", "Median", "2.5%", "97.5%", "25%", "75%")
}
} else {
statnames <- c("Mean", paste(100*qs[1:length(qs)],"%",sep=""))
}
if ( !is.numeric(qs) )
stop(paste("qs has to be a numeric vector; qs:",qs))
##xtsvar <- matrix(nrow = nchain(x), ncol = nvar(x))
if (is.matrix(x[[1]])) {
xlong <- do.call("rbind", x)
}
else {
xlong <- as.matrix(x)
}
## xmean <- apply(xlong, 2, mean)
## varquant <- t(apply(xlong, 2, quantile, qs))
nc <- ncol(xlong)
xmean <- numeric(nc)
for ( i in 1:nc )
xmean[i] <- mean(xlong[,i],na.rm=TRUE)
varquant <- matrix(nrow=nc,ncol=length(qs))
for ( i in 1:nc )
varquant[i,] <- quantile(xlong[,i],probs=qs,na.rm=TRUE)
varstats <- matrix(nrow = nc, ncol = length(statnames),
dimnames = list(varnames(x), statnames))
varstats[, 1] <- xmean
if ( with.se )
{
xsd <- numeric(nc)
for ( i in 1:nc )
xsd[i] <- sd(xlong[,i])/sqrt(n)
varstats[, 2] <- xsd
varstats[, 3:(length(qs)+2)] <- varquant
} else {
varstats[, 2:(length(qs)+1)] <- varquant
}
varstats <- drop(varstats)
return(varstats)
}