Skip to content

Instantly share code, notes, and snippets.

View mike-lawrence's full-sized avatar

Mike Lawrence mike-lawrence

  • Halifax, Nova Scotia, Canada
View GitHub Profile
@mike-lawrence
mike-lawrence / gp_example.R
Created December 20, 2016 20:13
Example application of Gaussian Process modelling
#load packages
library("curl")
library("ggplot2")
library("rstan")
#retrieve the data
tmpf = tempfile()
curl_download("http://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.5.0.0.annual_ns_avg.txt", tmpf)
gtemp = read.table(tmpf, colClasses = rep("numeric", 12))[, 1:2] # only want some of the variables
names(gtemp) = c("Year", "Temperature")
@mike-lawrence
mike-lawrence / heavy_partially_pooled.stan
Created November 30, 2016 22:18
Multiple comarisons & Stan
data {
#N: number of cases
int N ;
#: K: number of outcome variables
int K ;
#X: matrix of outcomes for each case & variable
matrix[N,K] X ;
#Y: outcomes
vector[N] Y;
}
@mike-lawrence
mike-lawrence / gen_data_and_sample.R
Last active October 12, 2016 15:11
Exploring consequences of failing to model correlated within-subject parameters
library(rstan)
library(MASS)
# Generate some fake data -------------------------------------------------
set.seed(1)
N = 100 #number of subjects
K = 10 #number of observations per subject per condition
#population parameters
@mike-lawrence
mike-lawrence / covMat_by_fun.stan
Last active September 14, 2016 18:04
Stan cov_exp_quad performance test
data {
// N: num unique x values
int<lower=1> N ;
// x: x values
real x[N] ;
// y: data
vector[N] y ;
}
transformed data{
vector[N] zeros ;
@mike-lawrence
mike-lawrence / sim_and_sample.r
Last active September 9, 2016 21:29
Weird Stan behaviour
library(rstan)
#set random seed for reproducibility
set.seed(1)
#generate fake data
#variables setting data size
N = 30 #number of x-axis grid points
M = 2 #number of noisy replicates per grid point
@mike-lawrence
mike-lawrence / many_reps.r
Last active December 3, 2016 04:31
GPs with many replicates per grid point
library(ggplot2)
library(rstan)
N = 100 #number of x-axis grid points
M = 10 #number of noisy replicates per grid point
noise = 1 #amount of noise added to each replicate
#define the x-axis grid
x = seq(-10,10,length.out=N)
@mike-lawrence
mike-lawrence / hmod.R
Created August 14, 2016 19:29
Hierarchical multivariate modelling using RStan
# Example model from a typical Psychology experiment where mutiple
# human subjects each contribute multiple observations ("trials")
# in each of two conditions. We model the subject population as
# having a mean intercept and mean difference-between-conditions,
# and subjects as having potentially-correlated deviations from
# these means. For a given subject in a given condition, trial-by-
# trial observations are sampled with normal error, with a common
# error magnitude used across all subjects/conditions.
#clear workspace to ensure a fresh start
Last login: Tue Jun 28 15:06:37 on ttys003
Mikes-MacBook-Air:~ mike$ R
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
@mike-lawrence
mike-lawrence / gp_multi_effect.R
Last active May 27, 2016 17:30
Gaussian Process modelling of an intercept+effect model with noisy replicates
library(ggplot2)
library(rstan)
#set random seed for reproducibility
set.seed(1)
NX = 100 #number of x-axis grid points
NZ = 10 #number of noisy replicates per condition
#define the x-axis grid
@mike-lawrence
mike-lawrence / global_temp_gp_fit.R
Last active September 23, 2016 15:22
Global Temperature data modelled by a Gaussian Process
#load packages
library("curl")
library("ggplot2")
library("rstan")
#retrieve the data
tmpf <- tempfile()
curl_download("http://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.5.0.0.annual_ns_avg.txt", tmpf)
gtemp <- read.table(tmpf, colClasses = rep("numeric", 12))[, 1:2] # only want some of the variables
names(gtemp) <- c("Year", "Temperature")