forked from ashattock/epi50
-
Notifications
You must be signed in to change notification settings - Fork 4
/
auxiliary.R
511 lines (400 loc) · 15.5 KB
/
auxiliary.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
###########################################################
# AUXILIARY FUNCTIONS
#
# A series of helpful R functions.
#
# Written by A.J.Shattock
###########################################################
# ---------------------------------------------------------
# Simple wrapper for selecting all names of an object
# ---------------------------------------------------------
all_names = function(x) all_of(names(x))
# ---------------------------------------------------------
# Simple wrapper for selecting any names of given object
# ---------------------------------------------------------
any_names = function(x) any_of(names(x))
# ---------------------------------------------------------
# Set as datatable and rename columns in one line
# ---------------------------------------------------------
as_named_dt = function(x, new_names) {
# Convert to datatable
dt = as.data.table(x)
# Check new names are correct length
old_names = names(dt)
if (length(old_names) != length(new_names))
stop("Inconsistent number of column names provided")
# Set new column names
named_dt = setnames(dt, old_names, new_names)
return(named_dt)
}
# ---------------------------------------------------------
# Clear the console
# ---------------------------------------------------------
clc = function() cat("\014")
# ---------------------------------------------------------
# Clear all figures
# ---------------------------------------------------------
clf = function() graphics.off()
# ---------------------------------------------------------
# Create colour scheme
# ---------------------------------------------------------
colour_scheme = function(map, pal = NULL, n = 1, ...) {
# Has colour palette been defined
if (is.null(pal)) {
# That's ok as long as it's defined within the map argument
if (!grepl("::", map))
stop("Palette not defined - Use 'pal = my_pal' or 'map = my_map::my_pal'")
# Seperate out the map and the palette
pal = stringr::str_remove(map, ".*\\::")
map = stringr::str_remove(map, "\\::.*")
}
# Initiate colours variable
colours = NULL
# Built in colour schemes
if (map == "base")
colours = get(pal)(n, ...)
# A load of colour maps from the pals package
#
# See: https://www.rdocumentation.org/packages/pals/versions/1.6
if (map == "pals")
colours = get(pal)(n, ...)
# Stylish HCL-based colour maps
#
# See: https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
if (grepl("_hcl$", map))
colours = get(map)(palette = pal, n = n, ...)
# Colour Brewer colour schemes
if (map == "brewer")
colours = brewer_pal(palette = first_cap(pal), ...)(n)
# Viridis colour schemes
if (map == "viridis")
colours = viridis_pal(option = pal, ...)(n)
# Throw an error if colours not yetr defined
if (is.null(colours))
stop("Colour map '", map, "' not recognised (supported: base, pals, hcl, brewer, viridis)")
return(colours)
}
# ---------------------------------------------------------
# Load data from package and store in self named list
# ---------------------------------------------------------
data_package = function(..., package = NULL) {
# Initiate list to store loaded data
data_list = list()
# Unpack variable input arguments - these are files to load
load_files = unlist(list(...))
# Iterate through files to load
for (load_file in load_files) {
# Load data from specified package into local environment
eval_str("data(", load_file, ", ",
"package = '", package, "', ",
"envir = environment())")
# Append data set to list using same name
data_list[[load_file]] = get(load_file)
# Remove data from local environment
rm(list = load_file, envir = environment())
}
return(data_list)
}
# ---------------------------------------------------------
# Apply lappy to each row of a dataframe or datatable
# ---------------------------------------------------------
dtapply = function(dt, fn, ...) {
y = lapply(seq_row(dt), function(i) fn(dt[i, ], ...))
return(y)
}
# ---------------------------------------------------------
# Reset R's most annoying default options
# ---------------------------------------------------------
default_R_options = function() {
options(dplyr.summarise.inform = FALSE,
stringsAsFactors = FALSE,
scipen = 999)
}
# ---------------------------------------------------------
# Evaluate a string (in calling function environment) using eval
# ---------------------------------------------------------
eval_str = function(...)
eval(parse(text = paste0(...)), envir = parent.frame(n = 1))
# ---------------------------------------------------------
# Exponential growth that goes through the origin
# ---------------------------------------------------------
exponential_growth = function(x, a, b) {
y = a * exp(b * x) - a
return(y)
}
# ---------------------------------------------------------
# Biphasic exponential function
# ---------------------------------------------------------
exp_biphasic = function(x, peak, p, d1, d2, vmax, alpha, beta) {
# Both exponential 'phases': short and long
exp1 = exp(-x * log(2)/d1) * p
exp2 = exp(-x * log(2)/d2) * (1-p)
# Combine the phases
bi_exp = peak * (exp1 + exp2)
# Bound above and below
bi_exp_scaled = vmax * (1 - 1 / (1 + (bi_exp / beta)^alpha))
return(bi_exp_scaled)
}
# ---------------------------------------------------------
# Double exponential function
# ---------------------------------------------------------
exp_double = function(x, b, g, h, d) {
y = (h * b * exp(-d * x)) / ((h - b) * exp(-g * x) + b)
return(y)
}
# ---------------------------------------------------------
# Platform specific file separator - for readability
# ---------------------------------------------------------
file_sep = function() {
platform_file_sep = .Platform$file.sep
return(platform_file_sep)
}
# ---------------------------------------------------------
# Capitalise first letter of a string (or vector of strings)
# ---------------------------------------------------------
first_cap = function(string) {
string_cap = paste0(toupper(substring(string, 1, 1)), substring(string, 2))
return(string_cap)
}
# ---------------------------------------------------------
# Format heterogeneous styles of dates
# ---------------------------------------------------------
format_date = function(dates, convert = "ymd") {
styles = c("dmy", "dmY", "ymd", "Ymd")
dates = parse_date_time(dates, styles)
dates = get(convert)(dates)
return(dates)
}
# ---------------------------------------------------------
# Interpolate time series trends
# ---------------------------------------------------------
interp_ts_trend = function(dt) {
interp_dt = dt %>%
model(lm = TSLM(log(value) ~ trend())) %>%
interpolate(dt)
return(interp_dt)
}
# ---------------------------------------------------------
# Convert list to datatable
# ---------------------------------------------------------
list2dt = function(x, ...) {
dt = rbindlist(lapply(x, as.data.table), ...)
return(dt)
}
# ---------------------------------------------------------
# Logistic growth that goes through the origin
# ---------------------------------------------------------
logarithmic_growth = function(x, a, b) {
y = a / (1 + exp(-b * x)) - a / 2
return(y)
}
# ---------------------------------------------------------
# Logistic function
# ---------------------------------------------------------
logistic = function(x, slope, mid, lower = 0, upper = 1) {
y = lower + (upper - lower) / (1 + (x / mid) ^ slope)
return(y)
}
# ---------------------------------------------------------
# Simple wrapper for number of unique observations
# ---------------------------------------------------------
n_unique = function(x) length(unique(x))
# ---------------------------------------------------------
# Wrapper for lapply that also extracts element name
# ---------------------------------------------------------
napply = function(x, fn, ...) {
y = lapply(seq_along(x), function(i) fn(x[[i]], name = names(x)[i], ...))
return(y)
}
# ---------------------------------------------------------
# Normalise a vector of values to between 0 and 1
# ---------------------------------------------------------
normalise_0to1 = function(x, x_min = NULL, x_max = NULL, direction = "forward") {
if (!tolower(direction) %in% c("forward", "backward"))
stop("Normalisation direction must be either 'forward' or 'backward'")
# Forward normalisation
if (tolower(direction) == "forward") {
# Take bounds from data unless given
if (is.null(x_min)) x_min = min(x)
if (is.null(x_max)) x_max = max(x)
# Normalisation equation
y = (x - x_min) / (x_max - x_min)
# Append original min and max values, needed to backtransform
attributes(y)$x_min = x_min
attributes(y)$x_max = x_max
}
# Backward normalisation
if (tolower(direction) == "backward") {
# Take bounds from attitubutes of pre-normalised data unless given
if (is.null(x_min)) x_min = attributes(x)$x_min
if (is.null(x_max)) x_max = attributes(x)$x_max
# Rearrange equation to solve for x
#
# NOTE: as.vector removes all attributes
y = as.vector(x * (x_max - x_min) + x_min)
}
return(y)
}
# ---------------------------------------------------------
# Equivalent of paste, but with an underscore instead of space
# ---------------------------------------------------------
paste1 = function(...) paste(..., sep = "_")
# ---------------------------------------------------------
# Convenience wrapper for readRDS
# ---------------------------------------------------------
read_rds = function(pth, ..., err = TRUE) {
# Special use case: pth is the full .rds file path
if (grepl(".*\\.rds$", pth)) {
full_path = pth
} else { # Otherwise standard use case
# Construct path and file name using inputs
file_path = o$pth[[pth]]
file_name = paste(unlist(list(...)), collapse = "_")
# Concatenate full .rds file path
full_path = paste0(file_path, file_name, ".rds")
}
# Check whether file exists
exists = file.exists(full_path)
# If file exists, load it
if (exists)
x = readRDS(file = full_path)
# If file does not exist
if (!exists) {
# Construct error / warning message
err_message = paste("Unable to load file '", full_path, "'")
# Either throw an error or warning depending on err argument
if (err) stop(err_message)
if (!err) warning(err_message)
# Return out trivial result
x = NULL
}
return(x)
}
# ---------------------------------------------------------
# Load Excel files from URL
# ---------------------------------------------------------
read_url_xls = function(url, sheet = 1) {
# Create temporary file
xls = tempfile()
# Download from URL to temporary file
download.file(url, xls, quiet = TRUE, mode = 'wb')
# Read the xls file (xlsx also handled)
url_dt = readxl::read_excel(
path = xls,
sheet = sheet) %>%
as.data.table()
# Delete temporary file
file.remove(xls)
return(url_dt)
}
# ---------------------------------------------------------
# Inverse of cumsum - use to extract the vector which created a cumsum
# ---------------------------------------------------------
rev_cumsum = function(x) {
# Return input if single value
if (length(x) == 1)
return(x)
# Take difference of x with a lag of one
y = x - c(0, x[1 : (length(x) - 1)])
return(y)
}
# ---------------------------------------------------------
# Wrapper for consistent behaviour of base::sample when length(x) is one
# ---------------------------------------------------------
sample_vec = function(x, ...)
x[sample.int(length(x), ...)]
# ---------------------------------------------------------
# Convenience wrapper for saveRDS
# ---------------------------------------------------------
save_rds = function(x, pth, ...) {
# Special use case: pth is the full .rds file path
if (grepl(".*\\.rds$", pth)) {
full_path = pth
} else { # Otherwise standard use case
# Construct path and file name using inputs
file_path = o$pth[[pth]]
file_name = paste(unlist(list(...)), collapse = "_")
# Concatenate full .rds file path
full_path = paste0(file_path, file_name, ".rds")
}
# Save as an RDS
saveRDS(x, file = full_path)
}
# ---------------------------------------------------------
# Simple wrapper for sequence along dataframe rows
# ---------------------------------------------------------
seq_row = function(x) seq_len(nrow(x))
# ---------------------------------------------------------
# Sigmodial growth going through the origin (inverse of logistic)
# ---------------------------------------------------------
sigmoidal_growth = function(x, slope, mid, max) {
y = 1 - logistic(x, slope, mid, lower = 1 - max, upper = 1)
return(y)
}
# ---------------------------------------------------------
# Initiate progress bar with normal-use options
# ---------------------------------------------------------
start_progress_bar = function(n) {
# Initiate progress bar from progress package
pb = progress_bar$new(
format = " [:bar] :percent (remaining: :eta)",
total = n, # Number of tasks to complete
complete = "-", # Completion bar character
incomplete = " ", # Incomplete bar character
current = ">", # Current bar character
clear = TRUE, # If TRUE, clears the bar when finish
width = 125) # Width of the progress bar
return(pb)
}
# ---------------------------------------------------------
# Bi-directional setdiff - elements not in both x and y
# ---------------------------------------------------------
symdiff = function(x, y) setdiff(union(x, y), intersect(x, y))
# ---------------------------------------------------------
# Format a number with thousand mark separators
# ---------------------------------------------------------
thou_sep = function(val) {
format_val = format(val, scientific = FALSE,
trim = TRUE,
drop0trailing = TRUE,
big.mark = ",")
return(format_val)
}
# ---------------------------------------------------------
# Load an file if it exists, throw an error if not
# ---------------------------------------------------------
try_load = function(pth, file, msg = NULL, type = "rds", throw_error = TRUE, sep = FALSE) {
# Initiate trivial output
file_contents = NULL
# Set default error message
if (is.null(msg))
msg = "Cannot load file"
# Switch case for loading function
loading_fnc = switch(
tolower(type),
# Support both RDS and CSV
"rds" = "readRDS",
"csv" = "read.csv",
# Throw an error if anything else requested
stop("File type '", type, "' not supported")
)
# Concatenate path and file name
file_name = paste0(pth, ifelse(sep, file_sep(), ""), file, ".", type)
# If file doesn't exist, throw an error if desired
if (!file.exists(file_name) && throw_error == TRUE)
stop(msg, " [missing: ", file_name, "]")
# If file exists, try to load it
if (file.exists(file_name)) {
# Get the loading function and attempt to load file
file_contents = tryCatch(
get(loading_fnc)(file_name),
# Catch the error - we may not want to throw it
error = function(e) {
# Throw descriptive error if desired
if (throw_error == TRUE)
stop(msg, " [unreadable: ", file_name, "]")
}
)
}
return(file_contents)
}