library(rPowerSampleSize)

# Set up the correlation matrix
# Based on:
#   5 outcomes x 2 time points
#   Correlation of 0.9 between time points within outcome
#   Correlation of 0.75 between outcomes within time point
rho.measure <- 0.75
rho.time <- 0.9

corr.both <- diag(10)
for (i in seq(1, 9, 2)) {
  corr.both[i, i+1] <- rho.time
  corr.both[i+1, i] <- rho.time
}
for (i in seq(1, 8, 1)) {
  for (j in seq(i+2, 10, 2)) {
    corr.both[i, j] <- rho.measure
    corr.both[j, i] <- rho.measure
  }
  if (i %% 2 == 0) {
    corr.both[i, i+1] <- rho.measure * rho.time
    corr.both[i + 1, i] <- rho.measure * rho.time
  }
  for (j in seq(i+2, 10, 2)) {
    if (j+1 <= 10) {
      corr.both[i, j+1] <- rho.measure * rho.time
      corr.both[j+1, i] <- rho.measure * rho.time
    }
  }
}

# Sample size for t-tests across 10 comparisons, with Holm correction
# NB this returns the number required in the "experimental" group (CHD cases here)
set.seed(20230327)
ss.t <- indiv.rm.ssc("Holm", asympt = FALSE, r = 1, m = 10, p = 10, nCovernE = 2,
                     delta = rep(0.3, 10), SigmaC = corr.both, SigmaE = corr.both,
                     power = 0.8, alpha = 0.05, interval = c(2, 2000), q = 1)
ss.t

# Main analysis will be linear regression
# As long as correlation between gestational age and outcome is higher than
#  correlation between CHD status and confounders, this SS will be sufficient

# Allow for 30% drop out
ss.final <- ss.t / (1 - 0.3)
ss.final