Standardisation of rates by age (or other variables) is common. Standardisation of means is less common, but also seems useful. For example, I wanted to compare the mean duration of hospital admission between group with different age and sex composition without having to stratify my results or use regression.
Calculating the adjusted mean is easy – you just apply the stratified means to a reference population, then divide the total by the total size of the reference population. Calculating a standard error for the adjusted mean could be more difficult. Luckily there is a method in the ‘survey’ package, based on the method described by the CDC.
# generate data in which older people and women have longer spell durations, but men are older n <- 250 datm <- data.frame(agegrp = sample(c('15-34', '35-44', '45-55'), n, T, c(0.1, 0.3, 0.6)), sex = 'm') datf <- data.frame(agegrp = sample(c('15-34', '35-44', '45-55'), n, T, c(0.6, 0.3, 0.1)), sex = 'f') dat <- rbind(datm, datf) dat$agegrp <- as.factor(dat$agegrp) dat$spell <- runif(n * 2, 0, 5) dat$spell <- dat$spell * ifelse(dat$agegrp == '35-44', 1.5, 1) dat$spell <- dat$spell * ifelse(dat$agegrp == '45-55', 2.0, 1) dat$spell <- dat$spell * ifelse(dat$sex == 'f', 1.3, 1) # calculate raw means: men higher than women; women higher than men within strata aggregate(dat$spell, by = list(dat$sex), FUN = 'mean') x <- aggregate(dat$spell, by = list(dat$agegrp, dat$sex), FUN = 'mean') # calculate age-adjusted means: women higher than men popage <- as.numeric(table(dat$agegrp)) sum(popage * x[1:3,3]) / sum(popage) # male age-adjusted mean sum(popage * x[4:6,3]) / sum(popage) # female age-adjusted mean # standard errors of age-adjusted means library(survey) design <- svydesign(ids = ~1, strata = ~agegrp, data = dat) stdes <- svystandardize(design, by = ~agegrp, over = ~sex, population = popage) y <- svyby(~spell, ~sex, svymean, design = stdes) y$lower <- y$spell - 1.96 * y$se y$upper <- y$spell + 1.96 * y$se y$spell / y$spell
The object ‘y’ contains the adjusted means, standard errors and confidence intervals. The adjusted means are identical to those calculated manually. The final line shows that the age-adjusted mean spell for women is approx 1.3 times that of men, as you would expect from the data.