-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_external_original.R
141 lines (93 loc) · 3.88 KB
/
example_external_original.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
132
133
134
135
136
137
138
# Intro ----
# Original example to demonstrate the use of external data
# It is based on an assessment of mercury concentrations with some data
# provided in an ICES extraction and other data provided from an external file
# Setup ----
# Sources functions (folder R) and reference tables (folder information)
# Only those functions needed for this example are sourced here
# The functions and reference tables folders are assumed to be in the current
# R project folder
rm(list = objects())
function_path <- file.path("R")
source(file.path(function_path, "import_functions.R"))
source(file.path(function_path, "import_check_functions.R"))
source(file.path(function_path, "import_external_data.R"))
source(file.path(function_path, "assessment_functions.R"))
source(file.path(function_path, "ctsm_lmm.R"))
source(file.path(function_path, "reporting_functions.R"))
source(file.path(function_path, "support_functions.R"))
source(file.path(function_path, "information_functions.R"))
# Read data from ICES extraction ----
# mercury data with supporting variables, station dictionary, and
# quality assurance (methods) file
biota_data <- ctsm_read_data(
compartment = "biota",
purpose = "OSPAR",
contaminants = "mercury_data.csv",
stations = "station_dictionary.csv",
QA = "quality_assurance.csv",
data_path = file.path("data", "example_external_data"),
info_files = list(determinand = "determinand_external_data.csv"),
info_path = "information",
extraction = "2022/01/11",
max_year = 2020L)
# saveRDS(biota_data, file.path("RData", "biota data.rds"))
# Adjust ICES extraction and import external AMAP data ----
# corrects known errors in data
# makes adjustments to data that can't be done easily in the core code
# import external AMAP data
# uses functions in 'R/import external data.r'
# the functions need to be generalised to deal with more determinands and to
# deal with the case where there is no ICES extraction; i.e. only external data
# the markdown also resolves some inconsistencies where the 'same data' are in
# both the ICES extraction and the external data file
# the report of the adjustments can be sent anywhere
rmarkdown::render(
"example_external_data_adjustments.Rmd",
output_file = "mercury_adjustments.html",
output_dir = file.path("output", "example_external_original")
)
# saveRDS(biota_data, file.path("RData", "biota data adjusted.rds"))
# Prepare data for next stage ----
# gets correct variable and streamlines some of the data files
biota_data <- ctsm_tidy_data(biota_data)
# Construct timeseries ----
biota_timeSeries <- ctsm_create_timeSeries(
biota_data,
determinands.control = list(
"LIPIDWT%" = list(det = c("EXLIP%", "FATWT%"), action = "bespoke")
),
get_basis = get_basis_biota_OSPAR
)
# saveRDS(biota_timeSeries, file.path("RData", "biota timeSeries.rds"))
# Assessment ----
## main runs ----
biota_assessment <- ctsm_assessment(
biota_timeSeries,
AC = c("BAC", "EQS.OSPAR", "HQS"),
get_assessment_criteria = get.AC.OSPAR,
parallel = TRUE
)
## check convergence ----
(wk_check <- check_convergence_lmm(biota_assessment$assessment))
# this time series has missing standard errors
# "3371 HG Mytilus edulis SB Not_applicable"
# refit with different numerical differencing arguments
biota_assessment <- ctsm_update_assessment(
biota_assessment, series == wk_check, hess.d = 0.01, hess.r = 8
)
# saveRDS(biota_assessment, file.path("RData", "biota assessment.rds"))
# Summary files ----
biota_web <- biota_assessment
biota_web$info$AC <- c("BAC", "EQS.OSPAR")
summary_table(
biota_web,
determinandGroups = list(levels = "Metals", labels = "Metals"),
classColour = list(
below = c("BAC" = "blue", "EQS.OSPAR" = "green"),
above = c("BAC" = "orange", "EQS.OSPAR" = "red"),
none = "black"
),
collapse_AC = list(EAC = "EQS.OSPAR"),
output_dir = file.path("output", "example_external_original")
)