##
#
#				 Metadata and script folders info
#
#########################################################################################################################

This metada file summarizes the necessary scripts and used data to replicate the analyses from: 
'Strong context-dependence in the relative importance of climate and habitat on macro-moth community changes in Finland'

The data available on dryad, the scripts as well as the produced resukts are under the license CC BY (See at the end of the document).
The name of the author will be added to this documents once the review process is over.

Note: Please check the directories when loading the data.
	I-  Scripts and data folders
			A- Scheme
			B- Description
	II- License
	III- Software Version
			A- R / Rstudio
			B- libraries


# ...............................................................................................................................................

## ..... I-  Scripts and data folders
##			A- Scheme

1. Data (Available on zenodo/supplementary material of dryad)
	1.EnvironmentalInfo
	2.ObsInfo
	3.ModelRun
	4.Predictions
		1.CommunityMeasures_Sites
		2.CommunityMeasures_FIN
2. Script (available directly in the code folder)
	1.CovarPrep&EnvirChange
		1.Hab_Clusters_plot
		2.prepare_envir_Sites
		3.prepare_envir_FIN
		4.Envir_change_plot
	2.PrepareDataforModel
	3.RunModel&Diagnostics
	4.VPpostanalysis
		1.VPfunctions
		2.VPconditional
		3.VPtraits
		4.VPall
	5.PredictionsCommunityMeasures
		1.Predictions_prep
		2.Predictions_Fin
		3.Predictions_Site
	7.ScenariosCommunity
		1.SiteLevel
		2.FINLevel
	8.Forplotting_traitSC
3. Metadata
	Metadata_submission

# ...............................................................................................................................................

## 				B- Description
Note: Below PA refer to presence absence data or model (Hurdle part 1), AB abundance data or model (hurdle part 2), VP refers to variance partitionning.


1. Data: Provide the data used in the analysis
	1.EnvironmentalInfo
	Not all intermediate site information calculation are provided but the scripts to obtain them are available in 2. Script/1.CovarPrep&EnvirChange/2.prepare_envir_Sites.
	We provide: 
		- Habitat information: at the site level 'HabSites1108.RDATA' or at the Finnish grid level 'HabSmallFIN1408.RDATA'. PropFINall_Jan23.RDATA is also provided to present the habitat profiles classification in  2. Script/1.CovarPrep&EnvirChange/1.Hab_Clusters_plot
		- Climatic information: at the site level 'ClimSite080623.RDATA' or at the Finnish grid level 'ClimSmallFIN_1408.RDATA'
		- Both habitat and climatic information
		- Finnish grid points design '3km_gridpoints'
		- Boreal Regions information at the sites 'BorealRegions.RDATA' and Finnish grid level
		- 'clusterdata2.RDATA': Finland cluster info results
		- 'CurrentSmallFIN_1408.RDATA': display enviropnmental information at the Finnish grid sites.
		
		
	2.ObsInfo
		* Moth_Monitoring_toRecbase_19072023.csv: full Moth data cleaned up on the 17/07/2023.
		* Traits_Moths.csv Provided by Pasi Sihlvonen. Used in the following publications Yazdanian, et al., (2023) and Hällfors, et al., (2021). 
		* 'prep_centCLC_J23.RDATA': prepared data to use in HMSC
		* spprevname.RDATA: summarize the focval species used in the analysis.

		
	3.ModelRun
		* test10908cent_yearPA.RDATA: PA results from HMSC (Hurdle model part 1)
		* test10908cent_yearAB.RDATA: AB results from HMSC (Hurdle model part 2)
		* space_barplot.RDATA: intermediate graphical information used for Variance partitioning plots.
		* 'PredSmal2408.RDATA': Provide the necessary elements from the model run to evaluated the predictive approach of the Variance partitioning at the Finnish grid Sites (See 2. Script/4.VPpostanalysis)
		* Between and within VP calculation at the finnish grid sites level (VP.bw): 'VPbwFIN_PA01408.RDATA' and 'VPbwFIN_AB01608.RDATA'
		* Conditional VP calculation at the finnish grid sites level (VP.cond) + overall VP (VP.a): 'VPcondAFIN100cdd_PA_1408.RDATA' and 'VPcondAFIN100cdd_AB_1408.RDATA'
		* Between, within and conditional VP calculation for the grid sites level: 'VPycdd-PAclust_1108.RDATA' and 'VPycdd-ABclust_1108.RDATA'
		* 'VPall_traits2FIN.RDATA' and 'VPall_traits_Sitesprep.RDATA': Prepared variance partitioning results for traits information at both the Finnish grid sites and at the sampling sites level.
		* 'VPall_Sites1808.RDATA' : prepared overall (VP.a) variance partitioning from HMSC not conditionned but recalculated without the Effort part and with full posterior distribution for plotting along side the Finnish grid overall results see 2.4.4.
		
	4.Predictions
		* 'colnamesp.RDATA': Saved colnames object that correspond to order of focal moth species in the predictions.
	
