How to Calculate the P-Value of a T-Score in R? - GeeksforGeeks

17 Dec, 2021
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.
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
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-value.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.
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-valuep_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.
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-valuep_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.
avtarkumar719
Picked
R-Statistics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to Change Axis Scales in R Plots?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R