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:
- 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.
- 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
Similar Reads
Hypothesis Testing in R Programming A hypothesis is made by the researchers about the data collected for any experiment or data set. A hypothesis is an assumption made by the researchers that are not mandatory true. In simple words, a hypothesis is a decision taken by the researchers based on the data of the population collected. Hypo
6 min read
Permutation Hypothesis Test in R Programming In simple words, the permutation hypothesis test in R is a way of comparing a numerical value of 2 groups. The permutation Hypothesis test is an alternative to: Independent two-sample t-test Mann-Whitney U aka Wilcoxon Rank-Sum Test Let's implement this test in R programming. Why use the Permutatio
6 min read
How to Resolve General Errors in R Programming Even though R programming Language is strong and flexible, errors can still happen.Resolving general errors can be a common challenge across various contexts, including software development, troubleshooting, and problem-solving in different domains. For any R user, regardless of expertise level, res
3 min read
T-Test Approach in R Programming The T-Test is a statistical method used to determine whether there is a significant difference between the means of two groups or between a sample and a known value.For Example: businessman who owns two sweet shops in a town. He wants to know if there's a significant difference in the average number
5 min read
Bartlettâs Test in R Programming In statistics, Bartlett's test is used to test if k samples are from populations with equal variances. Equal variances across populations are called homoscedasticity or homogeneity of variances. Some statistical tests, for example, the ANOVA test, assume that variances are equal across groups or sam
5 min read