Skip to content

Instantly share code, notes, and snippets.

View DrNickRedfern's full-sized avatar

Nick Redfern DrNickRedfern

View GitHub Profile
@DrNickRedfern
DrNickRedfern / scaleFUN.R
Created March 11, 2022 10:31
Formatting scales on axes
scaleFUN <- function(x) sprintf("%.1f", x)
@DrNickRedfern
DrNickRedfern / csl-entry_margin.CSS
Created March 11, 2022 13:32
Add bottom margin to csl-entry for bibliography in rmarkdown
.csl-entry {
margin-bottom: 10px;
}
@DrNickRedfern
DrNickRedfern / loglik2AIC.R
Last active March 18, 2022 09:01
Calculate AIC from log-likelihood (l) for k parameters
loglik2AIC <- function(l, k){round((-2 * l) + (2 * k), 3)}
@DrNickRedfern
DrNickRedfern / trimean.R
Last active March 11, 2022 13:59
Calcualate a trimean using Harrell-Davis quantile estimator (default = Tukey's trimean)
trimean <- function(x, p = 0.25, a = 0.25){
q <- Hmisc::hdquantile(x, probs = c(p, 0.5, 1-p), na.rm = TRUE, names = FALSE)
tm <- (a * q[1]) + ((1 - (2 * p)) * q[2]) + (a * q[3])
return(tm)
}
@DrNickRedfern
DrNickRedfern / cmd.R
Created March 11, 2022 13:42
Coefficient of median dispersion
cmd <- function(x){median(abs(x - median))/median(x)}
@DrNickRedfern
DrNickRedfern / q_summary.R
Last active March 11, 2022 13:53
Quantile-based summary of a numeric vector, returning the median, IQR, skewness, kurtosis, and quartile coefficient of dispersion
q_summary <- function (x) {
h <- Hmisc::hdquantile(x, probs = seq(0.125, 0.875, 0.125), na.rm = TRUE, names = FALSE, se = FALSE, weights = FALSE)
med = h[4]; iqr = h[6] - h[2]
skew = (h[2] + h[6] - (2 * h[4]))/iqr; kurt = ((h[7] - h[5]) + (h[3] - h[1]))/iqr
qcd = (iqr)/(h[6] + h[2])
out <- list(med, iqr, skew, kurt, qcd)
names(out) <- c("median", "IQR", "skewness", "kurtosis", "quatile coefficient of dispersion")
return(out)
}
loessggplot <- function(x, low = 0.1, high = 0.9, step = 0.01, title = "", ticks = 0.1){
n <- length(x)
t <- cumsum(x); t <- 100*t/max(t) # normalize the running time to percent
# Create an empty data fame to store the results of the for loop
df <- data.frame()
# Loop over the sequence of spans for the loess smoothers and store the result
for(s in seq(low, high, step)) {
@DrNickRedfern
DrNickRedfern / backstage.R
Created March 12, 2022 18:25
loessggplot for Backstage
loessggplot(backstage, low = 0.1, high = 0.9, step = 0.01, title = "Backstage (1919)", ticks = 0.1)
@DrNickRedfern
DrNickRedfern / checkobjectclass.R
Created May 13, 2022 20:33
How to check the class of an object in R
# Check the class of the output of seewave::acoustat()
class(insidious_chapter_3_nape)
## [1] "matrix" "array"
@DrNickRedfern
DrNickRedfern / syuzhet::simple_plot.R
Created August 16, 2022 08:31
The simple_plot function from the syuzhet package
function (raw_values, title = "Syuzhet Plot", legend_pos = "top",
lps = 10, window = 0.1)
{
wdw <- round(length(raw_values) * window)
rolled <- rescale(zoo::rollmean(raw_values, k = wdw, fill = 0))
half <- round(wdw/2)
rolled[1:half] <- NA
end <- length(rolled) - half
rolled[end:length(rolled)] <- NA
trans <- get_dct_transform(raw_values, low_pass_size = lps,