Skip to content

Instantly share code, notes, and snippets.

View JimGrange's full-sized avatar

Jim Grange JimGrange

View GitHub Profile
@JimGrange
JimGrange / regression_power.r
Created October 18, 2024 06:01
Simulating regression models for power estimates
library(tidyverse)
# Set the effect size
eff_size <- 0.2
# Function to simulate data, run regression, and check if individual predictors are significant
simulate_and_regress <- function(n) {
data <- tibble(
a = rnorm(n),
b = rnorm(n),
@JimGrange
JimGrange / sim_linear.r
Created October 18, 2024 06:00
Simulating effects of sample size and trial numbers from linear mixed models
library(tidyverse)
library(faux)
library(lme4)
library(afex)
options(dplyr.summarise.inform = FALSE)
set.seed(123)
sim_data <- function(n_subj, n_trials){
@JimGrange
JimGrange / individual_CAFs.R
Created January 31, 2024 11:08
flankr individual CAFs
library(tidyverse)
library(flankr)
# get example data from flankr
# ONLY SHOWING FOR CONGRUENT DATA.
d <- flankr::exampleData %>%
filter(congruency == "congruent")
# how many subjects?
n_subjects <- length(unique(d$subject))
@JimGrange
JimGrange / gist:2319340015675ad075fea4b7acee9b43
Created November 29, 2023 08:02
poole_cuing_analysis.R
# load packages -----------------------------------------------------------
library(tidyverse)
library(brms)
library(tidybayes)
library(bayesplot)
library(emmeans)
library(bayestestR)
@JimGrange
JimGrange / brm.R
Last active November 20, 2023 14:09
interaction term in brms
library(brms)
library(tidyverse)
set.seed(123)
# generate some subject-averaged data
n_subjects <- 100
data <- tibble(
id = 1:n_subjects,
@JimGrange
JimGrange / lmm_agg.R
Created November 10, 2023 14:57
linear modelling vs. aggregate analysis
library(tidyverse)
library(faux)
library(lme4)
library(afex)
options(dplyr.summarise.inform = FALSE)
set.seed(123)
sim_data <- function(n_subj, n_trials){
@JimGrange
JimGrange / contingency_bf.R
Created June 1, 2023 06:41
Bayes factor for contingency table
library(tidyverse)
library(BayesFactor)
# set seed for reproducibility
set.seed(234)
# define population-level parameters
p_text_yes <- 0.5
p_video_yes <- 0.5
@JimGrange
JimGrange / ectva_power_sims.R
Created November 29, 2021 11:31
Power analysis to support EPS small grant submission
library(tidyverse)
library(Superpower)
# set means & standard deviations for idealised data
# (note experiment 1 data comes from the pilot experiment)
exp_1_means <- c(1334, 1594, 1588, 1725)
exp_1_sds <- c(308, 295, 338, 343)
exp_2_means <- c(1030, 1080, 1120, 1150)
exp_2_sds <- c(200, 200, 200, 200)
@JimGrange
JimGrange / functions.r
Created November 13, 2021 09:07
Generate heatmaps of density estimates for NFL pass locations (https://bit.ly/3DdGac2)
# gaussian kernel function
gaussian_kernel <- function(u){
(1 / sqrt(2 * pi)) * exp(-0.5 * u ^ 2)
}
# kernel density estimate function
kde <- function(n, data, x_limit, y_limit, h_x, h_y){
x <- seq(from = x_limit[1],
mod_1 <- brm(value ~ questionnaire * condition,
data = idealised_data,
seed = 42,
cores = 4)
mod_2 <- brm(value ~ questionnaire + condition,
data = idealised_data,
seed = 42,
cores = 4)