lmer.output-fun.R 23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#### function to analyse lmer output


library(lme4)


read.lmer.output <- function(file.name){
  tryCatch(readRDS(file.name), error=function(cond)return(NULL))   # Choose a return value in case of error
}



summarise.lmer.output <- function(x){
14
15
16
17
18
 list( nobs = nobs(x),
       R2m =Rsquared.glmm.lmer(x)$Marginal,
       R2c =Rsquared.glmm.lmer(x)$Conditional,
       AIC = AIC(x),
       deviance = deviance(x),
19
       conv=x@optinfo$conv,
20
21
22
23
24
       effect.response.var=variance.fixed.glmm.lmer.effect.and.response(x),
       fixed.coeff.E=fixef(x),
       fixed.coeff.Std.Error=sqrt(diag(vcov(x))),
       fixed.var=variance.fixed.glmm.lmer(x))
}
25
26
27
28
29
30
31
32
33
34
35
36
37
38


summarise.lmer.output.list <- function(f ){
    output.lmer <- read.lmer.output(f)
    if(!is.null(output.lmer)){
	res <- list(files.details=files.details(f),
		   lmer.summary=summarise.lmer.output( output.lmer))
    }else{
        res <- NULL
    }
    return(res)
}


39
40
41
42
43
44
45
46
47
48
49
50
summarise.lmer.output.all.list <- function(f ){
    output.lmer <- read.lmer.output(f)
    if(!is.null(output.lmer)){
	res <- list(files.details=files.details.all(f),
		   lmer.summary=summarise.lmer.output( output.lmer))
    }else{
        res <- NULL
    }
    return(res)
}


51
52
53

files.details <- function(x){
	s <- data.frame(t(strsplit(x, "/", fixed=TRUE)[[1]]), stringsAsFactors= FALSE)
54
	names(s)  <- c("d1", "d2", "set", "ecocode", "trait", "filling", "model", "file" )
55
56
57
58
	s[-(1:2)]
}


59
60
61
62
63
64
files.details.all <- function(x){
	s <- data.frame(t(strsplit(x, "/", fixed=TRUE)[[1]]), stringsAsFactors= FALSE)
	names(s)  <- c("d1", "d2", "set", "trait", "filling", "model", "file" )
	s[-(1:2)]
}

65

66
#### R squared functions
67
68
69
70
71
72

# Function rsquared.glmm requires models to be input as a list (can include fixed-
# effects only models,but not a good idea to mix models of class "mer" with models
# of class "lme") FROM http://jslefche.wordpress.com/2013/03/13/r2-for-linear-mixed-effects-models/

Rsquared.glmm.lmer <- function(i){
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Get variance of fixed effects by multiplying coefficients by design matrix
      VarF <- var(as.vector(fixef(i) %*% t(i@pp$X)))
      # Get variance of random effects by extracting variance components
      VarRand <- colSums(do.call(rbind,lapply(VarCorr(i),function(j) j[1])))
      # Get residual variance
      VarResid <- attr(VarCorr(i),"sc")^2
      # Calculate marginal R-squared (fixed effects/total variance)
      Rm <- VarF/(VarF+VarRand+VarResid)
      # Calculate conditional R-squared (fixed effects+random effects/total variance)
      Rc <- (VarF+VarRand)/(VarF+VarRand+VarResid)
      # Bind R^2s into a matrix and return with AIC values
      Rsquared.mat <- data.frame(Class=class(i),Family="Gaussian",Marginal=Rm,
                              Conditional=Rc,AIC=AIC(i))
      return(Rsquared.mat)
}


