Skip to content

Instantly share code, notes, and snippets.

View jake-westfall's full-sized avatar

Jake Westfall jake-westfall

View GitHub Profile
@jake-westfall
jake-westfall / random_scale_item_sim.R
Created November 5, 2015 22:42
Estimate/test correlation between subjects' scores on two tests composed of random items
library(MASS) # for mvrnorm()
library(lme4) # for lmer()
# function to simulate data, fit models, do model comparison
sim <- function(n, # number of subjects
m, # number of items per test
mu, # mean for both tests
SD, # subject SD in test means
r, # correlation between subject test means
diff, # SD of item difficulties (intercepts)
dat <- read.table("http://pcl.missouri.edu/exp/effectSizePuzzler.txt", header=TRUE)
str(dat)
# 'data.frame': 2500 obs. of 3 variables:
# $ id : int 1 1 1 1 1 1 1 1 1 1 ...
# $ cond: int 1 1 1 1 1 1 1 1 1 1 ...
# $ rt : num 0.56 0.93 0.795 0.615 1.028 ...
(means <- with(dat, tapply(rt, cond, mean)))
# 1 2
@jake-westfall
jake-westfall / nested_contour.R
Created July 1, 2016 14:40
Produce statistical power contour plot for simple nested design w/ participants (pl) nested in labs (l)
# replace this with a path on your own machine
path <- "/Users/Jake/Desktop/nested_power.png"
# define function to compute statistical power
pow <- function(d, E, LC, l, pl){
t <- d/2/sqrt(E/(2^pl)/(2^l) + LC/(2^l))
DF <- (2^l) - 1
return(pt(qt(.975, DF), DF, ncp=t, lower.tail=F) +
pt(qt(.025, DF), DF, ncp=t))
}
# function to fold data into k folds. this returns a list of matrices where
# the 1st column in each is the response and all other columns are predictors
fold <- function(y, X, k){
n <- length(y)
lapply(0:(k-1)*n/k + 1, function(i){
cbind(y, X)[seq(from=i, length.out=n/k),]
})
}
# function to compute MSE for datasets with different numbers of folds
@jake-westfall
jake-westfall / RWiener.R
Created June 15, 2017 04:58
Simple illustration of getting maximum likelihood estimates of Wiener diffusion model (and profile likelihood confidence intervals) in R using RWiener and bbmle packages
library("RWiener")
library("bbmle")
# alpha = boundary separation parameter.
# tau = non-decision time parameter.
# beta = bias parameter.
# delta = drift rate parameter
dat <- rwiener(200, alpha=2, tau=.3, beta=.5, delta=1)
str(dat)
wiener_plot(dat)
@jake-westfall
jake-westfall / conditioning.R
Created December 3, 2018 15:50
Illustration of the concept of conditioning on a random variable
library("scatterplot3d")
library("MASS")
path <- "/Users/jakewestfall/Desktop/"
# simulate data from gaussian copula
covmat <- matrix(.9, nrow=3, ncol=3)
diag(covmat) <- 1
dat <- pnorm(mvrnorm(n=3000, mu=c(0,0,0), Sigma=covmat))
# pairs(dat)