# Type II Error in Hypothesis Testing with R Programming

Last Updated : 24 Jun, 2021

In this article, we will see what is Error in Hypothesis Testing, different types of errors that occur in hypothesis testing, and how to calculate them. The hypothesis is the summation of the various assumptions we assume to be true during model formulation for data. It is very easy to calculate the type II error in the hypothesis with R programming

### What is Error in Hypothesis Testing?

In Hypothesis testing, Error is the estimate of the approval or rejection of a particular hypothesis. There are mainly two types of errors in hypothesis testing:

1. Type I Error(also known as alpha error): Type I error occurs when we reject the Null hypothesis but the Null hypothesis is correct. This case is also known as a false positive.
2. Type II Error(also known as beta error): Type II error occurs when we fail to remove the Null Hypothesis when the Null hypothesis is incorrect/the alternative hypothesis is correct. This case is also known as a false negative.

Note:

P(X) is the probability of the event X happening.

Ho = NULL Hypothesis

Ha = Alternative Hypothesis

• Mathematical Definition of Type I Error: P(Probability of Rejecting Ho/Probability of Ho being true ) = P(Rejecting Ho | Ho True)
• Mathematical Definition of Type II Error: P(Probability of failing to remove Ho/Probability of Ho being false ) = P(Accept Ho | Ho False)

### Example: Jury/Court

In this example, we are considering the Jury/Court decision for a case. The two decisions that the jury can decide are the convict is guilty and not guilty. Hence the two hypotheses stated under hypothesis. For every decision the truth can be either, the convict is really guilty and the convict is not guilty in reality. Hence the two types of Errors.

• Ho = Not Guilty
• Ha = Guilty

In the above example,

• Type I Error will be: Innocent in Jail
• Type II Error will be: Guilty Set Free

### How to Calculate the Type II Error in R Programming?

Type II Error can be calculated by using the following formula. But in this article, we are going to calculate Type II Error using R programming.

P(Probability of failing to remove Ho / Probability of Ho being false ) = P(Accept Ho | Ho False)

Code to Calculate Type II Error in R:

## R

 `# A small function to calculate` `# the type II error in R` `typeII.test <- ``function``(mu0, TRUEmu, sigma, n, ` `                        ``alpha, iterations = 10000){` `  ``pvals <- ``rep``(``NA``, iterations)` `  ``for``(i ``in` `1 : iterations){` `    ``temporary.sample <- ``rnorm``(n = n, mean = TRUEmu, ` `                              ``sd = sigma)` `    ``temporary.mean <- ``sd``(temporary.sample)` `    ``temporary.sd <- ``sd``(temporary.sample)` `    ``pvals[i] <- 1 - ``pt``((temporary.mean - mu0)/(temporary.sd / ``sqrt``(n)), ` `                       ``df = n - 1)` `  ``}` `  ``return``(``mean``(pvals >= alpha))` `}`

First, copy the function above and run it in the R studio. Then do this.

## R

 `# Calculating the type II error` `# on a dummy Set`   `# sample size` `n = 10 `   `# standard deviation` `sigma = 3 `   `# significance level` `alpha = 0.03 `   `# hypothetical lower bound` `mu0 = 4 `   `# assumed actual mean` `TRUEmu = 10`   `# applying the function` `typeII.test``(mu0, TRUEmu, sigma, n, ` `            ``alpha, iterations = 10000)`

Output:

`[1] 3e-04`

Putting different values on the dummy set:

## R

 `# Calculating the type II error` `# on a dummy Set`   `# sample size` `n = 10 `   `# standard deviation` `sigma = 5`   `# significance level` `alpha = 0.03 `   `# hypothetical lower bound` `mu0 = 4 `   `# assumed actual mean` `TRUEmu = 10`   `# applying the function` `typeII.test``(mu0, TRUEmu, sigma, n, ` `            ``alpha, iterations = 10000)`

Output:

`[1] 0.0599`