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 / helper_functions.r
Last active April 23, 2024 13:03
Stan vs LME4 for hiearchical within-subjects designs with binomial outcomes
#' Installs any packages not already installed
#' @examples
#' \dontrun{
#' install_if_missing(c('tidyverse','github.com/stan-dev/cmdstanr'))
#' }
install_if_missing = function(pkgs){
missing_pkgs = NULL
for(this_pkg in pkgs){
@mike-lawrence
mike-lawrence / eeg2wav.R
Created May 13, 2012 01:31
Convert EEG data to wav
#This code assumes data is a matrix, stored as "a", where rows specify samples through time and columns specify different electrodes.
#This code also assumes that the colnames of the matrix specify the names of the electrodes using the 10-20 naming system
#get the last character of the 10-20 name (which should be the electrode number)
enum = substr(colnames(a),nchar(colnames(a)),nchar(colnames(a)))
#change any enums of zero to 10
enum[enum=='0']='10'
#create a list of column numbers
@mike-lawrence
mike-lawrence / mike_podcast_picks.txt
Created February 4, 2023 15:07
Mike’s podcast picks!
Mike’s Podcast picks
————
Comedic (all should be started at S01E01)
————
The adventure zone
Mission to Zyxx
Beef & Dairy Network Podcast
————
@mike-lawrence
mike-lawrence / hwg_sufficient.stan
Created January 18, 2023 19:39
hierarchical within-subjects gaussian with sufficient stats and reduce_sum
functions{
real partial_lpdf(
array[] real real_array_to_slice
, int start
, int end
, real sigma
, vector Zc
, matrix iZc_chol_mat
, matrix iZc
, array[] matrix iZc_
@mike-lawrence
mike-lawrence / PygameMultiprocessingIO.py
Created November 18, 2011 01:11
pygame & multiprocessing for IO
#import used modules
import sys
import time
import multiprocessing
#create a multiprocessing Queue object for sending messages to an input process
queueToInputProcess = multiprocessing.Queue()
#create a multiprocessing Queue object for receiving messages from an input process
@mike-lawrence
mike-lawrence / cfa.stan
Created March 21, 2017 15:10
Demo using Stan for Confirmatory Factor Analysis
data{
# n_subj: number of subjects
int n_subj ;
# n_y: number of outcomes
int n_y ;
# y: matrix of outcomes
matrix[n_subj,n_y] y ;
# n_fac: number of latent factors
int n_fac ;
# y_fac: list of which factor is associated with each outcome
@mike-lawrence
mike-lawrence / _.rproj
Created January 17, 2021 19:43
Hierarchical Within-Subjects model with Gaussian measurement noise
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: No
QuitChildProcessesOnExit: Yes
EnableCodeIndexing: Yes
UseSpacesForTab: No
NumSpacesForTab: 4
@mike-lawrence
mike-lawrence / helper_functions.r
Created January 5, 2021 05:41
hierarchical within-subjects model with Gaussian outcome & reduced-redundant-computation
#' Installs any packages not already installed
#' @examples
#' \dontrun{
#' install_if_missing(c('tidyverse','github.com/stan-dev/cmdstanr'))
#' }
install_if_missing = function(pkgs){
missing_pkgs = NULL
for(this_pkg in pkgs){
@mike-lawrence
mike-lawrence / composed_divergences.r
Last active January 3, 2021 21:19
composed_divergences
#preamble (installs/imports & custom functions) ----
# specify the packages used:
required_packages = c(
'rethinking' #for rlkjcorr & rmvrnom2
, 'crayon' #for coloring terminal output
, 'tidyverse' #for all that is good and holy
, 'progress' #for progress bar
, 'github.com/stan-dev/cmdstanr' #for Stan stuff
, 'github.com/mike-lawrence/ezStan' #for extra Stan stuff (here just get_contrast_matrix)
@mike-lawrence
mike-lawrence / binom_sim.r
Last active November 27, 2020 15:31
code to generate and plot posterior distributions for binomial outcomes
# load packages and define helper functions ----
library(cmdstanr) #for bayes goodness
library(posterior) #for posterior::as_draws_df()
library(progress) # for progress bars
library(viridis) #for better color scales
library(tidyverse) #for all that is good and holy
# helper function to create a progess bar in the global env
create_global_pb = function(x){
pb <<- progress::progress_bar$new(