This page presents 4 programs related to sample size requirements when comparing 2 means, and tables of these sample sizes.
The programs and tables on this page assumes that the measurements are continuous and normally distributed.
The following 4 programs are available on this page
- Sample Size requires the following input
- The probability of Type I Error (p, &alphs;) that will be used to determine significance. This is usually 0.05, but 0.1, 0.01, 1nd 0.001 are also used
- The power (1 - β) required. Usually this is 0.8, and sometimes 0.9
- The critical difference, the nominated or expected difference in means the model is to detect
- The nominated or anticipated within group Standard Deviation of the population being studied
The results is the sample size in each of the 2 groups, assuming that they are equal. Sample size for the 1 and 2 tail models are produced.
For example, if α of 0.05 and power of 0.8 are required, and the difference to detect is half (0.5) of the expected Standard Deviation, then the sample size required is 51 cases per group for one tail comparison and 64 for two tail.
- Power requires the following imput
- The probability of Type I Error (p, &alphs;) that will be used to determine significance. This is usually 0.05, but 0.1, 0.01, 1nd 0.001 are also used
- The difference between the two means observed
- The sample size and Standard Deviation observed in the 2 groups (n1, SD1) and (n2, SD2)
The results are the powers of the comparison, 1 and 2 tail models.
For example, in a comparison of birth weight between boys and girls, using α of 0.05 is to be used to determine statistical significance, the difference betwwen the means is 200 grams, 50 boys with Standard Deviations of 400 grams and 60 girls with Standard Deviation of 380 grams. The power is 0.85 for a 1 tail test and 0.77 two tail.
- Confidence Interval (CI) reuires the following input
- The percentage representing the level of confidence reqwuired. This is usually 95%, but sometimes 99% is used
- The sample size and Standard Deviation found in group 1 (n1, SD1) and group 2 (n2, SD2)
The results are the confidence interval of the difference, 1 and 2 tail
For example, if we use the same results as that in the power program, and wish to know the 95% confidence intervals of the difference. n1 = 50, SD1 = 400, n2 = 60, SD2 = 380. The results are ±123.6 grams for 1 tail and ±147.7 grams for 2 tail
With the difference found to be 200 grams
- The 1 tail confidence interval is >176.4 or <323.6. So if the question is whether girls are lighter than boys, we can conclude that boys are at least 176.4 grams heavier than girls
- The 2 tail confidence interval is 162.3 to 347.7 grams. So if the question is whther boys and girls differ in birth weight, without asking which is greater, the answer is no less than 162.3 grams and no greater than 347.7 grams
- Pilot Studies requires the following parameters
| 1 Tail | 2 Tail |
Ssiz | CI1 | Diff | Dec/case | %Dec/cas |
CI | Diff | Dec/case | %Dec/cas |
5 | 2.3522 | | | | 2.9169 |
| | | |
10 | 1.551 | 0.8012 | 0.1602 | 6.8 |
1.8791 | 1.0378 | 0.2076 | 7.1 |
15 | 1.2423 | 0.3087 | 0.0617 | 4 |
1.496 | 0.3832 | 0.0766 | 4.1 |
20 | 1.0663 | 0.1761 | 0.0352 | 2.8 |
1.2803 | 0.2156 | 0.0431 | 2.9 |
25 | 0.9488 | 0.1175 | 0.0235 | 2.2 |
1.1374 | 0.1429 | 0.0286 | 2.2 |
30 | 0.8632 | 0.0856 | 0.0171 | 1.8 |
1.0337 | 0.1037 | 0.0207 | 1.8 |
35 | 0.7973 | 0.0659 | 0.0132 | 1.5 |
0.954 | 0.0797 | 0.0159 | 1.5 |
40 | 0.7444 | 0.0528 | 0.0106 | 1.3 |
0.8903 | 0.0637 | 0.0127 | 1.3 |
45 | 0.7009 | 0.0435 | 0.0087 | 1.2 |
0.8379 | 0.0524 | 0.0105 | 1.2 |
50 | 0.6642 | 0.0367 | 0.0073 | 1 |
0.7938 | 0.0441 | 0.0088 | 1.1 |
55 | 0.6327 | 0.0315 | 0.0063 | 0.9 |
0.756 | 0.0378 | 0.0076 | 1 |
60 | 0.6054 | 0.0274 | 0.0055 | 0.9 |
0.7231 | 0.0329 | 0.0066 | 0.9 |
65 | 0.5813 | 0.0241 | 0.0048 | 0.8 |
0.6942 | 0.0289 | 0.0058 | 0.8 |
70 | 0.5598 | 0.0214 | 0.0043 | 0.7 |
0.6685 | 0.0257 | 0.0051 | 0.7 |
75 | 0.5406 | 0.0192 | 0.0038 | 0.7 |
0.6454 | 0.0231 | 0.0046 | 0.7 |
80 | 0.5232 | 0.0174 | 0.0035 | 0.6 |
0.6246 | 0.0208 | 0.0042 | 0.6 |
85 | 0.5074 | 0.0158 | 0.0032 | 0.6 |
0.6057 | 0.0189 | 0.0038 | 0.6 |
90 | 0.493 | 0.0144 | 0.0029 | 0.6 |
0.5883 | 0.0173 | 0.0035 | 0.6 |
95 | 0.4797 | 0.0133 | 0.0027 | 0.5 |
0.5725 | 0.0159 | 0.0032 | 0.5 |
100 | 0.4674 | 0.0123 | 0.0025 | 0.5 |
0.5578 | 0.0147 | 0.0029 | 0.5 |
105 | 0.4561 | 0.0114 | 0.0023 | 0.5 |
0.5442 | 0.0136 | 0.0027 | 0.5 |
- The probability of Type I Error (p, &alphs;) that will be used to determine significance. This is usually 0.05 or 0.1
- The expected within group, population Standard Deviation of the study
- The interval of sample size per group (intv) to exaamine changes in confidence interval as sample sizw=es increase. Usually this is between 3 and 10 cases
- The maximum sample size per group for the estimates. In most cases, pilot studies end in 30 to 40 cases, and there is no point having a pilot study with more than 100 cases per group. A common value used is 50
The program produces a table as shown to the right, nominationg a Standard Deviation of 1, and 95% confidence interval for the pilot. With increasing sample size, the reduction in confidence interval decreases, and it can be seen that beyond 30 cases per group, the further decrease in confidence interval become trivial. A conclusion can therefore be made that a sample size of 30 cases per group would be suitable for a pilot study, to define the expected Standard Deviation and difference in the 2 means, as well as providing information on the research environment, so that a formal comparison can be planned.
References
Armitage P (1980) Statistics in Medical Research. Blackwell Scientific Publication, Oxford. ISBN 0 632 05430 1 p.120
Machin D, Campbell M, Fayers, P, Pinol A (1997) Sample Size Tables for Clinical Studies. Second Ed. Blackwell Science IBSN 0-86542-870-0 p. 24-25
Johanson GA and Brooks GP (2010) Initial Scale Development: Sample Size for Pilot Studies. Educational and Psychological Measurement Vol.70,Iss.3;p.394-400
These tables provides sample sizes (per group) needed to compare the means of 2
groups, assuming that the data is normally distributed, that the variance
in the groups are similar, and that the sample sizes for the groups are the same
- Power = 1-β
- α = Probability of Type I Error
- es = Effect size = difference between means / population of within group Standard Deviation
- column header 1T and 2T represents 1 and 2 tail
- sample size is that for each of the 2 groups
Effect Size 0.05 to 1
Power | 0.8 | 0.9 | 0.95 |
α | 0.1 | 0.05 | 0.01 | 0.001 | 0.1 | 0.05 | 0.01 | 0.001 | 0.1 | 0.05 | 0.01 | 0.001 |
es | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T |
0.05 | 3607 | 4947 | 4947 | 6281 | 8031 | 9345 | 12370 | 13663 | 5256 | 6852 | 6852 | 8407 | 10415 | 11905 | 15293 | 16726 | 6852 | 8659 | 8659 | 10397 | 12618 | 14253 | 17940 | 19490 |
0.1 | 902 | 1238 | 1238 | 1571 | 2009 | 2338 | 3095 | 3418 | 1315 | 1714 | 1714 | 2103 | 2605 | 2978 | 3825 | 4184 | 1714 | 2166 | 2166 | 2600 | 3156 | 3565 | 4487 | 4875 |
0.15 | 402 | 551 | 551 | 699 | 894 | 1040 | 1377 | 1521 | 585 | 762 | 762 | 935 | 1159 | 1325 | 1702 | 1861 | 762 | 963 | 963 | 1157 | 1404 | 1586 | 1996 | 2168 |
0.2 | 226 | 310 | 310 | 394 | 504 | 586 | 776 | 857 | 329 | 429 | 429 | 527 | 653 | 746 | 959 | 1048 | 429 | 542 | 542 | 651 | 790 | 893 | 1124 | 1221 |
0.25 | 145 | 199 | 199 | 253 | 323 | 376 | 498 | 550 | 211 | 275 | 275 | 338 | 418 | 478 | 614 | 672 | 275 | 347 | 347 | 417 | 507 | 572 | 720 | 783 |
0.3 | 101 | 139 | 139 | 176 | 225 | 262 | 346 | 383 | 147 | 191 | 191 | 235 | 291 | 333 | 428 | 468 | 191 | 242 | 242 | 290 | 352 | 398 | 501 | 544 |
0.35 | 75 | 102 | 102 | 130 | 166 | 193 | 255 | 282 | 108 | 141 | 141 | 173 | 214 | 245 | 315 | 344 | 141 | 178 | 178 | 214 | 259 | 293 | 369 | 401 |
0.4 | 57 | 78 | 78 | 100 | 127 | 148 | 196 | 217 | 83 | 108 | 108 | 133 | 165 | 188 | 242 | 265 | 108 | 136 | 136 | 164 | 199 | 225 | 283 | 308 |
0.45 | 45 | 62 | 62 | 79 | 101 | 118 | 156 | 172 | 66 | 86 | 86 | 105 | 130 | 149 | 192 | 210 | 85 | 108 | 108 | 130 | 158 | 178 | 224 | 244 |
0.5 | 37 | 51 | 51 | 64 | 82 | 96 | 127 | 140 | 53 | 70 | 70 | 86 | 106 | 121 | 156 | 170 | 69 | 88 | 88 | 105 | 128 | 145 | 182 | 198 |
0.55 | 31 | 42 | 42 | 53 | 68 | 79 | 105 | 116 | 44 | 58 | 58 | 71 | 88 | 101 | 129 | 141 | 58 | 73 | 73 | 87 | 106 | 120 | 151 | 164 |
0.6 | 26 | 36 | 36 | 45 | 58 | 67 | 89 | 98 | 37 | 49 | 49 | 60 | 74 | 85 | 109 | 119 | 48 | 61 | 61 | 74 | 89 | 101 | 127 | 139 |
0.65 | 22 | 30 | 30 | 39 | 49 | 57 | 76 | 84 | 32 | 42 | 42 | 51 | 63 | 73 | 93 | 102 | 41 | 52 | 52 | 63 | 77 | 86 | 109 | 119 |
0.7 | 19 | 26 | 26 | 33 | 43 | 50 | 66 | 73 | 28 | 36 | 36 | 44 | 55 | 63 | 81 | 89 | 36 | 45 | 45 | 54 | 66 | 75 | 94 | 103 |
0.75 | 17 | 23 | 23 | 29 | 38 | 44 | 58 | 64 | 24 | 32 | 32 | 39 | 48 | 55 | 71 | 78 | 31 | 40 | 40 | 48 | 58 | 65 | 83 | 90 |
0.8 | 15 | 20 | 20 | 26 | 33 | 39 | 51 | 57 | 21 | 28 | 28 | 34 | 43 | 49 | 63 | 69 | 28 | 35 | 35 | 42 | 51 | 58 | 73 | 79 |
0.85 | 13 | 18 | 18 | 23 | 30 | 34 | 46 | 50 | 19 | 25 | 25 | 31 | 38 | 43 | 56 | 61 | 25 | 31 | 31 | 37 | 46 | 51 | 65 | 71 |
0.9 | 12 | 16 | 16 | 21 | 27 | 31 | 41 | 45 | 17 | 22 | 22 | 27 | 34 | 39 | 50 | 55 | 22 | 28 | 28 | 34 | 41 | 46 | 58 | 63 |
0.95 | 11 | 15 | 15 | 19 | 24 | 28 | 37 | 41 | 15 | 20 | 20 | 25 | 31 | 35 | 45 | 50 | 20 | 25 | 25 | 30 | 37 | 42 | 53 | 57 |
1.0 | 10 | 14 | 14 | 17 | 22 | 26 | 34 | 37 | 14 | 18 | 18 | 22 | 28 | 32 | 41 | 45 | 18 | 23 | 23 | 27 | 33 | 38 | 48 | 52 |
Effect Size 1.05 to 2
Power | 0.8 | 0.9 | 0.95 |
α | 0.1 | 0.05 | 0.01 | 0.001 | 0.1 | 0.05 | 0.01 | 0.001 | 0.1 | 0.05 | 0.01 | 0.001 |
es | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T |
1.05 | 9 | 12 | 12 | 16 | 20 | 23 | 31 | 34 | 13 | 17 | 17 | 21 | 25 | 29 | 38 | 41 | 16 | 21 | 21 | 25 | 30 | 34 | 44 | 47 |
1.1 | 8 | 11 | 11 | 14 | 18 | 21 | 28 | 31 | 12 | 15 | 15 | 19 | 23 | 27 | 34 | 38 | 15 | 19 | 19 | 23 | 28 | 32 | 40 | 43 |
1.15 | 8 | 11 | 11 | 13 | 17 | 20 | 26 | 29 | 11 | 14 | 14 | 17 | 22 | 25 | 32 | 35 | 14 | 18 | 18 | 21 | 26 | 29 | 37 | 40 |
1.2 | 7 | 10 | 10 | 12 | 16 | 18 | 24 | 27 | 10 | 13 | 13 | 16 | 20 | 23 | 29 | 32 | 13 | 16 | 16 | 20 | 24 | 27 | 34 | 37 |
1.25 | 7 | 9 | 9 | 12 | 15 | 17 | 23 | 25 | 9 | 12 | 12 | 15 | 19 | 21 | 27 | 30 | 12 | 15 | 15 | 18 | 22 | 25 | 32 | 34 |
1.3 | 6 | 8 | 8 | 11 | 14 | 16 | 21 | 23 | 9 | 11 | 11 | 14 | 17 | 20 | 26 | 28 | 11 | 14 | 14 | 17 | 21 | 23 | 29 | 32 |
1.35 | 6 | 8 | 8 | 10 | 13 | 15 | 20 | 22 | 8 | 11 | 11 | 13 | 16 | 18 | 24 | 26 | 10 | 13 | 13 | 16 | 19 | 22 | 27 | 30 |
1.4 | 6 | 7 | 7 | 9 | 12 | 14 | 19 | 21 | 8 | 10 | 10 | 12 | 15 | 17 | 22 | 25 | 10 | 12 | 12 | 15 | 18 | 20 | 26 | 28 |
1.45 | 5 | 7 | 7 | 9 | 11 | 13 | 18 | 19 | 7 | 9 | 9 | 11 | 14 | 16 | 21 | 23 | 9 | 11 | 11 | 14 | 17 | 19 | 24 | 26 |
1.5 | 5 | 7 | 7 | 8 | 11 | 13 | 17 | 18 | 7 | 9 | 9 | 11 | 13 | 15 | 20 | 22 | 9 | 11 | 11 | 13 | 16 | 18 | 23 | 25 |
1.55 | 5 | 6 | 6 | 8 | 10 | 12 | 16 | 17 | 6 | 8 | 8 | 10 | 13 | 15 | 19 | 21 | 8 | 10 | 10 | 12 | 15 | 17 | 22 | 23 |
1.6 | | 6 | 6 | 8 | 10 | 11 | 15 | 17 | 6 | 8 | 8 | 10 | 12 | 14 | 18 | 20 | 8 | 10 | 10 | 12 | 14 | 16 | 20 | 22 |
1.65 | | 6 | 6 | 7 | 9 | 11 | 14 | 16 | 6 | 7 | 7 | 9 | 11 | 13 | 17 | 19 | 7 | 9 | 9 | 11 | 13 | 15 | 19 | 21 |
1.7 | | 5 | 5 | 7 | 9 | 10 | 14 | 15 | 5 | 7 | 7 | 9 | 11 | 12 | 16 | 18 | 7 | 9 | 9 | 10 | 13 | 14 | 18 | 20 |
1.75 | | 5 | 5 | 7 | 8 | 10 | 13 | 14 | 5 | 7 | 7 | 8 | 10 | 12 | 15 | 17 | 7 | 8 | 8 | 10 | 12 | 14 | 18 | 19 |
1.8 | | 5 | 5 | 6 | 8 | 9 | 12 | 14 | 5 | 6 | 6 | 8 | 10 | 11 | 15 | 16 | 6 | 8 | 8 | 9 | 12 | 13 | 17 | 18 |
1.85 | | 5 | 5 | 6 | 8 | 9 | 12 | 13 | 5 | 6 | 6 | 8 | 9 | 11 | 14 | 15 | 6 | 8 | 8 | 9 | 11 | 13 | 16 | 17 |
1.9 | | 5 | 5 | 6 | 7 | 9 | 11 | 13 | 5 | 6 | 6 | 7 | 9 | 10 | 13 | 15 | 6 | 7 | 7 | 9 | 11 | 12 | 15 | 17 |
1.95 | | | | 6 | 7 | 8 | 11 | 12 | | 6 | 6 | 7 | 9 | 10 | 13 | 14 | 5 | 7 | 7 | 8 | 10 | 12 | 15 | 16 |
2.0 | | | | 5 | 7 | 8 | 11 | 12 | | 5 | 5 | 7 | 8 | 10 | 12 | 14 | 5 | 7 | 7 | 8 | 10 | 11 | 14 | 15 |
Effect Size 2.05 to 3
Power | 0.8 | 0.9 | 0.95 |
α | 0.1 | 0.05 | 0.01 | 0.001 | 0.1 | 0.05 | 0.01 | 0.001 | 0.1 | 0.05 | 0.01 | 0.001 |
es | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T | 1T | 2T |
2.05 | | | | 5 | 7 | 8 | 10 | 11 | | 5 | 5 | 6 | 8 | 9 | 12 | 13 | 5 | 6 | 6 | 8 | 9 | 11 | 14 | 15 |
2.1 | | | | 5 | 6 | 7 | 10 | 11 | | 5 | 5 | 6 | 8 | 9 | 12 | 13 | 5 | 6 | 6 | 7 | 9 | 10 | 13 | 14 |
2.15 | | | | 5 | 6 | 7 | 10 | 11 | | 5 | 5 | 6 | 7 | 9 | 11 | 12 | 5 | 6 | 6 | 7 | 9 | 10 | 13 | 14 |
2.2 | | | | 5 | 6 | 7 | 9 | 10 | | 5 | 5 | 6 | 7 | 8 | 11 | 12 | | 6 | 6 | 7 | 8 | 10 | 12 | 13 |
2.25 | | | | 5 | 6 | 7 | 9 | 10 | | 5 | 5 | 6 | 7 | 8 | 10 | 11 | | 5 | 5 | 7 | 8 | 9 | 12 | 13 |
2.3 | | | | | 6 | 7 | 9 | 10 | | | | 5 | 7 | 8 | 10 | 11 | | 5 | 5 | 6 | 8 | 9 | 11 | 12 |
2.35 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 7 | 8 | 10 | 11 | | 5 | 5 | 6 | 8 | 9 | 11 | 12 |
2.4 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 10 | 10 | | 5 | 5 | 6 | 7 | 8 | 11 | 12 |
2.45 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 9 | 10 | | 5 | 5 | 6 | 7 | 8 | 10 | 11 |
2.5 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 9 | 10 | | 5 | 5 | 6 | 7 | 8 | 10 | 11 |
2.55 | | | | | 5 | 6 | 8 | 8 | | | | 5 | 6 | 7 | 9 | 10 | | 5 | 5 | 5 | 7 | 8 | 10 | 11 |
2.6 | | | | | 5 | 6 | 7 | 8 | | | | 5 | 6 | 7 | 9 | 9 | | | | 5 | 7 | 7 | 10 | 10 |
2.65 | | | | | 5 | 5 | 7 | 8 | | | | | 6 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 9 | 10 |
2.7 | | | | | 5 | 5 | 7 | 8 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 9 | 10 |
2.75 | | | | | 5 | 5 | 7 | 8 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 9 | 10 |
2.8 | | | | | | 5 | 7 | 8 | | | | | 5 | 6 | 8 | 9 | | | | 5 | 6 | 7 | 9 | 9 |
2.85 | | | | | | 5 | 7 | 7 | | | | | 5 | 6 | 8 | 8 | | | | 5 | 6 | 7 | 8 | 9 |
2.9 | | | | | | 5 | 7 | 7 | | | | | 5 | 6 | 7 | 8 | | | | 5 | 6 | 6 | 8 | 9 |
2.95 | | | | | | 5 | 6 | 7 | | | | | 5 | 6 | 7 | 8 | | | | | 5 | 6 | 8 | 9 |
3 | | | | | | 5 | 6 | 7 | | | | | 5 | 5 | 7 | 8 | | | | | 5 | 6 | 8 | 9 |
This subpanel presents 4 R programs related to sample size for 2 unpaired means
Program 1: Estimating sample size
# Pgm 1: Sample Size
# data entry
dat = ("
Alpha Power Diff SD
0.05 0.8 0.5 1.0
0.01 0.8 0.5 1.0
0.05 0.9 0.5 1.0
0.01 0.9 0.5 1.0
")
df <- read.table(textConnection(dat),header=TRUE) # conversion to data frame
# vectors to store results
SSiz1Tail <- vector()
SSiz2Tail <- vector()
# Calculations
delta <- abs(df$Diff / df$SD) # effect size
zb <- abs(qnorm(1 - df$Power)) # z for beta
# 1 tail
za <- abs(qnorm(df$Alpha)) # 1 tail z for alpha
f <- (za + zb) / delta
SSiz1Tail <- append(SSiz1Tail,ceiling(2.0 * f**2 + za**2 / 4.0)) # 1 tail sample size
# 2 tail
za <- abs(qnorm(df$Alpha / 2)) # 2 tail z for alpha
#za
f <- (za + zb) / delta
SSiz2Tail <- append(SSiz2Tail,ceiling(2.0 * f**2 + za**2 / 4.0)) # 2 tail sample size
# append results to data frame
df$SSiz1Tail <- SSiz1Tail
df$SSiz2Tail <- SSiz2Tail
df # show data frame with input data and rsults
The results are as follows. Sample size is for each group
> df # show data frame with input data and rsults
Alpha Power Diff SD SSiz1Tail SSiz2Tail
1 0.05 0.8 0.5 1 51 64
2 0.01 0.8 0.5 1 82 96
3 0.05 0.9 0.5 1 70 86
4 0.01 0.9 0.5 1 106 121
Program 2: Power
# Pgm2: Power
# data entry
dat = ("
Alpha Diff N1 SD1 N2 SD2
0.05 0.5 64 1.0 64 1.0
0.01 0.5 96 1.0 96 1.0
0.01 0.5 64 1.0 96 1.0
0.05 0.5 86 1.0 86 1.0
0.01 0.5 121 1.0 121 1.0
0.01 0.5 86 1.0 121 1.0
")
df <- read.table(textConnection(dat),header=TRUE) # conversion to data frame
# vector to store results
Power1Tail <- vector()
Power2Tail <- vector()
# Calculations
se <- sqrt(((df$N1 - 1) * df$SD1**2 + (df$N2 - 1) * df$SD2**2) / (df$N1 + df$N2 - 2))
n <- (df$N1 + df$N2) / 2
delta <- abs(df$Diff / se)
# 1 tail
za <- abs(qnorm(df$Alpha)) # 1 tail z for alpha
zb <- delta * sqrt(n / 2.0 - za / 8.0) - za
Power1Tail <- append(Power1Tail,pnorm(zb)) # power 1 tail
# 2 tail
za <- abs(qnorm(df$Alpha / 2)) # 2 tail z for alpha
zb <- delta * sqrt(n / 2.0 - za / 8.0) - za
Power2Tail <- append(Power2Tail,pnorm(zb)) # power 2 tail
# append ewsults to data frame
df$Power1Tail <- Power1Tail
df$Power2Tail <- Power2Tail
df # show data frame with input data and results
The results are as follows
> df # show data frame with input data and results
Alpha Diff N1 SD1 N2 SD2 Power1Tail Power2Tail
1 0.05 0.5 64 1 64 1 0.8798970 0.8044474
2 0.01 0.5 96 1 96 1 0.8701805 0.8096574
3 0.01 0.5 64 1 96 1 0.7951479 0.7169130
4 0.05 0.5 86 1 86 1 0.9480270 0.9048008
5 0.01 0.5 121 1 121 1 0.9398340 0.9036948
6 0.01 0.5 86 1 121 1 0.8962385 0.8437134
Program 3: Confidence Interval
# Pgm3: Confidence Interval
# subroutine to calculate confidence interval
# subroutine to calculate confidence interval
CalCI <- function(alpha, n1, sd1, n2, sd2)
{
se = sqrt(((n1 - 1) * sd1**2 + (n2 - 1) * sd2**2) /
(n1 + n2 - 2) * (1 / n1 + 1 / n2))
degfm = n1 + n2 - 2
t1 = qt(1 - alpha, degfm)
ci1 = t1*se
t2 = qt(1 - alpha / 2, degfm)
ci2 = t2 * se
return (c(se, ci1, ci2))
}
The main program for confidence interval, using the subroutine CalCI
# data entry
dat = ("
Pc N1 SD1 N2 SD2
95 100 18.5 100 16.8
99 100 18.5 100 16.8
95 50 8.5 30 5.2
99 50 8.5 30 5.2
95 50 400 60 380
")
df <- read.table(textConnection(dat),header=TRUE) # conversion to data frame
# df
# vectors for results
SE <- vector() # Standard Error
CI1 <- vector() # Confidence interval 1 tail
CI2 <- vector() # Confidence interval 2 tail
# calculations
# 1 tail
for(i in 1 : nrow(df))
{
alpha <- (1 - df$Pc[i] / 100) # alpha 1 tail
ar <- CalCI(alpha,df$N1[i], df$SD1[i], df$N2[i], df$SD2[i])
#ar <- CalCI(1 - df$Pc[i] / 100,df$N1[i], df$SD1[i], df$N2[i], df$SD2[i])
SE <- append(SE, ar[1]) # Standard Error
CI1 <- append(CI1, ar[2]) # Confidence interval 1 tail
CI2 <- append(CI2, ar[3]) # Confidence interval 2 tail
}
df$SE <- SE
df$CI1 <- CI1
df$CI2 <- CI2
df
The results are as follows
- CI1 and CI2 are confidence intervals for 1 and 2 tails
- The interval is distance from mean to the limit, and should be represented as mean ± CI
> df # Data frame withinput data and results
Pc N1 SD1 N2 SD2 SE CI1 CI2
1 95 100 18.5 100 16.8 2.498980 4.129778 4.928032
2 99 100 18.5 100 16.8 2.498980 5.860928 6.499565
3 95 50 8.5 30 5.2 1.719553 2.862410 3.423366
4 99 50 8.5 30 5.2 1.719553 4.084128 4.540204
5 95 50 400.0 60 380.0 74.526406 123.645653 147.724266
Program 4: Pilot study
Please Note: This program shares the same subroutine as Program 3 to calculate SE and CI, but the subroutine is not duplicated here
# Program 4. Pilot study
# Parameters
pc = 95 # % confidence
sd = 1.0 # within group or population SD
intv = 5 # interval
maxN = 100 # maximum sample size
# vectors for results
SSiz <- vector() # sample size
CI1 <- vector() # confidence interval 1 tail
Diff1 <- vector() # difference in CI from previous row 1 tail
DecCase1 <- vector() # decrease in CI per case increase 1 tail
PDCase1 <- vector() # % decrease in CI per case increase 1 tailCI1 <- vector() # confidence interval 1 tail
CI2 <- vector() # confidence interval 2 tail
Diff2 <- vector() # difference in CI from previous row 2 tail
DecCase2 <- vector() # decrease in CI per case increase 2 tail
PDCase2 <- vector() # % decrease in CI per case increase 2 tail
# Calculations
alpha = 1 - pc / 100
# first row
n = intv
SSiz <- append(SSiz,n)
ar <- CalCI(alpha, n, sd, n, sd)
ci1 = ar[2] * 2
CI1 <- append(CI1,sprintf(ci1, fmt="%#.4f")) # confidence interval 1 tail
Diff1 <- append(Diff1,0) # difference in CI from previous row 1 tail
DecCase1 <- append(DecCase1,0) # decrease in CI per case increase 1 tail
PDCase1 <- append(PDCase1,0) # % decrease in CI per case increase 1 tailCI1 <- vector() # confidence interval 1 tail
ci2 = ar[3] * 2
CI2 <- append(CI2,sprintf(ci2, fmt="%#.4f")) # confidence interval 1 tail
Diff2 <- append(Diff2,0) # difference in CI from previous row 1 tail
DecCase2 <- append(DecCase2,0) # decrease in CI per case increase 1 tail
PDCase2 <- append(PDCase2,0) # % decrease in CI per case increase 1 tailCI1 <- vector() # confidence interval 1 tail
# subsequent rows
while(n < maxN)
{
n = n + intv
SSiz <- append(SSiz,n)
ar <- CalCI(alpha, n, sd, n, sd)
oldci1 = ci1
ci1 = ar[2] * 2
CI1 <- append(CI1,sprintf(ci1, fmt="%#.4f")) # confidence interval 1 tail
diff1 = oldci1 - ci1
Diff1 <- append(Diff1,sprintf(diff1, fmt="%#.4f")) # difference in CI from previous row 1 tail
decCase1 = diff1 / intv
DecCase1 <- append(DecCase1,sprintf(decCase1, fmt="%#.4f")) # decrease in CI per case increase 1 tail
pDCase1 = sprintf(decCase1 / oldci1 * 100, fmt="%#.1f")
PDCase1 <- append(PDCase1,pDCase1) # % decrease in CI per case increase 1 tail
oldci2 = ci2
ci2 = ar[3] * 2
CI2 <- append(CI2,sprintf(ci2, fmt="%#.4f")) # confidence interval 2 tail
diff2 = oldci2 - ci2
Diff2 <- append(Diff2,sprintf(diff2, fmt="%#.4f")) # difference in CI from previous row 2 tail
decCase2 = diff2 / intv
DecCase2 <- append(DecCase2,sprintf(decCase2, fmt="%#.4f")) # decrease in CI per case increase 2 tail
pDCase2 = sprintf(decCase2 / oldci2 * 100, fmt="%#.1f")
PDCase2 <- append(PDCase2,pDCase2) # % decrease in CI per case increase 2 tail
}
df <- data.frame(SSiz,CI1,Diff1,DecCase1,PDCase1,CI2,Diff2,DecCase2,PDCase2)
df # display results in data frame
The results are as follows. Please note:
- The width of sample size is twicw that in program 3 because it is the sum from both sides of the mean
- SSiz = sample size
- CI1 and CI2 = confidence intervals (1 and 2 tail)
- Diff1 and Diff 2 = difference in confidence interval from the previous row (1 and 2 tail)
- CaseDec1 and CaseDec2 = change in confidence interval per case increase (1 and 2 tail)
- PcDec1 and PcDec2 = % change in confidence interval per case increase (1 and 2 tail)
> df # display results in data frame
SSiz CI1 Diff1 DecCase1 PDCase1 CI2 Diff2 DecCase2 PDCase2
1 5 2.3522 0 0 0 2.9169 0 0 0
2 10 1.5510 0.8012 0.1602 6.8 1.8791 1.0378 0.2076 7.1
3 15 1.2423 0.3087 0.0617 4.0 1.4959 0.3832 0.0766 4.1
4 20 1.0663 0.1760 0.0352 2.8 1.2803 0.2156 0.0431 2.9
5 25 0.9488 0.1175 0.0235 2.2 1.1374 0.1430 0.0286 2.2
6 30 0.8632 0.0856 0.0171 1.8 1.0337 0.1037 0.0207 1.8
7 35 0.7973 0.0659 0.0132 1.5 0.9540 0.0797 0.0159 1.5
8 40 0.7444 0.0528 0.0106 1.3 0.8903 0.0637 0.0127 1.3
9 45 0.7009 0.0435 0.0087 1.2 0.8379 0.0524 0.0105 1.2
10 50 0.6642 0.0367 0.0073 1.0 0.7938 0.0441 0.0088 1.1
11 55 0.6328 0.0315 0.0063 0.9 0.7560 0.0378 0.0076 1.0
12 60 0.6054 0.0274 0.0055 0.9 0.7231 0.0329 0.0066 0.9
13 65 0.5813 0.0241 0.0048 0.8 0.6942 0.0289 0.0058 0.8
14 70 0.5598 0.0214 0.0043 0.7 0.6685 0.0257 0.0051 0.7
15 75 0.5406 0.0192 0.0038 0.7 0.6454 0.0231 0.0046 0.7
16 80 0.5232 0.0174 0.0035 0.6 0.6246 0.0208 0.0042 0.6
17 85 0.5074 0.0158 0.0032 0.6 0.6057 0.0189 0.0038 0.6
18 90 0.4930 0.0144 0.0029 0.6 0.5883 0.0173 0.0035 0.6
19 95 0.4797 0.0133 0.0027 0.5 0.5724 0.0159 0.0032 0.5
20 100 0.4674 0.0123 0.0025 0.5 0.5578 0.0147 0.0029 0.5