I have two groups (treatment and placebo) and for each group I have the 95% CI mean calculated as per here. I want to do the same for calculating the 95% sample size as per table 11 of this paper: they clearly write Sample size (95% CI), but I cannot find anywhere how to do it and which SD to use. Other mention the SD difference while others the pooled SD. This formula gives the sample size for a given effect size which is fairly straightforward to calculate in Python as it solves the formula for n.

def get_n(mean_diff, sd_diff):
    std_effect_size = mean_diff / sd_diff

    n = tt_ind_solve_power(effect_size=std_effect_size, alpha=0.05, power=0.8, ratio=1, alternative='two-sided')
    print('Number in *each* group: {:.5f}'.format(n))

P.S: I really do net get the negative votes. It's a niche question that is very Pythonic and statistic at the same time.

  • $\begingroup$ I didn't down-vote the question, but I confess that amongst the two multi-click links and the Python code, I am not exactly sure what you're asking. Perhaps my Answer is useful. $\endgroup$ – BruceET Mar 26 '19 at 14:23

If you have a sample of size $n$ from a normal distribution with unknown mean $\mu$ and standard deviation $\sigma,$ then a 95% confidence interval for $\mu$ is of the form $$\bar X \pm t^*S/\sqrt{n},$$ where $\bar X$ is the sample mean, $S$ is the sample standard deviation, and $t*$ cuts probability $0.25$ from the upper tail of the symmetrical Student t distribution with $n-1$ degrees of freedom.

If $n > 30,$ then $t^* \approx 2.0.$ (This 'rule of 30' works only for 95% CIs.)

In this situation, the margin of error (half-width) of the CI is $M = 2S/\sqrt{n}.$ If you are able to make a reasonable guess for the value of $\sigma$ and want the sample size $n$ necessary for a particular margin of error $M,$ then
$$ n \approx 4(\sigma/M)^2.$$

| cite | improve this answer | |
  • $\begingroup$ I have two groups and I know the means and SD for each group. How can I calculate the sample size 95% from that? $\endgroup$ – azal Mar 26 '19 at 14:27
  • $\begingroup$ If the sample sizes and variances are equal for the two groups, then the approximate margin of error is $M = 2\sigma\sqrt{2/n}$ and the CI is $D \pm M,$ where $D = \bar X_1 - \bar X_2.$ From that you can solve for $n$ in terms of $M,$ etc. // Perhaps you would do better to frame this as a test of hypothesis and do a computation for $n$ required to give reasonable power against a particular difference in means. $\endgroup$ – BruceET Mar 26 '19 at 14:37
  • $\begingroup$ I'm fixing power at 80% but the variance are not equal for the two groups; that's rarely the case, isn't it? $\endgroup$ – azal Mar 26 '19 at 14:53
  • $\begingroup$ Suggest you revise your question to include the specific information provided in your Comments. If you know the ratio of the two variances (or SDs), you should give that too. Then see if you get a useful answer. Right now I'm off to see my dentist. $\endgroup$ – BruceET Mar 26 '19 at 14:58

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.