# sample size calculation with within sample sizes

I want to determine the sample size (first of all) needed to determine a mean within a certain margin of error. The problem I am facing is that I have

N1 which is the sample of several measurements within a subject.

and

N2 which are measurements of several subjects.

What is the relationship between the sample sizes n1 (of N1) and n2 (of N2)?

Can I even determine n2 if I break assumptions of n1 (e.g. a margin of error = .1 ?

Does n2 affects the necessary margin of error in n1? Or can I add the measurement error determined in N1 for calculation of n2 ?

here some pilot data, all within one group, a to d represent different subjects:

a <- c(0.714636225, 0.411145658, 0.50188513, 0.289006262, 0.471759581)
b <- c(0.839101694, 0.313061247, 2.078540213, 1.75925039, 2.522377185)
c <- c(1.798825334, 0.886686529, 1.634739336, 0.965114864, 1.132786925)
d <- c(0.723889267, 1.228555935, 0.946520143, 1.438293598, 1.345298834)


Sampling is random, the population is infinite.

• How is the sampling performed? Is this is simple random sample? Is the population infinite (or can we assume so) or finite? – StatsStudent Oct 20 '16 at 5:59
• am I correctly assuming that one have to consider a an additive error model of e1 being the average SD within each subject and e2 the SD of all subjects? – rbru Oct 20 '16 at 7:21

If you are not interested in the technical aspects, skip all the following and jump to the conclusion.

Firstly, I am assuming you want to find a ($1-\alpha$)% confidence interval of a mean with a certain width $\delta$.

You have a certain number of subjects, say $K$ subjects and in each subject you have have a certain amount of measurements, say $M_k$ measurements, where $k = 1,2, ..., K$. For reasons of simplicity let's assume you have the same amount of measurements for each subject, so $M_1 = M_2 = ... = M_K = M$.

Let's look at a particular measurement $X_{i,j}$, meaning it's the j-th observation of the i-th subject ($i = 1, 2, ... , K$ and $j = 1, 2, ..., M$). You would expect the measurements of the same subject to have the same expected value and the same variance, so $E(X_{i,j}) = \mu_i$ and $Var(X_{i,j}) = \sigma^2_i$ (Note: If there is no good reason to assume that measurements of the same patient have the same expected value and variance, e.g. you are measuring their height, then their arm length and lastly their breath smell, then the following doesn't hold).

Now: Again, I might be misinterpreting your question, but I am assuming that you want to take the mean of the mean of the individual subject's measurements, i.e. you are interested in $\bar{X} = \frac{1}{K}\sum_{i=1}^K \frac{1}{M}\sum_{j=1}^MX_{i,j}$. Let's break this down step by step:

Fixing a subject, say subject 1 (i.e. $i=1$), we have $\frac{1}{M}\sum_{j=1}^MX_{1,j} := \bar{X_1}$, which follows (either asymptotically or under the assumption of normality of the $X_{1,j}$) an $N(\mu_1, \frac{\sigma_1^2}{M})$ distribution. Similarly, $\bar{X_2}$ ~ $N(\mu_2, \frac{\sigma_2^2}{M})$ etc., so in general $\bar{X_i}$ ~ $N(\mu_i, \frac{\sigma_i^2}{M})$.

Now let's simplify our previous formula of $\bar{X}= \frac{1}{K}\sum_{i=1}^K \frac{1}{M}\sum_{j=1}^MX_{i,j} = \frac{1}{K} (\bar{X_1} + \bar{X_2} + ... + \bar{X_K})$. Again, we know that $(\bar{X_1} + \bar{X_2} + ... + \bar{X_K})$ ~ $N(\sum_{i=1}^K \mu_i, \sum_{i=1}^K \frac{\sigma_i^2}{M})$, ergo $\bar{X} = \frac{1}{K}(\bar{X_1} + \bar{X_2} + ... + \bar{X_K})$ ~ $N(\frac{1}{K}\sum_{i=1}^K \mu_i,\frac{1}{K^2} \sum_{i=1}^K \frac{\sigma_i^2}{M})$.

If we now assume all subjects to have the same variance $\sigma^2$, so $\sigma_1^2 = \sigma_2^2 = ... = \sigma_K^2 = \sigma^2$, this simplifies to $\bar{X}$ ~ $N(\frac{1}{K}\sum_{i=1}^K \mu_i,\frac{\sigma^2}{KM})$.

To find the required subject sample size of a ($1-\alpha$)% confidence interval for the unknown mean with a certain width $\delta$, we now use the necessary condition $\frac{\delta}{2} \geq Q^{N(0,1)}(1-\frac{\alpha}{2}) * SE(\bar{X}) = Q^{N(0,1)}(1-\frac{\alpha}{2}) * \frac{\sigma}{{\sqrt{KM}}} \implies K \geq (\frac{2*Q^{N(0,1)}(1-\frac{\alpha}{2})*\sigma}{\sqrt{M}\delta})^2$ As you can clearly see, the sample size of subjects depends on the measurements taken per a subject.

For a given $\alpha = 0.05$, $\delta = 1$ and $\sigma = 1$, I calculated corresponding values of $K$ and $M$:

$M=1 \implies K=16$

$M=2 \implies K=8$

$M=3 \implies K=6$

$M=4 \implies K=4$

In your case you simply need to choose $\alpha$, $\delta$ and $\sigma$ and then you either have a restraint on $M$ or $K$ or you can just play around to find numbers you like.

I hope I didn't make any mistakes (I am especially concerned about the last steps and the fact that by changing the subject sample size we in fact change the mean we want to construct a CI for) - I am kindly asking careful readers to point out mistakes or limitations to my answer.

• Great answer. I have two questions. What if we do not assume that the subjects have the same variances? Also, if we take that the means $\overline{X_i}$ follow normal distribution assimptotically, doesn't that impose a constraint on $M$ to be large enough so that CLT holds? For example, $M\geq 30$. – Milos Oct 21 '16 at 14:04
• Thank you for your comment! I think I made a mistake in the last step, I will update my answer and briefly adress the situation in which the subjects don't have the same variance. As to the constraint for the CLT to hold: Theoretically you are obviously right, though in practice I have never seen that taken into account, therefore I neglected any comment on that. – elmo Oct 21 '16 at 14:33
• I updated my answer and included a step in the calculation of the required sample size. If the subjects don't have the same variance, you need to look at $\bar{X}$'s distribution before simplification and just plug in its standard error where it says "$SE(\bar{X})$" in the formula. This will still require some information on the subject's standard deviation and lead to more algebra. I haven't done it tbh, but I am convinced it wouldn't be too hard). – elmo Oct 21 '16 at 14:58
• Thanks! I wait for some more comments and will try it out before I accept... – rbru Oct 21 '16 at 17:09