rm(list=ls(all=TRUE)) # clear all variables graphics.off() # clear all graphics # Greg Francis # PSY 626 # 14 November 2025 # Modified to find a sample size that gives support for the null verbose=TRUE DesiredPower=0.9 # Bayes Factor if(1==0){ library(BayesFactor) EvidenceThreshold=1/10 for(n in seq(2000,10000)){ CountSufficientEvidence=0 numReps=1000 # Generate simulated data sets for(rep in seq(1, numReps)){ scores<- rnorm(n, mean=7.2, sd=1.2) # Subtract the value in the null hypothesis (create difference scores) diffScores = scores - 7.2 test = ttestBF(x = diffScores) bf = extractBF(test, logbf=FALSE, onlybf=TRUE) if(bf DesiredPower){ cat('Bayes Factor, smallest sample size with desired power is ', n, ' with power of ', powerEstimate, '\n') break } } } if(1==1){ AweightThreshold = 0.9 # Stan/Ulam with rethinking library(rethinking) numReps=20 for(n in seq(20, 1000)){ CountSufficientEvidence=0 # Generate simulated data sets for(rep in seq(1, numReps)){ scores<- rnorm(n, mean=7.2, sd=1.2) # set up data frame data <- data.frame(Participant=seq(1,n), Score =scores ) # Model comparison # Null model NullModel <- ulam( alist( Score ~ dnorm(7.2, sigma), sigma ~ dnorm(0, 10) ), data= data, constraints=list(sigma="lower=0"), log_lik=TRUE, chains=4 ) # Alt model AltModel <- ulam( alist( Score ~ dnorm(mu, sigma), mu ~ dnorm(0, 10), sigma ~ dnorm(0, 10) ), data= data, constraints=list(sigma="lower=0"), log_lik=TRUE, chains=4 ) WAICtable <-compare(NullModel, AltModel) # Identify which model wins the competition RowNames <- rownames(WAICtable) if(RowNames[1]=="NullModel"){ # Check Akaike weight thisWeight = WAICtable$weight[1] if(thisWeight > AweightThreshold){ CountSufficientEvidence = CountSufficientEvidence +1 } } } powerEstimate = CountSufficientEvidence/numReps if(verbose){ cat('With n=', n, ' WAIC power for null threshold ', AweightThreshold,' is ', powerEstimate, '\n') } if(powerEstimate> DesiredPower){ cat('For model comparison, smallest sample size with desired null power is ', n, ' with power of ', powerEstimate, '\n') break } } }