variance.fixed.glmm.lmer <- function(i){
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
# Get variance of for each fixed effects by multiplying coefficients by design matrix
var.vec <- apply(fixef(i) * t(i@pp$X),MARGIN=1,var)
# Get variance of fixed effects by multiplying coefficients by design matrix
VarF <- var(as.vector(fixef(i) %*% t(i@pp$X)))
# Get variance of random effects by extracting variance components
VarRand <- colSums(do.call(rbind,lapply(VarCorr(i),function(j) j[1])))
# Get residual variance
VarResid <- attr(VarCorr(i),"sc")^2
var.vec <- var.vec/(VarF+VarRand+VarResid)
names(var.vec) <- paste(names(var.vec),"VAR",sep=".")
return(var.vec)
}

variance.fixed.glmm.lmer.effect.and.response <- function(i){
if(sum(c("sumTfBn","sumTnBn") %in% names(fixef(i)))==2){
# Get variance of for each fixed effects by multiplying coefficients by design matrix
var.vec <- var(as.vector(fixef(i)[c("sumTfBn","sumTnBn")] %*% t(i@pp$X[,c("sumTfBn","sumTnBn")])))
# Get variance of fixed effects by multiplying coefficients by design matrix
VarF <- var(as.vector(fixef(i) %*% t(i@pp$X)))
# Get variance of random effects by extracting variance components
VarRand <- colSums(do.call(rbind,lapply(VarCorr(i),function(j) j[1])))
# Get residual variance
VarResid <- attr(VarCorr(i),"sc")^2
var.vec <- var.vec/(VarF+VarRand+VarResid)
}else{
var.vec <- NA
}    
names(var.vec) <- paste("effect.response","VAR",sep=".")
return(var.vec)
}
122
123


Georges Kunstler's avatar
Georges Kunstler committed
124
125
126
127


## function to turn lmer output from list to DF
fun.format.in.data.frame <- function(list.res,names.param){
128
129
130
131
dat.t <- data.frame(t(rep(NA,3*length(names.param))))
names(dat.t) <-  c(names.param,paste(names.param,"Std.Error",sep=".")
                   ,paste(names.param,"VAR",sep="."))
dat.t[,match(names(list.res$lmer.summary$fixed.coeff.E),names(dat.t))] <-
Georges Kunstler's avatar
Georges Kunstler committed
132
    list.res$lmer.summary$fixed.coeff.E
133
dat.t[,length(names.param)+match(names(list.res$lmer.summary$fixed.coeff.E),names(dat.t))] <-
Georges Kunstler's avatar
Georges Kunstler committed
134
    list.res$lmer.summary$fixed.coeff.Std.Error
135
136
dat.t[,match(names(list.res$lmer.summary$fixed.var),names(dat.t))] <-
    list.res$lmer.summary$fixed.var
137
res <- data.frame(list.res$files.details,list.res$lmer.summary[1:7],dat.t)
Georges Kunstler's avatar
Georges Kunstler committed
138
139
return(res)
}
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


#################################################################
#################################################################
##### FUNCTION TO analyse the results AIC effect size

## function to compute delat R2
fun.compute.criteria.diff <- function(i,DF.results,criteria.selected){
select.simple.compet <- DF.results$id==DF.results$id[i] &
                    DF.results$trait==DF.results$trait[i] &
                    DF.results$filling==DF.results$filling[i] &
                    DF.results$model=='lmer.LOGLIN.simplecomp.Tf'
select.no.compet <- DF.results$id==DF.results$id[i] &
                    DF.results$trait==DF.results$trait[i] &
                    DF.results$filling==DF.results$filling[i] &
                    DF.results$model=='lmer.LOGLIN.nocomp.Tf'
if(sum(select.simple.compet)==1){
diff.criteria.simple.compet <- DF.results[[criteria.selected]][i] - DF.results[[criteria.selected]][
                                                                                select.simple.compet]
}else{
diff.criteria.simple.compet <- NA
}
if(sum(select.no.compet)==1){
diff.criteria.no.compet <- DF.results[[criteria.selected]][i] - DF.results[[criteria.selected]][
                                                                              select.no.compet]
}else{
diff.criteria.no.compet <- NA
}

df.res <- data.frame(diff.criteria.simple.compet,diff.criteria.no.compet)
names(df.res) <- paste(criteria.selected,c('simplecomp','nocomp'),sep=".")

return(df.res)
}



