Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Type II Error in Hypothesis Testing with R Programming
Next article icon

Type II Error in Hypothesis Testing with R Programming

Last Updated : 24 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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


 


Next Article
Type II Error in Hypothesis Testing with R Programming

S

samrat2825
Improve
Article Tags :
  • R Language
  • R Data-science

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences