-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_convergence.R
80 lines (73 loc) · 3.01 KB
/
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
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
evaluate_convergence <- function(modelDir, samples_list, thin_list, nChains, panelsDir) {
nst = length(thin_list)
ma.beta = ma.V = NULL
na = NULL
for (Lst in 1:nst) {
thin = thin_list[Lst]
samples = samples_list[Lst]
#filenameout = paste("MCMCconvergence_thin_", as.character(thin),
# "_samples_", as.character(samples),
# "_chains_",as.character(nChains),
# ".pdf",sep = "")
#if (file.exists(filenameout)) {
# print(paste("File already exists -- Skipping:", filenameout))
#
#} else {
filename = paste("models_thin_", as.character(thin),
"_samples_", as.character(samples),
"_chains_",as.character(nChains),".Rdata",sep = "")
filename = file.path(modelDir, filename)
print(" ")
print(paste("Examining: ", filename))
load(filename)
nm = length(models)
for(j in 1:nm){
print(modelnames[[j]])
# We first extract the posterior distribution from the model object and
# convert it into a coda object (needed to examine its convergence).
mpost = convertToCodaObject(models[[j]], spNamesNumbers = c(T,F), covNamesNumbers = c(T,F))
# Explore the MCMC convergence
# Effective size of the posterior sample.
# beta-parameters (species niches)
ess.beta = effectiveSize(mpost$Beta)
hist(ess.beta, xlab = expression("Effective sample size" ~ beta ~ ""))
title(main="Histogram of ess.beta",
sub=paste("Actual sample size: ", samples * nChains))
# V-parameters (variation in species niches)
ess.V = effectiveSize(mpost$V)
hist(ess.V, xlab = "Effective sample size V")
title(main="Histogram of ess.V",
sub=paste("Actual sample size: ", samples * nChains))
# Gelman diagnostics, i.e. the Potential scale reduction factors
psrf.beta = gelman.diag(mpost$Beta,multivariate=FALSE)$psrf
tmp = summary(psrf.beta)
print("Gelman Diagnostic Beta parameters")
print(tmp)
psrf.V = gelman.diag(mpost$V,multivariate=FALSE)$psrf
tmp = summary(psrf.V)
print("Gelman Diagnostic V parameters")
print(tmp)
if(is.null(ma.beta)){
ma.beta=psrf.beta[,1]
ma.V=psrf.V[,1]
na = paste0(as.character(thin),",",as.character(samples))
} else {
ma.beta = cbind(ma.beta,psrf.beta[,1])
ma.V = cbind(ma.V,psrf.V[,1])
if(j==1){
na = c(na,paste0(as.character(thin),",",as.character(samples)))
} else {
na = c(na,"")
}
}
}
#pdf(file=file.path(panelsDir, filenameout))
#par(mfrow=c(2,1))
vioplot(ma.beta,col=rainbow_hcl(nm),names=na,ylim=c(0,max(ma.beta)),main="psrf(beta)")
vioplot(ma.beta,col=rainbow_hcl(nm),names=na,ylim=c(0.9,1.1),main="psrf(beta)")
vioplot(ma.V,col=rainbow_hcl(nm),names=na,ylim=c(0,max(ma.V)),main="psrf(V)")
vioplot(ma.V,col=rainbow_hcl(nm),names=na,ylim=c(0.9,1.1),main="psrf(V)")
#dev.off()
#}
}
}