Skip to content

Instantly share code, notes, and snippets.

View debruine's full-sized avatar
🏳️‍🌈

Lisa DeBruine debruine

🏳️‍🌈
View GitHub Profile
@debruine
debruine / design
Created May 14, 2019 14:01
Example design spec for scienceverse
library(faux)
within <- list(
pet = c("dog" = "Has a dog", "cat" = "Has a cat"),
time = c("day" = "Tested during the day", "night" = "Tested at night")
)
wcells <- faux:::cell_combos(within)
between <- list(
country = c("UK" = "United Kingdom", "NL" = "Nederland"),
@debruine
debruine / inatt.R
Created May 15, 2019 13:57
Power calculation for inattentive raters
library(faux)
library(tidyverse)
inatt <- function(within, between, n, mu, sd, r, prop_bad = 0) {
good_n <- n * (1-prop_bad)
bad_n <- n * prop_bad
good_dat <- sim_design(within, between, n = good_n, mu, sd, r)
if (bad_n > 0) {
# should r be 0 for inattentive responders?
bad_dat <- sim_design(within, between, n = bad_n, unlist(mu)%>% mean(), sd, r) %>%
@debruine
debruine / interactive_test
Last active May 16, 2019 11:29
Testing interactive input of study design
#devtools::install_github("debruine/faux")
library(faux)
# make unique pairs of level names to ask for cors
unique_pairs <- function(v) {
if (is.numeric(v)) { v <- LETTERS[1:v] }
expand.grid(a = v, b = v) %>%
filter(a != b) %>% t() %>%
tibble::as_tibble() %>%
@debruine
debruine / readline_check
Created May 16, 2019 13:04
R function to validate the input from readline
#' Check readline input
#'
#' @param prompt the prompt for readline
#' @param type what type of check to perform, one of c("numeric", "character", "length", "minlength", "maxlength", "exact", "grep")
#' @param compare the comparator for exact and (min|max)length types
#' @param ... other arguments to pass to grep
#'
#' @return the validated result of readline
#' @export
#'
library(faux)
library(tidyverse)
data <- sim_design(within = list(T = c("T1", "T2", "T3", "T4", "T5")),
id = "ID")
processed_data <- data %>%
gather(T, raw, T1:T5) %>% # put in long format
group_by(ID) %>% # start doing calculations within groups
mutate(Mean_T = mean(raw),
@debruine
debruine / proso_sim
Last active June 10, 2019 14:28
Simulate prosopagnosia data
#devtools::install_github("debruine/faux")
library(faux)
library(afex)
library(tidyverse)
## Null data (no effect)
des_null <- check_design(list(task = c("test", "white", "asian")),
n = 400,
mu = c(100, 100, 90),
#devtools::install_github("debruine/faux")
library(faux)
# compare the correlations between simulated normally distributed data and the likert-scale version
check_likert <- function(n = 100, r = 0.5,
prob = c(.05, .1, .2, .3, .2, .1, .05)) {
df <- faux::rnorm_multi(n, 2, 0, 1, r) %>%
dplyr::mutate(
L1 = norm2likert(X1, prob, 0, 1),
L2 = norm2likert(X2, prob, 0, 1)
@debruine
debruine / demo.Rmd
Created August 6, 2019 15:36
2 conditions pupil size example (bad defaults)
---
title: 'Example'
output: html_document
---
```{r, message=FALSE}
# load required packages
library("lme4") # model specification / estimation
library(faux)
library(tidyverse)
sub_sd <- 1 # subject intercept SD
stim_sd <- 1 # stimulus intercept SD
err_sd <- 2 # residual error SD
grand.i <- 600 # grand interecpt (mean RT)
ord_eff <- 0.5 # coefficient of order effect
sch_eff <- 0.5 # coefficient of schizotypy effect
ord_sch_ixn <- 0.5 # coefficient of order*schizotypy interaction
@debruine
debruine / sum_vars.R
Last active August 27, 2019 21:19
Create a list of summary variables by group
a <- rep(c(1, 2), each = 4)
b <- c(1,2,3,4,5,6,7,8)
frame <- data.frame(a,b)
library(dplyr)
library(tidyr)
x <- frame %>%
group_by(a) %>%
summarise(Mean = mean(b),