plots.resid.regression.lines.R 11.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
##### SCRIPT TO TEST plots resuls with regression lines
source("R/analysis/lmer.run.R")



output.dir.lmer <- function (model, trait, set, ecoregion,type.filling) {
  file.path("output/lmer", set,ecoregion,trait,type.filling,model)
}




fun.compute.resid <- function(i){
return(fitted(i) +resid(i) -i@pp$X %*%fixef(i))
}    

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fun.plot.colors.density <- function(x,y,...){
mat   <- cbind(x,y)
data <- as.data.frame(mat)
colors.dens  <- densCols(mat)
plot(x,y, col=colors.dens, pch=20,...)
}

fun.points.colors.density <- function(x,y,...){
mat   <- cbind(x,y)
data <- as.data.frame(mat)
colors.dens  <- densCols(mat)
points(x,y, col=colors.dens, pch=20,...)
}


32
33
34
35
fun.boxplot.breaks <-  function(x,y,Nclass=15,...){
breakss <- seq(min(x),max(x),length=Nclass+1)
x.cut <- cut(x,breakss)
mid.points <- breakss[-(Nclass+1)]+(breakss[2]-breakss[1])/2
36
boxplot(y~x.cut,at=mid.points,add=TRUE,names=NA,outline=FALSE)
37
38
39
40
41
42
43
}

seq.from.to.quantile <- function(x,length.out=20,probs=c(0.001,0.999)){
qq <- quantile(x,probs)
return(seq(from=qq[1],to=qq[2],length.out=length.out))
}

