Skip to content

Instantly share code, notes, and snippets.

View Lakens's full-sized avatar

Daniel Lakens Lakens

View GitHub Profile
@Lakens
Lakens / Meta-Analysis in R
Created August 24, 2014 07:06
Perform a meta-analysis in R
#Script based on Carter & McCullough (2014) doi: 10.3389/fpsyg.2014.00823
#Load Libraries
library(meta)
library(metafor)
#Insert effect sizes and sample sizes
es.d<-c(0.38,0.41,-0.14,0.63,0.22)
n1<-c(75,48,22,18,60)
n2<-c(75,52,21,20,55)
@Lakens
Lakens / Calculte_CI_Around_ES_Within_Design_MBESS
Last active August 29, 2015 14:06
Calculate Confidence Intervals around Effect Sizes for Within Designs using MBESS package. Use of the ci.pvaf function returns an error for within designs. The code below calculates CI for within designs.
library(MBESS)
Lims <- conf.limits.ncf(F.value = 7, conf.level = 0.90, df.1 <- 4, df.2 <- 50)
Lower.lim <- Lims$Lower.Limit/(Lims$Lower.Limit + df.1 + df.2 + 1)
Upper.lim <- Lims$Upper.Limit/(Lims$Upper.Limit + df.1 + df.2 + 1)
Lower.lim
Upper.lim
@Lakens
Lakens / gist:1afffd4c8237f94b821b
Last active August 29, 2015 14:06
Bayes Factors and p-values for independent t-test
#####
p1 <-numeric(200) #set up empty container for all p-values
bf1 <-numeric(200) #set up empty container for all bf
p2 <-numeric(200) #set up empty container for all p-values
bf2 <-numeric(200) #set up empty container for all bf
p3 <-numeric(200) #set up empty container for all p-values
bf3 <-numeric(200) #set up empty container for all bf
t <- 1.96 #set start point t-value
for (i in 1:201)
@Lakens
Lakens / gist:26a27f4b146489a85bb2
Created September 22, 2014 19:56
Simulate Z-scores
#####SIMULATE & PLOT Z-SCORES#########
nSims <- 100000 #number of simulated experiments
p <-numeric(nSims) #set up empty container for all simulated p-values
z <-numeric(nSims) #set up empty container for z-scores
for(i in 1:nSims){ #for each simulated experiment
x<-rnorm(n = 23, mean = 100, sd = 20) #produce simulated participants
y<-rnorm(n = 23, mean = 110, sd = 20) #produce simulated participants
t<-t.test(x,y) #perform the t-test
@Lakens
Lakens / gist:b53522d538063cc3bb1f
Last active August 29, 2015 14:06
Kuhberger Power Estimation
############DATA############
#All significant Z values: 1.9609,1.9611,1.9727,1.9859,2.0063,2.017,2.0188,2.0231,2.0316,2.0377,2.04,2.0444,2.0494,2.0579,2.062,2.0624,2.0662,2.0737,2.0802,2.0803,2.0821,2.0863,2.0895,2.095,2.0964,2.0983,2.1,2.1,2.1113,2.1219,2.124,2.1299,2.1314,2.1318,2.1444,2.1448,2.1481,2.1491,2.1551,2.1584,2.1599,2.1615,2.1622,2.163,2.1656,2.1711,2.1807,2.1836,2.1865,2.1879,2.1909,2.2068,2.2151,2.2205,2.2212,2.2237,2.2316,2.2369,2.2457,2.2572,2.2668,2.2716,2.2777,2.2892,2.2993,2.3001,2.3034,2.31,2.3171,2.3183,2.3232,2.3282,2.337,2.343,2.3455,2.3459,2.3473,2.3517,2.354,2.3543,2.36,2.3623,2.3697,2.3707,2.3767,2.3781,2.3833,2.3871,2.396,2.4221,2.43,2.4408,2.4463,2.4584,2.4714,2.4751,2.485,2.488,2.4889,2.5121,2.5141,2.5306,2.5387,2.5531,2.5698,2.5711,2.5726,2.5793,2.5803,2.5988,2.6322,2.6335,2.6396,2.6397,2.6468,2.6475,2.6664,2.6687,2.6788,2.702,2.7032,2.7212,2.7245,2.7289,2.729,2.7324,2.736,2.7371,2.7402,2.744,2.7537,2.7578,2.761,2.7775,2.7846,2.7929,2.7931,2.8054,2.8131,2.8474,2.8605,2.8657,2.87
@Lakens
Lakens / gist:9f5da48aa7e4ef6daf66
Created September 28, 2014 14:01
Simulate Pdistribution ML Fig4
nSims <- 11000 #number of simulated experiments
psig <-numeric(nSims) #set up empty container for all simulated p-values
pnonsig <-numeric(nSims) #set up empty container for all simulated p-values
z <-numeric(nSims) #set up empty container for z-scores
for(i in 1:nSims){ #for each simulated experiment
x<-rnorm(n = 38, mean = 100, sd = 20) #produce simulated participants
y<-rnorm(n = 38, mean = 108, sd = 20) #produce simulated participants
t<-t.test(x,y) #perform the t-test
if(t$p.value<=0.05&t$p.value>=0.01) {
@Lakens
Lakens / gist:7f516efbcc47de9d40a0
Last active August 29, 2015 14:07
PcurveTrueAndPhackedStudiesFig1_Fig2
# phack
# Requires
# {psych}
# See Also http://rynesherman.com/blog/phack-an-r-function-for-examining-the-effects-of-p-hacking/
phack <- function(initialN=50, hackrate=10, grp1M=0, grp2M=0, grp1SD=1, grp2SD=1, maxN=100, alpha=.05, alternative="greater", graph=TRUE, sims=100000) {
require(psych)
outmat <- matrix(NA, nrow=sims, ncol=6)
@Lakens
Lakens / z-scores
Created October 19, 2014 19:06
Simulate Z-scores v2
#####EFFECT WITH PUBLICATION BIAS#########
nSims <- 10000 #number of simulated experiments
z1 <-numeric(nSims) #set up empty container for z-scores
for(i in 1:nSims){ #for each simulated experiment
x<-rnorm(n = 23, mean = 100, sd = 20) #produce simulated participants
y<-rnorm(n = 23, mean = 114, sd = 20) #produce simulated participants
t<-t.test(x,y) #perform the t-test
if(t$p.value<0.05) {
#Adapted from on EV Nordheim, MK Clayton & BS Yandell https://www.stat.wisc.edu/~yandell/st571/R/append7.pdf
n.draw = 20 #number of tests you draw
mu = 0.3 #true difference
SD = 1
#determine sample sizes for three tests
library(pwr)
pwr.t.test(d=0.3, power=0.9, sig.level=0.05, type = "one.sample", alternative="two.sided")
pwr.t.test(d=0.3, power=0.9, sig.level=0.01, type = "one.sample", alternative="two.sided")
@Lakens
Lakens / plot v
Created November 18, 2014 13:47
plot v
#You will need to load the R package "hypergeo" to use the vstat function
library(hypergeo)
#Below, I'm vectorizing the function so that I can plot curves.
#The rest is unchanged from the vstat function by Stober-Davis & Dana.
#If you want to use R unbiased, remove the # before the Rsq adjustment calculation below
vstat <- Vectorize(function(n,p,Rsq)
{
#Rsq = Re(1-((n-2)/(n-p))*(1-Rsq)*hypergeo(1,1,(n-p+2)*.5,1-Rsq))