-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.R
224 lines (196 loc) · 6.99 KB
/
models.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
## LIBRARY IMPORT --------------
library(rJava)
library(rjags)
library(runjags)
library(coda)
library(mcmcplots)
library(sqldf)
library(XLConnect)
library(dplyr)
library(data.table)
library(ggplot2)
library(reshape2)
library(gridExtra)
library(scales)
library(grid)
library(vegan)
## CHECK INPUTS -----------------
## You can run this script at the command line with arguments
args = commandArgs(trailingOnly=TRUE)
## Or you can set the arguments manually in the choice variable
## (and the model variable - see below)
if (exists("choice"))
args <- choice
if (length(args) > 0)
{
## setting run_dic variable will set the default (otherwise TRUE)
## the argument --run_dic / --no_dic will calculate DIC or not
if (!exists("run_dic"))
run_dic <- TRUE
if (any(args == "--run_dic"))
run_dic <- TRUE
if (any(args == "--no_dic"))
run_dic <- FALSE
## setting use_wish variable will set the default (otherwise TRUE)
## the argument --use_wish / --no_wish will use Wishart priors or not
if (!exists("use_wish"))
use_wish <- TRUE
if (any(args == "--use_wish"))
use_wish <- TRUE
if (any(args == "--no_wish"))
use_wish <- FALSE
## Normalise variables to mean 0, sd 1 or mean 500, sd 100
norm_mean <- 0
norm_sd <- 1
if (any(args == "--mean_0"))
{
norm_mean <- 0
norm_sd <- 1
}
if (any(args == "--no_mean_0"))
{
norm_mean <- 500
norm_sd <- 100
}
args <- args[!grepl("--", args)]
} else {
if (!exists("run_dic"))
run_dic <- TRUE
if (!exists("use_wish"))
use_wish <- TRUE
if (!exists("norm_mean"))
{
norm_mean <- 0
norm_sd <- 1
}
}
# Do we want to do something specific? Or test everything?
## Models to run should either be in the argument list or in a variable
## called model. If neither are supplied run all models v briefly to
## check functionality
if ((length(args) == 0) && (exists("model")))
args <- as.character(model)
## DATA IMPORT FUNCTION -------------
read.file <- function(basefile, terroot, ttrroot)
{
terfile <- paste0(terroot, ".R")
ttrfile <- paste0(ttrroot, ".R")
base <- readChar(basefile, file.info(basefile)$size)
ter <- readChar(terfile, file.info(terfile)$size)
ttr <- readChar(ttrfile, file.info(ttrfile)$size)
sprintf(base, ter, ttr)
}
## NAME MODELS ---------------
ter.0 <- "ter.one"
ter.models <- c("ter.species", "ter.species.mean", "ter.species.mean.e",
"ter.species.mean.t", "ter.species.mean.et")
if (use_wish)
{
ttr.0 <- c("ttr.wish.one", "ttr.wish.onev")
ttr.models <- c("ttr.wish.species", "ttr.wish.speciesv") #, "ttr.wish.species.mean")
} else {
ttr.0 <- c("ttr.diag", "ttr.one")
ttr.models <- c("ttr.species", "ttr.species.mean", "ttr.species.mean.t")
}
## BUILD MODELS ----------------
models<- list()
# Null model
for (ttr in ttr.0)
models[[ttr]] <- read.file("base.R", ter.0, ttr)
# Models with null TTR
for (ter in ter.models)
for (ttr in ttr.0)
models[[paste(ter, ttr, sep="_")]] <- read.file("base.ter.R", ter, ttr)
# Models with null TER
for (ttr in ttr.models)
models[[ttr]] <- read.file("base.ttr.R", ter.0, ttr)
# Models with species-specific TER and TTR
for (ter in ter.models)
for (ttr in ttr.models)
models[[paste(ter, ttr, sep="_")]] <- read.file("base.ter.ttr.R", ter, ttr)
## CREATING NEW DATABASE -------------
db <- dbConnect(SQLite())
## IMPORTING EXCEL DATA ---------------
wb <- loadWorkbook("data.xlsx")
setMissingValue(wb, value = "NA")
Tables <- readWorksheet(wb, sheet = getSheets(wb))
names(Tables) <- c("PLOT", "SPECIES", "TRAIT") # Change the names of the data frames
str(Tables) # structure of Tables
names(Tables) # Names of the elements of Tables
## INSERTING TABLES INTO DATABASE ---------------
with(Tables, {
dbWriteTable(conn = db, name = "PLOT", value = PLOT,
row.names = FALSE, overwrite = TRUE)
dbWriteTable(conn = db, name = "SPECIES", value = SPECIES,
row.names = FALSE, overwrite = TRUE)
dbWriteTable(conn = db, name = "TRAIT", value = TRAIT,
row.names = FALSE, overwrite = TRUE)
})
## INDIVIDUAL SPECIES PLOT WISE ----------------
tree <- dbGetQuery(db,"SELECT PLOT_TRAIT, TRAIT.SPECIES_TRAIT, PLOT.NH4, PLOT.P,
PLOT.K, PLOT.SALINITY, PLOT.SILT, PLOT.PH, PLOT.URP,
PLOT.HH, TRAIT.HEIGHT, TRAIT.SLA, TRAIT.WD, TRAIT.SC
FROM PLOT JOIN TRAIT ON PLOT.PLOT_PLOT = TRAIT.PLOT_TRAIT
WHERE TRAIT.SLA IS NOT NULL AND TRAIT.WD IS NOT NULL AND
TRAIT.SC IS NOT NULL AND
SPECIES_TRAIT IN ('AMUR','BAEN',
'GEWA','GORAN','KAKRA','KEORA',
'POSUR','SINGRA','SUNDRI')")
n <- length(tree[,1]) # Sample size
f <- factor(tree$SPECIES_TRAIT)
species <- as.integer(f)
ns <- max(species)
HEIGHT <- (tree$HEIGHT - mean(tree$HEIGHT)) / sd(tree$HEIGHT)
SLA <- (tree$SLA - mean(tree$SLA)) / sd(tree$SLA)
WD <- (tree$WD - mean(tree$WD)) / sd(tree$WD)
SC <- (tree$SC - mean(tree$SC)) / sd(tree$SC)
traits <- as.matrix(cbind(HEIGHT,SLA,WD,SC)) * norm_sd + norm_mean
nt <- ncol(traits) # Number of traits to be considered
ttrI <- diag(nt)
NH4 <- (tree$NH4 - mean(tree$NH4)) / sd(tree$NH4)
P <- (tree$P - mean(tree$P)) / sd(tree$P)
K <- (tree$K - mean(tree$K)) / sd(tree$K)
SALINITY <- (tree$SALINITY - mean(tree$SALINITY)) / sd(tree$SALINITY)
SILT <- (tree$SILT - mean(tree$SILT)) / sd(tree$SILT)
PH <- (tree$PH - mean(tree$PH)) / sd(tree$PH)
URP <- (tree$URP - mean(tree$URP)) / sd(tree$URP)
HH <- (tree$HH - mean(tree$HH)) / sd(tree$HH)
envir <- as.matrix(cbind(NH4, P, K, SALINITY, SILT, PH, URP, HH)) * norm_sd +
norm_mean
ne <- ncol(envir) + 1
## RUN MODELS IN JAGS -----------------
## All remaining arguments are numbers of models to run
if (length(args) > 0)
{
print(paste("I'm going to run", length(args), "model(s)."))
print(paste("I", (if (run_dic) "will" else "won't"), "run DIC."))
for (arg in args)
{
model.num <- as.integer(arg)
print(paste0("Running model ", arg, ": ", names(models)[model.num]))
cat(models[[model.num]])
init <- run.jags(models[[model.num]], n.chains=2,
burnin=4000, sample=10000,
modules=c("glm","dic"))
extend <- extend.jags(init, sample=20000)
if (run_dic)
{
# DIC
dic <- extract(extend, what='dic') # DIC, pD
print(dic)
write.csv(data.frame(model=arg,
name=names(models)[model.num],
dic=sum(dic$deviance+dic$penalty)),
paste0(arg, ".csv"))
}
s <- add.summary(extend)
}
} else { ## If no model specified run all very briefly to check they work
print("Testing all models!")
for (name in names(models))
{
print(name)
run.jags(models[[name]], n.chains = 2, adapt = 2, burnin = 2, sample = 2,
modules = c("glm", "dic"))
}
}