# WP5 Functions for Simulation
# Version 14
#
# The following changes have been made compared to the preregistered version:
# Line 107: fixef(mod)[["treatmentt"]] instead of fixef(mod)[2] (cleaner)
# Line 111: abs(ma_means_diff) / sigma(mod) instead of 
# abs(fixef(mod)[2] / coef(summary(mod))[4])
# Line 182-205: The way how coverage is calculated has been corrected:
# according to plan it should be: whether the SMD of the meta analysis
# falls within the CI of the study (which also has to be standardized)













library(lme4)
library(metafor)
library(car)

# Functions -----------------------------

# The function s_pooled_function requires as input two vectors of values and calculates
# the pooled standard deviation

s_pooled_function <- function(treatment_values, control_values) {
  sqrt(
    ((length(control_values) - 1) * sd(control_values)^2 +
       (length(treatment_values) -1) * sd(treatment_values)^2) /
      (length(control_values) + length(treatment_values) - 2)
  )
}


# Power analysis and LxT Factor
#
# The function power_lxt requires as input a dataframe of a set of studies in the form
# as described above. It performs several power analyses described below and gives
# as output a vector of length 9 with the following variables:
# the correction factor gamma for the RLM (gamma_square), the number of studies in the
# set of studies (set1_k), the required sample size per group based on a meta-analysis
# over all studies to achieve a power of 0.8 (sample_size_meta_08), the required sample
# size per group based a power-analysis of a single study randomly sampled from the set
# of studies to achieve a power of 0.8 (sample_size_one_08), the required sample size
# per group based on a meta-analysis over all studies to achieve a power of 0.9
# (sample_size_meta_09), the required sample size per group based a power-analysis
# of a single study randomly sampled from the set of studies to achieve a power of 0.9
# (sample_size_one_09), Proxy for Cohen's d based on a meta analysis, the estimated
# means difference based on the regression model (ma_means_diff), and an indicator
# whether a warning message was issued (1) or not (0).

power_lxt <- function(set_1_studies){
  k <- nrow(set_1_studies) # k: number of studies in the set
  
  # Creating sampled data for all studies of set1.
  # For each study measurement values are simulated by sampling n times from normal
  # distributions with parameters reported for the study, where n is the original sample
  # size of the study. The resulting data set is represented by three vectors:
  
  treatment <- c() # categorical variable: "t": treatment group, "c": control group
  lab <- c() # variable indicating study number from 1 to k
  y <- c() # simulated measurement value
  
  for (i in 1:k){
    study <- set_1_studies[i,]
    treatment_values <- rnorm(study$n_treatment, mean = study$mean_treatment,
                              sd = study$sd_treatment)
    control_values <- rnorm(study$n_control, mean = study$mean_control,
                            sd = study$sd_control)
    treatment <- c(treatment,rep("t",study$n_treatment),rep("c",study$n_control))
    lab <-c(lab, rep(i,(study$n_treatment + study$n_control)))
    y <-c(y, treatment_values, control_values)
  }
  
  treatment <- factor(treatment, levels = c("c","t"))
  lab <- factor(lab)
  
  has_message_warning <- 0
  
  # Calculate mixed effect model (and watch out for messages or warnings)
  mod <- withCallingHandlers(
    {
      lmer(y ~ treatment + (1|lab) + (1|lab:treatment))
    },
    message = function(m) {
      has_message_warning <<- 1
      invokeRestart("muffleMessage")
    },
    warning = function(w) {
      has_message_warning <<- 1
      invokeRestart("muffleWarning")
    }
  )
  
  # Calculate gamma-squared and variance ratio
  var_txl <- VarCorr(mod)$`lab:treatment`[1] # Var(TxL)
  gamma_square <- var_txl / attr(VarCorr(mod), "sc")
  
  # means difference as treatment effect of the mixed effect model
  ma_means_diff <- fixef(mod)[["treatmentt"]]
  
  # calculating a standardized effect size (treatment effect/Sigma)
  # also removing the sign
  ma_d <- abs(ma_means_diff) / sigma(mod)

  # restriction of the range of d to safeguard against error msg
  # from power.t.test
  if(ma_d > 1e23){
    d_ipd <- 1e23
  } else if (ma_d < 1e-23){
    d_ipd <- 1e-23
  } else {
    d_ipd <- ma_d
  }
  
  sample_size_meta_08 <- power.t.test(d = d_ipd, sig.level = alpha, power = 0.8 ,
                                      type = "two.sample", alternative = "one.sided")$n
  
  sample_size_meta_08 <- min(ceiling(sample_size_meta_08),max_size)
  sample_size_meta_08 <- max(sample_size_meta_08,2)
  
  # Calculating sample size based on model above for power 0.9
  # (see above for sample size restrictions)
  sample_size_meta_09 <- power.t.test(d = d_ipd, sig.level = alpha, power = 0.9 ,
                                      type = "two.sample", alternative = "one.sided")$n
  
  sample_size_meta_09 <- min(ceiling(sample_size_meta_09),max_size)
  sample_size_meta_09 <- max(sample_size_meta_09,2)
  
  # Calculating sample size based on one random study for power 0.8
  # (see above for sample size restrictions)
  ref_study_id <- sample(1:k, 1)
  ref_study_treatment <- y[lab == ref_study_id & treatment == "t"]
  ref_study_control <- y[lab == ref_study_id & treatment == "c"]
  ref_study_sigma <- s_pooled_function(ref_study_treatment,ref_study_control)
  ref_cohens_d <- abs((mean(ref_study_treatment) 
                       - mean(ref_study_control))/ref_study_sigma)

  if(ref_cohens_d > 1e23){
    d_one <- 1e23
  } else if(ref_cohens_d < 1e-23){
    d_one <- 1e-23
  } else{
    d_one <- ref_cohens_d
  }
  sample_size_one_08 <- power.t.test(d = d_one, sig.level = alpha, power = 0.8 ,
                                     type = "two.sample", alternative = "one.sided")$n
  
  sample_size_one_08 <- min(ceiling(sample_size_one_08),max_size)
  sample_size_one_08 <- max(sample_size_one_08,2)
  
  
  # Calculating sample size based on one random study for power 0.9
  # (see above for sample size restrictions)
  
  sample_size_one_09 <- power.t.test(d = d_one, sig.level = alpha, power = 0.9 ,
                                     type = "two.sample", alternative = "one.sided")$n
  
  sample_size_one_09 <- min(ceiling(sample_size_one_09),max_size)
  sample_size_one_09 <- max(sample_size_one_09,2)
  
  # Creating outcome vector
  c(gamma_square, k, sample_size_meta_08, sample_size_one_08, sample_size_meta_09,
    sample_size_one_09,ma_d,ma_means_diff,has_message_warning)
}