fun.compute.delta.AIC <- function(i,DF.results){
select.model.trait.fill <- DF.results$id==DF.results$id[i] &
                    DF.results$trait==DF.results$trait[i] &
                    DF.results$filling==DF.results$filling[i]
if(sum(select.model.trait.fill)>0){
delta.AIC <- DF.results[['AIC']][i] - min(DF.results[['AIC']][select.model.trait.fill])
if (sum( DF.results$nobs[select.model.trait.fill]!=DF.results$nobs[i])>0)
    stop('no same number of observation')
}else{
delta.AIC <- NA
}
df.res <- data.frame(delta.AIC=delta.AIC)
return(df.res)
}

## function to compute ratio of variance explained by a trait variable over the variance explained by the BATOT
fun.ratio.var.fixed.effect <- function(DF.results){
194
195
mat.ratio <- DF.results[,c('sumTnTfBn.abs.VAR','sumTfBn.VAR','sumTnBn.VAR',
                           'effect.response.var')]/
196
197
198
199
200
201
202
    DF.results[,'sumBn.VAR']
names(mat.ratio) <- c('abs.dist','Response','Effect','Effect.Response')
return(mat.ratio)
}

### FUNCTION TO REPORT BEST MODEL PER ECOREGION AND TRAITS
fun.AIC <- function(id2.one,DF.results){
203
204
205
206
207
208
209
 models <- c('lmer.LOGLIN.nocomp.Tf', 'lmer.LOGLIN.simplecomp.Tf',
             'lmer.LOGLIN.HD.Tf',
             'lmer.LOGLIN.E.Tf','lmer.LOGLIN.R.Tf','lmer.LOGLIN.ER.Tf',
             'lmer.LOGLIN.AD.Tf')
 best <- as.vector(DF.results[DF.results$id2==id2.one,c('id2','trait','set',
                                  'ecocode','filling','MAT','MAP','model')])[
                           which.min(DF.results$AIC[DF.results$id2==id2.one]),]
210
211
212
213
 AIC.all <- as.vector(DF.results[DF.results$id2==id2.one,c('AIC')])
 names(AIC.all) <- as.vector(DF.results[DF.results$id2==id2.one,c('model')])
 AIC.all <- AIC.all[models]-min(AIC.all)
 res <- data.frame((best),t(AIC.all))
214
215
 names(res) <- c('id2','trait','set','ecocode','filling','MAT','MAP',
                 'best.model',paste('AIC',models,sep='.'))
216
217
218
219
 return(res)
}

fun.AICc <- function(id2.one,DF.results){
220
221
222
 models <- c('lmer.LOGLIN.nocomp.Tf', 'lmer.LOGLIN.simplecomp.Tf',
             'lmer.LOGLIN.HD.Tf','lmer.LOGLIN.E.Tf','lmer.LOGLIN.R.Tf',
             'lmer.LOGLIN.ER.Tf','lmer.LOGLIN.AD.Tf')
223
224
225
226
227
228
 Deviance.all <- DF.results[DF.results$id2==id2.one,'deviance']
 names(Deviance.all) <- DF.results[DF.results$id2==id2.one,'model']
 Deviance.all <- Deviance.all[models]
 nobs.all <- DF.results[DF.results$id2==id2.one,'nobs']
 names(nobs.all) <- DF.results[DF.results$id2==id2.one,'model']
 nobs.all <- nobs.all[models]
229
 n.param <- c(2,3,4,4,4,5,4)
230
231
232
233
234
235
236
237
238
239
240
241
 AICc <-  Deviance.all+2*n.param*(nobs.all)/(nobs.all-n.param-1)
 id2.n <- unique(DF.results[DF.results$id2==id2.one,c('id2')])
 res <- data.frame(id2.n,models[which.min(AICc)],t(AICc),row.names=NULL)
 names(res) <- c('id2','best.model',models)
 return(res)
}