2. Script: script that make use of 1.Data to make the analyse by step.
	1.CovarPrep&EnvirChange: This folder provide the script to extract the different environmental information at the sampling sites (2.prepare_envir_Sites) 
	We did not provide all the extraction steps for the Finnish grid sites but this can be done similarly. We also show how we derived the habitat profiles using classification of the Finnish grid sites and 
	the clustering of the sampling sites based on the Finnish grid sites classification (1.Hab_Clusters_plot). Finally in (4.Envir_change_plot) we provide the 
	script to look at the trend and variability of environemtnal changes.
	
		1.Hab_Clusters_plot
			* FinlandCluster.r allow to derive the habitat profiles based on kmeans clustering
			* Cluster_site_Finland.r: Display Finnish grid classification and site clustering
		
		2.prepare_envir_Sites
			* Frag_Moth: Provide the fragmentation calculation at the site within the 0.5km buffer
			* Hab_Moth: Provide the habitat proportion extraction at the site within the 0.5km buffer
			* Prep_allClimSites: Provide the script to calculate all climatic variables ('SnowAV0806.RDATA', 'gcddwintFMI0806.RDATA', 'Sgdd-Summer080623.RDATA' and 'ClimSite080623.RDATA')
			* prepAllSitesVar: Combine all Site environmental information to use ('Site_env2508.RDATA')
		## similarly we extracted the information at the grid sites. For simplicity we do not provide the script here but the resulting data extraction are available in 1. Data/1.EnvironmentalInfo
		
		4.Envir_change_plot
			* Fin_env_study_1408: Uses extracted habitat and climatic information at the sampling sites level ('HabSites1108.RDATA', 'ClimSite080623.RDATA') and 
									Finnish grid levels ('HabSmallFIN1408.RDATA', 'ClimSmallFIN_1408.RDATA')to evaluate trends and variability of environmental drivers studied.


	2.PrepareDataforModel
		* PrepMod_cCLC_August23.r: Prepare the data to be fed into Hmsc combining environmental information, study design and Moth observations and traits ('prep_centCLC_J23.RDATA' is obtained to run the model).
		* plots_HPclust.r: This script helped create some of the additional plots from the manuscript.
		
	3.RunModel&Diagnostics
		* HMSC_CLCcent_0723.r: Details on Hmsc run.
		* Test_Diagnostics.txt: Example of diagnostics from Hmsc results.
	
	4.VPpostanalysis
		1.VPfunctions
			* VP_function_FIN.r AND VP_function_sites.r: General function to get Variance partition full posterior distribution and calculation of conditional VP, within and between groups. 
			VP_function_FIN calculates the VP measures at the population level (Finnish grid sites) and VP_function_sites at the sampling sites level.
			
		2.VPconditional
			* VPclust_cond_FIN.r and VPclust_cond_sites.r Uses the results from 1.Data/3.ModelRun to plot the conditional variance partitioning at the Finnish grid sites and sampling sites respectively.
			VPclust_cond_FIN.r uses 'VPbwFIN_PA01408.RDATA', 'VPcondAFIN100cdd_PA_1408.RDATA' for PA data and 'VPbwFIN_AB01608.RDATA', 'VPcondAFIN100cdd_AB_1408.RDATA' for AB data; 
			VPclust_cond_sites.r uses 'VPycdd-PAclust_1108.RDATA', for PA data and 'VPycdd-ABclust_1108.RDATA' for AB data.
			
		3.VPtraits
			* prep_VP_traits.r Prepare the variance partitioning results for traits information at both the Finnish grid sites and at the sampling sites level: 'VPall_traits2FIN.RDATA' and 'VPall_traits_Sitesprep.RDATA'.
			It uses all RDATA file mentioned above in section 2.4.2.VPconditional.
			* VP_cond_HPclust_FIN_Sites.r: Uses the results from above script ('VPall_traits2FIN.RDATA' and 'VPall_traits_Sitesprep.RDATA') for plotting.
			
		4.VPall
			* VPall_noEff23_Sites.r prepare the overall (VP from HMSC not conditionned but recalculated without the Effort part and with full posterior distribution): 'VPall_Sites1808.RDATA'
			* VPall_noEff23_FIN.r: uses saved 'VPcondAFIN100cdd_PA_1408.RDATA' and 'VPcondAFIN100cdd_AB_1408.RDATA' (Because the overall vp also called VP.a was saved at the same time as VP.cond already used in 2.4.2.VPconditional) 
			and above prepared sites VP all information 'VPall_Sites1808.RDATA' to plot the VP from HMSC not conditionned but recalculated without the Effort part and with full posterior distribution.
			
	5.PredictionsCommunityMeasures
		To simplify the results from the below scripts are not provided directly. BUt all analyses can be run on their own based on the model results.
		1.Predictions_prep
			* Pred_prep1408.r : provide the script to prepare the predictive approach information 'Predsmall2408.RDATA'
		2.Predictions_Fin
			* PredCurrent_simp.r : provide the script to run the PA predictions and calculate the species richness at the Finnish grid sites for a chosen year i=xx (for simplification). Note that for the full analyses we run this script for all years.
			* PredCurrent_simp_climsc and PredCurrent_simp_habsc: run similar scripts to above but under the climate scenario (Habitat is constant during the study period) and habitat scenario (Climate is constamt during the study period).
			* For AB data similar scripts to above are provided which calculate the species eveness instead of species richness:  PredCurrentAB_simp.r, PredCurrentAB_simp_climsc.r and PredCurrentAB_simp_habsc.r
		3.Predictions_Site
			* Site_pred_currentPAAAB.r: provide the script for both PA and AB predictions with species richness and Species envenness calculations at the sampling sites for a particular year i=XX.  Note that for the full analyses we run this script for all years.
			* Site_pred_climscPAAB.r and Site_pred_habscPAAB.r: run similar scripts to above but under the climate scenario (Habitat is constant during the study period) and habitat scenario (Climate is constamt during the study period).
			
	7.ScenariosCommunity
		1.SiteLevel
			* PA_trendtraits_currentSITE.r: script to prepare PA predictions (Species richness) at the sampling sites per traits information (overall, WL- Wing length and HP - Hostplant form).
			* AB_trendtraits_currentSITE.r: script to prepare AB predictions (Species eveness) at the sampling sites results per traits information (overall, WL- Wing length and HP - Hostplant form)overall, .
			Note that Similar code exists for the predictions based on the climate and habitat scenario. For simplicity we did not provide them here.
			
		2.FINLevel
			* PA_trendtraits_current.r: script to prepare PA predictions (Species richness) at the Finnish grid sites per traits information (overall, WL- Wing length and HP - Hostplant form).
			* AB_trendtraits_current.r: script to prepare AB predictions (Species eveness) at the Finnish grid sites per traits information (overall, WL- Wing length and HP - Hostplant form).
			Note that Similar code exists for the predictions based on the climate and habitat scenario. For simplicity we did not provide them here.
			
	8.Forplotting_traitSC
			* Plot_Summary_allsc.r: allows to display the conmparison of overall community changes (empirical/climate /habitat scenario) for both species richness and evenness (Mean/trend/variability over the study period)
			* Plot_pred_trendvarHP.r: allows to display the conmparison of community changes per hostplant form (empirical) for both species richness and evenness (Mean/trend/variability over the study period)
			* Plot_pred_trendvarWL.r: allows to display the conmparison of community changes per Wing length (empirical) for both species richness and evenness (Mean/trend/variability over the study period)
			Note that for simplicity we did not provide the scripts of the climate and habitat scenario for Plot_pred_trendvarHP and Plot_pred_trendvarWl, but they are build from the above script with only the prepared data input that changes (predictions under the scenarios and community measures calculations 2.7).