44
fun.plot.residual.plus.regression.lines.all <- function(df.lmer,res.fix.no,res.fix.simple,
45
46
47
48
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling){
par(mfrow=c(2,3),oma=c(0,0,2,0))
## Effect /reponse
49
fun.plot.colors.density(df.lmer$sumBn,res.fix.no,cex=0.1,
50
51
52
                   xlab="sum of basal area",
                   ylab="growth residual",
                   main="Effect/response model")
53
fun.boxplot.breaks(df.lmer$sumBn,res.fix.no)
54
55
56
mtext(paste(trait,set,ecoregion,type.filling), side=3,line=0.1,outer=TRUE)
lines(seq.from.to.quantile(df.lmer$sumBn),
      seq.from.to.quantile(df.lmer$sumBn)*fixef(ERcomp)['sumBn'],
57
58
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumBn']>0)+1])
fun.plot.colors.density(df.lmer$sumTnBn,res.fix.simple,cex=0.1,
59
60
61
                   xlab="sum of basal area x Tn",
                   ylab="growth residual",
                   main="Effect/response model")
62
fun.boxplot.breaks(df.lmer$sumTnBn,res.fix.simple)
63
lines(seq.from.to.quantile(df.lmer$sumTnBn),
64
65
66
      seq.from.to.quantile(df.lmer$sumTnBn)*fixef(ERcomp)['sumTnBn'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTnBn']>0)+1])
fun.plot.colors.density(df.lmer$sumTfBn,res.fix.simple,cex=0.1,
67
68
69
                   xlab="sum of basal area x Tf",
                   ylab="growth residual",
                   main="Effect/response model")
70
 fun.boxplot.breaks(df.lmer$sumTfBn,res.fix.simple)
71
lines(seq.from.to.quantile(df.lmer$sumTfBn),
72
73
      seq.from.to.quantile(df.lmer$sumTfBn)*fixef(ERcomp)['sumTfBn'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTfBn']>0)+1])
74
## Absolute distance model
75
fun.plot.colors.density(df.lmer$sumBn,res.fix.no,cex=0.1,
76
77
78
                   xlab="sum of basal area",
                   ylab="growth residual",
                   main="Absolute distance model")
79
fun.boxplot.breaks(df.lmer$sumBn,res.fix.no)
80
81
lines(seq.from.to.quantile(df.lmer$sumBn),
      seq.from.to.quantile(df.lmer$sumBn)*fixef(ADcomp)['sumBn'],
82
83
84
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumBn']>0)+1])
fun.plot.colors.density(df.lmer$sumTnTfBn.abs,res.fix.no,cex=0.1,
                   xlab="sum of basal area  x absolute trait distance",
85
86
                   ylab="growth residual",
                   main="Absolute distance model")
87
fun.boxplot.breaks(df.lmer$sumTnTfBn.abs,res.fix.simple)
88
lines(seq.from.to.quantile(df.lmer$sumTnTfBn.abs),
89
90
      seq.from.to.quantile(df.lmer$sumTnTfBn.abs)*fixef(ADcomp)['sumTnTfBn.abs'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTnTfBn.abs']>0)+1])
91
92
93
}


94
95
96
97
98
99
100
101
102
103
104
105
fun.plot.residual.plus.regression.lines.Tn <- function(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling){
fun.plot.colors.density(df.lmer$sumTnBn,res.fix.simple,cex=0.1,
                   xlab="sum of basal area x Tn",
                   ylab="growth residual",
                   main="Effect/response model")
## fun.boxplot.breaks(df.lmer$sumTnBn,res.fix.simple)
lines(seq.from.to.quantile(df.lmer$sumTnBn),
      seq.from.to.quantile(df.lmer$sumTnBn)*fixef(ERcomp)['sumTnBn'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTnBn']>0)+1])
}
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
fun.points.residual.plus.regression.lines.Tn <- function(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling){
fun.points.colors.density(df.lmer$sumTnBn,res.fix.simple,cex=0.1,
                   xlab="sum of basal area x Tn",
                   ylab="growth residual",
                   main="Effect/response model")
## fun.boxplot.breaks(df.lmer$sumTnBn,res.fix.simple)
lines(seq.from.to.quantile(df.lmer$sumTnBn),
      seq.from.to.quantile(df.lmer$sumTnBn)*fixef(ERcomp)['sumTnBn'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTnBn']>0)+1])
}

fun.plot.residual.plus.regression.lines.Tf <- function(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling){
fun.plot.colors.density(df.lmer$sumTfBn,res.fix.simple,cex=0.1,
                   xlab="sum of basal area x Tf",
                   ylab="growth residual",
                   main="Effect/response model")
 ## fun.boxplot.breaks(df.lmer$sumTfBn,res.fix.simple)
lines(seq.from.to.quantile(df.lmer$sumTfBn),
      seq.from.to.quantile(df.lmer$sumTfBn)*fixef(ERcomp)['sumTfBn'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTfBn']>0)+1])
}

fun.points.residual.plus.regression.lines.Tf <- function(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling){
fun.points.colors.density(df.lmer$sumTfBn,res.fix.simple,cex=0.1,
                   xlab="sum of basal area x Tf",
                   ylab="growth residual",
                   main="Effect/response model")
 ## fun.boxplot.breaks(df.lmer$sumTfBn,res.fix.simple)
lines(seq.from.to.quantile(df.lmer$sumTfBn),
      seq.from.to.quantile(df.lmer$sumTfBn)*fixef(ERcomp)['sumTfBn'],
      col=c('red','green')[as.numeric(fixef(ERcomp)['sumTfBn']>0)+1])
}



fun.load.data.for.residual <- function(trait,set,ecoregion,type.filling){
149
150
151
152
df.lmer <- load.and.prepare.data.for.lmer(trait,set,ecoregion,
                                           min.obs=10, sample.size=NA,
                                         type.filling) # return a DF

153
154
155
156
157
158
159
160
161
162
163
164
165
166
simple <- readRDS(file.path("output/lmer", set,ecoregion,
                            trait,type.filling,
                            "lmer.LOGLIN.simplecomp.Tf",
                            "results.rds"))
nocomp <- readRDS(file.path("output/lmer", set,ecoregion,
                            trait,type.filling,
                            "lmer.LOGLIN.nocomp.Tf",
                            "results.rds"))
ERcomp <- readRDS(file.path("output/lmer", set,ecoregion,trait,
                            type.filling,"lmer.LOGLIN.ER.Tf",
                            "results.rds"))
ADcomp <- readRDS(file.path("output/lmer", set,ecoregion,trait,
                            type.filling,"lmer.LOGLIN.AD.Tf",
                            "results.rds"))
167
168
169

res.fix.simple <- fun.compute.resid(simple)
res.fix.no <- fun.compute.resid(nocomp)
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

return(list(df.lmer=df.lmer,res.fix.no=res.fix.no,
            res.fix.simple=res.fix.simple,
            ERcomp=ERcomp,ADcomp=ADcomp,trait=trait,
            set=set,ecoregion=ecoregion,
            type.filling=type.filling))

}



fun.load.data.and.plot.residual.plus.regression.lines <- function(trait,
                                                                  set,
                                                                  ecoregion,
                                                                  type.filling){
list.resid.data <- fun.load.data.for.residual(trait,set,ecoregion,type.filling)

187
dir.create("figs/plot.resid", recursive=TRUE, showWarnings=FALSE)
188

189
190
191
png(paste("figs/plot.resid/",
          paste(trait,set,ecoregion,type.filling,"residual.png",sep="."),sep=""),
    width = 1500, height = 1000)
192
193
194

with(list.resid.data,
fun.plot.residual.plus.regression.lines.all(df.lmer,res.fix.no,res.fix.simple,
195
                                         ERcomp,ADcomp,trait,set,ecoregion,
196
197
                                         type.filling))

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
dev.off()
}


## function to get all set and ecoregion to plot

get.ecoregions.for.set <- function(set, base.dir = "./output/processed/"){
  sub(paste(base.dir,set,"/",sep=""),"",list.dirs(paste(base.dir,set,sep="")))[-1]
}

plot.residual.for.set.all.traits  <- function(set,
               traits = c("SLA", "Wood.density","Max.height","Leaf.N","Seed.mass"),
               type.fillings=c("species","genus") , ...){
ecoregions <- get.ecoregions.for.set(set, base.dir = "./output/processed/")    
  for(trait in traits){
      for (ecoregion in ecoregions){
          for (fill in type.fillings){
          try(fun.load.data.and.plot.residual.plus.regression.lines(trait,set,
                                                          ecoregion,
                                                          type.filling=fill))
         }
     }
 }     
}


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
plot.residual.for.set.all.traits.per.ecoregion  <- function(set,
               traits = c("SLA", "Wood.density","Max.height","Leaf.N","Seed.mass"),
               type.fillings=c("species","genus") , ...){
ecoregions <- get.ecoregions.for.set(set, base.dir = "./output/processed/")    
  for(trait in traits){
      for (type.filling in type.fillings){
png(paste("figs/plot.resid/",
          paste(trait,set,type.filling,"residual.per.ecocode.Tn.png",sep="."),sep=""),
    width = 1500, height = 1000)
          
ecoregion <- ecoregions[1]
try(list.resid.data <- fun.load.data.for.residual(trait,set,ecoregion,type.filling))
try(with(list.resid.data,fun.plot.residual.plus.regression.lines.Tn(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling)))

          for (ecoregion in ecoregions[-1]){
try(list.resid.data <- fun.load.data.for.residual(trait,set,ecoregion,type.filling))
try(with(list.resid.data,fun.points.residual.plus.regression.lines.Tn(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling)))
         }
dev.off()

png(paste("figs/plot.resid/",
          paste(trait,set,type.filling,"residual.per.ecocode.Tf.png",sep="."),sep=""),
    width = 1500, height = 1000)
          
ecoregion <- ecoregions[1]
try(list.resid.data <- fun.load.data.for.residual(trait,set,ecoregion,type.filling))
try(with(list.resid.data,fun.plot.residual.plus.regression.lines.Tf(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling)))

          for (ecoregion in ecoregions[-1]){
try(list.resid.data <- fun.load.data.for.residual(trait,set,ecoregion,type.filling))
try(with(list.resid.data,fun.points.residual.plus.regression.lines.Tf(df.lmer,res.fix.simple,
                                         ERcomp,ADcomp,trait,set,ecoregion,
                                         type.filling)))
         }
dev.off()


   }
 }     
}



for (set in c("BCI","Canada","France","Fushan","NVS","Paracou","Spain","US","Swiss","Sweden","NSW","Mbaiki","Luquillo","Japan")){#
274
275
    ## 
plot.residual.for.set.all.traits(set)
276
plot.residual.for.set.all.traits.per.ecoregion(set)
277
278
279
280
}    


### TODO NEED TO PLOT ALL REGRESSION LINES OF ALL SET ON SAME FIGURE FOR EACH TRAIT AND SEE WHY NOT GOING IN SAME DIRECTION