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
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
# 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
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
# 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) |
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("briatte/ggnet") | |
library(network) | |
library(sna) | |
library(ggplot2) | |
library(viridis) | |
library(ggnet) | |
# random graph | |
set.seed(1) | |
n <- 500 |
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
# make a nice viridis GMRF tesselation for Richard | |
rm(list = ls()) | |
set.seed(1) | |
library(INLA) | |
library(raster) | |
library(viridis) | |
library(fields) | |
# grid sizes for sampling the GRF and for the final image |
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
#!/bin/bash | |
# Trigger an Rstudio-server instance in docker, linked to the current filesystem | |
# and open in a browser. | |
# **WARNING** this will create a .gitconfig file in the working directory, potentially overwriting one that's already there! | |
# it will also create a .rstudio folder, and possibly other things | |
# This script should be run from the Docker Quickstart Terminal on OSX. | |
# I.e. it uses the an up-to-date docker installation which handles the VM side of things, *not boot2docker* |
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
#!/bin/bash | |
# modify RStudio's options to send plots to quartz by default, | |
# and open help and local html in the default browser | |
echo " | |
# ~~~~~~~~~~~ | |
# Tweaks to view help and html in the browser, and plots in quartz, not the RStudio viewer pane | |
browser <- '/usr/bin/open' | |
device <- 'quartz' |
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
# set RNG seed | |
set.seed(1) | |
# make some fake data | |
n <- 100 | |
m <- 5 | |
y <- rbinom(n, 1, 0.85) | |
x <- sample(letters[1:m], n, replace = TRUE) | |
# fit a logistic regression model |