Skip to content

Instantly share code, notes, and snippets.

View davebraze's full-sized avatar

Dave Braze davebraze

View GitHub Profile
@davebraze
davebraze / block-randomize.R
Last active February 2, 2018 19:59
Block randomize a stimulus list with dplyr
library(dplyr)
## Toy data
df <- data_frame(block=rep(1:3, each=4), item=rep(1:4,3),
cond=rep(LETTERS[1:2], 6),
stim=paste0(rep(letters[1:2],6), rep(1:6,each=2)))
## Randomize blocks and trials within blocks
df.rand <- df %>%
group_by(block) %>%
@davebraze
davebraze / random-effects.R
Last active September 27, 2018 10:46
Basic random effect structures and non-convergence in lmer()
library(lme4)
library(nlme)
library(ggplot2)
data(Oxboys)
head(Oxboys, 12)
ggplot(aes(y=height, x=age), data=Oxboys) +
geom_point() + geom_path(aes(group=Subject))
@davebraze
davebraze / multicollinearity.R
Last active March 29, 2018 15:33
Assessing Multi-collinearity
library(car)
library(Hmisc)
library(perturb)
n <- 200
x0 <- rnorm(n)
x1 <- rnorm(n)
x2 <- x1 + rnorm(n)/2
x3 <- x2 + rnorm(n)/1.25
@davebraze
davebraze / logits.R
Last active February 2, 2018 19:57
probabilities, odds, log odds (logits)
p <- seq(0,1,.05)
odds <- p/(1-p)
logOdds <- log(odds)
cbind(p, odds, logOdds)
## p odds logOdds
## [1,] 0.00 0.000000 -Inf
## [2,] 0.05 0.052632 -2.94444
## [3,] 0.10 0.111111 -2.19722
@davebraze
davebraze / fixations.R
Last active March 6, 2018 15:46
Convert wide format fixation report to long format samples
library(tidyverse)
## toy fixation report
fixations <- tibble(fixidx=as.integer(1:3), start=c(100,250,372), end=c(202, 348, 426),
x=sample(0:1000,3), y=sample(0:1000,3))
## convert to long format
fixations <- fixations %>%
gather(start, end, -c(fixidx,x,y)) %>%
arrange(fixidx) %>%
@davebraze
davebraze / mlm-df-p.R
Last active February 27, 2019 19:04
place holder for thoughts on df correction and pvals in MLMs
lmerTest:: satterthwaite df estimation
pbkrtest:: kenward-roger df estimation; also parametric-bootstrap per Judd, Westfall & Kenny (2012)
mertools:: provides prediction intervals for parameters estimates in lme4 models.
semTools:: supplements lavaan.
mcmcglmm:: bayesian mixed models.
glmmPQL::
plotmcmc::
GLMM FAQ:
@davebraze
davebraze / level-order.R
Last active September 25, 2018 15:29
Basics of factor level ordering
##### Basic factor level ordering and (treatment) contrasts
## set up data.frame with 1 continuous variable and 1 factor with 8 levels.
set.seed(1234)
x <- rnorm(80)
fac <- factor(rep(LETTERS[8:1], 10))
df <- data.frame(x, fac)
df$x[as.integer(df$fac) %% 5 == 0] <- rnorm(10, 1)
head(df, 16) ## Note the order of factor levels in this data is reverse
@davebraze
davebraze / R-tables.md
Last active July 30, 2023 04:59
R Tables

Intro

Getting the right tables for a project can be fiddly in R, both for content and format. What I really prefer is clean separation between functions for generating content of a table, from those to do with its formatting. I am often, but not always, working in the context of an rmarkdown based workflow. There, fine control over format details will usually require making use of tools peculiar to the output type of the document (pdf, html, etc). This can complicate things a bit.

Table Content

These methods and tools are primarily about getting table content right.

Summary tables from dataframes

dplyr::

@davebraze
davebraze / R-ggplot2-tools.md
Last active June 28, 2019 17:40
ggplot2 tools and techniques
@davebraze
davebraze / yaml-rmarkdown.md
Last active June 30, 2019 17:47
Access yaml variables within rmarkdown file

Given a yaml block like this:

---
title: My Data Summary (DRAFT)
subtitle: Fall 2019 Data
author: David Braze
institute: Haskins Laboratories
date: "`r format(Sys.time(), '%B %d, %Y')`"