Skip to content

Instantly share code, notes, and snippets.

extend <- function (x, factor = 2) {
# given an evenly-spaced vector `x` of cell centre locations, extend it to the
# shortest possible vector that is at least `factor` times longer, has a
# length that is a power of 2 and nests the vector `x` approximately in the
# middle. This is used to define each dimension of a grid mapped on a torus
# for which the original vector x is approximately a plane.
# get cell number and width
n <- length(x)
width <- x[2] - x[1]
# an 'unpack' operator, to do python/MatLab-like multiple assignment from
# functions
`%=%` <- function (expr, list) {
if (!is.list(list))
stop ('the right hand side must be a list')
# grab the names to assign to
expr <- substitute(expr)
simulate_graf <- function (graf_model, n = 10, newdata = NULL) {
# simulate n draws from the predictive posterior of a GRaF model, optionally
# to new data
# get the mean of the latent Gaussian process
mean_f <- predict(graf_model,
newdata = newdata,
type = 'latent',
CI = NULL)[, 1]
as_column_array <- function (x) {
x <- as.array(x)
if (length(dim(x)) == 1)
dim(x) <- c(dim(x), 1)
x
}
add_attribute <- function (object, attribute, name) {
attr(object, name) <- attribute
object
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()
# 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))
# 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)
@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
@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 / 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)