-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAnalysis_RealData_GNUparallel_Setup.R
More file actions
159 lines (119 loc) · 4.48 KB
/
Analysis_RealData_GNUparallel_Setup.R
File metadata and controls
159 lines (119 loc) · 4.48 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
library(tidyverse)
library(nimble)
# SETUP #
#-------#
## Set seed
mySeed <- 32
set.seed(mySeed)
## Set number of chains
nchains <- 5
## Source all functions in "R" folder
sourceDir <- function(path, trace = TRUE, ...) {
for (nm in list.files(path, pattern = "[.][RrSsQq]$")) {
if(trace) cat(nm,":")
source(file.path(path, nm), ...)
if(trace) cat("\n")
}
}
sourceDir('R')
## Set and store switches/toggles
# (Re-)downloading data
# downloadData <- FALSE
downloadData <- TRUE
# Recruitment per adult or per adult female
R_perF <- FALSE
# Drop observations of juveniles with no adults present
R_parent_drop0 <- TRUE
# Aggregation level for reproduction data
# NOTE: if this is not defined, will default to group level
sumR.Level <- "line" # Summing at the line level
# Time variation in survival
survVarT <- TRUE
# Rodent covariate on reproduction
fitRodentCov <- TRUE
# Use of telemetry data from Lierne
telemetryData <- TRUE
# Test run or not
testRun <- FALSE
# Run MCMC in parallel
parallelMCMC <- FALSE
# Store as .RDS
storeToggles(downloadData = downloadData,
R_perF = R_perF,
R_parent_drop0 = R_parent_drop0,
sumR.Level = sumR.Level,
survVarT = survVarT,
fitRodentCov = fitRodentCov,
telemetryData = telemetryData,
testRun = testRun,
parallelMCMC = parallelMCMC)
# DOWNLOAD/FETCH DATA #
#---------------------#
if(downloadData){
#Rype_arkiv <- downloadLN(datasets = "Fjellstyrene", versions = 1.6, save = TRUE)
Rype_arkiv <- downloadLN(datasets = c("Fjellstyrene", "Statskog", "FeFo"), versions = c(1.7, 1.8, 1.12), save = TRUE)
}else{
stop("downloadData = FALSE not supported yet. There is an issue with encoding when using LivingNorwayR::initializeDwCArchive() that needs to be resolved first.")
#Rype_arkiv <- initializeDwCArchive("data/Rype_arkiv.zip")
}
# WRANGLE LINE TRANSECT DATA #
#----------------------------#
## Set localities/areas and time period of interest
localities <- listLocations()
areas <- listAreas()
#areas <- listAreas()[c(5, 17, 34)]
minYear <- 2007
maxYear <- 2021
## List duplicate transects to remove
duplTransects <- listDuplTransects()
## Extract transect and observational data from DwC archive
LT_data <- wrangleData_LineTrans(DwC_archive_list = Rype_arkiv,
duplTransects = duplTransects,
#localities = localities,
areas = areas,
areaAggregation = TRUE,
minYear = minYear, maxYear = maxYear)
## Save line transect data for use in post-processing
saveRDS(LT_data, file = "LT_data.rds")
# WRANGLE KNOWN FATE CMR DATA #
#-----------------------------#
## Read in and reformat CMR data
d_cmr <- wrangleData_CMR(minYear = minYear)
# WRANGLE RODENT DATA #
#---------------------#
## Load and reformat rodent data
d_rodent <- wrangleData_Rodent(duplTransects = duplTransects,
#localities = localities,
areas = areas,
areaAggregation = TRUE,
minYear = minYear, maxYear = maxYear)
# PREPARE INPUT DATA FOR INTEGRATED MODEL #
#-----------------------------------------#
## Reformat data into vector/array list for analysis with Nimble
input_data <- prepareInputData(d_trans = LT_data$d_trans,
d_obs = LT_data$d_obs,
d_cmr = d_cmr,
d_rodent = d_rodent,
#localities = localities,
areas = areas,
areaAggregation = TRUE,
excl_neverObs = TRUE,
R_perF = R_perF,
R_parent_drop0 = R_parent_drop0,
sumR.Level = "line",
dataVSconstants = TRUE,
save = TRUE)
# SEED SIMULATION #
#-----------------#
## Expand seeds for simulating initial values
MCMC.seeds <- expandSeed_MCMC(seed = mySeed,
nchains = nchains)
# --> write seed combinations into a .txt file (tab-delimited)
seedList <- list()
seedList <- c()
for(i in 1:length(MCMC.seeds)){
#seedList[[i]] <- paste0(mySeed, "\t", MCMC.seeds[i])
seedList <- c(seedList, paste0(mySeed, "\t", MCMC.seeds[i]) )
}
writeLines(seedList,
"inputSeeds.txt")