Skip to content
Related Articles

Related Articles

How to Calculate the P-Value of a T-Score in R?
  • Last Updated : 25 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

In order to accept or reject a sample that is given to you by your company or someone or basically simply just to examine data, p-value is calculated. After calculating the p-value we compare the p-value with the level of significance(α) i.e. either 0.05 or 0.01 according to the company. And if the calculated p-value is less than the level of significance we may reject the sample otherwise we may accept the sample.

R has a simple function that when called with appropriate parameters produces a desired result.

Syntax: pt(q, df, lower.tail = TRUE)

Parameters:

  • q: t-score
  • df: degrees of freedom(n-1)
  • lower.tail: If the value is TRUE then we are going to calculate the probability to the left of q which is called as left-tailed test otherwise the probability right of q i.e. FALSE.  Which is called as right-tailed test.

Approach:

  • Define all required values to be passed to the function
  • Calculate the p-value by using t-score, df and the function
  • Compare the p-value with the level of significance.
  • Print result

Left-tailed Test:

A Hypothesis Test where the rejection region is located to the extreme left of the distribution. If the alternate hypothesis(Ha) contains less than inequality then it is a left-tailed test. Let us take an example by taking the t-score as -1.549 and df as 14.



Example:

R




# Finding the p-alue.
p_value=pt(q=-1.549, df=14, lower.tail = TRUE)
  
p_value

Output:

0.07184313

The resultant p-value is greater than the level of significance 0.05, and it is also greater than the level of significance 0.1. Thus, we can conclude, We may accept the sample(null Hypothesis) at the α=0.05 level of significance and at the α=0.01 level of significance.

Right-tailed Test:

A Hypothesis Test where the rejection region is located to the extreme right of the distribution. If the alternate hypothesis contains greater than inequality then it is a left-tailed test. Let us take an example by taking the t-score as 1.87 and df as 24.

Example:

R






# Finding the p-value
p_value=pt(q=1.87, df=24, lower.tail=FALSE)
  
p_value

Output:

 0.03686533

The resultant p-value is less than the level of significance 0.05 and it can also be seen that it is greater than the level of significance 0.1. We can thereby conclude that Null hypothesis should be rejected at the α=0.05 level of significance, and we can accept the sample at the α=0.01 level of significance.

Two-tailed Test:

A Hypothesis Test where the rejection region is divided equally between 2 critical values at the extremities of the distribution is known as the two-tailed test.
A two-tailed test is applied when an alternative hypothesis (HA) equals a given quantity (HA = x ). By multiplying 2 to the function pt(q, df, lower.tail = FALSE) we can get required p-value using this hypothesis test.

Let us take an example by taking the t-score as 1.24 and df as 22.

Example:

R




# Finding the p-value
p_value=2*pt(q=1.24, df=22, lower.tail=FALSE)
  
p_value

Output:

0.228039

The p-value we get is less than the level of significance 0.05 and greater than the level of significance 0.1. Thus, here it can be concluded that Null hypothesis should be rejected at α=0.05 level of significance, and we can accept the sample at the α=0.01 level of significance.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :