-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript_template_generic.R
More file actions
241 lines (170 loc) · 5.13 KB
/
Copy pathscript_template_generic.R
File metadata and controls
241 lines (170 loc) · 5.13 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#### Analysis of <PROJECT NAME> ####
## In this study, <brief description of study>.
## <Important study details>.
## <Experimental design>.
################################################################################
### GUIDELINES FOR RECORDING AND REPRODUCING THE ANALYSIS ENVIRONMENT
###
### We recommend using R package renv.
###
### Recommended workflow to record your R environment:
### renv::init()
### renv::snapshot()
###
### Recommended workflow to restore your R environment:
### renv::restore()
###
### This restores package versions recorded in a renv.lock file and helps
### reproduce the original analysis environment.
################################################################################
################################################################################
### GUIDELINES FOR COMMENTING YOUR SCRIPTS
###
### Comments should explain:
###
### - Why an operation is performed
### - How it relates to the manuscript
### - Important assumptions
### - Data processing decisions
###
### Avoid comments that merely repeat the code.
###
### Good:
### "Convert temperature to Kelvin because the model requires
### absolute temperature"
###
### Poor:
### "Add 273.15 to temperature"
################################################################################
## Setup -----------------------------------------------------------------------
## This section loads the required packages, reads in the data required for the
## analysis, and provides some simple summaries of the data structure.
library(<package>) # <describe purpose of package in this script>
library(<package>) # <describe purpose of package in this script>
## User configuration ----------------------------------------------------------
## set to TRUE to save figures
save_figures <- TRUE
## set to TRUE to save tables
save_tables <- TRUE
## create output directory if saving is enabled
if(save_figures | save_tables) {
if(!dir.exists("output")) dir.create("output")
if(!dir.exists("output/result")) dir.create("output/result")
if(!dir.exists("output/fig")) dir.create("output/fig")
}
## Load data -------------------------------------------------------------------
data <- <add your code to read in the data>
## Data preparation -----------------------------------------------------------
## Purpose:
##
## <Describe how the raw data are transformed into the final dataset
## used for the analysis.>
analysis_data <- data %>%
mutate(...) %>%
select(...) %>%
filter(...)
## Reproducibility checks
### Examples:
### - expected sample size
### - expected number of sites
### - expected number of taxa
### - expected factor levels
### - absence of duplicated observations
### - successful data joins
test_that("<description of expected property>", {
expect_equal(...)
})
## Check structure of processed dataset
head(analysis_data)
str(analysis_data)
summary(analysis_data)
## <Add additional notes relevant for interpretation>
##
## ...
## Summary statistics ------------------------------------
## <Describe the purpose of the summary statistics.>
##
## i.e. These analyses should help understand the data structure and
## identify potential issues but should not replace formal analyses.
summary_data <- ...
head(summary_data)
## Exploratory figure ---------------------------------------------------------
exploratory_plot <- ggplot(...) +
...
## View figure
exploratory_plot
## Save figure
if(save_figures) {
ggsave(
filename = "output/fig/<figure_name>.png",
plot = exploratory_plot,
width = 200,
height = 150,
units = "mm",
dpi = "print"
)
}
## Primary analysis -----------------------------------------------------------
## Analysis objective:
##
## Response variable(s):
## Explanatory variable(s):
## Assumptions:
##
## Relation to manuscript:
## Figure/Table:
##
## <Explain WHY this analysis is performed.>
analysis_result <- <analysis_function>(...)
## Diagnostics and validation
### Examples:
### - model assumptions
### - convergence checks
### - simulation diagnostics
### - sensitivity analyses
### - cross-validation
### - posterior predictive checks
<diagnostic_code>
## View results
summary(analysis_result)
## Extract results into a table
analysis_results <- ...
## Save numerical output
if(save_tables) {
write.csv(
analysis_results,
file = "output/result/<analysis_results>.csv",
row.names = TRUE
)
}
## Derived outputs ------------------------------------------------------------
### Optional section
###
### Examples:
### - predictions
### - estimated effects
### - ordination scores
### - diversity metrics
### - simulations
### - summary statistics
### - scenario projections
###
### Remove this section if not applicable.
derived_output <- ...
head(derived_output)
## Final figure or output -----------------------------------------------------
final_plot <- ggplot(...) +
...
final_plot
if(save_figures) {
ggsave(
filename = "output/fig/<final_figure>.png",
plot = final_plot,
width = 200,
height = 150,
units = "mm",
dpi = "print"
)
}
## End of analysis
### +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++