Skip to content

Instantly share code, notes, and snippets.

View Lakens's full-sized avatar

Daniel Lakens Lakens

View GitHub Profile
@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 / emailing_students_from_R.R
Created September 2, 2018 13:36
Emailing students from R using mailR package
#Load packages
library(readxl)
library(mailR)
#Read student data
info <- read_excel("student_names_email.xls",
sheet = 1,
col_names = TRUE)
#Loop from 1 to the number of email addresses in the spreadsheet
---
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 / 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)
}
@Lakens
Lakens / standardized_alpha_power_analysis.R
Created December 1, 2018 21:08
Calculating standardized p-values and alpha levels (Good, 1982)
#Standardized p-value
#This is untested alpha software provided with no guarantees - use at your own risk
#Good 1982: P SQRT(N/100)
#Enter the observed p-value and the sample size
p_stan <- function(p, N){
p * sqrt(N/100)
}
@Lakens
Lakens / CI_vs_CP.R
Created March 2, 2016 04:54
confidence intervals vs capture percentages
if(!require(ggplot2)){install.packages('ggplot2')}
library(ggplot2)
n=20 #set sample size
nSims<-100000 #set number of simulations
x<-rnorm(n = n, mean = 100, sd = 15) #create sample from normal distribution
#95%CI
CIU<-mean(x)+qt(0.975, df = n-1)*sd(x)*sqrt(1/n)
@Lakens
Lakens / PlotScopusData.R
Created December 13, 2015 13:35
PlotScopusData.R
require(ggplot2)
#Save downloaded Scopus data in your working directory
scopusdata<-read.csv("scopusPS2010_2015.csv")
plot1<-ggplot(scopusdata, aes(x=Cited.by)) +
geom_histogram(colour="#535353", fill="#84D5F0", binwidth=2) +
xlab("Number of Citations") + ylab("Number of Articles") +
ggtitle("Citation Data for Psychological Science 2011-2015") +
coord_cartesian(xlim = c(-5, 250))
@Lakens
Lakens / 4study_meta_50%_true_effects.R
Created April 30, 2016 16:29
Internal meta-analysis on 4 studies, 50% of which are true effects
if(!require(meta)){install.packages('meta')}
library(meta)
nSims <- 1000000 #number of simulated experiments
numberstudies<-4 # nSim/numberofstudies should be whole number
p <-numeric(nSims) #set up empty container for all simulated p-values
metapran <-numeric(nSims/numberstudies) #set up empty container for all simulated p-values for random effects MA
metapfix <-numeric(nSims/numberstudies) #set up empty container for all simulated p-values for fixed effects MA
heterog.p<-numeric(nSims/numberstudies) #set up empty container for test for heterogeneity
d <-numeric(nSims) #set up empty container for all simulated d's
@Lakens
Lakens / p-value_misconceptions_figures.R
Created December 5, 2017 11:51
figures to explain p-value misconceptions
options(scipen=999) #disable scientific notation for numbers
#Figure 1 & 2 (set to N <- 5000 for Figure 2)
# Set x-axis upper and lower scalepoint (to do: automate)
low_x<--1
high_x<-1
y_max<-2
#Set sample size per group and effect size d (assumes equal sample sizes per group)
N<-50 #sample size per group for indepndent t-test