Skip to content

Instantly share code, notes, and snippets.

View jkeirstead's full-sized avatar

James Keirstead jkeirstead

View GitHub Profile
credentials.py
__pycache__/
archived_tweets*.json
@jkeirstead
jkeirstead / secret_santa.R
Created November 20, 2016 14:54
Draw names for a Secret Santa gift exchange
# James Keirstead
# 20 November 2016
library(TSP)
#' Draw names for a Secret Santa gift exchange
#'
#' In a 'Secret Santa' gift exchange, a group of people are randomly divided
#' into pairs. These pairs could be drawn in many different ways, depending on
#' whether the pairs are reciprocal and whether all pairs combinations are
category value sector
UK production emissions 632 UK
Carbon flows from EU 88 EU
Carbon flows to EU -61 EU
Carbon flows from other Annex 1 82 Annex 1
Carbon flows to other Annex 1 -39 Annex 1
Carbon flows from non-Annex 1 104 Other non-Annex 1
Carbon flows from non-Annex 1 64 China
Carbon flows to non-Annex 1 -25 Non-Annex 1
UK consumption emissions 845 UK
@jkeirstead
jkeirstead / analysis.r
Last active August 29, 2015 14:05
Demo of error in poweRlaw's bootstrap_p
## Define the problem data
x <- read.csv("x-values.csv")$x
## Fit the power law. Looks okay with plots
library(poweRlaw)
mod <- conpl$new(x)
xmin <- estimate_xmin(mod, xmins=head(sort(x), n=10))
mod$setXmin(xmin)
pars <- estimate_pars(mod)
mod$setPars(pars)
@jkeirstead
jkeirstead / prose-vector.r
Last active August 29, 2015 14:05
Convert a vector of values to a prose list
##' Converts a vector of values to a formatted prose list
##'
##' @param vals a vector of values
##' @param oxford a boolean indicating whether Oxford comma should be used
##' @param and word before ultimate entry
##' @return a character string
prose_vector <- function(vals, oxford=FALSE, and="and") {
if (length(vals)>1) {
start <- head(vals, -1)
start <- paste0(start, collapse=", ")
@jkeirstead
jkeirstead / epsrc-fellowships-funnel.r
Last active June 11, 2019 15:33
A funnel plot analysis of EPSRC Fellowship success rates
# Analysis of EPSRC fellowship success rates
# 18 November 2013
# James Keirstead
##' Calculates the bounds for a funnel plot
##'
##' Calculates the upper and lower confidence interval limits for use
##' in a funnel plot. Assumes a binomial discrete distribution with a
##' probability of success theta.
##'
@jkeirstead
jkeirstead / ls_functions.r
Created September 4, 2013 13:57
List all functions in a R source file
## List the functions in a file, or if not specified the global environment
##
## @param src an optional source file
##
## @return a character vector of function names
ls_functions <- function(src=NULL) {
if (is.null(src)) {
classes <- sapply(ls(.GlobalEnv), function(x) class(eval(parse(text=x))))
} else {
e <- new.env()
@jkeirstead
jkeirstead / gp-predict.stan
Last active April 18, 2018 01:34
A demo of Gaussian processes using RStan
// Predict from Gaussian Process
// All data parameters must be passed as a list to the Stan call
// Based on original file from https://code.google.com/p/stan/source/browse/src/models/misc/gaussian-process/
data {
int<lower=1> N1;
vector[N1] x1;
vector[N1] y1;
int<lower=1> N2;
vector[N2] x2;
@jkeirstead
jkeirstead / district-heat-ahp-demo.r
Last active December 18, 2015 19:08
Demonstration of analytic hierarchy process applied to district heat load forecasting
## District heating AHP example
##
## This example demonstrates how the analytic hierarchy process can be
## used to select a preferred technique for forecasting the thermal
## load of a district heating system.
##
## James Keirstead (j.keirstead@imperial.ac.uk)
## Presented 25 June 2013 at ISIE Conference, Ulsan, South Korea
##
@jkeirstead
jkeirstead / AHP.r
Last active December 18, 2015 18:39
Calculates weights for use in the Analytic Hierarchy Process
#' Calculate analytic hierarchy process weights
#'
#' The analytic hierarchy process (AHP) can be used for multi-criteria
#' decision analysis. It performs a pairwise comparison of a list of
#' options, with a respect to a particular goal.
#'
#' @detail The script will ask you to compare pairs of options.
#' Answer each question with respect to the statement goal and use
#' scores from 1,3,5,7,9 (or their inverse) where 1 = equally
#' important, 3 = moderately preferred, 5 = strongly preferred, 7 =