Commit af84e86f authored by Georges Kunstler's avatar Georges Kunstler
Browse files

fixed problem in initial value

parent a4806faa
...@@ -43,12 +43,12 @@ $(D2)/TRY/data.TRY.std.rds: ...@@ -43,12 +43,12 @@ $(D2)/TRY/data.TRY.std.rds:
GLOBAL: $(D3)/Done.txt GLOBAL: $(D3)/Done.txt
$(D3)/Done.txt: scripts/process.data/remove.out.R scripts/process.data/summarise.data.R $(D3)/Done.t.txt $(D3)/Done.txt: scripts/process.data/remove.out.R scripts/process.data/summarise.data.R $(D3)/Done.t.txt
Rscript $< R CMD BATCH $<
Rscript scripts/process.data/summarise.data.R R CMD BATCH scripts/process.data/summarise.data.R
Rscript scripts/process.data/plot.data.all.R R CMD BATCH scripts/process.data/plot.data.all.R
$(D3)/Done.t.txt: scripts/process.data/merge.all.processed.data.R $(D3)/Done.t.txt: scripts/process.data/merge.all.processed.data.R
Rscript $< R CMD BATCH $<
#------------------------------------------------------- #-------------------------------------------------------
TEST.TREE: $(D4)/Done.txt tree.all.sites TEST.TREE: $(D4)/Done.txt tree.all.sites
......
...@@ -12,38 +12,38 @@ load.model <- function(){ ...@@ -12,38 +12,38 @@ load.model <- function(){
init.fun = function(chain_id= 1, list){ init.fun = function(chain_id= 1, list){
step <- (chain_id-1)/10 step <- (chain_id-1)/10
init <- init <-
list(intercept = 0+step, list(intercept = 0 + step,
intercept_species = rnorm(list$N_species, 0, 0.5+step), intercept_species = rnorm(list$N_species, 0, 0.3 + step),
param_sumBn_species = rnorm(list$N_species, 0, 0.5+step), param_sumBn_species = rnorm(list$N_species, 0, 0.1 + step),
param_logD_species = rnorm(list$N_species, 0, 0.5+step), param_logD_species = rnorm(list$N_species, 0, 0.3 + step),
intercept_plot = rnorm(list$N_plot, 0, 0.5 +step), intercept_plot = rnorm(list$N_plot, 0, 0.3 + step),
intercept_set = rnorm(list$N_set, 0, 0.5 +step), intercept_set = rnorm(list$N_set, 0, 0.1 + step),
mean_logD = 2/3+step, mean_logD = 2/3 + step,
param_Tf = 2/3+step, param_Tf = -0.1 + step,
param_sumBn = 2/3+step, param_sumBn = -0.1 + step,
param_sumTfBn = 2/3+step, param_sumTfBn = -0.1 + step,
param_sumTnBn = 2/3+step, param_sumTnBn = -0.1 + step,
param_sumTnTfBn_abs= 2/3+step, param_sumTnTfBn_abs= -0.1 + step,
sigma_inter_species = 0.5 +step, sigma_inter_species = 0.5 + step,
sigma_inter_plot = 0.5 + +step, sigma_inter_plot = 0.3 + step,
sigma_inter_set = 0.5 + +step, sigma_inter_set = 0.1 + step,
sigma = 0.5 +step, sigma = 0.5 +step,
sigma_sumBn_species = 0.5 +step, sigma_sumBn_species = 0.1 +step,
sigma_logD_species = 0.5 +step) sigma_logD_species = 0.3 +step)
return(init) return(init)
}, },
stan = " stan = "
data { data {
int<lower=0> N_indiv; int<lower=0> N_indiv;
int<lower=0> N_species; int<lower=0> N_species;
int<lower=0> N_plot; int<lower=0> N_plot;
int<lower=0> N_set; int<lower=0> N_set;
int species_id[N_indiv]; int species_id[N_indiv];
int plot_id[N_indiv]; int plot_id[N_indiv];
int set_id[N_indiv]; int set_id[N_indiv];
real logG[N_indiv]; real logG[N_indiv];
real logD[N_indiv]; real logD[N_indiv];
real sumBn[N_indiv]; real sumBn[N_indiv];
...@@ -93,27 +93,31 @@ transformed parameters { ...@@ -93,27 +93,31 @@ transformed parameters {
model { model {
# constants for prior # constants for prior
## real sigma0; real sigma0;
sigma0 <- 10;
################################################################################
######################## growth model with STAN #############################
############################################### ###############################################
########### Hierarchical parameters ######## ########### Hierarchical parameters ########
### species random param ### species random param
param_logD_species ~ normal(0,sigma_logD_species); param_logD_species ~ normal(0,sigma_logD_species);
param_sumBn_species ~ normal(0,sigma_sumBn_species); param_sumBn_species ~ normal(0,sigma_sumBn_species);
intercept_species ~ normal(0, sigma_inter_species); intercept_species ~ normal(0, sigma_inter_species);
### plot random param ### plot random param
intercept_plot ~ normal(0,sigma_inter_plot); intercept_plot ~ normal(0,sigma_inter_plot);
### set random effect ### set random effect
intercept_set ~ normal(0, sigma_inter_set); intercept_set ~ normal(0, sigma_inter_set);
### biomes fixed param
param_Tf ~ normal(0, sigma0); ###############################################
param_sumBn ~ normal(0,sigma0); ########### Non-Hierarchical parameters ########
param_sumTfBn ~ normal(0 ,sigma0); # slope and intercept
param_sumTnBn ~ normal(0 ,sigma0); intercept ~ normal(0,sigma0);
param_sumTnTfBn_abs ~ normal(0 , sigma0); mean_logD ~ normal(0,sigma0);
### biomes fixed param
param_Tf ~ normal(0, sigma0);
param_sumBn ~ normal(0,sigma0);
param_sumTfBn ~ normal(0 ,sigma0);
param_sumTnBn ~ normal(0 ,sigma0);
param_sumTnTfBn_abs ~ normal(0 , sigma0);
############################################### ###############################################
......
...@@ -13,26 +13,26 @@ load.model <- function(){ ...@@ -13,26 +13,26 @@ load.model <- function(){
step <- (chain_id-1)/10 step <- (chain_id-1)/10
init <- init <-
list(intercept = 0+step, list(intercept = 0+step,
intercept_species = rnorm(list$N_species, 0, 0.5+step), intercept_species = rnorm(list$N_species, 0, 0.3+step),
param_sumBn_species = rnorm(list$N_species, 0, 0.5+step), param_sumBn_species = rnorm(list$N_species, 0, 0.1+step),
param_logD_species = rnorm(list$N_species, 0, 0.5+step), param_logD_species = rnorm(list$N_species, 0, 0.3+step),
intercept_plot = rnorm(list$N_plot, 0, 0.5 +step), intercept_plot = rnorm(list$N_plot, 0, 0.3 +step),
intercept_set = rnorm(list$N_set, 0, 0.5 +step), intercept_set = rnorm(list$N_set, 0, 0.1 +step),
mean_logD = 2/3+step, mean_logD = (2/3 - 0.1) +step,
param_Tf_biomes= rep(2/3+step, list$N_biomes), param_Tf_biomes= rep(-0.1+step, list$N_biomes),
param_sumBn_biomes= rep(2/3+step, list$N_biomes), param_sumBn_biomes= rep(-0.1+step, list$N_biomes),
param_sumTfBn_biomes= rep(2/3+step, list$N_biomes), param_sumTfBn_biomes= rep(-0.1+step, list$N_biomes),
param_sumTnBn_biomes= rep(2/3+step, list$N_biomes), param_sumTnBn_biomes= rep(-0.1+step, list$N_biomes),
param_sumTnTfBn_abs_biomes= rep(2/3+step, list$N_biomes), param_sumTnTfBn_abs_biomes= rep(-0.1+step, list$N_biomes),
sigma_inter_species = 0.5 +step, sigma_inter_species = 0.3 +step,
sigma_inter_plot = 0.5 + +step, sigma_inter_plot = 0.3 + +step,
sigma_inter_set = 0.5 + +step, sigma_inter_set = 0.1 + +step,
sigma = 0.5 +step, sigma = 0.5 +step,
sigma_sumBn_species = 0.5 +step, sigma_sumBn_species = 0.1 +step,
sigma_logD_species = 0.5 +step) sigma_logD_species = 0.3 +step)
return(init) return(init)
}, },
stan = " stan = "
data { data {
int<lower=0> N_indiv; int<lower=0> N_indiv;
...@@ -40,12 +40,12 @@ data { ...@@ -40,12 +40,12 @@ data {
int<lower=0> N_plot; int<lower=0> N_plot;
int<lower=0> N_biomes; int<lower=0> N_biomes;
int<lower=0> N_set; int<lower=0> N_set;
int species_id[N_indiv]; int species_id[N_indiv];
int plot_id[N_indiv]; int plot_id[N_indiv];
int biomes_id[N_indiv]; int biomes_id[N_indiv];
int set_id[N_indiv]; int set_id[N_indiv];
real logG[N_indiv]; real logG[N_indiv];
real logD[N_indiv]; real logD[N_indiv];
real sumBn[N_indiv]; real sumBn[N_indiv];
...@@ -80,12 +80,12 @@ transformed parameters { ...@@ -80,12 +80,12 @@ transformed parameters {
vector[N_indiv] theo_g; vector[N_indiv] theo_g;
for (i in 1:N_indiv){ for (i in 1:N_indiv){
theo_g[i] <- intercept + intercept_species[species_id[i]] theo_g[i] <- intercept + intercept_species[species_id[i]]
+ intercept_plot[plot_id[i]] + intercept_plot[plot_id[i]]
+ intercept_set[set_id[i]] + intercept_set[set_id[i]]
+ (mean_logD + param_logD_species[species_id[i]])*logD[i] + (mean_logD + param_logD_species[species_id[i]])*logD[i]
+ (param_Tf_biomes[biomes_id[i]])*Tf[i] + (param_Tf_biomes[biomes_id[i]])*Tf[i]
+ ( param_sumBn_biomes[biomes_id[i]] + param_sumBn_species[species_id[i]])*sumBn[i] + ( param_sumBn_biomes[biomes_id[i]] + param_sumBn_species[species_id[i]])*sumBn[i]
+ (param_sumTfBn_biomes[biomes_id[i]])*sumTfBn[i] + (param_sumTfBn_biomes[biomes_id[i]])*sumTfBn[i]
+ (param_sumTnBn_biomes[biomes_id[i]])*sumTnBn[i] + (param_sumTnBn_biomes[biomes_id[i]])*sumTnBn[i]
+ (param_sumTnTfBn_abs_biomes[biomes_id[i]])*sumTnTfBn_abs[i] + (param_sumTnTfBn_abs_biomes[biomes_id[i]])*sumTnTfBn_abs[i]
...@@ -94,48 +94,36 @@ transformed parameters { ...@@ -94,48 +94,36 @@ transformed parameters {
} }
model { model {
# constants for prior # constants for prior
## real sigma0; real sigma0;
sigma0 <- 10;
################################################################################
######################## growth model with STAN #############################
############################################### ###############################################
########### Hierarchical parameters ######## ########### Hierarchical parameters ########
### species random param ### species random param
param_logD_species ~ normal(0,sigma_logD_species); param_logD_species ~ normal(0,sigma_logD_species);
param_sumBn_species ~ normal(0,sigma_sumBn_species); param_sumBn_species ~ normal(0,sigma_sumBn_species);
intercept_species ~ normal(0, sigma_inter_species); intercept_species ~ normal(0, sigma_inter_species);
### plot random param ### plot random param
intercept_plot ~ normal(0,sigma_inter_plot); intercept_plot ~ normal(0,sigma_inter_plot);
### set random effect ### set random effect
intercept_set ~ normal(0, sigma_inter_set); intercept_set ~ normal(0, sigma_inter_set);
### biomes fixed param
## param_Tf_biomes ~ normal(0, sigma0);
## param_sumBn_biomes ~ normal(0,sigma0);
## param_sumTfBn_biomes ~ normal(0 ,sigma0);
## param_sumTnBn_biomes ~ normal(0 ,sigma0);
## param_sumTnTfBn_abs_biomes ~ normal(0 , sigma0);
############################################### ###############################################
########### Non-Hierarchical parameters ######## ########### Non-Hierarchical parameters ########
# constant for prior # slope and intercept
## sigma0 <- 10; intercept ~ normal(0,sigma0);
# slope and intercept mean_logD ~ normal(0,sigma0);
## intercept ~ normal(0,sigma0); ### biomes fixed param
## mean_logD ~ normal(0,sigma0); param_Tf_biomes ~ normal(0, sigma0);
## # sigma param_sumBn_biomes ~ normal(0,sigma0);
## sigma_inter_species~ uniform(0,6); param_sumTfBn_biomes ~ normal(0 ,sigma0);
## sigma_inter_set~ uniform(0,6); param_sumTnBn_biomes ~ normal(0 ,sigma0);
## sigma_inter_plot~ uniform(0,6); param_sumTnTfBn_abs_biomes ~ normal(0 , sigma0);
## sigma_inter_tree~ uniform(0,6);
## sigma_logD_species~ uniform(0,6);
## sigma_sumBn_species~ uniform(0,6);
## sigma~ uniform(0,6);
############################################### ###############################################
############ Likelihood ################### ############ Likelihood ###################
logG ~ normal(theo_g,sigma); logG ~ normal(theo_g,sigma);
} }
") ")
} }
......
...@@ -37,10 +37,10 @@ data { ...@@ -37,10 +37,10 @@ data {
int<lower=0> N_indiv; int<lower=0> N_indiv;
int<lower=0> N_species; int<lower=0> N_species;
int<lower=0> N_plot; int<lower=0> N_plot;
int species_id[N_indiv]; int species_id[N_indiv];
int plot_id[N_indiv]; int plot_id[N_indiv];
real logG[N_indiv]; real logG[N_indiv];
real logD[N_indiv]; real logD[N_indiv];
real sumBn[N_indiv]; real sumBn[N_indiv];
...@@ -90,28 +90,27 @@ model { ...@@ -90,28 +90,27 @@ model {
real sigma0; real sigma0;
sigma0 <- 10; sigma0 <- 10;
################################################################################ ###############################################
######################## growth model with STAN ############################# ########### Hierarchical parameters ########
### species random param
###############################################
########### Hierarchical parameters ########
### species random param
param_logD_species ~ normal(0,sigma_logD_species); param_logD_species ~ normal(0,sigma_logD_species);
param_sumBn_species ~ normal(0,sigma_sumBn_species); param_sumBn_species ~ normal(0,sigma_sumBn_species);
intercept_species ~ normal(0, sigma_inter_species); intercept_species ~ normal(0, sigma_inter_species);
### plot random param ### plot random param
intercept_plot ~ normal(0,sigma_inter_plot); intercept_plot ~ normal(0,sigma_inter_plot);
### biomes fixed param ### fixed param
intercept ~ normal(0,sigma0);
mean_logD ~ normal(0,sigma0);
### biomes fixed param
param_Tf ~ normal(0, sigma0); param_Tf ~ normal(0, sigma0);
param_sumBn ~ normal(0,sigma0); param_sumBn ~ normal(0,sigma0);
param_sumTfBn ~ normal(0 ,sigma0); param_sumTfBn ~ normal(0 ,sigma0);
param_sumTnBn ~ normal(0 ,sigma0); param_sumTnBn ~ normal(0 ,sigma0);
param_sumTnTfBn_abs ~ normal(0 , sigma0); param_sumTnTfBn_abs ~ normal(0 , sigma0);
###############################################
############################################### ############ Likelihood ###################
############ Likelihood ################### logG ~ normal(theo_g,sigma);
logG ~ normal(theo_g,sigma);
} }
") ")
} }
......
...@@ -26,16 +26,16 @@ fun.call.stan.parallel.and.save <- function(stan.model, list.stan, path.out, ...@@ -26,16 +26,16 @@ fun.call.stan.parallel.and.save <- function(stan.model, list.stan, path.out,
var.sample){ var.sample){
start <- Sys.time() start <- Sys.time()
set_cppo(mode = "fast") set_cppo(mode = "fast")
inits <- stan.model$init.fun(chain_id, inits <- list(stan.model$init.fun(chains,
list.stan) list.stan))
stan.output <- stan(model_code = stan.model$stan, stan.output <- stan(model_code = stan.model$stan,
data = list.stan, data = list.stan,
pars = stan.model$pars, pars = stan.model$pars,
init = 'random', init = inits,
iter = iter, iter = iter,
warmup = warmup, warmup = warmup,
chains = chains, chains = chains,
chain_id = chain_id, ## chain_id = chain_id,
thin = thin, thin = thin,
verbose = FALSE) verbose = FALSE)
end <- Sys.time() end <- Sys.time()
...@@ -57,7 +57,7 @@ fun.call.stan.and.save <- function(stan.model, list.stan, path.out, ...@@ -57,7 +57,7 @@ fun.call.stan.and.save <- function(stan.model, list.stan, path.out,
stan.output <- stan(model_code = stan.model$stan, stan.output <- stan(model_code = stan.model$stan,
data = list.stan, data = list.stan,
pars = stan.model$pars, pars = stan.model$pars,
init = 'random', init = inits,
iter = iter, iter = iter,
warmup = warmup, warmup = warmup,
chains = chains, chains = chains,
......
### test stan ### test stan
## I changed the coding of the random effect with norm(0,10*sigma it seems more slow! ## I changed the coding of the random effect
## with norm(0,10*sigma it seems more slow!
## PROBLEM WITH STANDARDIZED VARIABLE DO I NEED TO INCLUDE AN INTERCEPT?? ## PROBLEM WITH STANDARDIZED VARIABLE DO I NEED TO INCLUDE AN INTERCEPT??
## VERY BAD CONVERGENCE ??? ## VERY BAD CONVERGENCE ???
...@@ -12,11 +13,12 @@ source('R/analysis/stan.run.R') ...@@ -12,11 +13,12 @@ source('R/analysis/stan.run.R')
### TEST simple model on France only ### TEST simple model on France only
df.lmer <- load.data.for.lmer(trait = 'SLA',fname = 'data.all.no.log.rds', cat.TF = FALSE, df.lmer <- load.data.for.lmer(trait = 'SLA',
sample.size = NA, fname = 'data.all.no.log.rds',
var.sample = NA, cat.TF = FALSE,
sample.vec.TF. = FALSE, sample.size = 500,
select.set. = 'France') var.sample = 'ecocode',
sample.vec.TF. = FALSE)
stan.list <- fun.turn.in.list.for.jags.stan(df.lmer, stan.list <- fun.turn.in.list.for.jags.stan(df.lmer,
cat.TF = FALSE) cat.TF = FALSE)
...@@ -26,8 +28,12 @@ source('R/analysis/model.stan/model.stan.LOGLIN.size.fixed.R', local = TRUE) ...@@ -26,8 +28,12 @@ source('R/analysis/model.stan/model.stan.LOGLIN.size.fixed.R', local = TRUE)
fun.init.stan <- function(chain_id= 1, stan.list){ fun.init.stan <- function(chain_id= 1, stan.list){
init <- list(intercept = 0+(chain_id-1)/10, init <- list(intercept = 0+(chain_id-1)/10,
intercept_species = rnorm(stan.list$N_species, 0, 0.5+(chain_id-1)/10), intercept_species = rnorm(stan.list$N_species,
intercept_plot = rnorm(stan.list$N_plot, 0, 0.5 +(chain_id-1)/10), 0,
0.5+(chain_id-1)/10),
intercept_plot = rnorm(stan.list$N_plot,
0,
0.5 +(chain_id-1)/10),
mean_logD = 2/3+(chain_id-1)/10, mean_logD = 2/3+(chain_id-1)/10,
sigma_inter_species = 0.5 +(chain_id-1)/10, sigma_inter_species = 0.5 +(chain_id-1)/10,
sigma_inter_plot = 0.5 + +(chain_id-1)/10, sigma_inter_plot = 0.5 + +(chain_id-1)/10,
...@@ -48,33 +54,12 @@ system.time( stan.output <- stan(model_code = model$stan, ...@@ -48,33 +54,12 @@ system.time( stan.output <- stan(model_code = model$stan,
chains = 3, chains = 3,
verbose = FALSE)) verbose = FALSE))
source('R/analysis/model.stan/model.stan.LOGLIN.ER.AD.Tf.fixed.oneset.R', local = TRUE) source('R/analysis/model.stan/model.stan.LOGLIN.ER.AD.Tf.fixed.biomes.R', local = TRUE)
model <- load.model() model <- load.model()
fun.init.stan <- function(chain_id= 1, stan.list){ inits <- list(model$init.fun(1, stan.list),
step <- (chain_id-1)/10 model$init.fun(2, stan.list),
init <- list(intercept = 0+step, model$init.fun(3, stan.list))
intercept_species = rnorm(stan.list$N_species, 0, 0.5+step),
param_sumBn_species = rnorm(stan.list$N_species, 0, 0.5+step),
param_logD_species = rnorm(stan.list$N_species, 0, 0.5+step),
intercept_plot = rnorm(stan.list$N_plot, 0, 0.5 +step),
mean_logD = 2/3+step,
param_Tf = 2/3+step,
param_sumBn = 2/3+step,
param_sumTfBn = 2/3+step,
param_sumTnBn = 2/3+step,
param_sumTnTfBn_abs= 2/3+step,
sigma_inter_species = 0.5 +step,
sigma_inter_plot = 0.5 + +step,
sigma = 0.5 +step,
sigma_sumBn_species = 0.5 +step,
sigma_logD_species = 0.5 +step
)
}
inits <- list(fun.init.stan(1, stan.list),
fun.init.stan(2, stan.list),
fun.init.stan(3, stan.list))
library(rstan) library(rstan)
...@@ -96,8 +81,35 @@ pairs(stan.output) ...@@ -96,8 +81,35 @@ pairs(stan.output)
library(ggmcmc) library(ggmcmc)
S <- ggs(stan.output) S <- ggs(stan.output2)
extract(stan.output, permuted = FALSE, inc_warmup=FALSE)) ggs_crosscorrelation(S)
ggs_crosscorrelation(S, absolute_scale = FALSE)
ggs_density(S)
ggs_traceplot(S)
ggs_running(S)
ggs_autocorrelation(S)
ggs_Rhat(S)
ggs_geweke(S)
ggs_caterpillar(S)
#######
## read results of simul on cluster
source("R/analysis/stan.output-fun.R")
stan.out.clust <- fun.merge.chain(
path.out = "output/stan/all.no.log/SLA/species/LOGLIN.ER.AD.Tf.fixed.biomes/",
var.sample = 'ecocode', chains.vec = 1:3)
stan.out.clust
plot(stan.out.clust)
pairs(stan.out.clust)
traceplot(stan.out.clust, ask = TRUE)
## base on traceplot the initial value seems to be
## a big problem for the lack of convergence
library(ggmcmc)
S <- ggs(stan.out.clust)
ggs_crosscorrelation(S)
ggs_crosscorrelation(S, absolute_scale = FALSE) ggs_crosscorrelation(S, absolute_scale = FALSE)
ggs_density(S) ggs_density(S)
ggs_traceplot(S) ggs_traceplot(S)
...@@ -108,3 +120,23 @@ ggs_geweke(S) ...@@ -108,3 +120,23 @@ ggs_geweke(S)
ggs_caterpillar(S) ggs_caterpillar(S)
run.multiple.model.for.set.one.trait(model.files.stan.Tf.1, run.stan, 'SLA',
type.filling='species', sample.size = 10000, var.sample = 'ecocode',
iter = 1000, warmup = 500, chains = 1, chain_id = 1,
init.TF = FALSE)
run.multiple.model.for.set.one.trait(model.files.stan.Tf.1, run.stan, 'SLA',
type.filling='species', sample.size = 1000, var.sample = 'ecocode',
iter = 1000, warmup = 500, chains = 3, parallel.TF = FALSE,
init.TF = FALSE)
out.stan3 <- readRDS('output/stan/all.no.log/SLA/species/LOGLIN.ER.AD.Tf.fixed.biomes/ecocode.results.stan.rds')
traceplot(out.stan3, ask = TRUE)
plot(out.stan3)