DOI: 10.6084/m9.figshare.3563493
title: Supplement 1. Annotated Mathematica code for the Bayesian state-space model and simulations.

description: File List <b>All Files at Once:</b> Zheng_et_al_Supplement.zip (MD5: 6230624ab714d9525b38704cc2523360) consisting of these folders: <b>Inference</b>, <b>Packages</b>, and <b>Postsimulation</b>. <b>Packages</b> has subfolders <i>Metropolis</i>, <i>RandomGenerator</i>, and <i>Toolbox</i>. <pre>List of all the files: mcstate_SmallLand_Realdata_withHI.txt res_InsectsOaks_Adp_SmallLand_Realdata_withHI.txt res_InsectsOaks_All_SmallLand_Realdata_withHI.txt res_InsectsOaks_Concise_SmallLand_Realdata_withHI.txt Step1_Inference_Metcommunity.nb Step2_PredictiveCheck.nb Metropolis.nb _2.cfs segments.gen segments_7 AdaptiveMetropolis.nb AdaptiveProposal.nb AdaptiveProposalScale.nb BlockAdaptiveMetropolis.nb BlockMetropolis.nb BlockStudentTMetropolis.nb dir.txt DomainConstraint.nb InitialProposal.nb InitialProposalScale.nb IsDiscrete.nb IsReturnLast.nb Metropolis.nb MultiTryMetropolis.nb PrintDetails.nb SamplingPattern.nb StartLogpdf.nb StudentTDisplacements.nb StudentTMetropolis.nb _a.cfs segments.gen segments_n init.m RandomGenerator.nb _0.cfs _0.cfx segments.gen segments_2 DiscreteRatioOfUniform.nb RandomBinomial.nb RandomCanonicalMultiNorm.nb RandomCanonicalMultiNorm2.nb RandomMultiNormPrecision.nb RandomMultiNormVariance.nb RandomNegativeBinomial.nb RandomOneSideTruncNorm.nb RandomPoisson.nb _0.cfx _4.cfs segments.gen segments_2 init.m init.m CalculateTR.m InsectOakSampler.m logpdf1F1.m MergeLand.m recurInter1F1.txt mcstate_SmallLand_Realdata_withHI.txt res_InsectsOaks_Adp_SmallLand_Realdata_withHI.txt res_InsectsOaks_All_SmallLand_Realdata_withHI.txt res_InsectsOaks_Concise_SmallLand_Realdata_withHI.txt Step1_Inference_Metcommunity.nb Step2_PredictiveCheck.nb col_perCaptia_dynamics_posterior_simland_ntree_100_crownA_0_cluster_1.txt col_perCaptia_dynamics_posterior_simland_ntree_100_crownA_10_cluster_1.txt col_perCaptia_dynamics_posterior_simland_ntree_100_crownA_10_cluster_10.txt col_perCaptia_dynamics_posterior_simland_ntree_100_crownA_10_cluster_5.txt col_perCaptia_dynamics_posterior_simland_ntree_100_crownA_20_cluster_1.txt col_perCaptia_dynamics_posterior_simland_ntree_100_crownA_5_cluster_1.txt col_perCaptia_dynamics_posterior_simland_ntree_200_crownA_10_cluster_1.txt col_perCaptia_dynamics_posterior_simland_ntree_50_crownA_10_cluster_1.txt dynamics_posterior_simland_ntree_100_crownA_0_cluster_1.txt dynamics_posterior_simland_ntree_100_crownA_10_cluster_1.txt dynamics_posterior_simland_ntree_100_crownA_10_cluster_10.txt dynamics_posterior_simland_ntree_100_crownA_10_cluster_5.txt dynamics_posterior_simland_ntree_100_crownA_20_cluster_1.txt dynamics_posterior_simland_ntree_100_crownA_5_cluster_1.txt dynamics_posterior_simland_ntree_200_crownA_10_cluster_1.txt dynamics_posterior_simland_ntree_50_crownA_10_cluster_1.txt R0_dynamics_posterior_simland_ntree_100_crownA_0_cluster_1.txt R0_dynamics_posterior_simland_ntree_100_crownA_10_cluster_1.txt R0_dynamics_posterior_simland_ntree_100_crownA_10_cluster_10.txt R0_dynamics_posterior_simland_ntree_100_crownA_10_cluster_5.txt R0_dynamics_posterior_simland_ntree_100_crownA_20_cluster_1.txt R0_dynamics_posterior_simland_ntree_100_crownA_5_cluster_1.txt R0_dynamics_posterior_simland_ntree_200_crownA_10_cluster_1.txt R0_dynamics_posterior_simland_ntree_50_crownA_10_cluster_1.txt res_InsectsOaks_All_SmallLand_Realdata_withHI.txt simland_ntree_100_crownA_0_cluster_1.txt simland_ntree_100_crownA_10_cluster_1.txt simland_ntree_100_crownA_10_cluster_10.txt simland_ntree_100_crownA_10_cluster_5.txt simland_ntree_100_crownA_20_cluster_1.txt simland_ntree_100_crownA_5_cluster_1.txt simland_ntree_200_crownA_10_cluster_1.txt simland_ntree_50_crownA_10_cluster_1.txt Step3_SimulateDynamics.nb Step4_Colonization per Captia.nb Step5_Plot_SimulateResult.nb </pre> Description <b>Organization of the three folders</b> 1) The folder <i>Packages</i> contains mathematica packages used in inference and simulations of metacommunity dynamics. 1.1) The subfolder <i>Metropolis</i> is the general metropolis algorithm used in mcmc simulations. 1.2) The subfolder <i>RandomGenerator</i> is a collection of random generators not provided with the basic functions of mathematica. 1.3) The subfolder <i>Toolbox</i> is a collection of miscellaneous tools including plots and mcmc diagnostics. 1.4) The metacommunity related package files. 1.4.1) MergeLand.m: merge closely related trees. 1.4.2) logpdf1F1.m and recurInter1F1.txt: precomputation of the confluent hypergeometric function 1F1, aiming to improve computational speed. 1.4.3) InsectOakSampler.m: block update functions for Gibbs sampler. 2) The folder <i>Inference</i> contains code used for parameter estimation of the metacommunity model via mcmc 2.1) The notebook Step1_Inference_Metcommunity.nb 2.1.1) The input datafile should contain the definitions of allspp, prileafden, mergtree, isextdata, occdata, abunddata, dispdata. Here allspp is a list of species names (abbreviations); prileafden is the prior species density per leaf in log scale, mergtree is the merged set of trees including their locations and radii; isextdata is the indicator of extinction experiments for each year and each tree and each species, in the format of sparse array; occdata is the occupancy data; abunddata is the abundance data; dispdata is the data from dispersal experiments. 2.1.2) The temporary files such as mcstate_SmallLand_Realdata_withHI.txt saves the mcmc state, so that the long mcmc chain can restart from unexpected/manual loop break. 2.1.3) There are three mcmc output files 2.1.3.1) res_InsectsOaks_Adp_SmallLand_Realdata_withHI.txt saves the adaptation parameter values 2.1.3.2) res_InsectsOaks_Concise_SmallLand_Realdata_withHI.txt saves all the model parameter values except the large size of latent states (abundance). 2.1.3.3) res_InsectsOaks_All_SmallLand_Realdata_withHI.txt saves all the model parameter values including the latent states. 2.2) The notebook Step2_PredictiveCheck.nb performs model evaluation by posterior predictive checks including observed abundance and species richness. 3) The folder <i>PostSimulation</i> contains code used in simulation of metacommunity dynamics in hypothetical landscapes 3.1) The notebook Step3_SimulateDynamics.nb simulates the metacommunity dynamics based on the posterior distributions of model parameters (for illustration, only five species were analyzed in step2 mcmc), in eight scenarios of habitat configurations. Output eight land files simland_ntree_*.txt and eight dynamics files dynamics_posterior_simland_*.txt. 3.2) The notebook Step4_Colonization per captia.nb calculates basic reproduction number R0 saved in R0_dynamics_*.txt, and calculates the per-capita colonization rate saved in col_perCaptia_*.txt. 3.3) The notebook Step5_SimulateResult_Plot.nb plots Fig. 3 in the manuscript, based on the simulation results and calculated R0 and colonization rates.