Scripts & model output underlying the publication: 
Increasing spatial dispersion in ecosystem restoration mitigates risk in disturbance-driven environments
Fivash et al. 2022, Journal of Applied Ecology, doi: 10.4121/18133766
---------

This archived dataset contains the scripts used to run simulations of the model that appears in the publication: 
'Increasing spatial dispersion in ecosystem restoration mitigates risk in disturbance-driven environments' (Journal of Applied Ecology, 2022).

It also includes: 
(1) the model output produced by our simluations, 
(2) the scripts used to produces the figures that appear in the manuscript, as well as 
(3) a set of functions that can be used to create randomized vegetation patterns (a key feature of the study).

--------
Folders:

Functions:
The folder 'Functions' contains a number of custom functions required to run the analyses and produce figures. 
This folder will be accessed automatically by the scripts.

Model simulation:
The script used to run the model simulations and produce model output can be found in the folder 'Model simulation'.

Initial pattern generation:
The functions used to creat initial vegetation configurations can be found in the folder 'Initial pattern generation'. 
The function 'initalPatchMaker.R' creates the patterns and contains a readme to instruct the user on the use of the function. 
This function uses the other functions in the folder as dependencies.

Figures:
The folder 'figures' contains the most resources. 
It includes a subfolder for each of the 4 main figures, which each contain:

(1) the final figures in high quality (.tif), as well as in a layered format which can be opened in the software Clip Stuido (.clip).
(2) the layered components of the figures in (.tif) in the subfolder 'Figure components' (note: these separate figure panels often lack annotations added in the layered image).
(3) the .R scripts used to create the figures
(4) the inital vegetation pattern matrixes used for the simulations, and results of the simulation runs, saved as binary R data files (.rda), which are loaded automatically in the scripts used to create the figures, found in the 'RDA' folder.

--------
Using R scripts:
- Libraries used in the scripts are listed at the beginning of each script ('raster', 'scales', 'fields', etc.).  They are available for download from CRAN via 'Tools> Install Packages...' tab in RStudio. 
- You should set the 'rootDirectory' variable to the location of the downloaded folder on your own PC.  This variable is empty on the first line of each script. Keep the original folder structure intact for ease of use.
- Each script will automatically load all the custom functions used in the script using the 'callFunctions.R' function in the first lines of each script.
- Annotations on the purpose of each block of code can be found within each script

-------
RDA file organization:
There are 2 main types of .rda files: 
(1) '_grids.rda' 
(2) '_trials.rda'
 
Most figures access a unique pair of 'grid' and 'trial' RDA files for the analysis required to produce that figure.  These are loaded into memory within the scripts using the load() function.
All '_trials.rda' files are created from '_grids.rda' files in the script 'Model simulation script.R

'grids.rda' 
When loaded, these files will create the variable 'MiSeries'
MiSeries contains a stack of 'n' (n = between 3 and 428) 100 x 100 matrices in a 3-dimensional array (dimensions 100, 100, n).  
Each 100 x 100 matrix represents one initial pattern of vegetation inputed into the model simulations.
Tip: Use the command plot(raster::raster(MiSeries[,,n])) to view the nth pattern in the array.

'trials.rda'
When loaded, these files will create the variable 'data'
'data' is a data.frame with 4 columns: 'frame.ID', 'VMR', 'failure.perc', and 'avgTimeToSuccess'
Each row in the data.frame represents the simulated output produced from one initial vegetation pattern.
(1) 'frame.ID' is a list of ID numbers for each pattern in each row (i.e., 1 - n)
(2) 'VMR" contians the varience-to-mean ratio characterizing each pattern.
(3) 'failure.perc' contains the percentage (0 - 100%) of trials which resulted in a restoration failure (e.g., vegetation cover = 0 at the end of the simulation)
(4) 'avgTimeToSuccess' contains the mean-log average interval required before ~successful~ trials reached 100% vegetation cover.

'Archetype_trials.rda' is a unique file that contains a record of the entire simulation at each time step in order to produce the time series seen in Figure 1.
It is organized as a 3-dimensional array. 
Dimensions: (time, trial run, vegetation patterns). 
Dimension size: (20000, 100, 3) 
Each simulated trial concluded after 20000 time steps.
Each pattern was exposed to 100 unique trials featuring unique disturbances. 
The file includes 3 archetypal vegetation patterns (see Figure 1)

