Skip to content

Instantly share code, notes, and snippets.

# devtools::install_github("briatte/ggnet")
library(network)
library(sna)
library(ggplot2)
library(viridis)
library(ggnet)
# random graph
set.seed(1)
n <- 500
@goldingn
goldingn / zoon_module_lookup.R
Last active May 27, 2016 08:16
functions to create a version/sha lookup table for modules
# functions to compile a package/version-sha lookup table
library(git2r)
ModuleVersion <- function (modulePath, version_string = '^Version: ') {
# given a path to a module, return the module version, or NA if it doesn't
# have one
# copy the roxygen header to a temporary file, in case the R code is unparseable
file.create(f <- tempfile())
lines <- readLines(modulePath, warn = FALSE)
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