# ...............................................................................................................................................

## ..... II - License
The license for this work is available below (CC BY as required per the journal where the analyses were published):
https://creativecommons.org/licenses/by/4.0/


# ...............................................................................................................................................
  
## ..... III- Software Version
				A- R / Rstudio
				
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          2.1                         
year           2022                        
month          06                          
day            23                          
svn rev        82513                       
language       R                           
version.string R version 4.2.1 (2022-06-23)


RStudio 2023.06.2+561 "Mountain Hydrangea" Release (de44a3118f7963972e24a78b7a1ad48b4be8a217, 2023-08-23) for Ubuntu Focal
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) rstudio/2023.06.2+561 Chrome/110.0.5481.208 Electron/23.3.0 Safari/537.36


				B- libraries
[1] "abind 1.4.5"
[1] "ape 5.6.2"
[1] "base 4.2.1"
[1] "bench 1.1.2"
[1] "cluster 2.1.4"
[1] "clValid 0.7"
[1] "colorspace 2.0.3"
[1] "corrplot 0.92"
[1] "cowplot 1.1.1"
[1] "curl 4.3.2"
[1] "data.table 1.14.2"
[1] "diffeR 0.0.7"
[1] "dplyr 1.0.10"
[1] "ggExtra 0.10.0"
[1] "ggh4x 0.2.1"
[1] "ggplot2 3.3.6"
[1] "ggpubr 0.4.0"
[1] "ggridges 0.5.3"
[1] "ggtext 0.1.1"
[1] "ggthemes 4.2.4"
[1] "glue 1.6.2"
[1] "graphics 4.2.1"
[1] "grDevices 4.2.1"
[1] "grid 4.2.1"
[1] "gridExtra 2.3"
[1] "gridtext 0.1.4"
[1] "gtable 0.3.1"
[1] "Hmsc 3.0.11"
[1] "htmltools 0.5.2"
[1] "landscapemetrics 1.5.5"
[1] "landscapetools 0.5.0"
[1] "lattice 0.20.45"
[1] "lubridate 1.8.0"
[1] "magrittr 2.0.3"
[1] "mapdata 2.3.1"
[1] "maps 3.4.0"
[1] "mapsFinland 0.1.1"
[1] "maptools 1.1.4"
[1] "MASS 7.3.58"
[1] "Matrix 1.5.1"
[1] "mclust 5.4.10"
[1] "mgcv 1.8.42"
[1] "patchwork 1.1.1"
[1] "plyr 1.8.7"
[1] "pollen 0.82.0"
[1] "purrr 1.0.1"
[1] "raster 3.5.15"
[1] "RColorBrewer 1.1.3"
[1] "Rcpp 1.0.9"
[1] "reshape2 1.4.4"
[1] "rgdal 1.6.4"
[1] "rgeos 0.6.3"
[1] "scales 1.2.1"
[1] "sf 1.0.12"
[1] "sp 1.4.7"
[1] "stats 4.2.1"
[1] "stringr 1.4.1"
[1] "terra 1.5.21"
[1] "tibble 3.1.8"
[1] "tidyr 1.2.1"
[1] "tidyselect 1.2.0"
[1] "timeDate 3043.102"
[1] "tools 4.2.1"
[1] "vegan 2.6.2"
[1] "vioplot 0.3.7"
[1] "viridis 0.6.2"
[1] "viridisLite 0.4.1"