# Online Supplement 1: R Code rep_mean2 <- function(alpha, m11, m12, sd11, sd12, n11, n12, m21, m22, sd21, sd22, n21, n22) { # Computes confidence intervals for a 2-group mean difference in original and follow-up studies, # the difference in mean differences, and the average of the mean differences (equal variances # within and between studies is not assumed). # Arguments: # alpha: alpha level for 1-alpha confidence # m11: sample mean for group 1 in original study # m12: sample mean for group 2 in original study # m21: sample mean for group 1 in follow-up study # m22: sample mean for group 2 in follow-up study # sd11: sample SD for group 1 in original study # sd12: sample SD for group 2 in original study # sd21: sample SD for group 1 in follow-up study # sd22: sample SD for group 2 in follow-up study # n11: sample size for group 1 in original study # n12: sample size for group 2 in original study # n21: sample size for group 1 in follow-up study # n22: sample size for group 2 in follow-up study # Returns: # estimate, SE, df, t-value, p-value, confidence interval # Reference: # Snedecor, G. W. & Cochran, W. G. (1980). Statistical methods, 7th ed. Ames, IA: Iowa State # University Press. v11 <- sd11^2; v12 <- sd12^2; v21 <- sd21^2; v22 <- sd22^2 est1 <- m11 - m12 est2 <- m21 - m22 est3 <- est1 - est2 est4 <- (est1 + est2)/2 # compute standard errors se1 <- sqrt(v11/n11 + v12/n12) se2 <- sqrt(v21/n21 + v22/n22) se3 <- sqrt(se1^2 + se2^2) se4 <- se3/2 v1 <- v11^2/(n11^3 - n11^2) v2 <- v12^2/(n12^3 - n12^2) v3 <- v21^2/(n21^3 - n21^2) v4 <- v22^2/(n22^3 - n22^2) df1 <- (se1^4)/(v1 + v2) df2 <- (se2^4)/(v3 + v4) df3 <- (se3^4)/(v1 + v2 + v3 + v4) # compute test statistics t1 <- est1/se1 t2 <- est2/se2 t3 <- est3/se3 t4 <- est4/se4 # compute p-values pval1 <- 2*(1 - pt(abs(t1),df1)) pval2 <- 2*(1 - pt(abs(t2),df2)) pval3 <- 2*(1 - pt(abs(t3),df3)) pval4 <- 2*(1 - pt(abs(t4),df3)) tcrit1 <- qt(1 - alpha/2, df1) tcrit2 <- qt(1 - alpha/2, df2) tcrit3 <- qt(1 - alpha/2, df3) tcrit4 <- qt(1 - alpha/2, df3) # compute confidence intervals ll1 <- est1 - tcrit1*se1; ul1 <- est1 + tcrit1*se1 ll2 <- est2 - tcrit2*se2; ul2 <- est2 + tcrit2*se2 ll3 <- est3 - tcrit3*se3; ul3 <- est3 + tcrit3*se3 ll4 <- est4 - tcrit4*se4; ul4 <- est4 + tcrit4*se4 out1 <- t(c(est1, se1, df1, t1, pval1, ll1, ul1)) out2 <- t(c(est2, se2, df2, t2, pval2, ll2, ul2)) out3 <- t(c(est3, se3, df3, t3, pval3, ll3, ul3)) out4 <- t(c(est4, se4, df3, t4, pval4, ll4, ul4)) out = rbind(out1, out2, out3, out4) colnames(out) <- c("Estimate", "SE", "df", "t", "p", "LL", "UL") rownames(out) <- c("Original:", "Follow-up:", "Difference:", "Average:") return(out) } rep_mean_ps <- function(alpha, m11, m12, sd11, sd12, cor1, n1, m21, m22, sd21, sd22, cor2, n2) { # Computes confidence intervals for a paired-samples mean difference in original and follow-up # studies, the difference in mean differences, and the average of the mean differences (equal # variances within and between studies is not assumed). # Arguments: # alpha: alpha level for 1-alpha confidence # m11: sample mean for group 1 in original study # m12: sample mean for group 2 in original study # m21: sample mean for group 1 in follow-up study # m22: sample mean for group 2 in follow-up study # sd11: sample SD for group 1 in original study # sd12: sample SD for group 2 in original study # sd21: sample SD for group 1 in follow-up study # sd22: sample SD for group 2 in follow-up study # n1: sample size in original study # n2: sample size in follow-up study # cor1: sample correlation of paired observations in original study # cor2: sample correlation of paired observations in follow-up study # Returns: # estimate, SE, df, t-value, p-value, confidence interval # Reference: # Snedecor, G. W. & Cochran, W. G. (1980). Statistical methods, 7th ed. Ames, IA: Iowa State # University Press. v11 <- sd11^2; v12 <- sd12^2; v21 <- sd21^2; v22 <- sd22^2 vd1 <- v11 + v12 - 2*cor1*sd11*sd12 vd2 <- v21 + v22 - 2*cor2*sd21*sd22 est1 <- m11 - m12 est2 <- m21 - m22 est3 <- est1 - est2 est4 <- (est1 + est2)/2 # compute standard errors se1 <- sqrt(vd1/n1) se2 <- sqrt(vd2/n2) se3 <- sqrt(se1^2 + se2^2) se4 <- se3/2 df1 <- n1 - 1 df2 <- n2 - 1 df3 <- se3^4/(se1^4/df1 + se2^4/df2) # compute test statistics t1 <- est1/se1 t2 <- est2/se2 t3 <- est3/se3 t4 <- est4/se4 # compute p-values pval1 <- 2*(1 - pt(t1, df1)) pval2 <- 2*(1 - pt(t2, df2)) pval3 <- 2*(1 - pt(t3, df3)) pval4 <- 2*(1 - pt(t4, df3)) tcrit1 <- qt(1 - alpha/2, df1) tcrit2 <- qt(1 - alpha/2, df2) tcrit3 <- qt(1 - alpha/2, df3) tcrit4 <- qt(1 - alpha/2, df3) # compute confidence intervals ll1 <- est1 - tcrit1*se1; ul1 <- est1 + tcrit1*se1 ll2 <- est2 - tcrit2*se2; ul2 <- est2 + tcrit2*se2 ll3 <- est3 - tcrit3*se3; ul3 <- est3 + tcrit3*se3 ll4 <- est4 - tcrit4*se4; ul4 <- est4 + tcrit4*se4 out1 <- t(c(est1, se1, t1, df1, pval1, ll1, ul1)) out2 <- t(c(est2, se2, t2, df2, pval2, ll2, ul2)) out3 <- t(c(est3, se3, t3, df3, pval3, ll3, ul3)) out4 <- t(c(est4, se4, t4, df3, pval4, ll4, ul4)) out <- rbind(out1, out2, out3, out4) colnames(out) <- c("Estimate", "SE", "t", "df", "p", "LL", "UL") rownames(out) <- c("Original:", "Follow-up:", "Difference:", "Average:") return(out) } rep_stdmean2 <- function(alpha, m11, m12, sd11, sd12, n11, n12, m21, m22, sd21, sd22, n21, n22) { # Computes confidence intervals for a 2-group standardized mean difference in original and # follow-up studies, the difference in standardized mean differences, and the average of # standardized mean differences (equal variances within and between studies is not assumed). # Arguments: # alpha: alpha level for 1-alpha confidence # m11: sample mean for group 1 in original study # m12: sample mean for group 2 in original study # m21: sample mean for group 1 in follow-up study # m22: sample mean for group 2 in follow-up study # sd11: sample SD for group 1 in original study # sd12: sample SD for group 2 in original study # sd21: sample SD for group 1 in follow-up study # sd22: sample SD for group 2 in follow-up study # n11: sample size for group 1 in original study # n12: sample size for group 2 in original study # n21: sample size for group 1 in follow-up study # n22: sample size for group 2 in follow-up study # Returns: # estimate, SE, confidence interval # Reference: # Bonett, D.G. (2008). Confidence intervals for standardized linear contrasts of means. # Psychological Methods, 13, 99-109. zcrit <- qnorm(1 - alpha/2) v11 <- sd11^2 v12 <- sd12^2 v21 <- sd21^2 v22 <- sd22^2 df11 <- n11 - 1 df12 <- n12 - 1 df21 <- n21 - 1 df22 <- n22 - 1 s1 <- sqrt((v11 + v12)/2) s2 <- sqrt((v21 + v22)/2) a1 <- 1 - 3/(4*(n11 + n12) - 9) a2 <- 1 - 3/(4*(n21 + n22) - 9) # compute standardized mean differences est1 <- (m11 - m12)/s1 est2 <- (m21 - m22)/s2 est3 <- est1 - est2 est4 <- (a1*est1 + a2*est2)/2 # compute standard errors se1 <- sqrt(est1^2*(v11^2/df11 + v12^2/df12)/(8*s1^4) + (v11/df11 + v12/df12)/s1^2) se2 <- sqrt(est2^2*(v21^2/df21 + v22^2/df22)/(8*s2^4) + (v21/df21 + v22/df22)/s2^2) se3 <- sqrt(se1^2 + se2^2) se4 <- se3/2 # compute confidence intervals ll1 <- est1 - zcrit*se1; ul1 <- est1 + zcrit*se1 ll2 <- est2 - zcrit*se2; ul2 <- est2 + zcrit*se2 ll3 <- est3 - zcrit*se3; ul3 <- est3 + zcrit*se3 ll4 <- est4 - zcrit*se4; ul4 <- est4 + zcrit*se4 out1 <- t(c(est1, se1, ll1, ul1)) out2 <- t(c(est2, se2, ll2, ul2)) out3 <- t(c(est3, se3, ll3, ul3)) out4 <- t(c(est4, se4, ll4, ul4)) out <- rbind(out1, out2, out3, out4) colnames(out) <- c("Estimate", "SE", "LL", "UL") rownames(out) <- c("Original:", "Follow-up:", "Difference:", "Average:") return(out) } rep_stdmean_ps <- function(alpha, m11, m12, sd11, sd12, cor1, n1, m21, m22, sd21, sd22, cor2, n2) { # Computes confidence intervals for a paired-samples standardized mean difference in original # and follow-up studies, the difference in standardized mean differences, and the average of # standardized mean differences (equal variances within and between studies is not assumed). # Arguments: # alpha: alpha level for 1-alpha confidence # m11: sample mean for group 1 in original study # m12: sample mean for group 2 in original study # m21: sample mean for group 1 in follow-up study # m22: sample mean for group 2 in follow-up study # sd11: sample SD for group 1 in original study # sd12: sample SD for group 2 in original study # sd21: sample SD for group 1 in follow-up study # sd22: sample SD for group 2 in follow-up study # n1: sample size in original study # n2: sample size in follow-up study # cor1: sample correlation of paired observations in original study # cor2: sample correlation of paired observations in follow-up study # Returns: # estimate, SE, confidence interval # Reference: # Bonett, D.G. (2008). Confidence intervals for standardized linear contrasts of means. # Psychological Methods, 13, 99-109. zcrit <- qnorm(1 - alpha/2) v11 <- sd11^2 v12 <- sd12^2 v21 <- sd21^2 v22 <- sd22^2 df1 <- n1 - 1 df2 <- n2 - 1 s1 <- sqrt((v11 + v12)/2) s2 <- sqrt((v21 + v22)/2) vd1 <- v11 + v12 - 2*cor1*sd11*sd12 vd2 <- v21 + v22 - 2*cor2*sd21*sd22 a1 <- sqrt((n1 - 2)/df1) a2 <- sqrt((n2 - 2)/df2) # compute standardized mean differences est1 <- (m11 - m12)/s1 est2 <- (m21 - m22)/s2 est3 <- est1 - est2 est4 <- (a1*est1 + a2*est2)/2 # compute standard errors se1 <- sqrt(est1^2*(v11^2 + v12^2 + 2*cor1^2*v11*v12)/(8*df1*s1^4) + vd1/(df1*s1^2)) se2 <- sqrt(est2^2*(v21^2 + v22^2 + 2*cor2^2*v21*v22)/(8*df2*s2^4) + vd2/(df2*s2^2)) se3 <- sqrt(se1^2 + se2^2) se4 <- se3/2 # compute confidence intervals ll1 <- est1 - zcrit*se1; ul1 <- est1 + zcrit*se1 ll2 <- est2 - zcrit*se2; ul2 <- est2 + zcrit*se2 ll3 <- est3 - zcrit*se3; ul3 <- est3 + zcrit*se3 ll4 <- est4 - zcrit*se4; ul4 <- est4 + zcrit*se4 out1 <- t(c(est1, se1, ll1, ul1)) out2 <- t(c(est2, se2, ll2, ul2)) out3 <- t(c(est3, se3, ll3, ul3)) out4 <- t(c(est4, se4, ll4, ul4)) out <- rbind(out1, out2, out3, out4) colnames(out) <- c("Estimate", "SE", "LL", "UL") rownames(out) <- c("Orginal:", "Follow-up:", "Difference:", "Average:") return(out) } rep_cor <- function(alpha, cor1, cor2, n1, n2, s) { # Computes a confidence interval for a Pearson correlation or partial correlation in original # and follow-up studies, the difference in correlations, and the average of correlations. # Arguments: # alpha: alpha value for 1-alpha confidence # cor1: sample Pearson correlation between y and x in original study # cor2: sample Pearson correlation between y and x in follow-up study # n1: sample size in original study # n2: sample size in follow-up study # s: number of control variables in each study # Returns: # estimate, SE, z test statistic, p-value, confidence interval # References: # Bonett, D.G. (2008). Meta-analytic interval estimation for bivariate correlations. Psychological # Methods, 13, 173-189. # Zou, G. Y. (2007). Toward using confidence intervals to compare correlations. Psychological # Methods, 12, 399-413. zcrit <- qnorm(1 - alpha/2) zr1 <- log((1 + cor1)/(1 - cor1))/2 zr2 <- log((1 + cor2)/(1 - cor2))/2 v1 <- (1 - cor1^2)^2/(n1 - 3 - s) v2 <- (1 - cor2^2)^2/(n2 - 3 - s) dif <- cor1 - cor2 ave <- (cor1 + cor2)/2 ave.z <- log((1 + ave)/(1 - ave))/2 # compute standard errors se1 <- sqrt(1/((n1 - 3 - s))) se2 <- sqrt(1/((n2 - 3 - s))) v1 <- (1 - cor1^2)^2/(n1 - 3 - s) v2 <- (1 - cor2^2)^2/(n2 - 3 - s) se3 <- sqrt(v1 + v2) se4 <- sqrt(v1 + v2)/2 se4.z <- sqrt(((v1 + v2)/4)/(1 - ave^2)) # compute test statistics t1 <- cor1*sqrt(n1 - 2)/sqrt(1 - cor1^2) t2 <- cor2*sqrt(n2 - 2)/sqrt(1 - cor2^2) t3 <- (zr1 - zr2)/sqrt(se1^2 + se2^2) t4 <- (zr1 + zr2)/sqrt(se1^2 + se2^2) # compute p-values pval1 <- 2*(1 - pt(abs(t1), n1 - 2 - s)) pval2 <- 2*(1 - pt(abs(t2), n2 - 2 - s)) pval3 <- 2*(1 - pnorm(abs(t3))) pval4 <- 2*(1 - pnorm(abs(t4))) # compute confidence intervals ll0 <- zr1 - zcrit*se1; ul0 <- zr1 + zcrit*se1 ll1 <- (exp(2*ll0) - 1)/(exp(2*ll0) + 1) ul1 <- (exp(2*ul0) - 1)/(exp(2*ul0) + 1) ll0 <- zr2 - zcrit*se2; ul0 <- zr2 + zcrit*se2 ll2 <- (exp(2*ll0) - 1)/(exp(2*ll0) + 1) ul2 <- (exp(2*ul0) - 1)/(exp(2*ul0) + 1) ll3 <- dif - sqrt((cor1 - ll1)^2 + (ul2 - cor2)^2) ul3 <- dif + sqrt((ul1 - cor1)^2 + (cor2 - ll2)^2) ll0 <- ave.z - zcrit*se4.z ul0 <- ave.z + zcrit*se4.z ll4 <- (exp(2*ll0) - 1)/(exp(2*ll0) + 1) ul4 <- (exp(2*ul0) - 1)/(exp(2*ul0) + 1) out1 <- t(c(cor1, se1, t1, pval1, ll1, ul1)) out2 <- t(c(cor2, se2, t2, pval2, ll2, ul2)) out3 <- t(c(dif, se3, t3, pval3, ll3, ul3)) out4 <- t(c(ave, se4, t4, pval4, ll4, ul4)) out = rbind(out1, out2, out3, out4) colnames(out) <- c("Estimate", "SE", "z", "p", "LL", "UL") rownames(out) <- c("Original:", "Follow-up:", "Difference:", "Average:") return(out) } rep_general <- function(alpha, est1, se1, est2, se2) { # Computes approximate confidence intervals for a general effect size parameter for original # and follow-up studies, the difference across studies, and the average across studies. # Arguments: # alpha: alpha level for 1-alpha confidence # est1: estimate from original study # se1: standard error of estimate from original study # est2: estimate from follow-up study # se2: standard error of estimate from follow-up study # Returns: # estimate, SE, z test statistic, p-value, confidence interval est3 <- est1 - est2 est4 <- (est1 + est2)/2 # compute standard errors se3 <- sqrt(se1^2 + se2^2) se4 <- se3/2 # compute test statistics z1 <- est1/se1 z2 <- est2/se2 z3 <- est3/se3 z4 <- est4/se4 # compute p-values pval1 <- 2*(1 - pnorm(abs(z1))) pval2 <- 2*(1 - pnorm(abs(z2))) pval3 <- 2*(1 - pnorm(abs(z3))) pval4 <- 2*(1 - pnorm(abs(z4))) zcrit <- qnorm(1 - alpha/2) # compute confidence intervals ll1 <- est1 - zcrit*se1; ul1 <- est1 + zcrit*se1 ll2 <- est2 - zcrit*se2; ul2 <- est2 + zcrit*se2 ll3 <- est3 - zcrit*se3; ul3 <- est3 + zcrit*se3 ll4 <- est4 - zcrit*se4; ul4 <- est4 + zcrit*se4 out1 <- t(c(est1, se1, z1, pval1, ll1, ul1)) out2 <- t(c(est2, se2, z2, pval2, ll2, ul2)) out3 <- t(c(est3, se3, z3, pval3, ll3, ul3)) out4 <- t(c(est4, se4, z4, pval4, ll4, ul4)) out = rbind(out1, out2, out3, out4) colnames(out) <- c("Estimate", "SE", "z", "p", "LL", "UL") rownames(out) <- c("Original:", "Follow-up:", "Difference:", "Average:") return(out) } ci_mean_lc <- function(alpha, m, sd, n, c) { # Computes confidence interval and test statistic for a linear contrast of population # means in k independent samples (equal variances is not assumed). # Arguments: # alpha: alpha level for 1-alpha confidence # m: kx1 vector of sample means # sd: kx1 vector of sample standard deviations # n: kx1 vector of sample sizes # c: kx1 vector of contrast coefficients # Returns: # estimate, SE, df, t-value, p-value, confidence interval # Reference: # Snedecor, G. W. & Cochran, W. G. (1980). Statistical methods, 7th ed. Ames, IA: # Iowa State University Press. est <- t(c)%*%m v <- diag(sd^2)%*%(solve(diag(n))) se <- sqrt(t(c)%*%v%*%c) t <- est/se # compute Satterthwaite df df <- (se^4)/sum(((c^4)*(sd^4)/(n^2*(n - 1)))) pval <- 2*(1 - pt(abs(t),df)) tcrit <- qt(1 - alpha/2, df) # compute confidence interval ll <- est - tcrit*se; ul <- est + tcrit*se out <- t(c(est, se, t, df, pval, ll, ul)) colnames(out) <- c("Estimate", "SE", "t", "df", "p-value", "LL", "UL") return(out) } ci_odds <- function(alpha, p1, p2, n1, n2) { # Computes adjusted Wald confidence interval for a population log-odds ratio # and a population odds ratio # Arguments: # alpha: alpha level for 1-alpha confidence # p1: sample proportion in group 1 # p2: sample proportion in group 2 # n1: group 1 sample size # n2: group 2 sample size # Returns: # estimate, standard error, confidence interval # Reference: # Fleiss, J. L., Levin, B, & Paik, M. C. (2003). Statistical methods for rates and # proportions, 3rd ed. New York: Wiley. z <- qnorm(1 - alpha/2) f00 <- n1*p1 f01 <- n1*(1 - p1) f10 <- n2*p2 f11 <- n2*(1 - p2) lor <- log((f11 + .5)*(f00 + .5)/((f01 + .5)*(f10 + .5))) se1 <- sqrt(1/(f00 + .5) + 1/(f01 + .5) + 1/(f10 + .5) + 1/(f11 + .5)) or <- exp(lor) se2 <- or*se1 # compute confidence interval ll1 <- lor - z*se1; ul1 <- lor + z*se1; ll2 <- exp(ll1); ul2 <- exp(ul1) out1 <- t(c(lor, se1, ll1, ul1)) out2 <- t(c(or, se2, ll2, ul2)) out = rbind(out1, out2) colnames(out) <- c("Estimate", "SE", "LL", "UL") rownames(out) <- c("Log-odds Ratio:", "Odds Ratio:") return(out) } ci_prop_lc <- function(alpha, p, n, c) { # Computes adjusted Wald confidence interval for a linear contrast of population # proportions in k independent samples. # Arguments: # alpha: alpha level for 1-alpha confidence # p: kx1 vector of sample proportions # n: kx1 vector of sample sizes # c: kx1 vector of contrast coefficients # Returns: # estimate, standard error, confidence interval # Reference: # Price, R.M. & Bonett, D.G. (2004). Improved confidence interval for a linear function of # binomial proportions. Computational Statistics & Data Analysis, 45, 449-456. zcrit <- qnorm(1 - alpha/2) m <- length(c) - length(which(c==0)) p.adj <- (n*p + 2/m)/(n + 4/m) se <- sqrt(t(c)%*%diag(p.adj*(1 - p.adj))%*%solve(diag(n + 4/m))%*%c) est1 <- t(c)%*%p est2 <- t(c)%*%p.adj # compute confidence interval ll <- est2 - zcrit*se; ul <- est2 + zcrit*se out <- t(c(est1, se, ll, ul)) colnames(out) <- c("Estimate", "SE", "LL", "UL") return(out) } ci_general_lc <- function(alpha, est, se, c) { # Computes Wald confidence interval for a linear contrast of population parameters # (general) in k independent samples. # Arguments: # alpha: alpha level for 1-alpha confidence # est: kx1 vector of sample parameter estimates # se: kx1 vector of standard errors # c: kx1 vector of contrast coefficients # Returns: # estimate, standard error, z test statistic, p-value, confidence interval zcrit <- qnorm(1 - alpha/2) se <- sqrt(sum(c^2*se^2)) est.lc <- t(c)%*%est z <- est.lc/se pval <- 2*(1 - pnorm(abs(z))) # compute confidence interval ll <- est.lc - zcrit*se; ul <- est.lc + zcrit*se out <- t(c(est.lc, se, z, pval, ll, ul)) colnames(out) <- c("Estimate", "SE", "z", "p-value", "LL", "UL") return(out) } size_ci_mean_lc <- function(alpha, var, w, c) { # Computes the sample size per group required to estimate a linear contrast of population means # with desired precision in a k-group follow-up study. # Arguments: # alpha: alpha level for 1-alpha confidence # var: planning value of average within-group variance # w: desired confidence interval width in follow-up study # c: kx1 vector of contrast coefficients # Returns: # required sample size per group in follow-up study z <- qnorm(1 - alpha/2) m <- length(c) - sum(c == 0) n <- ceiling(4*var*(t(c)%*%c)*(z/w)^2 + z^2/(2*m)) return(n) } size_ci_stdmean2 <- function(alpha, d, w, r) { # Computes the sample size per group required to estimate a population standardized mean # difference with desired precision in a 2-group follow-up study. # Arguments: # alpha: alpha level for 1-alpha confidence # d: planning value of standardized mean difference # w: desired confidence interval width in follow-up study # r: desired n2/n1 ratio # Returns: # required sample size per group (or n1 if r not equal to 1) in follow-up study # Reference: # Bonett, D.G. (2009). Estimating standardized linear contrasts of means with desired # precision. Psychological Methods, 14, 1-5. z <- qnorm(1 - alpha/2) n <- ceiling((d^2*(1 + r)/(2*r) + 4*(1 + r)/r)*(z/w)^2) return(n) } size_ci_stdmean_ps <- function(alpha, d, cor, w) { # Computes the sample size required to estimate a population standardized mean difference # with desired precision in a paired-samples follow-up study. # Arguments: # alpha: alpha level for 1-alpha confidence # d: planning value of standardized mean difference # cor: planning value of correlation # w: desired confidence interval width in follow-up study # Returns: # required sample size in follow-up study # Reference: # Bonett, D.G. (2009). Estimating standardized linear contrasts of means with desired precision. # Psychological Methods, 14, 1-5. z <- qnorm(1 - alpha/2) n <- ceiling(4*(d^2*(1 + cor^2)/4 + 2*(1 - cor))*(z/w)^2) return(n) } size_ci_prop_lc <- function(alpha, p, w, c) { # Computes the sample size per group required to estimate a linear contrast of population # proportions with desired precision in a k-group follow-up study. # Arguments: # alpha: alpha level for 1-alpha confidence # p: kx1 vector of proportion planning values # w: desired confidence interval width in follow-up study # c: kx1 vector of contrast coefficients # Returns: # required sample size per group in follow-up study # Reference: # Mathews, P. (2010). Sample size calculations: Practical methods for engineers and # scientists. Fairport Harbor, OH: Mathews Malnar and Bailey, Inc. z <- qnorm(1 - alpha/2) n <- ceiling((4*t(c)%*%diag(p*(1 - p))%*%c)*(z/w)^2) return(n) } size_ci_cor <- function(alpha, cor, w, s) { # Computes sample size required to estimate a Pearson correlation or a partial # correlation with desired precision in a follow-up study. # Arguments: # alpha: alpha value for 1-alpha confidence # cor: planning value of correlation # w: desired confidence interval width in follow-up study # s: number of control variables # Returns: # required sample size in follow-up study # Reference: # Bonett, D.G. & Wright, T.A. (2000). Sample size requirements for estimating # Pearson, Kendall, and Spearman correlations. Psychometrika, 65, 23-28. z <- qnorm(1 - alpha/2) n0 <- ceiling(4*(1 - cor^2)^2*(z/w)^2 + 3 + s) zr <- log((1 + cor)/(1 - cor))/2 se <- sqrt(1/(n0 - 3 - s)) ll0 <- zr - z*se; ul0 <- zr + z*se ll <- (exp(2*ll0) - 1)/(exp(2*ll0) + 1) ul <- (exp(2*ul0) - 1)/(exp(2*ul0) + 1) n <- ceiling((n0 - 3 - s)*((ul - ll)/w)^2 + 3 + s) return(n) } size_ci_slope <- function(alpha, n1, se1, w) { # Computes sample size required to estimate a slope coefficient (for any type of # statistical model) with desired precision in a follow-up study. # Arguments: # alpha: alpha value for 1-alpha confidence # n1: sample size in original study # se1: standard error for slope in original study # w: desired confidence interval width in follow-up study # Returns: # required sample size in follow-up study z <- qnorm(1 - alpha/2) var <- n1*se1^2 n <- ceiling(4*var*(z/w)^2) return(n) } size_test_mean_lc <- function(alpha, var, pow, es, c) { # Computes the sample size required to perform a directional two-sided test of a linear # contrast of population means with desired power in a k-group follow-up study (to test # directional replication or non-replication). # Arguments: # alpha: alpha level for test # var: planning value of average within-group variance # pow: desired power of test in follow-up study # es: planning value of linear contrast of means (effect size) # c: kx1 vector of contrast coefficients # Returns: # required sample size per group in follow-up study # Reference: # Mathews, P. (2010). Sample size calculations: Practical methods for engineers and # scientists. Fairport Harbor, OH: Mathews Malnar and Bailey, Inc. za <- qnorm(1 - alpha/2) zb <- qnorm(pow) m <- length(c) - sum(c == 0) n <- ceiling(var*(t(c)%*%c)*(za + zb)^2/es^2 + za^2/(2*m)) return(n) } size_test_prop_lc <- function(alpha, p, pow, es, c) { # Computes sample size per group required to perform a directional two-sided test of a # linear contrast of proportions with desired power in a k-group follow-up study (for # test of directional replication or non-replication). # Arguments: # alpha: alpha level for 1-alpha confidence # p: kx1 vector of proportion planning values # pow: desired power of test in follow-up study # es: effect size # c: kx1 vector of contrast coefficients # Returns: # required sample size in follow-sup study za <- qnorm(1 - alpha/2) zb <- qnorm(pow) n <- ceiling((t(c)%*%diag(p*(1 - p))%*%c)*(za + zb)^2/es^2) return(n) } size_test_cor <- function(alpha, cor, b, pow, s) { # Computes sample size required to perform a directional two-sided test of a Pearson # correlation or partial correlation with desired power in a follow-up study (for # test of directional replication or non-replication). # Arguments: # alpha: alpha value for test # cor: planning value of correlation # b: hypothesized value of correlation # pow: desired power of test in follow-up study # s: number of control variables # Returns: # required sample size in follow-up study # Reference: # Cohen, J. (1988). Statistical power analysis for the behavioral sciences, 2nd ed. # Hillsday, NJ: Lawrence Erlbaum. za <- qnorm(1 - alpha/2) zb <- qnorm(pow) zr <- log((1 + cor)/(1 - cor))/2 zo <- log((1 + b)/(1 - b))/2 es <- zr - zo n <- ceiling((za + zb)^2/es^2 + 3 + s) return(n) } size_ci_diff <- function(alpha, n1, se1, w) { # Computes sample size required to estimate the difference in original and # follow-up parameters with desired precision. # Arguments: # alpha: alpha value for 1-alpha confidence # n1: sample size in original study # se1: standard error in original study # w: desired confidence interval width for difference in original # and follow-up parameters (must be greater than CI width in # original study) # Returns: # required sample size in follow-up study z <- qnorm(1 - alpha/2) var <- n1*se1^2 w1 <- 2*z*se1 if (w < w1) {w = w1} n <- ceiling(4*var*z^2/(w^2 - w1^2)) return(n) } ci_diff <- function(alpha, est1, ll1, ul1, est2, ll2, ul2) { # Computes a confidence interval for a difference in two population # parameters in the original and follow-up studies # Arguments: # alpha: alpha value for 1-alpha confidence # est1: sample estimate in original study # ll1: lower limit in original study # ul1: upper limit in original study # est2: sample estimate in follow-up study # ll2: lower limit in follow-up study # ul2: upper limit in follow-up study # Returns: # confidence interval for difference in population parameters ll <- est1 - est2 - sqrt((est1 - ll1)^2 + (ul2 - est2)^2) ul <- est1 - est2 + sqrt((ul1 - est1)^2 + (est2 - ll2)^2) ci <- c(ll, ul) return(ci) }