Instantly share code, notes, and snippets.

# sTeamTraen/Effect-size-D.R

Last active October 14, 2019 11:34
An interesting phenomenon with (perhaps extreme?) data in PET-PEESE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 set.seed(1) # Number of datasets to generate. iter <- 1000 # Generate random sample sizes. # Initially I only put the Nmin parameter there to avoid silly sizes like 0 or 1, # but then I found that something interesting happens. # Try setting Nmean to 10 instead of 50, so that all sample sizes are 20, and watch # what happens to the correlation between d and its SE. # For extra impact of this, try setting n2 equal to n1 (uncomment the line below). Nmean <- 50 Nmin <- 20 n1 <- pmax(floor(runif(iter) * 2 * Nmean), Nmin) n2 <- floor(n1 * (0.9 + (runif(iter) * 0.3))) # add a bit of variation #n2 <- n1 # Generate random effect sizes. d <- runif(iter) * 0.4 + 0.2 # range 0.2-0.6; put a # before the * to have a range from 0 to 1 # Compute SE of Cohen's d. # Formula from http://willgervais.com/blog/2015/6/25/putting-pet-peese-to-the-test-1 t1a <- (n1 + n2) / (n1 * n2) t1b <- (d ^ 2) / (2 * (n1 + n2 - 2)) t2 <- (n1 + n2) / (n1 + n2 - 2) t2 <- 1 se <- sqrt((t1a + t1b) * t2) # Run the PET regression. It's interesting to plot() this. wls <- lm(d ~ se, weights=(1 / se^2)) # Compute relative effect of the d term in that formula. d_term <- t1b / t1a # Report. cat("Mean (SD) N1: ", mean(n1), " (", sd(n1), ") Mean N2: ", mean(n2), " (", sd(n2), ")", "\nMean (SD) d: ", mean(d), " (", sd(d), ") Mean (SD) SE:", mean(se), " (", sd(se), ")", "\nMean (SD) fractional effect of d term: ", mean(d_term), " (", sd(d_term), ")", "\nCorrelation between d and its SE: ", cor(d, se), "\n", sep="")