#################################333

### function to get the data for a given model with criteria to select
fun.select.ecoregions.trait <- function(DF.results,trait.name,model.selected,
                                      nobs.min=1000,filling.selected="species",
242
                                      threshold.delta.AIC){
243
244
245
246
247
248
249
250
251
252
DF.results[DF.results$nobs>nobs.min &
           DF.results$filling==filling.selected &
           DF.results$trait==trait.name &
           DF.results$model %in% model.selected &
           DF.results$delta.AIC<threshold.delta.AIC,]
}

### function to get the data for a given model with criteria to select only site with competition
fun.select.ecoregions.trait.compet <- function(DF.results,trait.name,model.selected,
                                      nobs.min=1000,filling.selected="species",
253
                                      threshold.delta.AIC){
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
DF.results[DF.results$nobs>nobs.min &
           DF.results$filling==filling.selected &
           DF.results$trait==trait.name &
           DF.results$model %in% model.selected &
           DF.results$sumBn < 0.0 &
           ## DF.results$delta.AIC==0,]
           DF.results$delta.AIC<threshold.delta.AIC,]
}




#########################
##### FUNCTIONS FOR PLOTS
fun.plot.lmer.res.x.y <- function(model.selected,trait.name,DF.results,var.x,var.y,threshold.delta.AIC,...){
269
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,model.selected=model.selected,threshold.delta.AIC=10000000)
270
271
272
273
plot(df.selected[[var.x]],df.selected[[var.y]],...)
}

fun.plot.lmer.res.x.y.2 <- function(model.selected,trait.name,DF.results,var.x,var.y,col.vec,pch.AIC=TRUE,cex.AIC=TRUE,col.set=TRUE,...){
274
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,model.selected=model.selected,threshold.delta.AIC=10000000)
275
276
if(pch.AIC) {pch.vec <- c(1,16)[as.numeric(df.selected[['delta.AIC']]==0)+1]}else{pch.vec <- 1}
if(cex.AIC) {cex.vec <- c(1,1.5)[as.numeric(df.selected[['delta.AIC']]==0)+1]}else{cex.vec <- 1}
277
if(col.set) {col.vec2 <- col.vec[unclass(df.selected[['set']])]}else{col.vec2 <- 1}
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
plot(df.selected[[var.x]],df.selected[[var.y]],
     pch=pch.vec,
     cex=cex.vec,
     col=col.vec2,...)
}

fun.plot.lmer.res.boxplot <- function(model.selected,trait.name,DF.results,var.y,...){
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,model.selected=model.selected)
boxplot(df.selected[[var.y]],...)
}

fun.plot.lmer.res.boxplot.compare.model <- function(model.selected,trait.name,DF.results,var.y,...){
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,model.selected=model.selected)
boxplot(df.selected[[var.y]]~df.selected[['model']],...)
## compute percentage of model beteer than null
print(tapply(df.selected[['AIC.simplecomp']]<0,
       INDEX=df.selected[['model']],
       FUN=sum)/tapply(df.selected[['AIC.simplecomp']]<0,
                       INDEX=df.selected[['model']],
                       FUN=length))
}




303
fun.plot.param.error.bar <- function(df.selected,var.x,param,small.bar,col.vec){
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
segments( df.selected[[var.x]],df.selected[[param]] - 1.96*df.selected[[paste(
    param,"Std.Error",sep=".")]],
          df.selected[[var.x]],df.selected[[param]] + 1.96*df.selected[[paste(
              param,"Std.Error",sep=".")]],col=col.vec)
segments( df.selected[[var.x]]-small.bar,df.selected[[param]] - 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],
          df.selected[[var.x]]+small.bar,df.selected[[param]] - 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],col=col.vec)
