Skip to content
Prev 3756 / 5636 Next

[R-meta] Question on effect sizes

Dear David,

I haven't looked at your post in detail, but I think you might be after this:

# Suppose we have the mean, SD, and size of several subgroups, but we
# need the mean and SD of the total/combined groups. Code below shows
# what we need to compute to obtain this.

# simulate some data
n.total <- 100
grp <- sample(1:4, size=n.total, replace=TRUE)
y   <- rnorm(n.total, mean=grp, sd=2)

# means and SDs of the subgroups
ni  <- c(by(y, grp, length))
mi  <- c(by(y, grp, mean))
sdi <- c(by(y, grp, sd))

# want to get mean and SD of the total group
mean(y)
sd(y)

# mean = weighted mean (weights = group sizes)
m.total <- sum(ni*mi)/sum(ni)

# SD = sqrt((within-group sum-of-squares plus between-group sum-of-squares) / (n.total - 1))
sd.total <- sqrt((sum((ni-1) * sdi^2) + sum(ni*(mi - m.total)^2)) / (sum(ni) - 1))

# check that we get the right values
m.total
sd.total

Best,
Wolfgang