# Calculating additional outcomes for a simulated study:
# The function add_outcomes_to_p requires as input a vector of treatment values, a
# vector of control values and the p_value from the one-sided t-test and gives as
# output a vector of length 6, giving in this order: the p-value from the one-sided
# t-test, the means difference, 95%CI lower bound, 95%CI upper bound, degrees of
# freedom (i.e. n_treatment + n_control - 2), and coverage, which is 0 if the means
# difference is outside the 95 CI of the initial meta analysis and 1 if it is inside.

add_outcomes_to_p <- function(treatment_values,control_values,p_val){
  two_sided <- t.test(x = treatment_values,
                      y = control_values,
                      alternative = "two.sided", paired = FALSE, var.equal = TRUE)
  t_value <- as.numeric(two_sided$statistic)
  n_t <- as.numeric(length(treatment_values))
  n_c <- as.numeric(length(control_values))
  cohens_d <- abs(t_value * sqrt(1/n_t + 1/n_c))
  se_d <- sqrt(
    (n_t + n_c) / (n_t * n_c) +
      (cohens_d^2) / (2 * (n_t + n_c - 2))
  )
  ci_lower <- cohens_d - 1.96 * se_d
  ci_upper <- cohens_d + 1.96 * se_d

  output <- c(p_val,
              cohens_d,
              ci_lower,
              ci_upper,
              two_sided$parameter,
              abs(meta_mean) >  ci_lower & abs(meta_mean) < ci_upper) # adds coverage 
  names(output) <-c("p","means_diff","ci_lower","ci_upper","df","coverage")
  output
}

# Interventions:
# For all interventions, the first input is a study (i.e. one row of a data frame with
# 6 variable names as defined above). The second input is a variable "effect". If the
# variable effect gets the value "Null" (for null effect) assigned, then also the
# simulated treatment values are drawn from a normal distribution with parameters
# of the control group (creating a scenario where the true difference between
# treatment and control is zero), otherwise simulated treatment values are drawn
# from a normal distribution with parameters from the treatment group of the study.
# Some interventions require additional input.

