This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |