Skip to content

Instantly share code, notes, and snippets.

View JimGrange's full-sized avatar

Jim Grange JimGrange

View GitHub Profile
# step 1 (before)
tmp <- installed.packages()
installedpackages <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
save(installedpackages, file="installed_packages.rda")
# now install new version of R
# step 2 (after)
load("installed_packages.rda")
for(i in 1:length(installedpackages)){
@JimGrange
JimGrange / correlation_power_calculations.R
Last active October 22, 2019 08:09
generate table of required sample sizes for various correlation effect sizes and various levels of desired power in a study
# generate table of required sample sizes for various correlation effect sizes
# and various levels of desired power in a study
# you need the pwr package
library(pwr)
# vector of effect sizes to explore
effect_sizes <- seq(from = 0.05, to = 0.95, by = 0.05)
# vector of desired power
@JimGrange
JimGrange / mazes.csv
Created March 3, 2020 20:59
Blindfold Chess Piece Mazes
Piece Start End
Bishop a4 e8
Rook e6 d5
Bishop b3 a4
Knight h4 c4
Knight b1 d5
Bishop d6 b4
Knight e5 a1
Queen g3 e7
Bishop b8 a1
#------------------------------------------------------------------------------
### Packages & functions
library(dplyr)
library(afex)
library(here)
# generate data from a multivariate normal distribution with known
# correlations between values
mvrnorm <- function(n, nValues, meanValues, sdValues, corMatrix) {
@JimGrange
JimGrange / gist:9ba16e2aa515b8e9913cc55c07f57c63
Last active December 16, 2020 11:23
Power analysis when true effect larger than planned effect
library(pwr)
library(effsize)
library(ggplot2)
# set "true" effect size and the effect size entered into power analysis
true_effect <- 0.2
power_effect <- 1.0
# How many participants required?
power_analysis <- pwr.t.test(d = power_effect,
# iterate over starting parameters and assess density-----------------
do_fit <- function(current_data){
# mu <- seq(from = 0.2, to = 1.8, by = 0.2)
# sigma <- seq(from = 0.05, to = 0.65, by = 0.2)
# tau <- seq(from = 0.05, to = 0.85, by = 0.2)
mu <- 0.5
sigma <- 0.05
# sample size
n <- 500
# simulate data for each predictor
# (note predictor b is categorical)
predictor_a <- rnorm(n, 0, 1)
predictor_b <- rbinom(n, 1, .5)
# population-level beta values
b_predictor_a <- -0.20
library(tidyverse)
library(rstan)
rstan_options(auto_write = TRUE)
#--- declare the data
# n_c = total number of people who drink caffeine
# n_nc = total number of people who do not drink caffeine
# p_c = observed proportion of people who drink caffeine that have a favourite mug
# p_nc = observed proportion of people who drink caffeine that DO NOT have a favourite mug
stan_data <- list(
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)
@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],