segments( df.selected[[var.x]]-small.bar,df.selected[[param]] + 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],
          df.selected[[var.x]]+small.bar,df.selected[[param]] + 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],col=col.vec)
}

fun.plot.all.param.x.y.c <- function(model.selected,trait.name,DF.results,var.x,
                                     params,small.bar,threshold.delta.AIC,
                                     col.vec,col.set=TRUE,ylim=NA,
                                     ylim.same=FALSE,add.name = FALSE, ...){
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,
                                           model.selected=model.selected,
                                           threshold.delta.AIC=threshold.delta.AIC)
if(col.set) {col.vec2 <- col.vec[unclass(df.selected[['set']])]}else{
    col.vec2 <- 1}
if(!ylim.same) {ylim <- range(c(df.selected[[params[1]]] - 1.96*
                           df.selected[[paste(params[1],"Std.Error",sep=".")]],
                df.selected[[params[1]]] + 1.96*
               df.selected[[paste(params[1],"Std.Error",sep=".")]]),na.rm=TRUE)}
331
plot(df.selected[[var.x]],df.selected[[params[1]]],col=col.vec2,ylim=ylim,...)
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
if (add.name) text(df.selected[[var.x]], df.selected[[params[1]]],
                   labels = paste(df.selected[['set']],
                       df.selected[['ecocode']]), cex = 0.5, pos = 2)
fun.plot.param.error.bar(df.selected,var.x,param=params[1],
                         small.bar,col=col.vec2)
}


fun.plot.param.error.bar.std <- function(df.selected,var.x,param,small.bar,col.vec){
segments( df.selected[[var.x]],df.selected[[param]] + df.selected[['sumBn']]  - 1.96*df.selected[[paste(
    param,"Std.Error",sep=".")]],
          df.selected[[var.x]],df.selected[[param]]  + df.selected[['sumBn']] + 1.96*df.selected[[paste(
              param,"Std.Error",sep=".")]],col=col.vec)
segments( df.selected[[var.x]]-small.bar,df.selected[[param]] + df.selected[['sumBn']] - 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],
          df.selected[[var.x]]+small.bar,df.selected[[param]]  + df.selected[['sumBn']] - 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],col=col.vec)
segments( df.selected[[var.x]]-small.bar,df.selected[[param]]  + df.selected[['sumBn']] + 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],
          df.selected[[var.x]]+small.bar,df.selected[[param]]  + df.selected[['sumBn']] + 1.96*
         df.selected[[paste(param,"Std.Error",sep=".")]],col=col.vec)
}

fun.plot.all.param.x.y.std <- function(model.selected,trait.name,
                                       DF.results,var.x,
                                     params,small.bar,threshold.delta.AIC,
                                     col.vec,col.set=TRUE,ylim=NA,
                                     ylim.same=FALSE,add.name = FALSE, ...){
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,
                                           model.selected=model.selected,
                                           threshold.delta.AIC=threshold.delta.AIC)
if(col.set) {col.vec2 <- col.vec[unclass(df.selected[['set']])]}else{
    col.vec2 <- 1}
plot(df.selected[[var.x]],df.selected[[params[1]]] + df.selected[['sumBn']],
     col = col.vec2, ...)
if (add.name) text(df.selected[[var.x]],
                   df.selected[[params[1]]] + df.selected[['sumBn']],
                   labels = paste(df.selected[['set']],
                       df.selected[['ecocode']]), cex = 0.5, pos = 2)
fun.plot.param.error.bar.std(df.selected,var.x,param=params[1],
                         small.bar,col=col.vec2)

}



fun.plot.all.param.boxplot <- function(model.selected,trait.name,DF.results,
                                       params,small.bar,...){
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,
                                           model.selected=model.selected)
