-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMASTER-InfVal.R
131 lines (119 loc) · 4.75 KB
/
MASTER-InfVal.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#' ####################################################################### #
#' PROJECT: [InfVal; Master Script for Parametrising Entire Analyses]
#' CONTENTS:
#' - Master File for Framework Execution
#' DEPENDENCIES:
#' -
#' AUTHOR: [Erik Kusch]
#' ####################################################################### #
rm(list = ls())
set.seed(42)
# PREAMBLE ================================================================
## Directories ------------------------------------------------------------
message("Registering Directories")
Dir.Base <- getwd()
Dir.Data <- file.path(Dir.Base, "Data")
Dir.Models <- file.path(Dir.Base, "Models")
Dir.Exports <- file.path(Dir.Base, "Exports")
Dir.Concept <- file.path(Dir.Base, "Concept")
Dir.Scripts <- file.path(Dir.Base, "R Scripts")
Dirs <- c(Dir.Data, Dir.Models, Dir.Exports, Dir.Concept)
CreateDir <- sapply(Dirs, function(x) if(!dir.exists(x)) dir.create(x))
if(!dir.exists(Dir.Data)){dir.create(Dir.Data)}
## Packages ---------------------------------------------------------------
message("Loading Libraries")
install.load.package <- function(x) {
if (!require(x, character.only = TRUE))
install.packages(x, repos='http://cran.us.r-project.org')
require(x, character.only = TRUE)
}
package_vec <- c(
## Data Simulation Packages
"parallel",
"doParallel",
"foreach",
"doSNOW",
"pbapply",
"randcorr",
"lubridate",
# Inference Packages
"Hmsc", # for HMSC models
# "cooccur", # for COOCCUR models
"igraph", # for graph representation
# "devtools", # to install betalink
# "betalink", # for computation of network dissimilarities
## Result Packages
"ggplot2", # for plotting
"tidybayes", # for plotting
"brms",
"rethinking",
"reshape2",
"cowplot",
"scales",
"ggnewscale",
"ggpubr"
)
sapply(package_vec, install.load.package)
## Functionality -------------------------------------------------------
`%nin%` <- Negate(`%in%`)
### Data Simulation Functions
source(file.path(Dir.Scripts, "SimulationFrameworkFunctions.R"))
### Whole-Network Inference Accuracy Function
FUN_Matcomparison <- function(mat1, mat2){
eq <- mat1==mat2 # avoid to later compute this twice
100-round(sum(!eq, na.rm = TRUE)/sum(!is.na(eq))*100, 2) # get the percentage of non equal values
}
# RUN SET-UP ===========================================================
## Simulation Parameters -----------------------------------------------
n_runs <- 1e3 # number of networks to simulate and infer for
## Network Creation
n_spec = 30 # number of species per network
NetworkType = "Association" # type of links
Sparcity = 0 # how many % of associations should be exactly 0
MaxStrength = 20 # absolute maximum of interspecific links
## Initial Individual Creation
n_individuals = 5e2 # number of individuals for initialisation
n_mode = "each" # how to interpret the above number
Env_range = c(0, 10) # environmental landscape range
Trait_sd = 1 # standard deviation of traits per species
## Carrying Capacity Creation
k_range = c(300,300)
## Simulation Parameters
d0 = 0.4 # base death rate
b0 = 0.6 # base birth rate
env.xy = function(x = NULL, y = NULL){x} #environmental maladpation function
t_max = 20 # simulation time
t_inter = 1 # when to record data
Env_sd = 5 # environmental maladaption SD, higher = more permissive environment
migration = 0.5 # sd of 0 centred normal for relocation of offspring
Effect_Dis = 1 # distance at which link effect manifests
verbose = FALSE # whether to produce simulation progress tracker in console
## File Naming
RunName <- "Revision"
## Inference Settings --------------------------------------------------
### HMSC MCMC Settings
nSamples <- 7e3
thin <- 1
nWarmup <- round(nSamples*0.3*thin, 0)
nChains <- 4
### Gridding of data along each axis:
n_Grid <- 5
## Cluster for parallel computation ------------------------------------
message("Registering Clusters")
DesiredCores <- ifelse(n_runs > 100, 100, n_runs)
ncores <- ifelse(parallel::detectCores() > DesiredCores, DesiredCores, parallel::detectCores())
cl <- parallel::makeCluster(ncores
# , outfile = "Log.txt"
)
parallel::clusterExport(cl, varlist = ls(), envir = environment())
doSNOW::registerDoSNOW(cl)
# METADATA FOR RUN WRITING =============================================
MetaF <- file.path(Dir.Data, paste0("META-", RunName, ".RData"))
if(file.exists(MetaF)){stop("A run with this name has already been executed.")}
# DATA SIMULATION ======================================================
source(file.path(Dir.Scripts, "DataSimulations.R"))
# ASSOCIATION INFERENCE ================================================
source(file.path(Dir.Scripts, "Inference.R"))
# POST-INFERENCE ANALYSES ==============================================
source(file.path(Dir.Scripts, "Results.R"))
save.image(file = MetaF)