jags.run.R 8 KB
Newer Older
1
2
3
4
#########################
## Functions to Run JAGS

source('R/analysis/lmer.run.R')
kunstler's avatar
kunstler committed
5
#source('R/analysis/stan.run.R')
6

kunstler's avatar
kunstler committed
7
### TODO CHANGE TO FIT WITH NEW LMER DATA AND
8
9
10
11

### FUNCTION TO GENERATE THE GOOD FORMAT OF DATA

model.files.jags.Tf.1 <-
kunstler's avatar
kunstler committed
12
13
   c("R/analysis/model.jags/model.LOGLIN.ER.AD.Tf.r.set.species.R",
     "R/analysis/model.jags/model.LOGLIN.ER.AD.Tf.r.set.species.fixed.biomes.R")
14
model.files.jags.Tf.2 <-
kunstler's avatar
kunstler committed
15
    c("R/analysis/model.jags/model.LOGLIN.ER.AD.Tf.r.set.sepcies.tree.R")
16
17


18

kunstler's avatar
kunstler committed
19
20
21
22
23
24
25
26
27
28
29
fun.generate.init.one.param <- function(i, pars, list.jags,chain){
if(grepl('sigma',names(pars)[i])){
  a <- 0.1+(chain-1)*0.1
 }else{
  if(grepl('param',names(pars)[i])){
    a.t <- -0.3+(chain-1)*0.2
    a <- rnorm(list.jags[[pars[i]]],mean = a.t)
  }else{
    a <- -0.3+(chain-1)*0.2
  }
 }
30
31
    return(a)
}
kunstler's avatar
kunstler committed
32
33
34
35
36
37
38
fun.generate.init.all.param <- function(chain, list.jags, jags.model){
list.init <- lapply(seq_len(length(jags.model$pars)),
                    fun.generate.init.one.param,
                    jags.model$pars,
                    list.jags,
                    chain)
names(list.init) <- names(jags.model$pars)
kunstler's avatar
kunstler committed
39

kunstler's avatar
kunstler committed
40
41
return(list.init)
}
42

kunstler's avatar
kunstler committed
43
44
45
46
fun.generate.init <-  function(jags.model, list.jags, chains){
chains.v <- seq_len(chains)
inits <- lapply(chains.v,fun.generate.init.all.param, list.jags, jags.model)
return(inits)
47
48
}

49
50
51
52
fun.call.jags.and.save <- function(jags.model,
                                   list.jags,
                                   path.out,
                                   var.sample,
53
54
                                   iter = 5000,
                                   warmup = 500,
55
                                   chains = 3,
56
                                   thin = 50,
kunstler's avatar
kunstler committed
57
                                   method = 'rjags'){
kunstler's avatar
kunstler committed
58
require(runjags)    
59
 start <-  Sys.time()
kunstler's avatar
kunstler committed
60
inits <-  fun.generate.init(jags.model, list.jags[[1]], chains)
61
 ### SEND to jags
kunstler's avatar
kunstler committed
62
63
64
65
66
67
68
69
70
71
 jags.output <- run.jags(data=list.jags[[1]],
                     inits = inits,
                     model = file.path(path.out,"model.file.bug"),
                     monitor = names(jags.model$pars),
                     n.chains = chains ,
                     sample = iter,
                     burnin = warmup,
                     adapt = 2000,
                     thin = thin,
                     modules=  'glm on',
kunstler's avatar
kunstler committed
72
                     method = method)
73
74
75
 end <- Sys.time()
 print(end -start)
 print(jags.output)
kunstler's avatar
kunstler committed
76
77
78
 saveRDS(list(output = jags.output, list.sd = list.jags[[2]]),
         file = file.path(path.out, paste(var.sample,
         "results.jags.rds", sep = '.')))
79
 return(jags.output)
80
81
}

