Skip to content

Instantly share code, notes, and snippets.

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
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]
# 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)
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]
@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)
# devtools::install_github("briatte/ggnet")
library(network)
library(sna)
library(ggplot2)
library(viridis)
library(ggnet)
# random graph
set.seed(1)
n <- 500
# 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
@goldingn
goldingn / docker_rstudio.sh
Last active September 13, 2015 15:46
short bash script to start a docker rstudio-server instance on OSX
#!/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*
@goldingn
goldingn / rstudio_external.sh
Last active August 29, 2015 14:25
Tweak RStudio desktop on OSX to put plots and docs in external windows
#!/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'
@goldingn
goldingn / sophie_davison_barplot.R
Created July 21, 2015 14:49
barplot on binomial GLM results as an option for this series of tweets: https://twitter.com/SophDavison1/status/623491784530915328
# 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