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
# 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 |
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
# 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 |
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) |
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
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) | |
} |
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
# styles for plotting | |
library(default) | |
.old_par <- list() | |
.current_style <- list() | |
.shims <- new.env() | |
remove_shims <- function () { | |
if ("shims" %in% search()) | |
detach ("shims") |
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
# 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('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
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() |