# Intervention t-test
intervention_t_test <- function(study, effect = "Effect"){
  if(effect == "Null"){
    mean_treatment <- study$mean_control 
    sd_treatment <- study$sd_control
  }
  else{
    mean_treatment <- study$mean_treatment 
    sd_treatment <- study$sd_treatment
  }
  treatment_values <- rnorm(study$n_treatment, mean = mean_treatment, sd = sd_treatment)
  control_values <- rnorm(study$n_control, mean = study$mean_control, sd = study$sd_control)
  recorded_sample_size <<- c(recorded_sample_size,study$n_treatment + study$n_control)
  p_val <- t.test(x = treatment_values,
                  y = control_values,
                  alternative = "greater", paired = FALSE, var.equal = TRUE)$p.value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}

# Intervention p<0.005
# note: in principle I could recycle the values from t-test, but
# I take new samples for all other interventions, so better be
# consistent (also explanation is easier if I always sample anew)
intervention_005 <- function(study, effect = "Effect"){
  if(effect == "Null"){
    mean_treatment <- study$mean_control 
    sd_treatment <- study$sd_control
  }
  else{
    mean_treatment <- study$mean_treatment 
    sd_treatment <- study$sd_treatment
  }
  treatment_values <- rnorm(study$n_treatment, mean = mean_treatment, sd = sd_treatment)
  control_values <- rnorm(study$n_control, mean = study$mean_control, sd = study$sd_control)
  recorded_sample_size <<- c(recorded_sample_size,study$n_treatment + study$n_control)
  p_val <- t.test(x = treatment_values,
                  y = control_values,
                  alternative = "greater", paired = FALSE, var.equal = TRUE)$p.value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}

# Intervention p<0.005+N adjusted
# for our range of reasonable sample sizes we can say as a rule of thumb that if the
# original study was sufficiently powered (assuming power 0.8), switching from p<0.05
# to p<0.005 requires to increase the sample size by a factor of 1.7 if one wants to
# keep the power of the t-test at the same level.
intervention_005_adj <- function(study, effect = "Effect"){
  if(effect == "Null"){
    mean_treatment <- study$mean_control 
    sd_treatment <- study$sd_control
  }
  else{
    mean_treatment <- study$mean_treatment 
    sd_treatment <- study$sd_treatment
  }
  n_treatment <- ceiling(sig_adj_005 * study$n_treatment)
  n_control <- ceiling(sig_adj_005 * study$n_control)
  
  treatment_values <- rnorm(n_treatment, mean = mean_treatment, sd = sd_treatment)
  control_values <- rnorm(n_control, mean = study$mean_control, sd = study$sd_control)
  recorded_sample_size <<- c(recorded_sample_size,n_treatment + n_control)
  p_val <- t.test(x = treatment_values,
                  y = control_values,
                  alternative = "greater", paired = FALSE, var.equal = TRUE)$p.value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}

# Intervention rule of 10
# Intervention N=50
# Intervention powered one study
# Intervention powered based on meta analysis
# Intervention minimally important difference
#
# In all five cases the same function can be used which needs as additional input variable
# sample_size. Intervention powered one study: the sample size is based on a power
# analysis using one randomly drawn study from set 1. Intervention powered based on meta
# analysis: the sample size is based on a power analysis using a meta-analysis of set 1.
# Intervention minimally important difference: at the start we calculate the required
# sample size for a t-test, one-tailed with alpha=0.025, d=1 and power 0.8 or 0.9
# respectively. As these values stay the same for the entire simulation they just need
# to be calculated once in the initialization (see "constants").

intervention_power <- function(study, effect = "Effect", sample_size){
  if(effect == "Null"){
    mean_treatment <- study$mean_control 
    sd_treatment <- study$sd_control
  }
  else{
    mean_treatment <- study$mean_treatment 
    sd_treatment <- study$sd_treatment
  }
  treatment_values <- rnorm(sample_size, mean = mean_treatment, sd = sd_treatment)
  control_values <- rnorm(sample_size, mean = study$mean_control, sd = study$sd_control)
  recorded_sample_size <<- c(recorded_sample_size,2 * sample_size)
  p_val <- t.test(x = treatment_values,
                  y = control_values,
                  alternative = "greater", paired = FALSE, var.equal = TRUE)$p.value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}

