How to find the sample size for t test in R?

R ProgrammingServer Side ProgrammingProgramming

To find the sample size for t test, we can use pwr.t.test function of pwr package, wherever we can pass the arguments for alternative hypothesis such as one-sided or two-sided, significance level, power of the test and difference for two samples.

Check out the below examples to understand how it works.

Example 1

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.80,d=1,sig.level=0.05,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

   n = 16.71472
   d = 1
sig.level = 0.05
   power = 0.8
alternative = two.sided

Note − n is number in *each* group.

Example 2

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.90,d=1,sig.level=0.05,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 22.02109
  d = 1
sig.level = 0.05
  power = 0.9
alternative = two.sided

Note − n is number in *each* group.

Example 3

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.90,d=1.24,sig.level=0.05,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 14.69768
  d = 1.24
sig.level = 0.05
  power = 0.9
alternative = two.sided

Note − n is number in *each* group.

Example 4

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.90,d=1.24,sig.level=0.01,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 21.08
  d = 1.24
sig.level = 0.01
  power = 0.9
alternative = two.sided

Note − n is number in *each* group.

Example 5

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.80,d=1.24,sig.level=0.10,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 8.80455
  d = 1.24
sig.level = 0.1
  power = 0.8
alternative = two.sided

Note − n is number in *each* group.

Example 6

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.85,d=1.24,sig.level=0.10,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 10.10416
  d = 1.24
sig.level = 0.1
  power = 0.85
alternative = two.sided

Note − n is number in *each* group.

Example 7

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.90,d=2,sig.level=0.10,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 5.130901
  d = 2
sig.level = 0.1
  power = 0.9
alternative = two.sided

Note − n is number in *each* group.

Example 8

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.95,d=2,sig.level=0.10,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 6.231837
  d = 2
sig.level = 0.1
  power = 0.95
alternative = two.sided

Note − n is number in *each* group.

Example 9

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.95,d=2,sig.level=0.05,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 7.608958
  d = 2
sig.level = 0.05
  power = 0.95
alternative = two.sided

Note − n is number in *each* group.

Example 10

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.80,d=0.78,sig.level=0.05,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 26.79675
  d = 0.78
sig.level = 0.05
  power = 0.8
alternative = two.sided

Note − n is number in *each* group.

Example 11

Consider the following code to find sample size for t test −

library("pwr")
pwr.t.test(power=0.80,d=1.5,sig.level=0.05,alternative="two.sided")

Output

If you execute the above given code, it generates the following Output for the two-sample t test power calculation −

  n = 8.060321
  d = 1.5
sig.level = 0.05
  power = 0.8
alternative = two.sided

Note − n is number in *each* group.

raja
Published on 02-Nov-2021 06:11:24
Advertisements