-
Notifications
You must be signed in to change notification settings - Fork 0
/
S4_evaluate_convergence.R
51 lines (44 loc) · 1.47 KB
/
S4_evaluate_convergence.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
#setwd("") # set directory to the folder where the folders "data", "models" and "panels" are
library(Hmsc)
library(colorspace)
library(vioplot)
#include in samples_list and thin_list only those models that you have actually fitted!
# samples_list = c(5,50, 250 )
# thin_list = c(1,10, 100)
samples_list = c(5,50, 250, 250 )
thin_list = c(1,10, 100, 1000)
nst = length(thin_list)
nChains = 4
ma = NULL
na = NULL
for (Lst in 1:nst) {
thin = thin_list[Lst]
samples = samples_list[Lst]
filename = paste("models/models_thin_", as.character(thin),
"_samples_", as.character(samples),
"_chains_",as.character(nChains),".Rdata",sep = "")
load(filename)
nm = length(models)
for(j in 1:nm){
mpost = convertToCodaObject(models[[j]], spNamesNumbers = c(T,F), covNamesNumbers = c(T,F))
psrf.beta = gelman.diag(mpost$Beta,multivariate=FALSE)$psrf
tmp = summary(psrf.beta)
if(is.null(ma)){
ma=psrf.beta[,1]
na = paste0(as.character(thin),",",as.character(samples))
} else {
ma = cbind(ma,psrf.beta[,1])
if(j==1){
na = c(na,paste0(as.character(thin),",",as.character(samples)))
} else {
na = c(na,"")
}
}
}
}
# dir.create(paste0(getwd(),"/","panels/"))
pdf(file=paste("panels/MCMC_convergence.pdf"))
par(mfrow=c(2,1))
vioplot(ma,col=rainbow_hcl(nm),names=na,ylim=c(0,max(ma)),main="psrf(beta)")
vioplot(ma,col=rainbow_hcl(nm),names=na,ylim=c(0.9,1.1),main="psrf(beta)")
dev.off()