The two algorithms for combining means and Standard Deviations from 2 or more groups are presented as R codes, for those who are wish to check the validity of the calculations, incorporate the algorithm into their own applications, or merely interested.
The R code is designed to run from the source panel of R Studio. Those unfamiliar with R should read R Explained Page
to learn how to set up and run R Studio.
The code is presented in maroon, and the results in navy. It is divided into 3 sections. Section 1 contains data entry, section 2 the first option, using the algorithm in StatsToDo, and section 3 the second option, using Cochrane's formulation.
# CombineMeanSD.R
# https://training.cochrane.org/handbook/current/chapter-06#section-6-5-2
# Section 1: Data entry
g1 <- c(10, 11.8, 2.4) #n mean,sd of grp 1
g2 <- c(20, 15.3, 3.2) #n mean,sd of grp 2
g3 <- c(15, 8.4, 4.1) #n mean,sd of grp 3
# Section 2: First option: Decomposition of means and sds
Calc1 <- function(myDat) # myDat is matrix with 3 cols of n, mean, and SD
{
m = nrow(myDat) # number of groups
tn = 0
tx = 0
txx = 0
for(i in 1:m)
{
n = myDat[i,1]
mean = myDat[i,2]
sd = myDat[i,3]
x = n * mean
xx = sd^2*(n - 1) + x^2 / n
out<-cat("grp",i," n=",n," mean=",mean," SD=", sd, " Ex=", x, " Exx=",xx, "\n")
tn = tn + n
tx = tx + x
txx = txx + xx
}
tmean = tx / tn
tsd = sqrt((txx - tx^2/tn) / (tn - 1))
out <- cat("Combined","n=",tn," mean=",tmean," SD=", tsd, " Ex=", tx, " Exx=",txx,"\n")
c(tn,tmean,tsd)
}
ar <- Calc1(matrix(data=c(g1,g2,g3), ncol=3, byrow=TRUE)) # combine 3 groups entry data into matrix and call function
# Section 3: Second Option: Cochrane formula combining two groups
Calc2 <- function(n1,m1,sd1,n2,m2,sd2) # n mean and SD of two groups
{
out<-cat("grp1"," n=",n1," mean=",m1," SD=", sd1, "\n")
out<-cat("grp2"," n=",n2," mean=",m2," SD=", sd2, "\n")
tn = n1 + n2
tmean = (n1*m1 + n2*m2) / (n1 + n2)
tsd = sqrt(((n1-1)*sd1^2 + (n2-1)*sd2^2 + n1 * n2 / (n1 + n2) * (m1^2 + m2^2 - 2 * m1 * m2)) / (n1 + n2 -1))
out<-cat("grp1+2"," n=",tn," mean=",tmean," SD=", tsd, "\n")
c(tn, tmean, tsd)
}
ar <- Calc2(g1[1],g1[2],g1[3],g2[1],g2[2],g2[3]) # combine group 1+2
ar <- Calc2(ar[1],ar[2],ar[3],g3[1],g3[2],g3[3]) # add grp 3 to result of combined 1+2
The results from section 2, first option, StatsToDo algorithm, are
> ar <- Calc1(matrix(data=c(g1,g2,g3), ncol=3, byrow=TRUE))
grp 1 n= 10 mean= 11.8 SD= 2.4 Ex= 118 Exx= 1444.24
grp 2 n= 20 mean= 15.3 SD= 3.2 Ex= 306 Exx= 4876.36
grp 3 n= 15 mean= 8.4 SD= 4.1 Ex= 126 Exx= 1293.74
Combined n= 45 mean= 12.22222 SD= 4.502822 Ex= 550 Exx= 7614.34
The results from section 3, second option, Cochrane algorithm, are
> ar <- Calc2(g1[1],g1[2],g1[3],g2[1],g2[2],g2[3]) # combine group 1+2
grp1 n= 10 mean= 11.8 SD= 2.4
grp2 n= 20 mean= 15.3 SD= 3.2
grp1+2 n= 30 mean= 14.13333 SD= 3.363427
> ar <- Calc2(ar[1],ar[2],ar[3],g3[1],g3[2],g3[3]) # add grp 3 to result of combined 1+2
grp1 n= 30 mean= 14.13333 SD= 3.363427
grp2 n= 15 mean= 8.4 SD= 4.1
grp1+2 n= 45 mean= 12.22222 SD= 4.502822
Please note: that Cochran's formula combines two groups only. when there are more than two groups, the first two groups are combined, then each subsequent group is combined sequentially with the results.
The final results from both algorithms are the same.