382
 if(length(params)>1){
383
384
DF.t <- data.frame(param=rep(names(params),each=nrow(df.selected)),value=c(
                            df.selected[[params[1]]],df.selected[[params[2]]]))
385
386
387
388
389
390
boxplot(DF.t[['value']]~DF.t[['param']],...)
}else{
boxplot(df.selected[[params[1]]],...)
}
}

391
392
393
394
fun.plot.all.param.er.diff.MAP <- function(model.selected,trait.name,
                                           DF.results,...){
df.selected <- fun.select.ecoregions.trait(DF.results,trait.name=trait.name,
                                           model.selected=model.selected)
395
396
397
398
399
plot(df.selected[['MAP']],df.selected[['sumTnBn']]-df.selected[['sumTfBn']],...)
}



400
401
fun.plot.panel.lmer.res.x.y <- function(models,traits,DF.results,var.x,var.y.l,
                                        express,...){
402
403
404
405
406
407
408
409
410
411
    ncols = length(traits)
    nrows = length(models)
    list.models <- as.list(names(models))
    names(list.models) <- rep('model',length(list.models))
    DF.results$set <- factor(DF.results$set)
    col.vec <- niceColors(n=nlevels(DF.results$set))
    par(mfrow = c(nrows, ncols), mar = c(2,2,1,1), oma = c(4,4,4,1) )
    for(i in 1:nrows)
        for(j in 1:ncols){
              fun.plot.lmer.res.x.y.2(models[i],traits[j],
412
                          DF.results,var.x,var.y=var.y.l[[i]],col.vec,...)
413
414
415
416
417
418
              abline(h=0,lty=3)
            if(i==1 )
                mtext(traits[j], side=3, line =1)
            if(i==nrows)
                mtext(var.x, side=1, line =4)
            if(j==1)
419
420
                mtext(paste('Effect size',list.models[i]), side=2, line =4,
                      cex=0.9)
421
422
423
424
425
426
427
            if(i==nrows & j==ncols)
                legend('topright',legend=levels(DF.results$set),pch=16,
                       col=col.vec,bty='n',ncol=2)
        }
}


428
429
fun.plot.panel.lmer.res.boxplot <- function(models,traits,DF.results,var.y,
                                            express,...){
430
431
432
433
434
435
436
437
438
439
440
441
442
    ncols = length(traits)
    nrows = length(models)
    list.models <- as.list(names(models))
    names(list.models) <- rep('model',length(list.models))
    par(mfrow = c(nrows, ncols), mar = c(2,2,1,1), oma = c(4,4,4,1) )
    for(i in 1:nrows)
        for(j in 1:ncols){
              fun.plot.lmer.res.boxplot(models[i],traits[j],
                          DF.results,var.x,var.y,...)
              abline(h=0,lty=3)
            if(i==1 )
                mtext(traits[j], side=3, line =1)
            if(j==1)
443
444
                mtext(paste("Effect size", list.models[i],sep=" "), side=2,
                      line =4,cex=0.9)
445
446
447
448
        }
}


449
450
fun.plot.panel.lmer.res.boxplot.compare <- function(models,traits,DF.results,
                                                    var.y,express,...){
451
452
453
454
455
456
457
458
459
460
461
462
463
464
    ncols = length(traits)
    list.models <- as.list(names(models))
    names(list.models) <- rep('model',length(list.models))
    par(mfrow = c(1, ncols), mar = c(2,2,1,1), oma = c(4,4,4,1) )
    for(j in 1:ncols){
              fun.plot.lmer.res.boxplot.compare.model(models,traits[j],
                          DF.results,var.y,names=names(models),...)
              abline(h=0,lty=3)
                mtext(traits[j], side=3, line =1,cex=2)
            if(j==1 )
                mtext("Effect size", side=2, line =4,cex=1.5)
        }
}

