library(magrittr)
# set.seed(113)
one <- rnorm(1000,0,1)
two <- rnorm(1000,1,1)

samp <- 5
reps <- 1000

# functions
# effect size
cohensd <- function(df) {
  x <- df[,1]
  y <- df[,2]
  lx <- length(x)- 1
  ly <- length(y)- 1
  md  <- abs(mean(x) - mean(y))       
  csd <- lx * var(x) + ly * var(y)
  csd <- csd/(lx + ly)
  csd <- sqrt(csd)                     
  md/csd                        
}

# ttest
myttest <- function(df) {
  x <- df[,1]
  y <- df[,2]
  t.test(x,y)$p.value
}

# whole population
data.frame(one,two) %>% myttest
## [1] 3.844146e-107
data.frame(one,two) %>% cohensd
## [1] 1.046043
# create samples
df <- cbind(one,two) %>% 
  apply(.,2,function(x) replicate(reps, sample(x,samp))) %>%
  data.frame %>%
  split(rep(1:reps,each=samp))


p.val <- df %>% lapply(myttest)
esize <- df %>% lapply(cohensd)
res <- mapply(c,p.val,esize) %>% 
  t %>% 
  data.frame %>% 
  set_names(c("p.value","efsize"))

plot(res$p.value,res$efsize, xlab="t test p-value", ylab="Cohens d effect size")
abline(h=1,v=0.05)

library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
res %>% filter(p.value<=0.05) %>% (function(x) boxplot(x$efsize, ylim=c(0,5),ylab="Cohens d when p.value<0.05"))  
abline(h=1)


# add significance level
res$significance[res$p.value<=0.05]<-"significant"
res$significance[res$p.value>0.05]<-"nonsignificant"

library(ggplot2)

library(ggthemes)
library(gridExtra)
## Loading required package: grid
thres_grob <- grobTree(textGrob("Statistical\nthreshold", x = 0.105, y = 0.9, hjust=0))
effect_grob <- grobTree(textGrob("True effect", x = 0.55, y = 0.22, hjust=0))

p <- res %>%
  qplot(p.value,efsize,data=.,xlab="p-value (t test)", ylab="Effect size (Cohen's d)") +
  geom_point() +
  geom_hline(aes(yintercept=1), colour="#990000", linetype="dashed") +
  geom_vline(aes(xintercept=0.05), colour="#990000") +
  annotation_custom(effect_grob) +
  annotation_custom(thres_grob)

q <- res %>% 
  ggplot(.,aes(x=significance,y=efsize)) + 
  geom_boxplot() +
  geom_hline(aes(yintercept=1), colour="#990000", linetype="dashed") +
  xlab(expression(paste("Significance (", alpha, "= 0.05)"))) +
  ylab("Effect size (Cohen's d)") +
  annotation_custom(effect_grob)


grid.arrange(p,q,ncol=2)