Mercurial > repos > ecology > ecoregion_taxa_seeker
comparison brt.R @ 3:18c4cd785a74 draft
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit e03df85746a3b61a382a5ee7e3357a8bf42a5097
author | ecology |
---|---|
date | Wed, 11 Sep 2024 09:20:14 +0000 |
parents | 9dc992f80c25 |
children |
comparison
equal
deleted
inserted
replaced
2:788b118910a7 | 3:18c4cd785a74 |
---|---|
1 #16/02/2023 | 1 #16/02/2023 |
2 ## Analyse BRT data | 2 ## Analyse BRT data |
3 | 3 |
4 ### Clean environment | 4 ### Clean environment |
5 rm(list = ls(all.names = TRUE)) | 5 rm(list = ls(all.names = TRUE)) |
6 options(warn=-1) | 6 options(warn=1) |
7 | 7 |
8 ### load packages | 8 ### load packages |
9 | 9 |
10 library(dismo, warn.conflicts = FALSE) | 10 library(dismo, warn.conflicts = FALSE) |
11 library(gbm, warn.conflicts = FALSE) | 11 library(gbm, warn.conflicts = FALSE) |
41 #Carbo,Grav,Maxbearing,Maxmagnit,Meancurmag,Meansal,Meantheta,Mud,Prof,Rugosity,Sand,Seaice_prod,Sili,Slope,Standcurmag,Standsal,Standtheta | 41 #Carbo,Grav,Maxbearing,Maxmagnit,Meancurmag,Meansal,Meantheta,Mud,Prof,Rugosity,Sand,Seaice_prod,Sili,Slope,Standcurmag,Standsal,Standtheta |
42 | 42 |
43 #Load functions | 43 #Load functions |
44 | 44 |
45 make.brt <- function(spe,data,pred.vars,env,nb_file){ | 45 make.brt <- function(spe,data,pred.vars,env,nb_file){ |
46 brt_step <- gbm.step(data= data, gbm.x = pred.vars, gbm.y = spe, family = "bernoulli", tree.complexity = 2, learning.rate = 0.0001,max.trees = 10000,plot.main = F) | 46 cat(paste(" ", spe,":\n -> optimising BRT model ",sep="")) |
47 lr <- 0.05 | |
48 no.trees <- 0 | |
49 while ( no.trees < 1000 & lr > 0.0005 ) { | |
50 cat(".") | |
51 try(brt_step <- gbm.step(data= data, gbm.x = pred.vars, gbm.y = spe, family = "bernoulli", tree.complexity = 2, learning.rate = lr,max.trees = 10000, plot.main = F)) | |
52 # if the gbm does not converge, the return object is null or of size 0 | |
53 if (!is.null(brt_step) ) { | |
54 if (object.size(brt_step) > 0 ) { | |
55 no.trees <- brt_step$gbm.call$best.trees | |
56 print(no.trees) | |
57 } | |
58 } else { | |
59 no.trees <- 0 | |
60 print(no.trees) | |
61 } | |
62 | |
63 # decrease the learning rate | |
64 lr <- lr / 2 | |
65 print(lr) | |
66 } | |
47 #plot | 67 #plot |
48 if (is.null(brt_step)==FALSE){ | 68 if (is.null(brt_step)==FALSE){ |
49 pdf(file = paste("BRT-",spe,".pdf")) | 69 pdf(file = paste("BRT-",spe,".pdf")) |
50 gbm.plot(brt_step, write.title = T,show.contrib = T, y.label = "fitted function",plot.layout = c(3,3)) | 70 gbm.plot(brt_step, write.title = T,show.contrib = T, y.label = "fitted function",plot.layout = c(3,3)) |
51 dev.off() | 71 dev.off() |
92 coord_quickmap() | 112 coord_quickmap() |
93 output_directory <- ggsave(paste("BRT-",spe,"_pred_plot.png")) | 113 output_directory <- ggsave(paste("BRT-",spe,"_pred_plot.png")) |
94 | 114 |
95 #Write prediction in a file | 115 #Write prediction in a file |
96 preds <- cbind(preds,spe) | 116 preds <- cbind(preds,spe) |
97 write.table(preds, paste(nb_file,"_brts_pred_ceamarc.tsv",sep=""), quote=FALSE, dec=".", row.names=F, col.names=T,append = T,sep="\t") | 117 write.table(preds, paste(nb_file,"_brts_pred_ceamarc.tsv",sep=""), quote=FALSE, dec=".", row.names=F, col.names=!file.exists(paste(nb_file,"_brts_pred_ceamarc.tsv",sep="")),append = T,sep="\t") |
98 } | 118 } |
99 | 119 |
100 #### RUN BRT #### | 120 #### RUN BRT #### |
101 nb_file = 0 | 121 nb_file = 0 |
102 | 122 |
123 # Creating the %!in% operator | |
124 `%!in%` <- Negate(`%in%`) | |
125 | |
126 # Data file browsing | |
103 for (file in data_files[[1]]) { | 127 for (file in data_files[[1]]) { |
128 | |
129 # Reading the file | |
104 species_data <- read.table(file, dec = dec_species, sep = "\t", header = TRUE, na.strings = "NA", colClasses = "numeric") | 130 species_data <- read.table(file, dec = dec_species, sep = "\t", header = TRUE, na.strings = "NA", colClasses = "numeric") |
105 nb_file = nb_file + 1 | 131 nb_file = nb_file + 1 |
106 `%!in%` <- Negate(`%in%`) | 132 |
133 # List to store species to predict | |
107 sp = list() | 134 sp = list() |
135 | |
136 # Selection of columns that are not in 'env' and that are not coordinates or stations | |
108 for (n in names(species_data)) { | 137 for (n in names(species_data)) { |
109 if (n %!in% names(env) && n != 'station'){ | 138 if (n %!in% names(env) && n != 'station' && n != 'decimalLatitude' && n != 'decimalLongitude' && n!='lat' && n!='long'){ |
110 sp = cbind(sp,n) | 139 sp = c(sp,n) |
111 } | 140 } |
112 } | 141 } |
142 # Making predictions for each species | |
113 for (spe in sp){ | 143 for (spe in sp){ |
114 try(make.prediction.brt(make.brt(spe,species_data,pred.vars,env,nb_file))) | 144 try(make.prediction.brt(make.brt(spe,species_data,pred.vars,env,nb_file))) |
115 } | 145 } |
116 } | 146 } |
117 | 147 |
148 #Display of abiotic parameters | |
118 cat("Here is the list of your abiotic parameters:\n") | 149 cat("Here is the list of your abiotic parameters:\n") |
119 cat(paste(pred.vars, collapse = ", "), "\n") | 150 cat(paste(pred.vars, collapse = ", "), "\n") |
120 | 151 |
121 | 152 |