Skip to content

Instantly share code, notes, and snippets.

# lookup table of error messages (coud be read in from a file in the package)
lookup <- cbind(from = "there is no package called ‘pineapples’",
to = "no pineapples here!")
# swap over the message if there's a better one in the lookup
swap_message <- function (message) {
idx <- match(message, lookup[, "from"])
if (length(idx == 1) && !is.na(idx))
message <- lookup[idx, "to"]
message
@goldingn
goldingn / pseudo_r2_is_bad.R
Created November 9, 2017 05:11
pseudo-R2 is bad
# demonstrating how bad an esitmate of model goodness fo fit pseudo R2 is with small integer data
# fake poisson glm
set.seed(1)
n <- 1000
x <- rnorm(n)
# the lower the rates, the worse the pseudo-r squared says the model is
intercept <- -2
# try twiddling the intercept to change the average rate for the Poisson
@goldingn
goldingn / dirty_caching.R
Last active November 8, 2017 04:14
quick and dirty caching of R objects, via a %<--% operator
# quick & dirty caching of R objects - run the expression in b iff an RDS file
# for the object doesn't exist, otherwise load the object
`%<--%` <- function (a, b) {
name <- deparse(substitute(a))
file <- paste0(name, ".rds")
if (file.exists(file)) {
obj <- readRDS(file)
} else {
obj <- b
saveRDS(obj, file)
@goldingn
goldingn / initial_values_hack.R
Last active May 17, 2018 06:32
hack to parse a list of initial values for use in greta
to_free <- function (node, data) {
lower <- node$lower
upper <- node$upper
fun <- switch(node$constraint,
none = function (x) x,
high = function (x) log(x - lower),
low = function (x) log(upper - x),
both = function(x) qlogis((y - lower) / (upper - lower)))
fun(data)
}
@goldingn
goldingn / parallel_zoon.R
Created October 31, 2017 22:19
example of executing a zoon workflow in parallel (using experimental branch)
# install the experimental parallel branch
# remotes::install_github("zoonproject/zoon@parallel")
library (zoon)
# example workflow for 4 independent models that may take a while to run
run_wf <- function () {
workflow(occurrence = UKAnophelesPlumbeus,
covariate = UKBioclim,
process = Replicate(Background(n = 1000), 4),
model = GBM(max.trees = 10000),
@goldingn
goldingn / styles_sketch.R
Created October 18, 2017 01:07
a sketch of theme-like behaviour for base r plots
# styles for plotting
library(default)
.old_par <- list()
.current_style <- list()
.shims <- new.env()
remove_shims <- function () {
if ("shims" %in% search())
detach ("shims")
@goldingn
goldingn / eco_evo_on_cran.R
Created September 28, 2017 07:09
how many R packages on CRAN mention ecology or evolution in their descriptions?
devtools::install_github("RhoInc/CRANsearcher")
pkg <- CRANsearcher:::getPackages()
strings <- paste(pkg[, "Title"], pkg[, "Description"])
idx <- grep(" ecolog*| evolut*", strings, ignore.case = TRUE)
length(idx)
# [1] 236
# fake data
n <- 1000
m <- 50
x <- matrix(rnorm(n * m), n, m)
b <- rnorm(m, 0, 2) * rbinom(m, 1, 0.2)
eta <- x %*% b
y <- rnorm(n, eta, 0.3)
library (greta)
# devtools::install_github('goldingn/greta')
devtools::load_all()
fixed_values <- function (...) {
# get the values and their names
values <- list(...)
names <- names(values)
stopifnot(length(names) == length(values))
library(greta)
# to be overwritten with correct method
greta_here <- function ()
invisible(NULL)
# create an environment, and define the model there
greta_model <- function (model_expression,
parameters = list()) {
env <- new.env()