# Intervention random lab model
# For the random lab model the t-test is modified, requiring two additional input
# variables for the function: gamma_square as calculated with the function power_lxt,
# and the number of studies (set1_k) in set1 used for calculating gamma_square.
intervention_rlm <- function(study, effect = "Effect", gamma_square, set_1_k){
  if(effect == "Null"){
    mean_treatment <- study$mean_control 
    sd_treatment <- study$sd_control
  }
  else{
    mean_treatment <- study$mean_treatment 
    sd_treatment <- study$sd_treatment
  }
  treatment_values <- rnorm(study$n_treatment, mean = mean_treatment, sd = sd_treatment)
  control_values <- rnorm(study$n_control, mean = study$mean_control, sd = study$sd_control)
  rlm_s <- s_pooled_function(treatment_values, control_values)
  rlm_es <- mean(treatment_values) - mean(control_values)
  rlm_t <- abs(rlm_es)/
    sqrt(rlm_s^2 * (1 / study$n_treatment + 1 / study$n_control) + 2 * gamma_square)
  # calculating the adjusted t-value
  
  num <- rlm_s^4 * (1 / study$n_control + 1 / study$n_treatment + 2 *(gamma_square / rlm_s^2))^2
  # numerator for df
  
  den<-rlm_s^4*(1/study$n_control+1/study$n_treatment)^2*(1/(study$n_control+study$n_treatment-2))+
    4 * rlm_s^4 * (gamma_square / rlm_s^2)^2 * (1 / ((set_1_k-1) * (2 - 1))) # denominator for df
  
  rlm_df <- num / den # calculating the adjusted degrees of freedom
  recorded_sample_size <<- c(recorded_sample_size,study$n_treatment + study$n_control)
  p_val <- pt(rlm_t, rlm_df, lower.tail = FALSE) # calculating adjusted p-value
  add_outcomes_to_p(treatment_values,control_values,p_val)

}

