Skip to content

Instantly share code, notes, and snippets.

View Lakens's full-sized avatar

Daniel Lakens Lakens

View GitHub Profile
@Lakens
Lakens / p_not_evidence.R
Created November 22, 2021 19:38
Why p-values are not measures of evidence
library(metafor)
g <- escalc(
measure = "SMD",
n1i = 43, # sample size in group 1 is 50
m1i = 1.3, # observed mean in group 1 is 5.6
sd1i = 1, # observed standard deviation in group 1 is 1.2
n2i = 43, # sample size in group 2 is 53
m2i = 1, # observed mean in group 1 is 4.9
sd2i = 1
@Lakens
Lakens / segmented_vs_sequential.R
Created August 29, 2020 07:49
Segmented vs. Sequential Analyses
library(segHT)
library(rpact)
looks <- 3
n_seg <- 50
alpha_level <- 0.05
true_d <- 0.5 # can not enter 0, segmented_hyp_test_outcomes gives error
###############################
# Segmented procedure ----
---
title: "Power Analysis for Interactions"
author: "Daniel Lakens"
date: "28-3-2020"
output:
html_document: default
pdf_document: default
word_document: default
editor_options:
chunk_output_type: console
@Lakens
Lakens / sim_study_scienceverse_2.R
Created March 12, 2020 09:21
scienceverse example for family-wise error control v2
# Scienceverse Sim
# install scienceverse
# devtools::install_github("scienceverse/scienceverse")
library(scienceverse)
library(faux)
set.seed(2) # set.seed(2) is a random draw where H1 is corroborated.
@Lakens
Lakens / sim_study_scienceverse_1.R
Created March 12, 2020 09:20
scienceverse example for family-wise error control v1
# Scienceverse Sim
# install scienceverse
# devtools::install_github("scienceverse/scienceverse")
library(scienceverse)
library(faux)
set.seed(2) # set.seed(2) is a random draw where H1 is corroborated.
@Lakens
Lakens / plot_single_pvalue_over_time.R
Created January 11, 2020 21:48
plot single p-value over time
n<-2000 #total number of datapoints (per condition) you are willing to collect after initial 10
D<-0.0 #True effect size (Keep SD below to 1, otherwise, this is just mean dif, not d)
SD<-1 #Set True standard deviation.
p<-numeric(n) #store p-values
x<-numeric(n) #store x-values
y<-numeric(n) #store y-values
n<-n+10 #script calculates p-values after 10 people in each condition, so add 10 to number of datapoints
---
title: "Observed Alpha Levels"
output:
word_document: default
html_document:
df_print: paged
editor_options:
chunk_output_type: console
---
@Lakens
Lakens / power_one-sided_two-sided.R
Last active July 26, 2019 17:07
power for one-sided vs two-sided tests
# Code by Chelsea Parlett, small additions by Daniel Lakens
library(pwr)
library(ggplot2)
#set up vector of effect sizes
es <- seq(0.01,1, length = 1000)
#specify power for test
pow <- 0.8
#calculate ns (sample size for 80% power in two-sided test)
@Lakens
Lakens / CI_standard_deviation.R
Last active January 15, 2020 15:13
Calculating Confidence Intervals around Standard Deviations.
#Install pwr package if needed
if(!require(pwr)){install.packages('pwr')}
library(pwr)
alpha_level = 0.05 #set alpha level
n = 100 #set number of observations
st_dev = 1 #set true standard deviation
SESOI <- 0.5 #set smallest effect size of interest (raw mean difference)
# calculate lower and upper critical values c_l and c_u
@Lakens
Lakens / optimal_alpha.R
Last active May 13, 2019 19:15
justify alpha part 2
optimal_alpha <- function(power_function, costT1T2 = 1, prior_H1H0 = 1, error = "minimal") {
#Define the function to be minimized
f = function(x, power_function, costT1T2 = 1, prior_H1H0 = 1, error = "minimal") {
y <- 1 - eval(parse(text=paste(power_function)))
print(c(x, y, x+y)) #optional: print alpha, beta, and objective
if(error == "balance"){
max((costT1T2*x - prior_H1H0*y)/(prior_H1H0+1), (prior_H1H0*y - costT1T2*x)/(prior_H1H0+1))
} else if (error == "minimal"){
2*(costT1T2*x + prior_H1H0*y)/(prior_H1H0+1)
}