66# ' replicates, model parameterizations, and general circulation models (GCMs).
77# '
88# ' @usage
9- # ' projection_variability(model_projections, by_replicate = TRUE, by_gcm = TRUE,
10- # ' by_model = TRUE, consensus = "median",
11- # ' write_files = FALSE, output_dir = NULL,
12- # ' return_rasters = TRUE, progress_bar = FALSE,
13- # ' verbose = TRUE, overwrite = FALSE)
9+ # ' projection_variability(model_projections, from_replicates = TRUE,
10+ # ' from_parameters = TRUE, from_gcms = TRUE,
11+ # ' consensus = "median", write_files = FALSE,
12+ # ' output_dir = NULL, return_rasters = TRUE,
13+ # ' progress_bar = FALSE, verbose = TRUE,
14+ # ' overwrite = FALSE)
1415# '
1516# ' @param model_projections a `model_projections` object generated by the
1617# ' \code{\link{project_selected}}() function. This object contains the file
1718# ' paths to the raster projection results and the thresholds used for binarizing
1819# ' the predictions.
19- # ' @param by_replicate (logical) whether to compute the variance originating
20+ # ' @param from_replicates (logical) whether to compute the variance originating
2021# ' from replicates.
21- # ' @param by_gcm (logical) whether to compute the variance originating from
22- # ' general circulation models (GCMs)
23- # ' @param by_model (logical) whether to compute the variance originating from
22+ # ' @param from_parameters (logical) whether to compute the variance originating from
2423# ' model parameterizations.
24+ # ' @param from_gcms (logical) whether to compute the variance originating from
25+ # ' general circulation models (GCMs)
2526# ' @param consensus (character) (character) the consensus measure to use for
2627# ' calculating changes. Available options are 'mean', 'median', 'range', and
2728# ' 'stdev' (standard deviation). Default is 'median'.
108109# ' out_dir = out_dir)
109110# '
110111# ' # Step 5: Compute variance from distinct sources
111- # ' v <- projection_variability(model_projections = p, by_replicate = FALSE)
112+ # ' v <- projection_variability(model_projections = p, from_replicates = FALSE)
112113# '
113- # ' #terra::plot(v$Present$by_replicate ) # Variance from replicates, present projection
114- # ' terra::plot(v$Present$by_model ) # From models
115- # ' #terra::plot(v$`Future_2041-2060_ssp126`$by_replicate ) # From replicates in future projection
116- # ' terra::plot(v$`Future_2041-2060_ssp126`$by_model ) # From models
117- # ' terra::plot(v$`Future_2041-2060_ssp126`$by_gcm ) # From GCMs
114+ # ' #terra::plot(v$Present$from_replicates ) # Variance from replicates, present projection
115+ # ' terra::plot(v$Present$from_parameters ) # From models with distinct parameters
116+ # ' #terra::plot(v$`Future_2041-2060_ssp126`$from_replicates ) # From replicates in future projection
117+ # ' terra::plot(v$`Future_2041-2060_ssp126`$from_parameters ) # From models
118+ # ' terra::plot(v$`Future_2041-2060_ssp585`$from_gcms ) # From GCMs
118119
119120
120121projection_variability <- function (model_projections ,
121- by_replicate = TRUE ,
122- by_gcm = TRUE ,
123- by_model = TRUE ,
122+ from_replicates = TRUE ,
123+ from_parameters = TRUE ,
124+ from_gcms = TRUE ,
124125 consensus = " median" , # MAKE IT WORK WHEN CONSENSUS IS FULL MODEL
125126 write_files = FALSE ,
126127 output_dir = NULL ,
@@ -140,13 +141,13 @@ projection_variability <- function(model_projections,
140141 }
141142
142143 if (length(consensus ) > 1 ) {
143- stop(" Argument 'consensus' must be a unique value." ,
144- " \n Available options are: 'median', 'range', 'mean' or 'stdev '." )
144+ stop(" Argument 'consensus' must be a single value." ,
145+ " \n Options are: 'median' or 'mean '." )
145146 }
146- consensus_out <- setdiff(consensus , c(" median" , " range " , " mean" , " stdev " ))
147+ consensus_out <- setdiff(consensus , c(" median" , " mean" ))
147148 if (length(consensus_out ) > 0 ) {
148149 stop(" Invalid 'consensus' provided." ,
149- " \n Available options are: 'median', 'range', 'mean' or 'stdev '." )
150+ " \n Options are: 'median' or 'mean '." )
150151 }
151152
152153 if (write_files & is.null(output_dir )) {
@@ -167,7 +168,10 @@ projection_variability <- function(model_projections,
167168 if (write_files ) {
168169 out_dir <- file.path(output_dir , " variance" )
169170 dir.create(out_dir , recursive = TRUE , showWarnings = FALSE )
170- } else {out_dir <- NULL }
171+ } else {
172+ out_dir <- NULL
173+ }
174+
171175 # ### Get data ####
172176 d <- model_projections [[" paths" ]]
173177
@@ -183,9 +187,6 @@ projection_variability <- function(model_projections,
183187
184188 # ###Iteration over combinations####
185189 res <- lapply(1 : nrow(uc ), function (z ) {
186-
187- # To test
188- # z = 1
189190 time <- uc $ Time [z ]
190191 period <- uc $ Period [z ]
191192 scenario <- uc $ Scenario [z ]
@@ -199,7 +200,7 @@ projection_variability <- function(model_projections,
199200 paths <- d_p $ output_path
200201
201202 # ### By replicate ####
202- if (by_replicate ) {
203+ if (from_replicates ) {
203204 if (verbose ) {
204205 message(" \n Calculating variability from distinct replicates: scenario " ,
205206 z , " of " , nrow(uc ))
@@ -208,42 +209,51 @@ projection_variability <- function(model_projections,
208209
209210 # ### By replicates ####
210211 # Get variance of replicates in each gcm, than get the average across gcms
211- var_rep_by_gcm <- terra :: rast(lapply(paths , var_models_rep_by_gcm ))
212- var_rep <- terra :: mean(var_rep_by_gcm )
213- } else {# End of by_replicate
212+ var_rep <- terra :: rast(lapply(paths , var_models_rep_by_gcm ))
213+ var_rep <- terra :: mean(var_rep )
214+ names(var_rep ) <- " from_replicates"
215+ } else {# End of from_replicates
214216 var_rep <- NULL
215217 }
216218
217219 # ### By Model ####
218- if (by_model ) {
220+ if (from_parameters ) {
219221 if (verbose ) {
220222 message(" Calculating variability from distinct models: scenario " ,
221223 z , " of " , nrow(uc ))
222224 }
223225
224226 # Get variance of models in each gcm, than get the average
225- var_model_by_gcm <- terra :: rast(lapply(paths , var_models_model_by_gcm , consensus ))
226- var_model <- terra :: mean(var_model_by_gcm )
227- names(var_model ) <- " by_model"
227+ if (names(model_projections $ thresholds [[1 ]])[1 ] == " Full_model" ) {
228+ var_model <- terra :: rast(lapply(paths , var_models_model_by_gcm ,
229+ " Full_model" ))
230+ } else {
231+ var_model <- terra :: rast(lapply(paths , var_models_model_by_gcm ,
232+ consensus ))
233+ }
234+
235+ var_model <- terra :: mean(var_model )
236+ names(var_model ) <- " from_parameters"
228237 } else { # End of by model
229238 var_model <- NULL
230239 }
231240
232241
233242 # ###By GCM####
234- if (by_gcm & period != " Present" ) {
243+ if (from_gcms & period != " Present" ) {
235244 if (verbose ) {
236245 message(" Calculating variability from distinct GCMs: scenario " ,
237246 z , " of " , nrow(uc ))
238247 }
239248
240249 var_gcm <- var_models_across_gcm(paths = paths , consensus = consensus )
241- names(var_gcm ) <- " by_gcm "
250+ names(var_gcm ) <- " from_gcms "
242251 } else {
243- var_gcm <- NULL }# End of by_gcm
252+ var_gcm <- NULL
253+ }# End of from_gcms
244254
245- all_var <- terra :: rast(c(" by_replicate " = var_rep , " by_model " = var_model ,
246- " by_gcm " = var_gcm ))
255+ all_var <- terra :: rast(c(" from_replicates " = var_rep , " from_parameters " = var_model ,
256+ " from_gcms " = var_gcm ))
247257
248258
249259 # Write results
0 commit comments