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
library(CircStats)
#define a data generator for the uvm model
get_mix_data = function( n , rho , kappa , seed ){
set.seed(seed)
to_return = rep(NA,n)
trial_is_vm = as.logical(rbinom(n,1,rho))
to_return[trial_is_vm] = rvm(sum(trial_is_vm),pi,kappa)
to_return[!trial_is_vm] = runif(sum(!trial_is_vm),0,2*pi)
to_return = to_return-pi #center the data on 0
ezANOVA <-
function(
data
, dv
, wid
, within = NULL
, between = NULL
, observed = NULL
, diff = NULL
, reverse_diff = FALSE
@mike-lawrence
mike-lawrence / ezPredict.R
Created December 28, 2010 15:45
A function to obtain predictions from a fitted lmer object.
#A function to obtain predictions from a fitted lmer object.
#If to_predict is null, the function will attempt to build a
#design matrix for prediction from the information in the fit
#itself. If a .() object is passed to as to_predict, variables
#listed in to_return are used to create the design matrix. A
#data frame may also be passed as to_predict.
ezPredict <-
function(
fit
@mike-lawrence
mike-lawrence / extrema_c.R
Created February 11, 2011 17:13
variants of EMD:extrema
extrema_c = function (y, ndata = length(y), ndatam1 = ndata - 1){
minindex <- maxindex <- NULL
nextreme <- 0
cross <- NULL
ncross <- 0
z1 <- sign(diff(y))
index1 <- seq(1, ndatam1)[z1 != 0]
z1 <- z1[z1 != 0]
if(!(is.null(index1) || all(z1 == 1) || all(z1 == -1))){
@mike-lawrence
mike-lawrence / get_vars_and_rel.R
Created April 6, 2011 23:19
an attempt to compute reliability (and contributing variances) using variance estimates from a mixed effects model
#define two useful helper functions
colMean = function(x){
dimx = dim(x)
.Internal(colMeans(x,dimx[1],dimx[2],na.rm=TRUE))
}
colVar = function(x){
dimx = dim(x)
x.mean = .Internal(colMeans(x,dimx[1],dimx[2],na.rm=TRUE))
err = t(t(x)-x.mean)
err.sq = err*err
@mike-lawrence
mike-lawrence / ezMixed_bug_R13.R
Created April 8, 2011 14:20
Demonstrates bugs associated with ezMixed and the mac GUI for R-13.0-beta
library(ez)
data(ANT)
########
#this will work
########
rt = ezMixed(
data = ANT[ANT$error==0,]
, dv = .(rt)
, random = .(subnum)
@mike-lawrence
mike-lawrence / analysis.R
Created April 11, 2011 19:02
minimal (well, somewhat) example to demonstrate strange time-travelling behaviour in the multiprocessing module
library(plyr)
a = read.table('temp.txt')
names(a) = c('trial','event','time')
a$row = as.numeric(row.names(a))
a$event = factor(a$event,levels=c('collect_data','A','B','C','write_data'))
b = ddply(
.data = a
, .variables = .(trial,event)
@mike-lawrence
mike-lawrence / 1_lba-math.R
Created April 25, 2011 21:48
Donkin et al LBA code and example of fitting the LBA to data
fptcdf = function(z,x0max,chi,driftrate,sddrift) {
zs = z*sddrift
zu = z*driftrate
chiminuszu = chi-zu
xx = chiminuszu-x0max
chizu = chiminuszu/zs
chizumax = xx/zs
tmp1 = zs*(dnorm(chizumax)-dnorm(chizu))
@mike-lawrence
mike-lawrence / gist:1122670
Created August 3, 2011 13:45
R config output
$ sudo ./configure --with-x=no --enable-R-shlib
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
loading site script './config.site'
loading build-specific script './config.site'
checking for pwd... /bin/pwd
checking whether builddir is srcdir... yes
checking for working aclocal... missing
checking for working autoconf... missing
checking for working automake... missing
@mike-lawrence
mike-lawrence / uvm.R
Created August 14, 2011 13:53
EM algorithms for estimating Uniform+VonMises mixture parameters from circular data
#Load the CicStats package
library(CircStats)
#Define a customization of circ.mean{CircStats} that permits weighting observations
circ.weighted.mean = function (x,rho){
sinr = sum(rho*sin(x))
cosr = sum(rho*cos(x))
circmean = atan2(sinr, cosr)
return(circmean)
}