# Intervention random lab model powered
# in addition to the previous intervention, the sample size is adjusted using the meta-
# analysis of set_1. The function requires therefore as additional input sample_size_meta_...
intervention_rlm_power <- function(study, effect = "Effect", gamma_square, set_1_k, sample_size){
  if(effect == "Null"){
    mean_treatment <- study$mean_control 
    sd_treatment <- study$sd_control
  }
  else{
    mean_treatment <- study$mean_treatment 
    sd_treatment <- study$sd_treatment
  }
  treatment_values <- rnorm(sample_size, mean = mean_treatment, sd = sd_treatment)
  control_values <- rnorm(sample_size, mean = study$mean_control, sd = study$sd_control)
  rlm_s <- s_pooled_function(treatment_values, control_values)
  rlm_es <- mean(treatment_values) - mean(control_values)
  rlm_t <- abs(rlm_es)/
    sqrt(rlm_s^2 * (1 / sample_size + 1 / sample_size) + 2 * gamma_square)
  # calculating the adjusted t-value
  
  den <- rlm_s^4 * (1 / sample_size + 1 / sample_size + 2 *(gamma_square / rlm_s^2))^2
  # numerator for df
  
  fr <- rlm_s^4 * (1/ sample_size + 1 / sample_size)^2 * (1 / (sample_size + sample_size-2)) +
    4 * rlm_s^4 * (gamma_square / rlm_s^2)^2 * (1 / ((set_1_k - 1) * (2 - 1))) # denominator for df
  rlm_df <- den / fr # calculating the adjusted degrees of freedom
  
  recorded_sample_size <<- c(recorded_sample_size,2 * sample_size)
  p_val <- pt(rlm_t, rlm_df, lower.tail = FALSE) # calculating adjusted p-value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}

# Intervention multi lab study
# The intervention "multi lab" simulates the situation where an experiment is distributed
# over two laboratories. The work is split up, so that for both treatment and control
# group half of the measurements are taken at lab 1 and the other half at lab 2. This is
# simulated by randomly sampling (without re-placement) a second study from the pool of
# studies in set 2. The total sample size per group is based on the sample size of the
# first study. In case of an uneven sample size it will be randomly chosen whether the
# first or the second lab contributes the extra data point. The function requires as
# additional input a second study (study_2). For the t-test simulated values from both
# labs are pooled, where "pooled" means that the values from both labs are simply merged
# in one vector.

intervention_multi_lab <- function(study, effect = "Effect", study_2){
  if(effect == "Null"){
    mean_treatment_1 <- study$mean_control 
    sd_treatment_1 <- study$sd_control
    mean_treatment_2 <- study_2$mean_control 
    sd_treatment_2 <- study_2$sd_control
  } else{
    mean_treatment_1 <- study$mean_treatment 
    sd_treatment_1 <- study$sd_treatment
    mean_treatment_2 <- study_2$mean_treatment 
    sd_treatment_2 <- study_2$sd_treatment
  }
  
  # Determine sample sizes per lab for treatment group
  if(study$n_treatment %% 2 == 0){
    n_treatment_1 <- study$n_treatment / 2
    n_treatment_2 <- study$n_treatment / 2
  } else{
    both_n_treatment <- sample(c(floor(study$n_treatment / 2), ceiling(study$n_treatment / 2)))
    n_treatment_1 <- both_n_treatment[1]
    n_treatment_2 <- both_n_treatment[2]
    # In case of an uneven total sample size, it is randomly
    # chosen which lab has the larger (+1) sample size 
  }
  
  # Determine sample sizes per lab for control group
  if(study$n_control %% 2 == 0){
    n_control_1 <- study$n_control / 2
    n_control_2 <- study$n_control / 2
  } else{
    both_n_control <- sample(c(floor(study$n_control / 2),ceiling(study$n_control / 2)))
    n_control_1 <- both_n_control[1]
    n_control_2 <- both_n_control[2]
    # In case of an uneven total sample size, it is randomly
    # chosen which lab has the larger (+1) sample size
  }
  
  treatment_values <- c(
    rnorm(n_treatment_1, mean = mean_treatment_1, sd = sd_treatment_1),
    rnorm(n_treatment_2, mean = mean_treatment_2, sd = sd_treatment_2)
  ) # simulated values from both labs are merged
  
  control_values <- c(
    rnorm(n_control_1, mean = study$mean_control, sd = study$sd_control),
    rnorm(n_control_2, mean = study_2$mean_control, sd = study_2$sd_control)
  ) # simulated values from both labs are merged
  
  recorded_sample_size <<- c(recorded_sample_size,
                             n_treatment_1 + n_treatment_2 + n_control_1 + n_control_2)
  
  p_val <- t.test(x = treatment_values,
                  y = control_values,
                  alternative = "greater", paired = FALSE, var.equal = TRUE)$p.value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}

# Intervention multi lab study powered
# in addition to using a two-lab design, the sample size is based on a
# power analysis over all sets in set 1.
intervention_multi_lab_power <- function(study, effect = "Effect", study_2, sample_size){
  if(effect == "Null"){
    mean_treatment_1 <- study$mean_control 
    sd_treatment_1 <- study$sd_control
    mean_treatment_2 <- study_2$mean_control 
    sd_treatment_2 <- study_2$sd_control
  } else{
    mean_treatment_1 <- study$mean_treatment 
    sd_treatment_1 <- study$sd_treatment
    mean_treatment_2 <- study_2$mean_treatment 
    sd_treatment_2 <- study_2$sd_treatment
  }
  
  # Determine sample sizes per lab for treatment group
  if(sample_size %% 2 == 0){
    n_treatment_1 <- sample_size / 2
    n_treatment_2 <- sample_size / 2
  } else{
    both_n_treatment <- sample(c(floor(sample_size / 2), ceiling(sample_size / 2)))
    n_treatment_1 <- both_n_treatment[1]
    n_treatment_2 <- both_n_treatment[2]
    # In case of an uneven total sample size, it is randomly
    # chosen which lab has the larger (+1) sample size 
  }
  
  # Determine sample sizes per lab for control group
  if(sample_size %% 2 == 0){
    n_control_1 <- sample_size / 2
    n_control_2 <- sample_size / 2
  } else{
    both_n_control <- sample(c(floor(sample_size / 2),ceiling(sample_size / 2)))
    n_control_1 <- both_n_control[1]
    n_control_2 <- both_n_control[2]
    # In case of an uneven total sample size, it is randomly
    # chosen which lab has the larger (+1) sample size
  }
  
  treatment_values <- c(
    rnorm(n_treatment_1, mean = mean_treatment_1, sd = sd_treatment_1),
    rnorm(n_treatment_2, mean = mean_treatment_2, sd = sd_treatment_2)
  ) # simulated values from both labs are merged
  
  control_values <- c(
    rnorm(n_control_1, mean = study$mean_control, sd = study$sd_control),
    rnorm(n_control_2, mean = study_2$mean_control, sd = study_2$sd_control)
  ) # simulated values from both labs are merged
  recorded_sample_size <<- c(recorded_sample_size,
                             n_treatment_1 + n_treatment_2 + n_control_1 + n_control_2)
  
  p_val <- t.test(x = treatment_values,
                  y = control_values,
                  alternative = "greater", paired = FALSE, var.equal = TRUE)$p.value
  add_outcomes_to_p(treatment_values,control_values,p_val)
}
# End of interventions


# Simulation run (main function)

# This simulates one run of the simulation study (to be repeated 100.000 times).
# As input the function requires a set of studies as a data frame as described
# above (preamble). In a first step the input data set is randomly split into two
# data sets: set 1 and set 2. In a second step, set 1 is used to make several power
# analyses and calculate gamma_squared (see function power_lxt for details). In a
# third step a study from set 2 is randomly selected and all interventions are
# applied to that study (with additional input created in step 2 as needed). The
# outcome of the function is a vector, listing the effect size, lower
# 95% CI bound, upper 95% CI bound, df (i.e. n1+n2-2) and p-values of the
# intervention's t-tests in this order: p_t_test,p_rule_ten,p_large_sample,p_005,
# p_005_adj,p_power_one_08,p_power_one_09,p_power_meta_08,p_power_meta_09,
# p_mid_s_08,p_mid_s_09,p_mid_m_08,p_mid_m_09,p_mid_l_08,
# p_mid_l_09,p_rlm,p_rlm_power,p_rlm_power,p_multi_lab,p_t_test_null,p_rule_ten_null,
# p_large_sample_null,p_005_null,p_005_adj_null,p_power_one_08_null,
# p_power_one_09_null,p_power_meta_08_null,p_power_meta_09_null,p_mid_s_08_null,
# p_mid_s_09_null,p_mid_m_08_null, p_mid_m_09_null,p_mid_l_08_null,
# p_mid_l_09_null,p_rlm_null,p_rlm_power_null,p_rlm_power_null,p_multi_lab_null,
# and additionally:
# gamma_square, set_1_k, sample_size_meta_08, sample_size_one_08, sample_size_meta_09,
# sample_size_one_09,ma_cohens_d, ma_means_diff, has_message_warning.

simulate_function <- function(all_studies){
  
  # Create set 1 and 2
  q <- dim(all_studies)[1]
  all_studies <- all_studies[sample(nrow(all_studies)),]
  set_1_studies <- all_studies[1:floor(q / 2),]
  set_2_studies <- all_studies[(floor(q / 2) + 1):q,]
  
  # Calculate variables from set 1 
  set_1 <- as.list(power_lxt(set_1_studies))
  names(set_1) <- c("gamma_square", "set_1_k", "sample_size_meta_08",
                    "sample_size_one_08", "sample_size_meta_09","sample_size_one_09",
                    "ma_cohens_d","ma_means_diff","has_message_warning")
  
  # Run simulation with set 2
  # Randomly sampling one study (study) and a second study (study2)
  # without replacement.
  studies <- set_2_studies[sample(nrow(set_2_studies), 2), ]
  study <- studies[1,]
  study_2 <- studies[2,]
  
  # starting the interventions
  recorded_sample_size <<- c()
  
  p_t_test <- intervention_t_test(study)
  
  p_rule_ten <- intervention_power(study, 
                                   sample_size = 10)
  
  p_large_sample <- intervention_power(study,
                                       sample_size = 50)
  p_005 <- intervention_005(study)
  
  p_005_adj <- intervention_005_adj(study)
  
  p_power_one_08 <- intervention_power(study,
                                       sample_size = set_1$sample_size_one_08)
  
  p_power_one_09 <- intervention_power(study,
                                       sample_size = set_1$sample_size_one_09)
  
  p_power_meta_08 <- intervention_power(study,
                                        sample_size = set_1$sample_size_meta_08)
  
  p_power_meta_09 <- intervention_power(study,
                                        sample_size = set_1$sample_size_meta_09)
  
  p_mid_s_08 <- intervention_power(study,
                                   sample_size = mid_s_n_08)
  
  p_mid_s_09 <- intervention_power(study,
                                   sample_size = mid_s_n_09)
  
  p_mid_m_08 <- intervention_power(study,
                                   sample_size = mid_m_n_08)
  
  p_mid_m_09 <- intervention_power(study,
                                   sample_size = mid_m_n_09)
  
  p_mid_l_08 <- intervention_power(study,
                                   sample_size = mid_l_n_08)
  
  p_mid_l_09 <- intervention_power(study,
                                   sample_size = mid_l_n_09) 
  
  p_rlm <- intervention_rlm(study,
                            gamma_square = set_1$gamma_square,
                            set_1_k = set_1$set_1_k)
  
  p_rlm_power_08 <- intervention_rlm_power(study,
                                           gamma_square = set_1$gamma_square,
                                           set_1_k = set_1$set_1_k,
                                           sample_size = set_1$sample_size_meta_08)
  
  p_rlm_power_09 <- intervention_rlm_power(study,
                                           gamma_square = set_1$gamma_square,
                                           set_1_k = set_1$set_1_k,
                                           sample_size = set_1$sample_size_meta_09)
  
  p_multi_lab <- intervention_multi_lab(study, effect = "Effect",
                                        study_2 = study_2)
  
  p_multi_lab_08 <- intervention_multi_lab_power(study, effect = "Effect",
                                                 study_2 = study_2,
                                                 sample_size = set_1$sample_size_meta_08)
  
  p_multi_lab_09 <- intervention_multi_lab_power(study, effect = "Effect",
                                                 study_2 = study_2,
                                                 sample_size = set_1$sample_size_meta_09)
  
  # adding recorded sample sizes to new data frame
  all_recorded_sample_sizes[nrow(all_recorded_sample_sizes) + 1,] <<- recorded_sample_size
  
  # and here the null effects:
  
  p_t_test_null <- intervention_t_test(study, effect= "Null")
  
  p_rule_ten_null <- intervention_power(study, effect= "Null",
                                        sample_size = 10)
  
  p_large_sample_null <- intervention_power(study, effect= "Null",
                                            sample_size = 50)
  
  p_005_null <- intervention_005(study, effect= "Null")
  
  p_005_adj_null <- intervention_005_adj(study, effect= "Null")
  
  p_power_one_08_null <- intervention_power(study, effect= "Null",
                                            sample_size = set_1$sample_size_one_08)
  
  p_power_one_09_null <- intervention_power(study, effect= "Null",
                                            sample_size = set_1$sample_size_one_09)
  
  p_power_meta_08_null <- intervention_power(study, effect= "Null",
                                             sample_size = set_1$sample_size_meta_08)
  
  p_power_meta_09_null <- intervention_power(study, effect= "Null",
                                             sample_size = set_1$sample_size_meta_09)
  
  p_mid_s_08_null <- intervention_power(study, effect= "Null",
                                        sample_size = mid_s_n_08)
  
  p_mid_s_09_null <- intervention_power(study, effect= "Null",
                                        sample_size = mid_s_n_09)
  
  p_mid_m_08_null <- intervention_power(study, effect= "Null",
                                        sample_size = mid_m_n_08)
  
  p_mid_m_09_null <- intervention_power(study, effect= "Null",
                                        sample_size = mid_m_n_09)
  
  p_mid_l_08_null <- intervention_power(study, effect= "Null",
                                        sample_size = mid_l_n_08)
  
  p_mid_l_09_null <- intervention_power(study, effect= "Null",
                                        sample_size = mid_l_n_09)
  
  p_rlm_null <- intervention_rlm(study, effect= "Null",
                                 gamma_square = set_1$gamma_square,
                                 set_1_k = set_1$set_1_k)
  
  p_rlm_power_08_null <- intervention_rlm_power(study, effect= "Null",
                                                gamma_square = set_1$gamma_square,
                                                set_1_k = set_1$set_1_k,
                                                sample_size = set_1$sample_size_meta_09)
  
  p_rlm_power_09_null <- intervention_rlm_power(study, effect= "Null",
                                                gamma_square = set_1$gamma_square,
                                                set_1_k = set_1$set_1_k,
                                                sample_size = set_1$sample_size_meta_09)
  
  p_multi_lab_null <- intervention_multi_lab(study, effect= "Null",
                                             study_2 = study_2)
  
  p_multi_lab_08_null <- intervention_multi_lab_power(study, effect = "Null",
                                                      study_2 = study_2,
                                                      sample_size = set_1$sample_size_meta_08)
  
  p_multi_lab_09_null <- intervention_multi_lab_power(study, effect = "Null",
                                                      study_2 = study_2,
                                                      sample_size = set_1$sample_size_meta_09)
  
  # Summarize the p-values in one outcome vector including
  # gamma squared and sample sizes based on power analyses
  c(
    "p_t_test" = p_t_test,
    "p_rule_ten" = p_rule_ten,
    "p_large_sample" = p_large_sample,
    "p_005" = p_005,
    "p_005_adj" = p_005_adj,
    "p_power_one_08" = p_power_one_08,
    "p_power_one_09" = p_power_one_09,
    "p_power_meta_08" = p_power_meta_08,
    "p_power_meta_09" = p_power_meta_09,
    "p_mid_s_08" = p_mid_s_08,
    "p_mid_s_09" = p_mid_s_09,
    "p_mid_m_08" = p_mid_m_08,
    "p_mid_m_09" = p_mid_m_09,
    "p_mid_l_08" = p_mid_l_08,
    "p_mid_l_09" = p_mid_l_09,
    "p_rlm" = p_rlm,
    "p_rlm_power_08" = p_rlm_power_08,
    "p_rlm_power_09" = p_rlm_power_09,
    "p_multi_lab" = p_multi_lab,
    "p_multi_lab_08" = p_multi_lab_08,
    "p_multi_lab_09" = p_multi_lab_09,
    "p_t_test_null" = p_t_test_null,
    "p_rule_ten_null" = p_rule_ten_null,
    "p_large_sample_null" = p_large_sample_null,
    "p_005_null" = p_005_null,
    "p_005_adj_null" = p_005_adj_null,
    "p_power_one_08_null" = p_power_one_08_null,
    "p_power_one_09_null" = p_power_one_09_null,
    "p_power_meta_08_null" = p_power_meta_08_null,
    "p_power_meta_09_null" = p_power_meta_09_null,
    "p_mid_s_08_null" = p_mid_s_08_null,
    "p_mid_s_09_null" = p_mid_s_09_null,
    "p_mid_m_08_null" = p_mid_m_08_null,
    "p_mid_m_09_null" = p_mid_m_09_null,
    "p_mid_l_08_null" = p_mid_l_08_null,
    "p_mid_l_09_null" = p_mid_l_09_null,
    "p_rlm_null" = p_rlm_null,
    "p_rlm_power_08_null" = p_rlm_power_08_null,
    "p_rlm_power_09_null" = p_rlm_power_09_null,
    "p_multi_lab_null" = p_multi_lab_null,
    "p_multi_lab_08_null" = p_multi_lab_08_null,
    "p_multi_lab_09_null" = p_multi_lab_09_null,
    "set_1" = set_1)
  
}
# End of main function

# The function edgington takes a vector of p_values and a threshold value, always adds
# up two consecutive p-values and checks if the sum is smaller than the threshold.
# The output is the proportion of times that the combined p_value was below the
# threshold (i.e. significant).
# Note that the input vector of p-values should be of even length.

edgington <- function(p_values,e_threshold){
  one_study <- p_values[seq(1, length(p_values), 2)]
  another_study <- p_values[seq(1, length(p_values), 2)+1]
  combined_p <- one_study + another_study
  sum(combined_p < e_threshold) / (length(p_values) / 2)
}

# The function consistency takes a vector of p_values and a threshold value
# and always compares the outcomes of two consecutive significance tests.
# The output is the proportion of times that either both of the results
# were significant or both of the results were not significant.
# f ({T,T,F,F} , {T,F,T,F}) -> {T,F,F,T}
# Note that the input vector should be of even length.

consistency <- function(p_values,threshold){
  one_study <- p_values[seq(1, length(p_values), 2)] < threshold
  another_study <- p_values[seq(1, length(p_values), 2)+1] < threshold
  sum((one_study & another_study) | (!one_study & !another_study)) / (length(p_values) / 2)
}

# The function two_trial takes a vector of p_values and a threshold value
# and always compares the outcomes of two consecutive significance tests.
# The output is the proportion of times that both of the results
# were significant. f ({T,T,F,F} , {T,F,T,F}) -> {T,F,F,F}
# (which is equivalent to max(p1,p2) < threshold )
# Note that the input vector should be of even length.

two_trial <- function(p_values,threshold){
  one_study <- p_values[seq(1, length(p_values), 2)] < threshold
  another_study <- p_values[seq(1, length(p_values), 2)+1] < threshold
  sum(one_study & another_study) / (length(p_values) / 2)
}

# End of Functions ---------------------------------------------------
