-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMCFN_BRTmodels.R
1184 lines (980 loc) · 45.4 KB
/
MCFN_BRTmodels.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(raster)
library(dismo)
library(rpart)
library(maptools)
library(data.table)
library(rgdal)
library(dplyr)
library(blockCV)
library(gbm)
library(viridis)
library(beepr)
library(sp)
library(rgdal)
load("D:/MCFN/BRT outputs/out1.RData")
#load data ####
load("D:/MCFN/BRTdata_pack.RData")
# prepare list with all data ####
list4 <- list(NA)
for (k in 1:length(list3)){
list5<-list(NA)
for(i in 1:length(list3[[k]]$speclist)){
specoff <- filter(OFF_ALL, SPECIES==as.character(list3[[k]]$speclist[i]))
specoff <- distinct(specoff)
specdat2001 <- filter(list3[[k]]$MCFNPC2001, SPECIES == as.character(list3[[k]]$speclist[i]))
x1 <- try(specdat2001x <- aggregate(specdat2001$ABUND,by=list("PKEY"=specdat2001$PKEY,"SS"=specdat2001$SS), FUN=sum))
if(class(x1)!="try-error"){
names(specdat2001x)[3] <- "ABUND"
dat1 <- right_join(specdat2001x,list3[[k]]$survey2001[,1:3],by=c("SS","PKEY"))
dat1$SPECIES <- as.character(list3[[k]]$speclist[i])
dat1$ABUND <- as.integer(ifelse(is.na(dat1$ABUND),0,dat1$ABUND))
s2001 <- left_join(dat1,specoff, by=c("SPECIES","PKEY"))
d2001 <- left_join(s2001, list3[[k]]$dat2001, by=c("SS"))
}
if(class(x1)=="try-error"){
dat1<-cbind(list3[[k]]$survey2001[,1:2],data.frame(ABUND=rep(0,nrow(list3[[k]]$survey2001))),data.frame(PCODE=list3[[k]]$survey2001[,3]),data.frame(SPECIES=rep(as.character(list3[[k]]$speclist[i]),nrow(list3[[k]]$survey2001))))
s2001 <- left_join(dat1,specoff, by=c("SPECIES","PKEY"))
d2001 <- left_join(s2001, list3[[k]]$dat2001, by=c("SS"))
}
specdat2011 <- filter(list3[[k]]$MCFNPC2011, SPECIES == as.character(list3[[k]]$speclist[i]))
x2 <- try(specdat2011x <- aggregate(specdat2011$ABUND,by=list("PKEY"=specdat2011$PKEY,"SS"=specdat2011$SS), FUN=sum))
if(class(x2)!="try-error"){
names(specdat2011x)[3] <- "ABUND"
dat1 <- right_join(specdat2011x,list3[[k]]$survey2011[,1:3],by=c("SS","PKEY"))
dat1$SPECIES <- as.character(list3[[k]]$speclist[i])
dat1$ABUND <- as.integer(ifelse(is.na(dat1$ABUND),0,dat1$ABUND))
s2011 <- left_join(dat1,specoff, by=c("SPECIES","PKEY"))
d2011 <- left_join(s2011, list3[[k]]$dat2011, by=c("SS"))
}
if(class(x2)=="try-error"){
dat1<-cbind(list3[[k]]$survey2011[,1:2],data.frame(ABUND=rep(0,nrow(list3[[k]]$survey2011))),data.frame(PCODE=list3[[k]]$survey2011[,3]),data.frame(SPECIES=rep(as.character(list3[[k]]$speclist[i]),nrow(list3[[k]]$survey2011))))
s2011 <- left_join(dat1,specoff, by=c("SPECIES","PKEY"))
d2011 <- left_join(s2011, list3[[k]]$dat2011, by=c("SS"))
}
datcombo<-rbind(d2001,d2011)
datcombo<-datcombo[which(!is.na(datcombo$wt)),]
list5[[i]]<-datcombo
names(list5)[[i]]<- paste0("datcombo.",as.character(list3[[k]]$speclist[i]))
}
list4[[k]]<-list5
rm(list5)
names(list4)[[k]]<-paste0("datcombo",names(list3)[[k]])
}
beep(sound = 4, expr = NULL)
rm(list=setdiff(ls(),c("LCC","list3","list4","pred_MCFN_mask")))
gc()
# check sites with >0 detections per spp and buffer
lapply(list4$datcomboMCFNSS0,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS50000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS100000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS150000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS200000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS250000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS300000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS350000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS400000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS450000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS500000,function(x){nrow(subset(x,ABUND>0))})
lapply(list4$datcomboMCFNSS0,nrow)
lapply(list4$datcomboMCFNSS50000,nrow)
lapply(list4$datcomboMCFNSS100000,nrow)
lapply(list4$datcomboMCFNSS150000,nrow)
lapply(list4$datcomboMCFNSS200000,nrow)
lapply(list4$datcomboMCFNSS250000,nrow)
lapply(list4$datcomboMCFNSS300000,nrow)
lapply(list4$datcomboMCFNSS350000,nrow)
lapply(list4$datcomboMCFNSS400000,nrow)
lapply(list4$datcomboMCFNSS450000,nrow)
lapply(list4$datcomboMCFNSS500000,nrow)
# extend prediction stack for better plotting
pred_MCFN_mask.ext<-extend(pred_MCFN_mask,100)
# A function that fits the BRT model ('gbm.step' from dismo package) on pre-defined folds, and saves outputs ####
brt_MCFN <- function(data=list4,buffer,species, pred.stack, seed = 1222, pred.variables ,output.folder, blocks=NULL, keep.out = TRUE, tc=3,lr=0.001,bf=0.5, save.points.shp=FALSE){
# Arguments for this function
## data: data.frame object containing data for model fitting
## pred.stack: the raster stack/brick used as prediction dataset
## pred.variables: a character vector giving the names of predictor variables that will be included in BRT models
## blocks: object resulting from 'spatialBlocks' function that contains classification of sample points into folds
## output.folder: path to output folder (if folder does not exist it is created)
## keep.out: logical, whether to keep the output in the workspace. both blocks object and the brt output are automatically saved in the output folder regardless
## tc: BRT tree complexity
## lr: BRT learning rate
## bf: BRT bag fraction
## save.points.shp: logical, whether to save survey points as a shapefile in output folder
# fit BRT models using pre-determined folds for
buffer<-as.character(buffer)
bufferlist<-c("0","50000","100000","150000","200000","250000","300000","350000","400000","450000","500000")
speclist<-c("CAWA","OSFL","RUBL","CONI")
dat<-data[[which(bufferlist==buffer)]]
datcombo<-dat[[which(speclist==species)]]
if(any(is.na(datcombo$logoffset))==T){
offset<-NULL
}
else{
offset<-datcombo$logoffset
}
if (is.null(blocks)){
folds<-NULL
n.folds<-10
}
else {
folds<-blocks$foldID
n.folds<-blocks$k
}
#fit models ####
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc,
learning.rate = lr,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc,
learning.rate = lr/10,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc,
learning.rate = lr/100,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc,
learning.rate = lr/1000,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc,
learning.rate = lr/10000,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc-1,
learning.rate = lr,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc-1,
learning.rate = lr/10,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc-1,
learning.rate = lr/100,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc-1,
learning.rate = lr/1000,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){#retry models that didn't converge with smaller learning rate
x1 <-
try(brt <-
gbm.step(
datcombo,
gbm.y = "ABUND",
gbm.x = pred.variables,
fold.vector = folds,
n.folds = n.folds,
family = "poisson",
tree.complexity = tc-1,
learning.rate = lr/10000,
bag.fraction = bf,
offset = offset,
site.weights = datcombo$wt,
keep.fold.models = T,
keep.fold.fit = T
))
}
if(class(x1)=="NULL"){
stop("Restart model with even smaller learning rate, or other predictors!")
}
# Define/create folders for storing outputs ####
if (class(x1) != "try-error") {
z <- output.folder
if (file.exists(z) == FALSE) {
dir.create(z, recursive = T)
}
if (is.null(blocks)){
save(brt, file = paste(z,buffer, species, "brtMCFN.R", sep = ""))
}
else {
save(blocks, file = paste(z,buffer, species, "blocks.R", sep = ""))
save(brt, file = paste(z,buffer, species, "brtAB.R", sep = ""))
}
## Model evaluation
varimp <- as.data.frame(brt$contributions)
write.csv(varimp, file = paste(z,buffer, species, "varimp.csv", sep = ""))
cvstats <- t(as.data.frame(brt$cv.statistics))
write.csv(cvstats, file = paste(z,buffer, species, "cvstats.csv", sep = ""))
pdf(paste(z,buffer, species, "_plot.pdf", sep = ""))
gbm.plot(
brt,
n.plots = length(pred.variables),
smooth = TRUE,
plot.layout = c(3, 3),
common.scale = T
)
dev.off()
pdf(paste(z,buffer, species, "_plot.var-scale.pdf", sep = ""))
gbm.plot(
brt,
n.plots = length(pred.variables),
smooth = TRUE,
plot.layout = c(3, 3),
common.scale = F,
write.title = F
)
dev.off()
## Model prediction
rast <-
predict(pred.stack,
brt,
type = "response",
n.trees = brt$n.trees)
writeRaster(
rast,
filename = paste(z,buffer, species, "_pred1km", sep = ""),
format = "GTiff",
overwrite = TRUE
)
data_sp <-SpatialPointsDataFrame(coords = datcombo[which(datcombo$ABUND>0),8:9], data = datcombo[which(datcombo$ABUND>0),], proj4string = LCC)
prev <- cellStats(rast, 'mean')
max <- 3*prev
png(paste(z,buffer, species, "_preds.png", sep = ""), height=600, width=850)
par(cex.main=1.8, mfcol=c(1,1), oma=c(0,0,0,0))
par(mar=c(0,0,5,0))
plot(rast, col=viridis(15)[15], axes=TRUE, legend=FALSE, main=paste0(species," ",buffer,"m buffer", " current prediction"))
plot(rast, col=viridis(15), zlim=c(0,max), axes=T, main="CAWA", add=TRUE, legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.83,0.87), axis.args=list(cex.axis=1.2))
text(350000,6850000,"Potential density (males/ha)", cex=1.3)
points(data_sp$X, data_sp$Y, cex = 1, col="red", pch=3)
dev.off()
if(save.points.shp==T){
writeOGR(
data_sp,
dsn = paste(z,buffer, "surveypoints.shp", sep = ""),
layer = "data_sp",
driver = "ESRI Shapefile"
)
}
if(keep.out==T) {return(brt)}
}
}
# assess SD among model predictors ####
SDtest<-apply(list4$datcomboMCFNSS0$datcombo.CONI[,16:62],2,sd, na.rm=T)
nonzeroSD<-names(list4$datcomboMCFNSS0$datcombo.CAWA[,16:62])[which(SDtest>0)]
# define prediction variables ####
pred.variables<-c(nonzeroSD,
"Structure_Biomass_TotalLiveAboveGround_v1",
"Structure_Stand_Age_v1",
"CTI_MCFN",
"water",
"AHM",
"DD18",
"MAT",
"MSP",
"FFP",
"TD"
)
# BRT models ####
list5<-list(NA)
bufferlist<-c("0","50000","100000","150000","200000","250000","300000","350000","400000","450000","500000")
speclist<-c("CAWA","OSFL","RUBL","CONI")
for(s in 1:length(speclist)){
list6<-list(NA)
for(b in 1:length(bufferlist)){
out.folder<-paste0("D:/MCFN/BRT outputs/",speclist[s],"/buffer_",bufferlist[b],"m/")
x1<- try(brt<-brt_MCFN(buffer=bufferlist[b],species = speclist[s], pred.variables=pred.variables, pred.stack = pred_MCFN_mask.ext, output.folder = out.folder,save.points.shp = TRUE,tc=3, lr=0.01 ))
if (class(x1)=="NULL"){
list6[[b]]<-NA
}
if (class(x1)=="try-error"){
list6[[b]]<-NA
}
if(class(x1)!="try-error"&&class(x1)!="NULL"){
list6[[b]]<-brt
}
names(list6)[[b]]<-paste0(speclist[s],"_buffer_",bufferlist[b],"m")
}
list5[[s]]<-list6
rm(list6)
names(list5)[[s]]<-speclist[s]
}
rm(list=setdiff(ls(),c("LCC","list3","list4","list5","pred_MCFN_mask","pred_MCFN_mask.ext","brt_MCFN")))
gc()
save.image("D:/MCFN/BRT outputs/out1.RData")
beep(sound = 4, expr = NULL)
# Compare model CV statistics to find best model (less bias in predictions) ####
compCVstats<- function(modlist, species){
library(ggplot2)
lsmod<-modlist[[species]]
if(any(is.na(lsmod))){
lsmod<-lsmod[-which(is.na(lsmod))]
}
# # Deviance
# df1<-as.data.frame(matrix(nrow=length(lsmod),ncol=3))
# colnames(df1)<-c("buffer","deviance","se")
# df1[,1]<-gsub("m","",gsub(paste0(species,"_buffer_"),"",names(lsmod)))
# df1[,1]<-factor(df1[,1],levels=df1[,1])
# df1[,2]<-unlist(lapply(lsmod,function(x){x$cv.statistics$deviance.mean}))
# df1[,3]<-unlist(lapply(lsmod,function(x){x$cv.statistics$deviance.se}))
# k1<-ggplot(df1, aes(buffer,deviance,ymin=deviance-se,ymax=deviance+se))
# k1+geom_pointrange()
# ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_deviance.pdf"))
# Deviance explained
df5<-as.data.frame(matrix(nrow=length(lsmod),ncol=3))
colnames(df5)<-c("buffer","deviance.explained","se")
df5[,1]<-gsub("m","",gsub(paste0(species,"_buffer_"),"",names(lsmod)))
df5[,1]<-factor(df5[,1],levels=df5[,1])
df5[,2]<-unlist(lapply(lsmod,function(x){(x$self.statistics$null - x$cv.statistics$deviance.mean)/x$self.statistics$null}))
df5[,3]<-unlist(lapply(lsmod,function(x){x$cv.statistics$deviance.se/x$self.statistics$null}))
k5<-ggplot(df5, aes(buffer,deviance.explained,ymin=deviance.explained-se,ymax=deviance.explained+se))
p1<-k5+geom_pointrange()+xlab("Buffer distance (km)")+scale_x_discrete(labels=as.numeric(levels(df5[,1]))/1000)+ylab("Deviance explained")
#ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_deviance_explained.pdf"))
# Correlation
df2<-as.data.frame(matrix(nrow=length(lsmod),ncol=3))
colnames(df2)<-c("buffer","correlation","se")
df2[,1]<-gsub("m","",gsub(paste0(species,"_buffer_"),"",names(lsmod)))
df2[,1]<-factor(df2[,1],levels=df2[,1])
df2[,2]<-unlist(lapply(lsmod,function(x){x$cv.statistics$correlation.mean}))
df2[,3]<-unlist(lapply(lsmod,function(x){x$cv.statistics$correlation.se}))
k2<-ggplot(df2, aes(buffer,correlation,ymin=correlation-se,ymax=correlation+se))
p2<-k2+geom_pointrange()+xlab("Buffer distance (km)")+scale_x_discrete(labels=as.numeric(levels(df2[,1]))/1000)+ylab("Correlation")
#ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_correlation.pdf"))
# Calibration: The first two statistics were the estimated intercepts and slopes of linear regression models of predictions against observations. The intercept measures the magnitude and direction of bias, with values close to 0 indicating low or no bias. The slope yields information about the consistency in the bias as a function of the mean, with a value of 1 indicating a consistent bias if the intercept is a nonzero value.
## intercept
df3<-as.data.frame(matrix(nrow=length(lsmod),ncol=3))
colnames(df3)<-c("buffer","calibration.intercept","se")
df3[,1]<-gsub("m","",gsub(paste0(species,"_buffer_"),"",names(lsmod)))
df3[,1]<-factor(df3[,1],levels=df3[,1])
df3[,2]<-unlist(lapply(lsmod,function(x){x$cv.statistics$calibration.mean[1]}))
df3[,3]<-unlist(lapply(lsmod,function(x){x$cv.statistics$calibration.se[1]}))
k3<-ggplot(df3, aes(buffer,calibration.intercept,ymin=calibration.intercept-se,ymax=calibration.intercept+se))+ylab("Calibration intercept")
p3<-k3+geom_pointrange()+xlab("Buffer distance (km)")+scale_x_discrete(labels=as.numeric(levels(df3[,1]))/1000)
#ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_calibration.intercept.pdf"))
## slope
df4<-as.data.frame(matrix(nrow=length(lsmod),ncol=3))
colnames(df4)<-c("buffer","calibration.slope","se")
df4[,1]<-gsub("m","",gsub(paste0(species,"_buffer_"),"",names(lsmod)))
df4[,1]<-factor(df4[,1],levels=df4[,1])
df4[,2]<-unlist(lapply(lsmod,function(x){x$cv.statistics$calibration.mean[2]}))
df4[,3]<-unlist(lapply(lsmod,function(x){x$cv.statistics$calibration.se[2]}))
k4<-ggplot(df4, aes(buffer,calibration.slope,ymin=calibration.slope-se,ymax=calibration.slope+se))
p4<-k4+geom_pointrange()+xlab("Buffer distance (km)")+scale_x_discrete(labels=as.numeric(levels(df4[,1]))/1000)+ylab("Calibration slope")
#ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_calibration.slope.pdf"))
p5<-grid.arrange(p1,p2,p3,p4,nrow=2,ncol=2)
ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_metrics.png"),plot=p5,width=16,height=16,units="cm")
#p5<-grid.arrange(p3,p4,nrow=1,ncol=2)
#ggsave(paste0("D:/MCFN/BRT outputs/", species, "/",species,"_CV_metrics.png"),plot=p5,width=16,height=8,units="cm")
}
compCVstats(list5,"CAWA")
compCVstats(list5,"OSFL")
compCVstats(list5,"CONI")
compCVstats(list5,"RUBL")
# Buffer0 model has very high calibration values , cannot distinguish values of other buffers in plot. re-run without buff0
list6<-list5
list6$CONI$CONI_buffer_0m<-NULL
compCVstats(list6,"CONI")
rm(list6)
gc()
compCVstats(list5,"RUBL")
# Bootstrap and CI for best model ####
boot_brt<-function(data,brtmodel,pred.data,nsamples=1000,output.folder){
z <- output.folder
if (file.exists(z) == FALSE) {
dir.create(z,recursive = TRUE)
}
for(i in 1:nsamples){
cat("loop",i,"\n") # this prints the loop number on console to track function progress
sample<-sample(1:nrow(data),size=nrow(data),replace=T)
data2<-data[sample,]
brt<-
gbm.fit(x = data2[,brtmodel$gbm.call$gbm.x],
y = data2[,brtmodel$gbm.call$gbm.y],
offset<-brtmodel$gbm.call$offset[sample],
distribution ="poisson",
w = data2$wt,
n.trees = brtmodel$n.trees,
interaction.depth = brtmodel$interaction.depth,
shrinkage = brtmodel$gbm.call$learning.rate,
bag.fraction = brtmodel$gbm.call$bag.fraction,
var.names = brtmodel$gbm.call$gbm.x,
response.name = brtmodel$gbm.call$gbm.y
)
rast <- predict(pred.data,
brt,
type = "response",
n.trees = brt$n.trees)
#stack <- addLayer(stack, rast)
#names(stack)[i+1]<-paste0("sample",i)
writeRaster(rast, filename=paste0(z, "sample",i,".tif"), format="GTiff",overwrite=TRUE)
rm(rast)
gc()
}
#fun0.05 <- function(x) {quantile(x, probs = 0.05, na.rm = TRUE)}
#lower<- calc(stack[[-1]],fun0.05)
#fun0.95 <- function(x) {quantile(x, probs = 0.95, na.rm = TRUE)}
#upper<- calc(stack[[-1]],fun0.95)
#writeRaster(lower, filename=paste0(z, " confint_lower.tif"), format="GTiff",overwrite=TRUE)
#writeRaster(upper, filename=paste0(z, " confint_upper.tif"), format="GTiff",overwrite=TRUE)
#return(stack)
}
#CAWA: buffer = 200000
start_time <- Sys.time()
boot_brt(data=list4$datcomboMCFNSS200000$datcombo.CAWA,brtmodel=list5$CAWA$CAWA_buffer_200000m,pred.data=pred_MCFN_mask,nsamples=500,output.folder = "D:/MCFN/BRT outputs/CAWA/buffer_200000m/confint/")
end_time <- Sys.time()
end_time - start_time
#OSFL: buffer = 100000
boot_brt(data=list4$datcomboMCFNSS100000$datcombo.OSFL,brtmodel=list5$OSFL$OSFL_buffer_100000m,pred.data=pred_MCFN_mask,nsamples=500,output.folder = "D:/MCFN/BRT outputs/OSFL/buffer_100000m/confint/")
#CONI
boot_brt(data=list4$datcomboMCFNSS450000$datcombo.CONI,brtmodel=list5$CONI$CONI_buffer_450000m,pred.data=pred_MCFN_mask,nsamples=500,output.folder = "D:/MCFN/BRT outputs/CONI/buffer_450000m/confint/")
#RUBL
boot_brt(data=list4$datcomboMCFNSS500000$datcombo.RUBL,brtmodel=list5$RUBL$RUBL_buffer_500000m,pred.data=pred_MCFN_mask,nsamples=500,output.folder = "D:/MCFN/BRT outputs/RUBL/buffer_500000m/confint/")
save.image("D:/MCFN/BRT outputs/out1.RData")
# Calculate CI based on quantiles for each model, calculate SE and map them ####
CI_SE_CV_plotfun<-function(species,buffer){
fun0.05 <- function(x) {quantile(x, probs = 0.05, na.rm = TRUE)}
fun0.95 <- function(x) {quantile(x, probs = 0.95, na.rm = TRUE)}
path<-paste0("D:/MCFN/BRT outputs/",species,"/buffer_",buffer,"m/confint/")
confintBRT<-brick(paste0(path,"sample1.tif"))
files <- list.files(path,pattern="tif$")
setwd(path)
for (i in 2:length(files)) { confintBRT <- addLayer(confintBRT, raster(files[i]))}
names(confintBRT)[1:500] <- paste0("sample",1:500)
lower<- calc(confintBRT,fun0.05)
upper<- calc(confintBRT,fun0.95)
SE<- calc(confintBRT,sd)
CV<-calc(confintBRT,function(x){sd(x)/mean(x)})
#plot(SE)
writeRaster(SE, filename=paste0(path, "SE.tif"), format="GTiff",overwrite=TRUE)
writeRaster(CV, filename=paste0(path, "CV.tif"), format="GTiff",overwrite=TRUE)
writeRaster(lower, filename=paste0(path, "90CIlower.tif"), format="GTiff",overwrite=TRUE)
writeRaster(upper, filename=paste0(path, "90CIupper.tif"), format="GTiff",overwrite=TRUE)
}
start_time2 <- Sys.time()
CI_SE_CV_plotfun("CAWA","200000")
end_time2 <- Sys.time()
end_time2- start_time2
beep(sound = 4, expr = NULL)
start_time2 <- Sys.time()
CI_SE_CV_plotfun("OSFL","100000")
end_time2 <- Sys.time()
end_time2- start_time2
beep(sound = 4, expr = NULL)
start_time2 <- Sys.time()
CI_SE_CV_plotfun("CONI","450000")
end_time2 <- Sys.time()
end_time2- start_time2
beep(sound = 4, expr = NULL)
start_time2 <- Sys.time()
CI_SE_CV_plotfun("RUBL","500000")
end_time2 <- Sys.time()
end_time2- start_time2
beep(sound = 4, expr = NULL)
save.image("D:/MCFN/BRT outputs/out1.RData")
beep(sound = 4, expr = NULL)
# Plot predictions from best model for each spp ####
pred_MCFN_mask.ext<-extend(brick("D:/MCFN/rasters/Prediction dataset/pred_MCFN_mask"),100)
plot_preds<-function(species,buffer,modlist=list5,addpoints=T,SD=FALSE, CV=TRUE){
lsmod<-modlist[[species]]
bufferlist<-c("0","50000","100000","150000","200000","250000","300000","350000","400000","450000","500000")
mod<-lsmod[[which(bufferlist==buffer)]]
rast<-predict(pred_MCFN_mask.ext,
mod,
type = "response",
n.trees = mod$n.trees)
prev <- cellStats(rast, 'mean')
max <- 3*prev
png(file=paste0("D:/MCFN/BRT outputs/",species,"/preds_",species,"_",buffer,".png"), height=600, width=850)
par(cex.main=1.8, mfcol=c(1,1), oma=c(0,0,0,0))
par(mar=c(0,0,5,0))
plot(rast, col=viridis(15)[15], axes=FALSE,box=FALSE, legend=FALSE, main=paste0(species," ",as.numeric(buffer)/1000," Km buffer"))
plot(rast, col=viridis(15), zlim=c(0,max), axes=FALSE,box=FALSE, main=species, add=TRUE, legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.83,0.87), axis.args=list(cex.axis=1.2))
text(1170000,6980000,"Potential density (males/ha)", cex=1.3)
if(addpoints==TRUE){
speclist<-c("CAWA","OSFL","RUBL","CONI")
dat<-list4$datcomboMCFNSS0
datcombo<-dat[[which(speclist==species)]]
if(mean(datcombo$ABUND)==0){
datcombo<-dat[[which(speclist=="CAWA")]]# any species here will do
data_sp <-SpatialPointsDataFrame(coords = datcombo[,c("X","Y")], data = datcombo, proj4string = LCC)
points(data_sp$X, data_sp$Y, cex = 1, col=1, pch=4)
}
else{
data_sp <-SpatialPointsDataFrame(coords = datcombo[,c("X","Y")], data = datcombo, proj4string = LCC)
points(data_sp$X[data_sp$ABUND==0], data_sp$Y[data_sp$ABUND==0], cex = 1, col=1, pch=4)
points(data_sp$X[data_sp$ABUND>0], data_sp$Y[data_sp$ABUND>0], cex = 1, col=2, pch=4)
}
}
dev.off()
magma2 <- colorRampPalette(rev(magma(15)), space="Lab", bias=2)
if (SD==TRUE){
SD<-raster(paste0("D:/MCFN/BRT outputs/",species,"/buffer_",buffer,"m/confint/SE.tif"))
SD_ext<-extend(SD,100)
png(file=paste0("D:/MCFN/BRT outputs/",species,"/SE_",species,"_",buffer,".png"), height=600, width=850)
par(cex.main=1.8, mfcol=c(1,1), oma=c(0,0,0,0))
par(mar=c(0,0,5,0))
plot(SD_ext, col=rev(magma(15))[15], axes=TRUE, legend=FALSE, main=paste0(species," ",as.numeric(buffer)/1000," Km buffer"))
plot(SD_ext, col=rev(magma(15)), axes=T, main=paste0(species," ",buffer,"m buffer SD"), legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.83,0.87), axis.args=list(cex.axis=1.2),add=T)
text(1170000,6980000,"Density SE", cex=1.3)
dev.off()
png(file=paste0("D:/MCFN/BRT outputs/",species,"/SE_",species,"_",buffer,"_2.png"), height=600, width=850)
par(cex.main=1.8, mfcol=c(1,1), oma=c(0,0,0,0))
par(mar=c(0,0,5,0))
plot(SD_ext, col=magma2(15)[15], axes=TRUE, legend=FALSE, main=paste0(species," ",as.numeric(buffer)/1000," Km buffer"))
plot(SD_ext, col=magma2(15), axes=T, main=paste0(species," ",buffer,"m buffer SD"), legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.83,0.87), axis.args=list(cex.axis=1.2),add=T)
text(1170000,6980000,"Density SE", cex=1.3)
dev.off()
}
if (CV==TRUE){
CV<-raster(paste0("D:/MCFN/BRT outputs/",species,"/buffer_",buffer,"m/confint/CV.tif"))
CV_ext<-extend(CV,100)
png(file=paste0("D:/MCFN/BRT outputs/",species,"/CV_",species,"_",buffer,".png"), height=600, width=850)
par(cex.main=1.8, mfcol=c(1,1), oma=c(0,0,0,0))
par(mar=c(0,0,5,0))
plot(CV_ext, col=rev(magma(15))[15], axes=TRUE, legend=FALSE, main=paste0(species," ",as.numeric(buffer)/1000," Km buffer"))
plot(CV_ext, col=rev(magma(15)), axes=T, main=paste0(species," ",buffer,"m buffer CV"), legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.83,0.87),axis.args=list(cex.axis=1.2),add=T)
text(1170000,6980000,"Density CV", cex=1.3)
dev.off()
png(file=paste0("D:/MCFN/BRT outputs/",species,"/CV_",species,"_",buffer,"_2.png"), height=600, width=850)
par(cex.main=1.8, mfcol=c(1,1), oma=c(0,0,0,0))
par(mar=c(0,0,5,0))
plot(CV_ext, col=magma2(15)[15], axes=TRUE, legend=FALSE, main=paste0(species," ",as.numeric(buffer)/1000," Km buffer"))
plot(CV_ext, col=magma2(15), axes=T, main=paste0(species," ",buffer,"m buffer CV"), legend.width=1.5, horizontal = TRUE,smallplot = c(0.65,0.90,0.83,0.87), axis.args=list(cex.axis=1.2),add=T)
text(1170000,6980000,"Density CV", cex=1.3)
dev.off()
}
}
plot_preds("CAWA","200000",SD=TRUE,CV=TRUE)
plot_preds("OSFL","100000",SD=TRUE,CV=TRUE)
plot_preds("CONI","450000",SD=TRUE,CV=TRUE)
plot_preds("RUBL","500000",SD=TRUE,CV=TRUE)
beep(sound = 4, expr = NULL)
plot_preds2<-function(species,buffer,modlist=list5,addpoints=T, wide=F){
lsmod<-modlist[[species]]
bufferlist<-c("0","50000","100000","150000","200000","250000","300000","350000","400000","450000","500000")
mod<-lsmod[[which(bufferlist==buffer)]]
rast<-predict(pred_MCFN_mask.ext,
mod,
type = "response",
n.trees = mod$n.trees)
prev <- cellStats(rast, 'mean')
max <- 3*prev
SD<-raster(paste0("D:/MCFN/BRT outputs/",species,"/buffer_",buffer,"m/confint/SE.tif"))
SD_ext<-extend(SD,100)
if(wide==TRUE){
height<-600
width<-850*2
mat<-matrix(c(1,2), 1, 2)
}
if(wide==F){
height<-600*2
width<-850
mat<-matrix(c(1,2), 2, 1)
}
png(file=paste0("D:/MCFN/BRT outputs/",species,"/preds_and_SD",species,"_",buffer,".png"), height=height, width=width)
layout(mat)
par(cex.main=1.8, oma=c(0,0,0,0),mar=c(0,0,0,0))
plot(rast, col=rev(viridis(15))[15], axes=F, legend=FALSE, main=NULL )
plot(rast, col=rev(viridis(15)), zlim=c(0,max), axes=F, main=NULL, add=TRUE, legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.92,0.96), axis.args=list(cex.axis=1.2))
text(1160000,6980000,"Potential density (males/ha)", cex=1.3)
text(710000,7000000, "A", cex=1.3, font=2)
if(addpoints==TRUE){
speclist<-c("CAWA","OSFL","RUBL","CONI")
dat<-list4$datcomboMCFNSS0
datcombo<-dat[[which(speclist==species)]]
if(mean(datcombo$ABUND)==0){
datcombo<-dat[[which(speclist=="CAWA")]]
data_sp <-SpatialPointsDataFrame(coords = datcombo[,c("X","Y")], data = datcombo, proj4string = LCC)
points(data_sp$X, data_sp$Y, cex = 1, col=1, pch=4)
}
else{
data_sp <-SpatialPointsDataFrame(coords = datcombo[,c("X","Y")], data = datcombo, proj4string = LCC)
points(data_sp$X[data_sp$ABUND==0], data_sp$Y[data_sp$ABUND==0], cex = 1, col=1, pch=4)
points(data_sp$X[data_sp$ABUND>0], data_sp$Y[data_sp$ABUND>0], cex = 1, col=2, pch=4)
}
}
plot(SD_ext, col=rev(magma(15))[15], axes=F, legend=FALSE, main=NULL)
plot(SD_ext, col=rev(magma(15)), axes=F, main=NULL, legend.width=1.5, horizontal = TRUE, smallplot = c(0.65,0.90,0.92,0.96), axis.args=list(cex.axis=1.2),add=T)
text(1170000,6980000,"Density SD", cex=1.3)
text(710000,7000000, "B", cex=1.3, font=2)
dev.off()
}
plot_preds2("CAWA","200000")
beep(sound = 4, expr = NULL)
plot_preds2("OSFL","100000")
plot_preds2("CONI","450000")
plot_preds2("RUBL","500000")
beep(sound = 4, expr = NULL)
save.image("D:/MCFN/BRT outputs/out1.RData")
beep(sound = 4, expr = NULL)
# Summarize predictions per land cover type ####
NALCC2005<-raster("M:/DataStuff/SpatialData/LCC05_NALCMS2010/Land_Cover_MXD/NA_LandCover_2005/data/NA_LandCover_2005/NA_LandCover_2005_LCC.img")
NALCC2005<-projectRaster(NALCC2005,pred_MCFN_mask.ext[[1]], method="ngb")
MCFN_LCC<- crop(NALCC2005,pred_MCFN_mask.ext[[1]])
MCFN_LCC<- mask(MCFN_LCC,pred_MCFN_mask.ext[[1]])
plot(MCFN_LCC)
ltnalc <- read.csv("C:/Users/voeroesd/Documents/Repos/bamanalytics/lookup/nalcms.csv")
# CAWA
mod<-list5$CAWA$CAWA_buffer_200000m
rast<-predict(pred_MCFN_mask.ext,
mod,
type = "response",
n.trees = mod$n.trees)
## log mean density boxplots
dens<-data.frame(Value=getValues(MCFN_LCC),Density=getValues(rast))
dens<-dens[!is.na(dens),]
dens$class<-ltnalc$Label[match(dens$Value, ltnalc$Value)]
dens<-dens[!is.na(dens$class),]
#png(filename = paste0("D:/MCFN/BRT outputs/CAWA/logmean_boxplots.png"), height=600, width=850)
p1<-ggplot(dens,aes(x=class,y=log(Density)))+
#geom_bar(aes(fill=Habitat),width=hab_dens$area2, stat="identity")+
#geom_errorbar(aes(ymin=mean.Density-sd.Density,ymax=mean.Density-sd.Density),width=0.2)+
geom_boxplot(aes(fill=class))+
xlab("Land cover class")+
ylab("Log mean density (males/ha)")+
theme(legend.position = "none", axis.title.x = element_blank())+
geom_text(x=9.3,y=0, label="A")
#dev.off()
## median density barplots w/ variable width
dens<-data.frame(Value=getValues(MCFN_LCC),Density=getValues(rast))
dens<-dens[!is.na(dens),]
dens$class<-ltnalc$Label[match(dens$Value, ltnalc$Value)]
medianDens<-aggregate(dens$Density,by=list(dens$class), FUN=median,na.rm=T)
areaHab<-aggregate(dens$Density,by=list(dens$class), FUN=function(x){6.25* length(x)})
hab_dens<-data.frame(Habitat = medianDens$Group.1,median.Density=medianDens$x,areaHab=areaHab$x/sum(areaHab$x)) # use median instead
hab_dens$area2<-hab_dens$areaHab
hab_dens$area2[which(hab_dens$areaHab<0.01)]<-0.01
hab_dens$labels<-as.character(round(hab_dens$areaHab,digits=2))
hab_dens$labels[which(hab_dens$areaHab<0.01)]<-"<0.01"
p2<-ggplot(hab_dens,aes(x=Habitat,y=median.Density))+
geom_bar(aes(fill=Habitat),width=hab_dens$area2, stat="identity")+
xlab("Land cover class")+
ylab("Median density (males/ha)")+
theme(legend.position = "none")+
geom_text(aes(label=hab_dens$labels),nudge_y = 0.01,size=2)+
geom_text(x=9,y=0.18,label="B")
hab_dens2<-hab_dens[-1,] #This to plot all habitats except agriculture
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
p3<-ggplot(hab_dens2,aes(x=Habitat,y=median.Density))+
geom_bar(aes(fill=Habitat),width=hab_dens2$area2, stat="identity")+
scale_fill_manual(values=gg_color_hue(9)[-1])+
xlab("Land cover class")+
ylab("Median density (males/ha)")+
theme(legend.position = "none")+
geom_text(aes(label=hab_dens2$labels),nudge_y = 0.0015,size=2)+
geom_text(x=8,y=0.022,label="C")
p4<-grid.arrange(p1,p2,p3,ncol=1)
ggsave(paste0("D:/MCFN/BRT outputs/CAWA/habitat_box_and_bar_plots.png"),plot=p4,width=16,height=20,units="cm")
#OSFL
mod<-list5$OSFL$OSFL_buffer_100000m
rast<-predict(pred_MCFN_mask.ext,
mod,
type = "response",
n.trees = mod$n.trees)
## log mean density boxplots
dens<-data.frame(Value=getValues(MCFN_LCC),Density=getValues(rast))
dens<-dens[!is.na(dens),]
dens$class<-ltnalc$Label[match(dens$Value, ltnalc$Value)]
dens<-dens[!is.na(dens$class),]
#png(filename = paste0("D:/MCFN/BRT outputs/OSFL/logmean_boxplots.png"), height=600, width=850)
p1<-ggplot(dens,aes(x=class,y=log(Density)))+
#geom_bar(aes(fill=Habitat),width=hab_dens$area2, stat="identity")+
#geom_errorbar(aes(ymin=mean.Density-sd.Density,ymax=mean.Density-sd.Density),width=0.2)+
geom_boxplot(aes(fill=class))+
xlab("Land cover class")+
ylab("Log mean density (males/ha)")+
theme(legend.position = "none", axis.title.x = element_blank())+
geom_text(x=9.3,y=0, label="A")
#dev.off()
## median density barplots w/ variable width
dens<-data.frame(Value=getValues(MCFN_LCC),Density=getValues(rast))
dens<-dens[!is.na(dens),]
dens$class<-ltnalc$Label[match(dens$Value, ltnalc$Value)]
medianDens<-aggregate(dens$Density,by=list(dens$class), FUN=median,na.rm=T)
areaHab<-aggregate(dens$Density,by=list(dens$class), FUN=function(x){6.25* length(x)})
hab_dens<-data.frame(Habitat = medianDens$Group.1,median.Density=medianDens$x,areaHab=areaHab$x/sum(areaHab$x)) # use median instead
hab_dens$area2<-hab_dens$areaHab
hab_dens$area2[which(hab_dens$areaHab<0.01)]<-0.01
hab_dens$labels<-as.character(round(hab_dens$areaHab,digits=2))
hab_dens$labels[which(hab_dens$areaHab<0.01)]<-"<0.01"
#png(filename = paste0("D:/MCFN/BRT outputs/OSFL/lc_median_denspreds.png"), height=600, width=850)
p2<-ggplot(hab_dens,aes(x=Habitat,y=median.Density))+