### Running a batch landscape simulation Source: https://fmestre1.github.io/MetaLandSim/reference/manage_landscape_sim.html Example showing the setup of simulation parameters, creation of the required data frame, and execution of the simulation. ```R #Setup the parameters for each simulation: PAR1_SPAN2 <- rep("ncsd",820)#parameter 1 for the span function PAR2_SPAN2 <- rep(seq(from=0,to=80,by=2), each=20)#parameter 2 for the span function PAR3_SPAN2 <- rep(seq(from=0,to=80,by=2),20)#parameter 3 for the span function PAR4_SPAN2 <- rep(2,820)#parameter 4 for the span function PAR5_SPAN2 <- rep(2,820)#parameter 5 for the span function NSEW_SPECIES2 <- rep("none",820)#where to start populating the landscape PARM_SPECIES2 <- rep(5,820)#parameter for the species function METHOD_SPECIES2 <- rep("percentage",820)#method for populating the landscape MAPSIZE2 <- rep(10000,820)#dimension of the landscape SPAN2 <- rep(100,820)#number of time steps of each simulation ITER2 <- rep(5,820)#number of iterations of each simulation NPATCH2 <- rep(800,820)#number of patches AREA_M2 <- rep(0.45,820)#mean area AREA_SD2 <- rep(0.2,820)#area sd MDST2 <- rep(0,820)#minimum distance between KERN <- rep("op1",820)#kernel CONN <- rep("op1",820)#connectivity function COLNZ <- rep("op1",820)#colonization function EXT <- rep("op1",820)#extinction function BETA1 <- rep("NULL",820) B <- rep(1,820) C1 <- rep("NULL",820) C2 <- rep("NULL",820) Z <- rep("NULL",820) R2 <- rep("NULL",820) DISPERSAL2 <- rep(800,820)#mean dispersal ability of the species SUCC <- rep("early",820) #Build parameter data frame (keep the order of the parameters): simulation <- data.frame(MDST2,NPATCH2,AREA_M2,AREA_SD2, MAPSIZE2,SPAN2,ITER2,PAR1_SPAN2,PAR2_SPAN2,PAR3_SPAN2,PAR4_SPAN2,PAR5_SPAN2, NSEW_SPECIES2,PARM_SPECIES2,METHOD_SPECIES2,KERN,CONN,COLNZ,EXT,BETA1,B,C1,C2,Z,R2,DISPERSAL2,SUCC) #Delete vectors used for data frame creation: rm('PAR1_SPAN2','PAR2_SPAN2','PAR3_SPAN2','PAR4_SPAN2','PAR5_SPAN2', 'NSEW_SPECIES2','PARM_SPECIES2','METHOD_SPECIES2','MAPSIZE2','SPAN2','ITER2', 'NPATCH2','AREA_M2','AREA_SD2','MDST2','KERN','CONN','COLNZ','EXT', 'BETA1','B','C1','C2','Z','R2','DISPERSAL2','SUCC') if (FALSE) { # \dontrun{ data(param1) ms2 <- manage_landscape_sim(par_df=simulation,parameters_spom=param1) } # } ``` -------------------------------- ### Example of components.graph Source: https://fmestre1.github.io/MetaLandSim/reference/components.graph.html An example demonstrating how to use components.graph with the rland dataset. ```R data(rland) components.graph(rl=rland) #> [1] 19 ``` -------------------------------- ### Install MetaLandSim development version from GitHub Source: https://fmestre1.github.io/MetaLandSim/index.html Install the development version using devtools. Ensure devtools is installed first. ```R # Install devtools if not already installed install.packages("devtools") # Install MetaLandSim from GitHub devtools::install_github("FMestre1/MetaLandSim") ``` -------------------------------- ### Initialization list example for ifm.naive.MCMC Source: https://fmestre1.github.io/MetaLandSim/reference/ifm.naive.MCMC.html Example of a named list used to initialize the MCMC chain. ```R init1=list(alpha=runif(1,1,30), b=runif(1,0,5),y=runif(1,0,20), e=runif(1,0,1),x=runif(1,0,5)). ``` -------------------------------- ### simulate_graph usage example Source: https://fmestre1.github.io/MetaLandSim/reference/simulate_graph.html An example demonstrating how to initialize data and call simulate_graph with specific landscape and parameter settings. ```R data(rland) data(landscape_change) data(param1) sim1 <- simulate_graph(rl=rland, rlist=landscape_change, simulate.start=TRUE, method="percentage", parm=50, nsew="none", succ = "none", param_df=param1, kern="op1", conn="op1", colnz="op1", ext="op1", beta1=NULL, b=1, c1=NULL, c2=NULL, z=NULL, R=NULL ) ``` -------------------------------- ### coda.create usage example Source: https://fmestre1.github.io/MetaLandSim/reference/coda.create.html An example demonstrating how to run an MCMC estimation and export the results using coda.create. ```R if (FALSE) { # \dontrun{ # quick run; actual estimation requires more iterations: data(simulatedifm) myniter=100 nsite=nrow(z.sim) nyear=ncol(z.sim) nthin=1 nburnin=0 init1=list(alpha=runif(1,1,30), b=runif(1,0,5),y=runif(1,0,20),e=runif(1,0,1),x=runif(1,0,5)) inm1 <- ifm.naive.MCMC(niter=myniter,init=init1,z.data = z.sim,site.distance=sim.distance,site.area=sim.area, sd.prop.alpha=4,sd.prop.b=0.6,sd.prop.y=40,sd.prop.e=0.05,sd.prop.x=0.4,nthin=1,print.by=1000) # write files in OpenBUGS format to working directory: coda.create(inm1,"sim_inm1",par.list=list("e.chain","x.chain","alpha.chain", "b.chain","y.chain"),niter=myniter,nthin=nthin) } # } ``` -------------------------------- ### Example usage of manage_expansion_sim Source: https://fmestre1.github.io/MetaLandSim/reference/manage_expansion_sim.html An example showing how to call manage_expansion_sim with specific parameters for a range expansion simulation. ```R if (FALSE) { # \dontrun{ data(param1) sim_range <- manage_expansion_sim(mapsize=1000, dist_m=0, areaM, areaSD=0.001, patch=300,percI=50, param=param1, b=1, tsteps=100, iter=100,variable="area",var_min=0.01, var_max=0.6,by=0.1) } # } ``` -------------------------------- ### Example usage of summary_landscape Source: https://fmestre1.github.io/MetaLandSim/reference/summary_landscape.html Example demonstrating how to load sample data and generate a summary for a landscape object. ```R data(rland) summary_landscape(object=rland) #> Value #> landscape area (hectares) 100.000 #> number of patches 60.000 #> mean patch area (hectares) 0.223 #> SD patch area 0.091 #> mean distance amongst patches (meters) 492.910 #> minimum distance amongst patches (meters) 20.872 # Value #landscape area (hectares) 100.000 #number of patches 60.000 #mean patch area (hectares) 0.061 #SD patch area 0.041 #mean distance amongst patches (meters) 528.345 #minimum distance amongst patches (meters) 51.780 ``` -------------------------------- ### Example: Calculating acceptance rates for a naive IFM model Source: https://fmestre1.github.io/MetaLandSim/reference/accept.calculate.html This example demonstrates initializing a chain, running the ifm.naive.MCMC function, and calculating the acceptance rates using accept.calculate. ```R data(simulatedifm) # Here, we run a chain with random initial values: init1=list(alpha=runif(1,1,30), b=runif(1,0,5),y=runif(1,0,20),e=runif(1,0,1),x=runif(1,0,5)) inm1 <- ifm.naive.MCMC(niter=1000,init=init1,z.data = z.sim,site.distance=sim.distance,site.area=sim.area, sd.prop.alpha=4,sd.prop.b=0.6,sd.prop.y=40,sd.prop.e=0.05,sd.prop.x=0.4,nthin=1,print.by=100) #> [1] 1 #> [1] 101 #> [1] 201 #> [1] 301 #> [1] 401 #> [1] 501 #> [1] 601 #> [1] 701 #> [1] 801 #> [1] 901 accept.calculate(inm1,model='naive') #> $e.chain #> [1] 0.493 #> #> $x.chain #> [1] 0.341 #> #> $y.chain #> [1] 0.547 #> #> $b.chain #> [1] 0.211 #> #> $alpha.chain #> [1] 0.216 #> #> $deviance.chain #> [1] 0.912 #> ``` -------------------------------- ### Example of plotting expansion simulations Source: https://fmestre1.github.io/MetaLandSim/reference/plot_expansion.html An example showing how to load simulation data and visualize it using plot_expansion. ```R data(rg_exp) plot_expansion(exp = rg_exp) #> starting httpd help server ... #> done ``` -------------------------------- ### addpoints example usage Source: https://fmestre1.github.io/MetaLandSim/reference/addpoints.html Example demonstrating how to add 10 patches to an existing landscape object and verify the change in the number of patches. ```R data(rland) #Checking the number of patches in the starting landscape: rland$number.patches #> [1] 60 #60 #Adding 10 patches to a landscape: rl1 <- addpoints(rl=rland, nr=10) #Checking the number of patches in the output landscape: rl1$number.patches #> [1] 70 #70 ``` -------------------------------- ### Example usage of range_expansion Source: https://fmestre1.github.io/MetaLandSim/reference/range_expansion.html An example showing how to run the range_expansion function with sample data. Note that this should be run with more than 100 iterations. ```R if (FALSE) { # \dontrun{ #Produce a model of range expansion: #Note: this function should be run with >100 iterations (parameter "iter"). data(rland) data(param2) rg_exp1 <- range_expansion(rl=rland, percI=80, param=param2, b=1, tsteps=100, iter=100) } # } ``` -------------------------------- ### Extracting a landscape example Source: https://fmestre1.github.io/MetaLandSim/reference/extract.graph.html Example showing how to extract the landscape at the 50th time step using rland and landscape_change data. ```R data(rland) data(landscape_change) #Extracting the landscape of the 50th time step: rl1 <- extract.graph(rl=rland, rlist=landscape_change, nr=50) ``` -------------------------------- ### Initialization list example for ifm.missing.MCMC Source: https://fmestre1.github.io/MetaLandSim/reference/ifm.missing.MCMC.html Example of a named list used to initialize the MCMC chain for the ifm.missing.MCMC function. ```R init1=list(z.missing=runif(nmissing),mupsi1=runif(1),alpha=runif(1,1,30), b=runif(1,0,5),y=runif(1,0,20), e=runif(1,0,1),x=runif(1,0,5)). ``` -------------------------------- ### Examples of plot_graph usage Source: https://fmestre1.github.io/MetaLandSim/reference/plot_graph.html Examples showing how to plot a landscape without species occupancy and with species occupancy. ```R data(rland) data(occ.landscape) #Without the species occupancy: plot_graph(rl=rland, species=FALSE, links=FALSE) #With the species occupancy: plot_graph(rl=occ.landscape, species=TRUE, links=FALSE) ``` -------------------------------- ### import.shape example usage Source: https://fmestre1.github.io/MetaLandSim/reference/import.shape.html An example of how to call import.shape with the required parameters for filename, path, column names, and dispersal ability. ```R if (FALSE) { # \dontrun{ rl1 <- import.shape(filename = "yourshapefile.shp" ,path = "C:/yourpath..." ,species.col= "column with species" ,ID.col="column with patch Id" ,area.col="Column with area" ,dispersal=800#Mean dispersal ability of the species #(used to generate patch clusters, or components) ) } # } ``` -------------------------------- ### Reclassify clusters example Source: https://fmestre1.github.io/MetaLandSim/reference/cluster.id.html Example demonstrating how to remove patches from a landscape and reclassify the remaining components using cluster.id, followed by comparing component counts. ```R data(rland) #After removing 30 (50%) of the patches of a landscape: rland2 <- removepoints(rl=rland, nr=35) #A reclassification might be needed to identify components: rland2 <- cluster.id(rl=rland2) #After removing 35 patches, there's a different number of components: components.graph(rl=rland) #> [1] 19 #21 components.graph(rl=rland2) #> [1] 20 #16 ``` -------------------------------- ### Example usage of ifm.missing.MCMC Source: https://fmestre1.github.io/MetaLandSim/reference/ifm.missing.MCMC.html A template for setting up the environment and parameters before running the MCMC estimation. ```R if (FALSE) { # \dontrun{ data(simulatedifm) library("coda") niter=2000 nsite=100 nyear=10 nthin=1 nburnin=1000 ## NOTE! The notation used here corresponds to MetaLandSim and differs from Risk et al 2011 ## Here ## e (in MetaLandSim) = mu (in Risk et al 2011) ## x = chi ## y = gamma ## b = beta ## alpha = alpha ## ``` -------------------------------- ### Example usage of ifm.naive.MCMC Source: https://fmestre1.github.io/MetaLandSim/reference/ifm.naive.MCMC.html A template for running the MCMC estimation with simulated data. ```R if (FALSE) { # \dontrun{ data(simulatedifm) library("coda") myniter=5000 nsite=nrow(z.sim) nyear=ncol(z.sim) nthin=1 nburnin=1000 ## NOTE! The notation used here corresponds to MetaLandSim and differs from Risk et al 2011 ## Here ## e (in MetaLandSim) = mu ## x = chi ## y = gamma ## b = beta ## alpha = alpha ## ``` -------------------------------- ### Summarizing metapopulation objects Source: https://fmestre1.github.io/MetaLandSim/reference/summary_metapopulation.html Examples of summarizing different metapopulation datasets, including the expected output format. ```R data(occ.landscape) data(occ.landscape2) summary_metapopulation(object=occ.landscape) #> Value #> landscape area (hectares) 100.000 #> number of patches 60.000 #> mean patch area (hectares) 0.061 #> SD patch area 0.041 #> mean distance amongst patches (meters) 528.345 #> minimum distance amongst patches (meters) 51.780 #> species occurrence - snapshot 1 50.000 # Value #landscape area (hectares) 100.000 #number of patches 60.000 #mean patch area (hectares) 0.061 #SD patch area 0.041 #mean distance amongst patches (meters) 528.345 #minimum distance amongst patches (meters) 51.780 #species occurrence - snapshot 1 50.000 summary_metapopulation(object=occ.landscape2) #> Value #> landscape area (hectares) 100.000 #> number of patches 60.000 #> mean patch area (hectares) 0.069 #> SD patch area 0.039 #> mean distance amongst patches (meters) 521.717 #> minimum distance amongst patches (meters) 45.905 #> species occurrence - snapshot 1 50.000 #> species occurrence - snapshot 2 58.333 #> species occurrence - snapshot 3 61.667 #> species occurrence - snapshot 4 61.667 #> species occurrence - snapshot 5 58.333 #> species occurrence - snapshot 6 60.000 #> species occurrence - snapshot 7 70.000 #> species occurrence - snapshot 8 68.333 #> species occurrence - snapshot 9 68.333 #> species occurrence - snapshot 10 56.667 # Value #landscape area (hectares) 100.000 #number of patches 60.000 #mean patch area (hectares) 0.069 #SD patch area 0.039 #mean distance amongst patches (meters) 521.717 #minimum distance amongst patches (meters) 45.905 #species occurrence - snapshot 1 50.000 #species occurrence - snapshot 2 58.333 #species occurrence - snapshot 3 61.667 #species occurrence - snapshot 4 61.667 #species occurrence - snapshot 5 58.333 #species occurrence - snapshot 6 60.000 #species occurrence - snapshot 7 70.000 #species occurrence - snapshot 8 68.333 #species occurrence - snapshot 9 68.333 #species occurrence - snapshot 10 56.667 ``` -------------------------------- ### Basic MetaLandSim workflow Source: https://fmestre1.github.io/MetaLandSim/index.html A basic example demonstrating how to load the package, generate a random landscape, simulate dynamics, plot the results, and compute connectivity metrics. ```R # Load the package library(MetaLandSim) # Generate a random landscape with 100 patches landscape <- rland.graph(npatch = 100, areaM = 50, distM = 1000) # Simulate metapopulation dynamics over 20 time steps simulation <- iterate.graph(rl = landscape, npatch = 100, nyears = 20) # Plot the resulting landscape plot_graph(landscape) # Compute connectivity metrics metrics <- metrics.graph(rl = landscape, metric = "NC") # Number of components ``` -------------------------------- ### Install MetaLandSim from CRAN Source: https://fmestre1.github.io/MetaLandSim/index.html Use this command to install the stable version of the package from the CRAN repository. ```R install.packages("MetaLandSim") ``` -------------------------------- ### Example usage of plotL.graph Source: https://fmestre1.github.io/MetaLandSim/reference/plotL.graph.html Example showing how to plot the 50th landscape from a sequence of landscape changes. ```R data(rland) data(landscape_change) plotL.graph(rl=rland, rlist=landscape_change, nr=50, species=FALSE, links=FALSE) ``` -------------------------------- ### Example of creating and displaying a parameter data frame Source: https://fmestre1.github.io/MetaLandSim/reference/create.parameter.df.html Demonstrates initializing a parameter data frame with specific values and displaying the resulting object. ```R param2 <- create.parameter.df(alpha=0.5, x=0.1, y=5, e=0.1) param2 #> par_output #> alpha 0.5 #> x 0.1 #> y 5.0 #> e 0.1 # par_output #alpha 0.5 #x 0.1 #y 5.0 #e 0.1 ``` -------------------------------- ### edge.graph Example Source: https://fmestre1.github.io/MetaLandSim/reference/edge.graph.html Example showing how to generate an edge data frame using the rland dataset. ```R data(rland) edge_df <- edge.graph(rl=rland) ``` -------------------------------- ### Example of removing species occupancy Source: https://fmestre1.github.io/MetaLandSim/reference/remove.species.html Example demonstrating the conversion of a metapopulation object to a landscape object using remove.species. ```R data(occ.landscape) rl1 <- remove.species(sp=occ.landscape) ``` -------------------------------- ### Calculating and visualizing landscape statistics Source: https://fmestre1.github.io/MetaLandSim/reference/list.stats.html Example demonstrating how to simulate landscape occupation using simulate_graph and then evaluate the species occupancy statistics with list.stats. ```R data(rland) data(landscape_change) data(param1) #First, using simulate graph, simulate the occupation on a dynamic landscape #(output of span.graph): sim1 <- simulate_graph( rl=rland, rlist=landscape_change, simulate.start=TRUE, method="percentage", parm=50, nsew="none", succ = "none", param_df=param1, kern="op1", conn="op1", colnz="op1", ext="op1", beta1=NULL, b=1, c1=NULL, c2=NULL, z=NULL, R=NULL) #Then evaluate species occupancy through the changes suffered by the landscape: occ <- list.stats(sim_list=sim1, stat="occupation", plotG=TRUE) #Checking the percentage of occupation in the 40 first landscapes: head(occ,40) #> [1] 50.000000 90.000000 86.666667 83.333333 66.666667 65.000000 65.000000 #> [8] 60.000000 55.000000 48.333333 45.000000 33.333333 33.333333 25.000000 #> [15] 21.666667 18.333333 15.000000 10.000000 8.333333 5.000000 6.666667 #> [22] 6.666667 6.666667 3.333333 3.333333 3.333333 3.333333 3.333333 #> [29] 3.333333 1.666667 1.666667 0.000000 0.000000 0.000000 0.000000 #> [36] 0.000000 0.000000 0.000000 0.000000 0.000000 #Output: #[1] 50.000000 65.000000 90.000000 96.666667 93.333333 91.666667 #[7] 91.666667 90.000000 93.333333 90.000000 85.000000 83.333333 #[13] 85.000000 88.333333 83.333333 86.666667 81.666667 68.333333 #[19] 70.000000 75.000000 80.000000 73.333333 63.333333 56.666667 #[25] 55.000000 51.666667 46.666667 41.666667 38.333333 21.666667 #[31] 13.333333 13.333333 10.000000 6.666667 5.000000 3.389831 #[37] 1.694915 1.694915 0.000000 0.000000 ``` -------------------------------- ### Example usage of cluster.graph Source: https://fmestre1.github.io/MetaLandSim/reference/cluster.graph.html Demonstrates how to run cluster.graph using the rland dataset and shows the resulting data frame structure. ```R data(rland) cluster.graph(rl=rland) #> cluster number of nodes #> 1 1 4 #> 2 2 1 #> 3 3 2 #> 4 4 9 #> 5 5 2 #> 6 6 3 #> 7 7 13 #> 8 8 2 #> 9 9 1 #> 10 10 2 #> 11 11 2 #> 12 12 3 #> 13 13 4 #> 14 14 1 #> 15 15 3 #> 16 16 4 #> 17 17 1 #> 18 18 2 #> 19 19 1 #Output: # cluster number of nodes #1 1 11 #2 2 1 #3 3 13 #4 4 1 #5 5 1 #6 6 15 #7 7 2 #8 8 1 #9 9 3 #10 10 1 #11 11 1 #12 12 2 #13 13 4 #14 14 1 #15 15 1 #16 16 1 #17 17 1 ``` -------------------------------- ### Create a Random Landscape Graph Source: https://fmestre1.github.io/MetaLandSim/reference/rland.graph.html Example demonstrating how to generate a landscape with 60 patches, a mean area of 0.05 hectares, and a dispersal ability of 120 meters. ```R #Example to create a random landscape graph with 60 patches with a mean area #of 0.05 hectares. #The landscape mosaic is a square with 1000 meters side. #The species mean dispersal ability is 120 meters (in order to connect the patches). #A plot with the landscape graph is displayed graphically. rl1 <- rland.graph(mapsize=1000, dist_m=80, areaM=0.05, areaSD=0.02, Npatch=60, disp=120, plotG=TRUE) ``` -------------------------------- ### Example of removing patches from a landscape Source: https://fmestre1.github.io/MetaLandSim/reference/removepoints.html Demonstrates loading a landscape, checking the initial patch count, removing 10 patches, and verifying the new patch count. ```R data(rland) #Checking the number of patches in the starting landscape: rland$number.patches #> [1] 60 #60 #Removing 10 patches from the landscape: rl1 <- removepoints(rl=rland, nr=10) #Checking the number of patches in the output landscape: rl1$number.patches #> [1] 50 #50 ```