Skip to content

Instantly share code, notes, and snippets.

View Lakens's full-sized avatar

Daniel Lakens Lakens

View GitHub Profile
@Lakens
Lakens / probabilityofp-values.R
Last active August 29, 2015 14:17
probabilities of p-values
nSims <- 100000 #number of simulated experiments (the more, the more accurate the numbers you get, but the longer it takes. I used 1000000 simulations for my blog)
N<-32 #number of participants
lowp<-0.04
highp<-0.05
#set up some variables
p<-numeric(nSims)
p2<-numeric(nSims)
obs_pwr<-numeric(nSims)
t<-numeric(nSims)
@Lakens
Lakens / Bem_Meta.R
Created April 4, 2015 13:22
Bem_Meta.r
#The necessary packages (please ensure that these have been #installed before running the following code).
#Many of the analyses are performed with both packages to double-check the calculations
library(meta)
library(metafor)
#The data for the full sample. All outputs that end in ".all" #are based on the full sample.
r=0.5 #set correlation between dv's to 0.5, using formula's for variance from dependent test for one-sample t-test
#effect sizes
d.all<-c(-0.054458115, 0.093112835, 0.206774549, 0.478004184, 0.141213676, 0.272767619, 0.251, 0.195142683, 0.258913272, 0.204022676, 0.223, 0.145336391, 0.092630988, 0.192, 0.418607214, 0.290090298, 0.108461538, 0.316418861, 0.417283465, -0.043799784, 0.147113469, 0.139283883, 0.060796002, -0.113389342, 0, 0.114947393, 0.169535252, -0.048878416, -0.029138576, -0.004628788, 0.086060345, 0.231428571, 0.161220346, -0.050911688, 0.044376016, 0.185846777, 0.251, 0.52, 0.355321158, 0.02665009, 0.052177581, -0.011716899, -0.023772371, 0.252476027, 0.210898339, 0.067797354,
@Lakens
Lakens / p-values_within_boundaries.R
Last active August 29, 2015 14:21
p-values within boundaries
nSims <- 1000000 #number of simulated experiments
p <-numeric(nSims) #set up empty container for all simulated p-values
d <-numeric(nSims) #set up empty container for all simulated d's
n=20
for(i in 1:nSims){ #for each simulated experiment
x<-rnorm(n = n, mean = 0.68, sd = 1) #produce n simulated participants
#with mean=100 and SD=20
y<-rnorm(n = n, mean = 0, sd = 1) #produce n simulated participants
@Lakens
Lakens / BiasES.R
Last active August 29, 2015 14:22
Okada, K. (2013). Is omega squared less biased? A comparison of three major effect size indices in one-way ANOVA
#Simulate bias in eta-squared, omega-squared, and epsilon-squared.
#R script by Kensuke Okada from: Okada, K. (2013). Is omega squared less biased? A comparison of three major effect size indices in one-way ANOVA. Behaviormetrika, 40(2), 129-147.
muvec <- c(0.00,0.00,0.8,0.8)
meanmu <- mean(muvec)
sigb <- sum((muvec-meanmu)^2)/4
eta2p <- sigb/(sigb+1)
k <- length(muvec)
nsim <- 1000000 #Bias should decrease as sample size increases. Set simulations to 1000000 for best results
njs <- c(10,20,30,40,50,60,70,80,90,100)
#Additional Analyses of Nuijten et al: https://mbnuijten.files.wordpress.com/2013/01/nuijtenetal_2015_reportingerrorspsychology1.pdf
#First run the original script to read in the data: https://osf.io/e9qbp/
#Select only errors.
subdata<-subset(data, data$Error == TRUE)
subdata$pdif<-subdata$Reported.P.Value-subdata$Computed #Compute difference in p-values.
#Plot differences in reported and computed p-values for all errors
ggplot(as.data.frame(subdata$pdif), aes(subdata$pdif)) +
geom_histogram(colour="black", fill="grey", binwidth = 0.01) + ggtitle("All Errors") + xlab("Reported P-value minus Computed P-value") + ylab("Frequency") + theme_bw(base_size=20)
@Lakens
Lakens / SampleSizesforStudies.R
Last active November 2, 2015 10:23
Simulate data, perform sequential analyses using p-values and BF
set.seed(2)
options(scipen=20) #disable scientific notation for numbers
nSim<-10 #numbber of simulated studies
library(pwr)
library(MBESS)
library(gsDesign) # The group sequential design package
library(BayesFactor)
@Lakens
Lakens / ErrorControlANOVA.R
Created January 1, 2016 09:50
Holm Error Control Simulation 2x2x2 ANOVA
library(reshape2)
library(mvtnorm)
library(ez)
#Install multtest# try http:// if https:// URLs are not supported
source("https://bioconductor.org/biocLite.R")
biocLite("multtest")
library(mutoss) #load multiple testing library for Holm function
#2x2x2 within design
N<-50 #sample size per group
@Lakens
Lakens / BayesianPowerTtest.R
Created January 14, 2016 12:37
Bayesian Power Analysis for an Independent t-test
#Bayesian Power Analysis
if(!require(BayesFactor)){install.packages('BayesFactor')}
library(BayesFactor)
D<-0.0 #Set the true effect size
n<-50 #Set sample size of your study (number in each group)
nSim<-100000 #Set number of simulations (it takes a while, be patient)
rscaleBF<-sqrt(2)/2 #Set effect size of alternative hypothesis (default = sqrt(2)/2, or 0.707)
threshold<-3 #Set threshold for 'support' - e.g., 3, 10, or 30
@Lakens
Lakens / spuriouscorrelation.R
Created January 29, 2016 17:21
spuriouscorrelationRPP
if(!require(ggplot2)){install.packages('ggplot2')}
library(ggplot2)
if(!require(MBESS)){install.packages('MBESS')}
library(MBESS)
#Set color palette
cbbPalette<-c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
N<-20
#Set mean and SD
@Lakens
Lakens / GKPW.R
Created March 6, 2016 08:47
GKPW.R
setwd("C:/Users/Daniel/Downloads/Gilbert, King, Pettigrew, Wilson 2016 replication files/variability analysis replication files/data")
load("many labs replication cis.RData")
## Drop the top rows which are statistics from pooling together all the replications
res <- lapply(res, function(x) x[-c(1:2),])
res[[12]] <- res[[12]][-1,]
names(res[[16]])[3:5] <- names(res[[15]])[3:5]
## For each replicated study, get the number of the other replicated
## studies that were outside the CI