465
466
467
468
fun.plot.panel.lmer.parameters.c <- function(models,traits,DF.results,var.x,
                                             list.params,threshold.delta.AIC,
                                             small.bar=10,ylim=NA,
                                             ylim.same=FALSE,...){
469
470
471
    ncols = length(traits)
    nrows = length(models)
    par(mfrow = c(nrows, ncols), mar = c(2,2,1,1), oma = c(4,4,4,1) )
472
473
    DF.results$set <- factor(DF.results$set)
    col.vec <- niceColors(n=nlevels(DF.results$set))
474
475

## ### TO COMPARE THE PARAMTERS WE NEED TO DIVIDE THE ABS>DIST per two
476
    ## DF.results$sumTnTfBn.abs <- DF.results$sumTnTfBn.abs/2
477
478
    for(i in 1:nrows)
        for(j in 1:ncols){
479
480
              try(fun.plot.all.param.x.y.c(models[i],traits[j],DF.results,var.x,
                                       params=list.params[[i]],
481
                                       small.bar=small.bar,
482
483
484
485
                                       threshold.delta.AIC=threshold.delta.AIC,
                                       col.vec=col.vec,col.set=TRUE,
                                       ylim=ylim,ylim.same=ylim.same,...))
              try(abline(h=0,lty=3))
486
487
488
489
490
491
492
493
494
495
              if(length(list.params[[i]])>1)
                  legend("topright",names(list.params[[i]]),
                         pch=rep(1,length(list.params[[i]])),
                         col=1:length(list.params[[i]]),bty='n',cex=1)
            if(i==1 )
                mtext(traits[j], side=3, line =1)
            if(i==nrows)
                mtext(var.x, side=1, line =4)
            if(j==1)
                mtext(paste('param',names(models)[i]), side=2, line =4,cex=1)
496
497
498
            if(i==nrows & j==ncols)
                legend('bottomright',legend=levels(DF.results$set),pch=16,
                       col=col.vec,bty='n',ncol=2)
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
        }
}



fun.plot.panel.lmer.parameters.boxplot <- function(models,traits,DF.results,list.params,small.bar=10,...){
    ncols = length(traits)
    nrows = length(models)
    par(mfrow = c(nrows, ncols), mar = c(2,2,1,1), oma = c(4,4,4,1) )
### TO COMPARE THE PARAMTERS WE NEED TO DIVIDE THE ABS>DIST per two
    ## DF.results$sumTnTfBn.abs <- DF.results$sumTnTfBn.abs/2
    for(i in 1:nrows)
        for(j in 1:ncols){
              fun.plot.all.param.boxplot(models[i],traits[j],DF.results,params=list.params[[i]],small.bar=small.bar,...)
              abline(h=0,lty=3)
            if(i==1 )
                mtext(traits[j], side=3, line =1)
            if(j==1)
                mtext(paste('param',names(models)[i]), side=2, line =4,cex=1)
        }
}

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542


## create a data base from the global data
fun.merge.results.global <-  function(list.res,
                                      names.param = c( "(Intercept)", "Tf",
                                          "logD", "sumBn", "sumTnBn",
                                          "sumTfBn", "sumTnTfBn.abs",
                                          "sumTnTfBn.diff")){
  df.details  <- data.frame(list.res$files.details[1:4],
                  list.res$lmer.summary[1:6])
  fun.rep.df <-  function(x, df = df.details) df
  n_rep <- nrow(list.res$lmer.summary$ecocode.BLUP)
  dat.t <- data.frame(matrix(rep(NA,n_rep * length(names.param)), nrow = n_rep,
                             ncol = length(names.param)))
  names(dat.t) <-  c(names.param)
  dat.t[, match(names(list.res$lmer.summary$ecocode.BLUP), names(dat.t))] <-
                                             list.res$lmer.summary$ecocode.BLUP
  df.res <- data.frame(ecocode = rownames(list.res$lmer.summary$ecocode.BLUP),
                       do.call( "rbind", lapply(1:n_rep, fun.rep.df)),
                       dat.t)
return(df.res)
}