R Programming

Power analysis for t-test using R

We provide effective and economically affordable online training courses for R and Python, click here for more details and course registration !

Power analysis is a part of planning analysis for experimental design. It is usually used to determine the minimum sample size required to detect a specified effect with a power level confidence, simultaneously with no larger than a significant level which wrongly reject and affirm the effect when it actually does not exist. There are four parts connected to a power analysis. They are sample size, effect size, power which is 1 – Type 2 error, and significant level which is Type 1 error in a hypothesis testing process. One of them can be calculated from the known values of the other three.

In R programming, power analysis is usually implemented with functions in pwr package. For a t-test, the power analysis function is pwr.t.test(n=, d=, sig.level=, power=, alternative=), in which

n is sample size, d is effect, sig.level is significant level and power is power, and alternative represents the hypothesis testing is two-sized or one-sized. For a independent sample t-test problem, effect can be calculated as the mean difference between samples divided by the pooled sample standard error.

Next we will show an example of power analysis for t-test in R.


#Say we want to carry out an experiment for testing medicine effect 
#between group treated and placebo. Say different of 0.1 is quite important 
#and earlier cases show that pooling standard deviation is 1.2. How many sample will
#the experiment require to 90% confidently detect such effect, and simultaneously #95% the probability that will not wrongly say it has such a effect when it actually #has no effect present? 
library(pwr)
pwr.t.test(d=.83, sig.level=.05, power=.85, type="two.sample",         
           alternative="two.sided")
#result
     Two-sample t test power calculation 

              n = 27.06182
              d = 0.83
      sig.level = 0.05
          power = 0.85
    alternative = two.sided

NOTE: n is number in *each* group

Results say that you need at least 27 observations in each group (55 in total) to have 85% of the power to detect such effect and not wrongly saying the effect exists when actually it does not.

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago