-
Notifications
You must be signed in to change notification settings - Fork 2
/
3.3_RUNNING_MLPE_LME.R
519 lines (413 loc) · 15.6 KB
/
3.3_RUNNING_MLPE_LME.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
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
274
275
276
277
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
303
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
331
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
##################### LANDSCAPE GENOMICS TUTORIAL ##################
######################## STEP 03: MLPE MODELS ######################
### Script prepared by Jeronymo Dalapicolla, Jamille C. Veiga, Carolina S. Carvalho, Luciana C. Resende-Moreira, and Rodolfo Jaffé ###
##### PRE-ANALYSIS -------
#AIM = RUNNING MIXED MODELS TO IBR AND CALCULATE IMPORTANCE OF VARIABLES
### Load packages
library(corMLPE)
library(nlme)
library(MuMIn)
library(fmsb)
library(GeNetIt)
library(tidyverse)
library(r2glmm)
library(snow)
library(parallel)
library(GGally)
library(reshape2)
library(gridExtra)
library(RColorBrewer)
library(ggradar) #devtools::install_github("ricardo-bion/ggradar", dependencies = TRUE)
library(scales)
### Load auxiliary functions:
## Check max correlation
max.r <- function(x){
if(class(x)[length(class(x))] == "lm"){
corm <- summary(x, correlation=TRUE)$correlation}
else if(class(x) =="lmerMod"){
corm <- cov2cor(vcov(x))}
else if(class(x) =="lmerModLmerTest"){
corm <- cov2cor(vcov(x))}
else if(class(x) =="glmerMod"){
corm <- cov2cor(vcov(x))}
else if(class(x)=="gls"){
corm <- summary(x)$corBeta}
else if(class(x)=="lme"){
corm <- summary(x)$corFixed}
else { print("Error: Invalid model class")}
corm <- as.matrix(corm)
if (length(corm)==1){
corm <- 0
max(abs(corm))
} else if (length(corm)==4){
cormf <- corm[2:nrow(corm),2:ncol(corm)]
cormf <- 0
max(abs(cormf))
} else {
cormf <- corm[2:nrow(corm),2:ncol(corm)]
diag(cormf) <- 0
max(abs(cormf))
}
}
# Calculate decorrelated model residuals for GLS objects:
decorltd_res_gls = function(object){
val <- object[["residuals"]]
fit <- fitted(object)
rawfit <- fit
raw <- resid(object) + fit
fit <- fit/attr(val, "std")
val <- val/attr(val, "std")
cSt <- object$modelStruct$corStruct
val <- corMLPE:::recalc.corMLPE(cSt, list(Xy = as.matrix(val)))$Xy
return(data.frame(pr=fit+val,fit=fit,raw=raw,rawfit=rawfit))
}
# Calculate decorrelated model residuals for LME objects
decorltd_res_lme = function(object){
object$fitted
object$residuals
val <- object[["residuals"]]
fit <- as.data.frame(object$fitted)$fixed
rawfit <- fit
raw <- resid(object) + fit
fit <- fit/attr(val, "std")
val <- val/attr(val, "std")
cSt <- object$modelStruct$corStruct
val <- corMLPE:::recalc.corMLPE(cSt, list(Xy = as.matrix(val)))$Xy
return(data.frame(pr=fit+val,fit=fit,raw=raw,rawfit=rawfit))
}
##### ANALYSIS
##### 1. LOAD FILES --------------
#A. Load MLPE table:
mlpe_steerei = read.csv("Metafiles/MLPE_table_steerei.csv", row.names = 1)
head(mlpe_steerei)
#B. Scale predictors/variables for use in models
mlpe_steerei[, 5:(length(mlpe_steerei)-1)] = as.data.frame(scale(mlpe_steerei[, 5:(length(mlpe_steerei)-1)]))
head(mlpe_steerei)
#C. Creating formula for MLPE:
#select variables for models:
vars_mlpe_st = names(mlpe_steerei)[5:(length(mlpe_steerei)-1)]
vars_mlpe_st
#use REL distance plus varaibles of your choice keeping one for geographic distance (Null Model):
vars_mlpe_st_rel = vars_mlpe_st[c(1:5)]
mlpe_formula_rel_st = as.formula(paste("RELgen ~ ",paste(vars_mlpe_st_rel, collapse = " + "),sep = ""))
mlpe_formula_rel_st #5 variables
##### 2. CREATE A CLUSTER TO RUN MLPE IN PARALLEL ------
#A. Make cluster
cluster = makeCluster(4, type = "SOCK") ## also need snow installed
#B. Use clusterExport to send data to global environment (aka ‘workspace’) of each node.
clusterExport(cluster,"mlpe_steerei")
#C. Load packages in the cluster
clusterEvalQ(cluster,
c(library(nlme), library(MuMIn), library(corMLPE)))
##### 3. RUNNING FULL MODEL -----------
#A. Full model:
Fullmodel = nlme::lme(mlpe_formula_rel_st,
random = ~1|POP,
correlation = corMLPE(form = ~ from_ID + to_ID),
data = mlpe_steerei,
method = "ML") #For model selection is ML
#B. Check model:
summary(Fullmodel)
RES <- residuals(Fullmodel, type="normalized")
FIT <- fitted(Fullmodel)
plot(FIT, RES) ; abline(0,0, col="red") #OK
acf(RES) ##there small or none spatial autocorrelation in residuals
##### 4. RUN MODEL SELECTION WITH PARALLELIZED DREDGE ----
#A. Check maximum correlation
max.r(Fullmodel) ## 0.9529138
#B. Specify the number of predictor variables and including the max.r function. 15 observations to each predictor:
max_var = length(mlpe_steerei[,1])/15
max_var #11
nrow(mlpe_steerei) ## 171
options(na.action = na.fail)
#C. Run pdredge:
start_time = Sys.time()
Allmodels = MuMIn::pdredge(Fullmodel, rank = "AIC",
m.lim=c(0, 11), extra = c(max.r), cluster)
end_time = Sys.time()
#D. Compute run time
start_time1 = start_time
end_time1 = end_time
diff1 = end_time - start_time
diff1 ##2.72156 secs
#E. Number of models
nrow(Allmodels) ## 32
#F. Save and load All Models:
save(Allmodels, file="./Results/steerei/FullModel/Allmodels_steerei.RData")
load(file = "./Results/steerei/FullModel/Allmodels_steerei.RData")
#G. Retrieve Not Colinearity Models, with max.r <=0.6. Get Not colinearity Models using cluster too!
NCM = get.models(Allmodels, subset = max.r <= 0.6, cluster)
#H. Number of Not colinear models
length(NCM) #7
#I. Save and load Not Colinear models
save(NCM, file="./Results/steerei/FullModel/NCM_steerei.RData")
load(file="./Results/steerei/FullModel/NCM_steerei.RData")
#J. Select Best Models using AIC
BM = model.sel(NCM, rank=AIC)
nrow(BM) #7
#Check best models
best_models = BM[BM$delta <= 2, ]
best_models
#K. Save and load Model selection
save(BM, file="./Results/steerei/FullModel/BM_steerei.RData")
load(file="./Results/steerei/FullModel/BM_steerei.RData")
#L. Save model selection as table/dataframe
df_model_sel = as.data.frame(BM)
head(df_model_sel)
write.csv(df_model_sel, "./Results/steerei/FullModel/ModelsSelection_AIC_steerei.csv", row.names = F)
#M. Save best models tables
as.data.frame(best_models)
write.csv(as.data.frame(best_models), "./Results/steerei/FullModel/BestModels_AIC_steerei.csv", row.names = F)
#####5. REFIT BEST MODELS USING REML AND MODEL VALIDATION ----
#A. TopM1 ----
TopM1 = get.models(BM, subset = 1)[[1]]
summary(TopM1)
intervals(TopM1)
TopM1
#B. Refit TopM1
Refit_TopM1 <- nlme::lme(RELgen ~ riverdistance + wetlands_L,
random = ~1|POP,
correlation = corMLPE(form = ~ from_ID + to_ID),
data = mlpe_steerei,
method = "REML") #get accurated estimates
summary(Refit_TopM1)
#C. Check autocorrelation of residuals
RES <- residuals(Refit_TopM1, type="normalized")
FIT <- fitted(Refit_TopM1)
plot(FIT, RES) ; abline(0,0, col="red") #ok
acf(RES) #ok
#D. Plot predictors x residuals
plot(mlpe_steerei$riverdistance, RES) ; abline(0,0, col="red") #ok
plot(mlpe_steerei$wetlands_L, RES) ; abline(0,0, col="red") #ok
#E. Save ACF graph:
acf(resid(Refit_TopM1,type='normalized')) ## Spatial dependence pattern
#Save graphs
pdf("Results/steerei/Figures/refit_ACF_LME_steerei_TopM1.pdf", onefile = T)
acf(resid(Refit_TopM1,type='normalized'))
dev.off()
#F. Summary for Best Models:
summ_st = as.data.frame(capture.output(summary(Refit_TopM1)))
write.csv(summ_st, file="Results/steerei/FullModel/refit_Summary_AjustedBestModels_steerei.csv", row.names = TRUE)
#G. Save as Rdata:
save(Refit_TopM1, file="./Results/steerei/FullModel/refit_REML_TopM1_steerei.RData")
##### 8. COEFFICIENT OF DETERMINATION R² -----
#Not applicable in GLS models, only LME objects
#ML models and REML models show differences < 0.01 or <1%. I chose the REML for comaparison with Estimates.
#A. RΣ2, the proportion of generalized variance explained by the fixed predictors - Jaeger et al. (2017)
r2_steerei = r2beta(Refit_TopM1, method = 'sgv', partial = 'TRUE')
r2_steerei
# Effect Rsq upper.CL lower.CL
#1 Model 0.668 0.732 0.599
#2 riverdistance 0.565 0.645 0.479
#3 wetlands_L 0.099 0.194 0.031
#B. Nakagawa and Schielzeth (2013)
#marginal R² statistic = variance explained by fixed effects
#conditional R² statistic variance explained by the entire model (fixed & random effects)
r2beta(Refit_TopM1, method = 'nsj', partial = 'TRUE')
#C. Based on Nakagawa et al. (2017)
MuMIn::r.squaredGLMM(Refit_TopM1)
# R2m R2c
# 0.06557444 0.8566096
#D. Save the results
write.csv(as.data.frame(r2_steerei), row.names=TRUE, file="Results/steerei/FullModel/refit_BestModel_PartialR2_REML_steerei.csv")
#### 9. PLOT RADAR CHART FOR IMPORTANCE VARIABLES ----
#A. Load if it is necessary:
load(file="./Results/steerei/FullModel/BM_steerei.RData")
#A. Calculate variable importance using best models. Need to be more than 1 model. Not used in this case, because the results will be the same than using uncorrelated models.
impor_best = importance(BM[BM$delta <= 2, ])
impor_best
write.csv(impor_best, "./Results/steerei/FullModel/ImportanceVariables_Best_steerei.csv")
#B. Create a dataframe for ploting results:
impor_best
impor_best2 = impor_best
df = as.data.frame(impor_best2)
df[nrow(df) + 1,] = 0
df[nrow(df) + 1,] = 0
df[nrow(df) + 1,] = 0
row.names(df)[5:7] = c("wetlands_VH", "topography", "eucl_dist")
df = as.data.frame(t(df))
group = c("Best Models")
df = cbind(group,df)
df
#C. Plot radar:
plot_radar =
ggradar(df,
base.size = 2,
values.radar = c("0%", "50%", "100%"),
axis.labels = c( "PET", "Precipitation", "River", "Temperature", "Habitat", "Topography", "Euclidean"),
grid.min = 0,
grid.mid = 0.5,
grid.max = 1,
grid.label.size = 4,
axis.label.size = 5,
axis.line.colour = "black",
group.line.width = 1.5,
group.point.size = 5,
group.colours = c("blue"),
background.circle.colour = "grey",
background.circle.transparency = 0.1,
gridline.min.linetype = "dashed",
gridline.mid.linetype = "dashed",
gridline.max.linetype = "dashed",
gridline.min.colour = "black",
gridline.mid.colour = "black",
gridline.max.colour = "black",
legend.title = "",
legend.text.size = 12,
legend.position = "bottom"
)
#D. Verify radar:
plot_radar
#E. Save
pdf("./Results/steerei/Figures/RelativePredictorWeight_MLPE_AllVars_steerei.pdf")
plot_radar
dev.off()
#F. Calculate variable importance using uncorrlebest models. Need to be more than 1 model:
impor_best = importance(BM)
impor_best
write.csv(impor_best, "./Results/steerei/FullModel/ImportanceVariables_Best_steerei.csv")
#G. Create a dataframe for ploting results:
impor_best
impor_best2 = impor_best
df = as.data.frame(impor_best2)
df = as.data.frame(t(df))
group = c("Uncorrelated Models")
df = cbind(group,df)
df
#C. Reorder like simonsi:
#steerei order = c( "Habitat", "Productivity", "River", "Euclidean", "Topography")
names(df)
df = df[,c(1,3,6,2,5,4)]
#H. Plot radar:
plot_radar =
ggradar(df,
base.size = 2,
values.radar = c("0%", "50%", "100%"),
axis.labels = c( "Habitat", "Productivity", "River", "Euclidean", "Topography"),
grid.min = 0,
grid.mid = 0.5,
grid.max = 1,
grid.label.size = 4,
axis.label.size = 5,
axis.line.colour = "black",
group.line.width = 1.5,
group.point.size = 5,
group.colours = c("blue"),
background.circle.colour = "grey",
background.circle.transparency = 0.1,
gridline.min.linetype = "dashed",
gridline.mid.linetype = "dashed",
gridline.max.linetype = "dashed",
gridline.min.colour = "black",
gridline.mid.colour = "black",
gridline.max.colour = "black",
legend.title = "",
legend.text.size = 12,
legend.position = "bottom"
)
#I. Verify radar:
plot_radar
#J. Save
pdf("./Results/steerei/Figures/RelativePredictorWeight_MLPE_AllVars_steerei_uncorrelated.pdf")
plot_radar
dev.off()
#### 10. PLOT UNIVARIATES PREDICTORS USING BEST MODEL ----
#A. LOAD UNSCALED DATA
DT_unscaled = read.csv("Metafiles/MLPE_table_steerei.csv", row.names = 1)
names(DT_unscaled)
#B. Verify variables in Best Model
TopM1
#riverdistance + wetlands_L
#C. Build model with River distance ----
M1 = nlme::lme(RELgen ~ riverdistance,
random = ~ 1|POP,
correlation = corMLPE(form = ~ from_ID + to_ID),
data = DT_unscaled, method = "REML")
# Decorrelate residulas
dec_resids = decorltd_res_lme(M1)
#dec_resids$dist_interval = DT_unscaled$dist_interval
dec_resids$geoDist = DT_unscaled$eucl_dist
# Change covar in dataframe to plot
cov = DT_unscaled$riverdistance
df2plot = as_tibble(data.frame(dec_resids, covar = cov))
names(df2plot)
## Plot relationship
plotA = ggplot(df2plot,
aes(x = covar, y = pr.fixed)) +
geom_point(aes(y=pr.fixed), alpha=0.3, size = 4) +
geom_line(aes(y=fit), size=1) +
geom_rug(sides = "b", alpha = 0.02) +
ylab("Relatedness (decorrelated)") +
annotate("text", x=c(5), y=c(15),label=c(""), size=7) +
xlab("River Distance") +
theme_bw() +
theme(axis.title.y = element_text(size=15, color = "black", face = "bold"),
axis.title.x = element_text(size=15, color = "black", face = "bold")) +
theme(axis.text = element_text(face = "bold", color = "black", size = 15))
#Verify
plotA
pdf("./Results/steerei/Figures/REL_deco_RiverDistance_steerei.pdf")
plotA
dev.off()
#D. Build model with Habitat ----
M2 = nlme::lme(RELgen ~ wetlands_L,
random = ~ 1|POP,
correlation = corMLPE(form = ~ from_ID + to_ID),
data = DT_unscaled, method = "REML")
# Decorrelate residulas
dec_resids = decorltd_res_lme(M2)
#dec_resids$dist_interval = DT_unscaled$dist_interval
dec_resids$geoDist = DT_unscaled$eucl_dist
# Change covar in dataframe to plot
cov = DT_unscaled$wetlands_L
df2plot = as_tibble(data.frame(dec_resids, covar = cov))
names(df2plot)
# Plot relationship
plotB = ggplot(df2plot,
aes(x = covar, y = pr.fixed)) +
geom_point(aes(y=pr.fixed), alpha=0.3, size = 4) +
geom_line(aes(y=fit), size=1) +
geom_rug(sides = "b", alpha = 0.02) +
ylab("Relatedness (decorrelated)") +
annotate("text", x=c(5), y=c(15),label=c(""), size=7) +
xlab("Habitat Resistance") +
theme_bw() +
theme(axis.title.y = element_text(size=15, color = "black", face = "bold"),
axis.title.x = element_text(size=15, color = "black", face = "bold")) +
theme(axis.text = element_text(face = "bold", color = "black", size = 15))
#Verify
plotB
pdf("./Results/steerei/Figures/REL_deco_Habitat_steerei.pdf")
plotB
dev.off()
#E. Build model with Eucliadean Distance ----
M4 = nlme::lme(RELgen ~ eucl_dist,
random = ~ 1|POP,
correlation = corMLPE(form = ~ from_ID + to_ID),
data = DT_unscaled, method = "REML")
## Decorrelate residulas
dec_resids = decorltd_res_lme(M4)
#dec_resids$dist_interval = DT_unscaled$dist_interval
dec_resids$geoDist = DT_unscaled$eucl_dist
## Change covar in dataframe to plot
cov = DT_unscaled$eucl_dist
df2plot = as_tibble(data.frame(dec_resids, covar = cov))
names(df2plot)
## Plot relationship
plotD = ggplot(df2plot,
aes(x = covar, y = pr.fixed)) +
geom_point(aes(y=pr.fixed), alpha=0.3, size = 4) +
geom_line(aes(y=fit), size=1) +
geom_rug(sides = "b", alpha = 0.02) +
ylab("Relatedness (decorrelated)") +
annotate("text", x=c(5), y=c(15),label=c(""), size=7) +
xlab("Euclidean Distance") +
theme_bw() +
theme(axis.title.y = element_text(size=15, color = "black", face = "bold"),
axis.title.x = element_text(size=15, color = "black", face = "bold")) +
theme(axis.text = element_text(face = "bold", color = "black", size = 15))
plotD
pdf("./Results/steerei/Figures/REL_deco_EuclideanDis_steerei.pdf")
plotD
dev.off()
#END