# How to find variance within and between batches

I have some data for 5 different batches of bacteria counts with 5 observations each -

• Batch 1: 3.890, 4.675, 7.345, 2.950, 5.675
• Batch 2: 4.345, 5.875, 3.665, 2.935, 5.455
• Batch 3: 7.145, 5.550, 6.445, 6.890, 4.985
• Batch 4: 2.675, 3.450, 4.125, 3.875, 5.125
• Batch 5: 3.350, 7.155, 6.255, 6.440, 4.960

I am trying to find the variance within each of the 5 batches, as well as the variance between the 5 batches collectively (will be using R for ease of calculations).

I'm not entirely sure how to go about doing this. Using R isn't a problem, I just don't fully understand how to actually calculate the variance parameters.

b1=c(3.890, 4.675, 7.345, 2.950, 5.675)
b2=c(4.345, 5.875, 3.665, 2.935, 5.455)
b3=c(7.145, 5.550, 6.445, 6.890, 4.985)
b4=c(2.675, 3.450, 4.125, 3.875, 5.125)
b5=c(3.350, 7.155, 6.255, 6.440, 4.960)

var_within_batches = c(var(b1),var(b2),var(b3),var(b4),var(b5))
for (k in 1:5){
print(var_within_batches[k])}


This gives a result of

[1] 2.862907
[1] 1.49075
[1] 0.8319575
[1] 0.810625
[1] 2.254908


This should be the variance within each individual batch, and then we do

mean(var_within_batches)
[1] 1.650229


and this should be the parameter estimate for variance within batches, right?

The calculation for variance between batches is also confusing me. Is it either of these? I'm a bit lost. Thanks for any help!

between_batch_var_guess1 = var(var_within_batches)
[1] 0.8090405

between_batch_var_guess2 = sum(var_within_batches)/5
[1] 1.65023


Computing Mean Squares from data. When batches have equal sample sizes and equal population variances $$\sigma^2,$$ MS(Residual) is the average of the batch variances, as you have. MS(Residual) estimates $$\sigma^2$$ whether or not the null hypothesis (that all batches have the same mean) is true.

b1=c(3.890, 4.675, 7.345, 2.950, 5.675)
b2=c(4.345, 5.875, 3.665, 2.935, 5.455)
b3=c(7.145, 5.550, 6.445, 6.890, 4.985)
b4=c(2.675, 3.450, 4.125, 3.875, 5.125)
b5=c(3.350, 7.155, 6.255, 6.440, 4.960)

(var(b1) + var(b2) + var(b3) + var(b4) + var(b5))/5
[1] 1.65023


If the null hypothesis is true and the common batch mean is $$\mu,$$ then each batch mean is an observation from $$\mathsf{Norm}(\mu, \sigma^2/5).$$ Then, the variance of the five batch means estimates $$\sigma^2/5.$$ Thus, the following quantity, MS(batch), estimates $$\sigma^2$$ under the null hypothesis.

5 * var(c(mean(b1),mean(b2),mean(b3),mean(b4),mean(b5)))
[1] 4.342956


Then the F-ratio for testing the null hypothesis is $$F = 4.343/1.650 = 2.632,$$ with $$DF_1 = 4, DF_4 = 20.$$

ANOVA computations in R. However, because you have the data, it may be best just to use R to do the one-way ANOVA.

x = c(b1,b2,b3,b4,b5)
b = as.factor(rep(1:5, each=5))
anova(lm(x ~ b))
Analysis of Variance Table

Response: x
Df Sum Sq Mean Sq F value  Pr(>F)
b          4 17.372  4.3430  2.6317 0.06481 .
Residuals 20 33.005  1.6502
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Heteroscedasticity: Moreover, because the batch variances seem to be quite different, it may be best to do a version of the one-way ANOVA that does not assume equal variances. [It uses a Satterthwaite approximation, similar to that of the Welch 2-sample t test. The result is that the denominator degrees of freedom are decreased.]

oneway.test(x ~ b)

One-way analysis of means (not assuming equal variances)

data:  x and b
F = 3.8952, num df = 4.0000, denom df = 9.8593, p-value = 0.03756


Interpretation. Here are boxplots of the five batches. If batches are use different processes you may want to try to explore differences among batch means with ad hoc tests, but you should use some method of avoiding 'false discovery'--especially because the ANOVA is significant only at about the 4% level. At least, it does seem safe to say that batches 3 and 4 are significantly different.

t.test(b3, b4)\$p.val  # Welch t test
[1] 0.003413997

boxplot(x ~ b, col="skyblue2")


Boxplots of samples of size five must be interpreted carefully: each of the five observations may correspond to one of: minimum, Q1, median, Q3, and maximum. The ends of the boxes are at Q1 and Q3.

• Thanks a lot sir, this was really insightful! – Adam Kensington Nov 1 '20 at 0:25