kunstler's avatar
kunstler committed
82
run.jags.b <- function (model.file, trait, init.TF,
kunstler's avatar
kunstler committed
83
84
85
86
87
88
89
                      data.type='simple', 
                      sample.size = NA,
                      ecocode.var = 'wwf',
                      Multi.type = 'a',
                      var.sample = NA,
                      select.set = NA,
                      merge.biomes.TF = FALSE,
90
                       ...) {
kunstler's avatar
kunstler committed
91
    require(runjags)
92
93
94
    source(model.file, local = TRUE)
    model <- load.model()

kunstler's avatar
kunstler committed
95
96
    path.out <- output.dir('lmer', model$name, trait, data.type)

97
98
    print(path.out)
    dir.create(path.out, recursive = TRUE, showWarnings = FALSE)
kunstler's avatar
kunstler committed
99
100
    cat(model$bug, file = file.path(path.out,"model.file.bug")
     , sep=" ", fill = FALSE, labels = NULL, append = FALSE)
101
102
103
    cat("run jags for model", model.file, " for trait",
         trait, "\n")
    if (init.TF) {
kunstler's avatar
kunstler committed
104
105
106
107
108
109
110
111
112
    jags.list <- fun.load.data.list(trait, data.type,
                               sample.size. = sample.size,
                               ecocode.var. = ecocode.var,
                               Multi.type. = Multi.type,
                               var.sample. = var.sample,
                               select.set. = select.set,
                               sample.vec.TF = FALSE,
                               merge.biomes.TF = merge.biomes.TF)
    cat("Ok data with Nobs", jags.list[[1]]$N_indiv,
113
114
115
         "\n")
    }
    if (!init.TF) {
kunstler's avatar
kunstler committed
116
117
118
119
120
121
122
123
124
    jags.list <- fun.load.data.list(trait, data.type,
                               sample.size. = sample.size,
                               ecocode.var. = ecocode.var,
                               Multi.type. = Multi.type,
                               var.sample. = var.sample,
                               select.set. = select.set,
                               sample.vec.TF = TRUE,
                               merge.biomes.TF = merge.biomes.TF)
    cat("Ok data with Nobs", jags.list[[1]]$N_indiv, 'run real',
125
         "\n")
126
    res <- fun.call.jags.and.save(jags.model = model,
127
128
129
130
                           list.jags = jags.list,
                           path.out = path.out,
                           var.sample = var.sample,
                           ...)
131
    return(res)
132
133
134
135
136
    }
 gc()
}


kunstler's avatar
kunstler committed
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
#========================================================================
fun.load.data.list <- function(trait, data.type ,
                          base.dir = 'output/processed', sample.size.,
                          ecocode.var., Multi.type., var.sample. = 'ecocode',
                          select.set. = NA, select.biome. = NA,
                          sample.vec.TF., merge.biomes.TF = FALSE){

    list.df.lmer <- load.data.for.lmer(trait,
                                  data.type ,
                                  base.dir,
                                  sample.size. = sample.size.,
                                  ecocode.var. = ecocode.var.,
                                  Multi.type. = Multi.type.,
                                  var.sample. = var.sample.,
                                  select.biome. = select.biome.,
                                  sample.vec.TF. = sample.vec.TF.,
                                  merge.biomes.TF = merge.biomes.TF)

stan.list <- fun.turn.in.list.for.jags.stan(list.df.lmer[[1]],
                                              data.type)
  return(list(stan.list, list.df.lmer[[2]]))
}

fun.turn.in.list.for.jags.stan <-  function(df, data.type){
if (data.type == 'Multi'){
    stan.list <- fun.turn.in.list.for.jags.stan.Multi(df)

  }else{
    stan.list <- fun.turn.in.list.for.jags.stan.no.Multi(df)
  }
return(stan.list)
}

fun.turn.in.list.for.jags.stan.no.Multi <-  function(df){
   stan.list <- list(N_indiv = nrow(df),
                     N_tree = nlevels(unclass(factor(df$tree.id))),
                     N_species = nlevels(unclass(factor(df$species.id))),
                     N_plot = nlevels(unclass(factor(df$plot.id))),
                     N_set = nlevels(unclass(factor(df$set.id))),
                     N_biomes = nlevels(unclass(factor(df$biomes.id))),
                     N_ecocode = nlevels(unclass(factor(df$ecocode.id))),
                     species_id = unclass(factor(df$species.id)),
                     plot_id = unclass(factor(df$plot.id)),
                     set_id = unclass(factor(df$set.id)),
                     tree_id = unclass(factor(df$tree.id)),
                     biomes_id = unclass(factor(df$biomes.id)),
                     ecocode_id = unclass(factor(df$ecocode.id)),
                     logG = df$logG,
                     logD = df$logD,
                     Tf = df$Tf,
                     sumBn = df$sumBn,
                     sumTfBn = df$sumTfBn,
                     sumTnBn = df$sumTnBn,
                     sumTnTfBn_abs = df$sumTnTfBn.abs)

    return(stan.list)
}

fun.turn.in.list.for.jags.stan.Multi <-  function(df){
stan.list <- as.list(df)

stan.list$N_indiv <- nrow(df)
stan.list$N_tree <- nlevels(unclass(factor(df$tree.id)))
stan.list$N_species <- nlevels(unclass(factor(df$species.id)))
stan.list$N_plot <- nlevels(unclass(factor(df$plot.id)))
stam.list$N_set <- nlevels(unclass(factor(df$set.id)))
stan.list$N_biomes <- nlevels(unclass(factor(df$biomes.id)))
stan.list$N_ecocode <- nlevels(unclass(factor(df$ecocode.id)))
stan.list$species_id <- unclass(factor(df$species.id))
stan.list$plot_id <- unclass(factor(df$plot.id))
stan.list$set_id <- unclass(factor(df$set.id))
stan.list$tree_id <- unclass(factor(df$tree.id))
stan.list$biomes_id <- unclass(factor(df$biomes.id))
stan.list$ecocode_id <- unclass(factor(df$ecocode.id))
    return(stan.list)
}