I need to write my own function in R for One-Way ANOVA with different sample sizes. On wiki is a nice example of how to calculate the F-ratio if the sample sizes are equal:


I already read multiple times that there is basically no difference in the way of calculation, however, I have a couple of questions (referring to the example in wikipedia):

Step 2: Calculate the overall mean:

With different sample sizes, do I still take the mean of the means or the mean of all observations from all groups together?

Step 3: I have to replace $n$ by $n_1, n_2,...$ where $n_i$ ist the number of observations in the i-th group, right?

Thanks in advance!


1 Answer 1


Taking the mean of means is not an appropriate method of estimating the grand mean when sample sizes are not equal.

In the case where all groups, let's say we have $m$ of them, have the same number of observations, call it $k$...

$$ \hat{\mu} = \sum \dfrac{x_i}{n} = \sum \dfrac{k \hat{\mu}_i}{m k} = \sum \dfrac{\hat{\mu}_i}{m} $$

So the mean of means is algebraically equivalent to taking the grand mean. But, when sample sizes are not equal, the factorization of $n=mk$ is not true. So taking the mean of means in this case gives too much weight to means with smaller sample sizes.

What you can do is weight the means according to how many samples are in each mean. This would be equivalent to weighting with the proportion of the total sample found in each group.

$$\hat{\mu} = \sum \dfrac{n_i \hat{\mu}_i}{n} = \dfrac{ \sum w_i \hat{\mu}_i}{\sum w_i }$$

It's easy to cook up an example of this


#Highly imbalanced groups
group = c(rep(0, 90), rep(1, 10))
x = rpois(100,5)
d = tibble(group = group, x = x) 

summarise(d, m=mean(x))

d %>% 
  group_by(group) %>% 
  summarise(m = mean(x), n = n()) %>% 
  ungroup() %>% 
  summarise(m = mean(m)) 

Run that and see that the mean of means is not the same as the grand mean.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.