existing function to combine standard deviations in R?
Are the populations non overlapping?
library(fishmethods)
combinevar
For instance the example in wikipedia would work like this:
xbar <- c(70,65)
s<-c(3,2)
n <- c(1,1)
combinevar(xbar,s,n)
and standard deviation would be sqrt(combinevar(xbar,s,n)[2])
if you don't want to download the library the function goes like this:
combinevar <-
function (xbar = NULL, s_squared = NULL, n = NULL)
{
if (length(xbar) != length(s_squared) | length(xbar) != length(n) |
length(s_squared) != length(n))
stop("Vector lengths are different.")
sum_of_squares <- sum((n - 1) * s_squared + n * xbar^2)
grand_mean <- sum(n * xbar)/sum(n)
combined_var <- (sum_of_squares - sum(n) * grand_mean^2)/(sum(n) -
1)
return(c(grand_mean, combined_var))
}
I don't know of a specific package or function name but it seems easy to roll your own function from Wikipedia's page. Assuming no overlap in the populations:
## N: vector of sizes
## M: vector of means
## S: vector of standard deviations
grand.mean <- function(M, N) {weighted.mean(M, N)}
grand.sd <- function(S, M, N) {sqrt(weighted.mean(S^2 + M^2, N) -
weighted.mean(M, N)^2)}