How to calculate critical value using R.
Published: 2022-03-25 12:22:05

## how to calculate the critical value using R
one of the important aspect of analsis is doing test. confidence interval, hpothesis etc all need critical value either Ztest or ttest, one tail or two tails. whichever we can easil calculate them using R. let go into it. 

### How to calculate critical value using Normal Distribution
to calculate the critical value using Z-test all that is need is just one function provided that ou know all the important parameters. 

**Normal distribution function**
**qnorm():** this is the function use to get the critical value, it can accept the following parameter. 
**a) sd:** this is the standard deviation it equal to 1 b default standard deviations. 
**b)** mean: this is the vector mean it is 0 b default
**c)** p: the probabilit given(level of significant is written here in decimal)
**d)** lower.tail: this is the tails ou are dealing with it is true b default, it can be assigned to False to walk in right tail. Hence when true and input level of significant our answer will surel be negative. otherwise it will be positive.

Let's take a look at the following examples
##### Example1

What is the critical value of a normal distribution data given 98% as confidence level assuming you are doing left tail test??

#### Solution
Step1:
CL=98%
α=1−CL=1−0.98=0.02
Here alpha=0.02 (no need to divide b 2 since it is one tailed test)
Then we can proceed and assigned P to the function! like below

```R
z=qnorm(p=0.02)
print(z)
```
Result>>
```
-2.053749
``` 
the above result is negative because it is lower tail, to do right tail then the code will look like the following
Codes>>
```R
z=qnorm(p=0.02, lower.tail=FALSE)
print(z)
```
**Result>>**
```
2.053749
```
**Note: TRUE o FALSE in capital letter.**

#### Example2 
What is the critical value when alpha is 5% assuming it is two tailed test.

#### Solution
**Step1**
Here α=5
α/2=0.05/2=0.025 (since it is two tailed test)

Step2 Now that we already know the important value we can easily know the z using codes below

Codes>>
```R
z=qnorm(p=0.025, lower.tail=FALSE)
print(z)
```
**Result>>**
```
1.959964
``` 

you can even use codes to calculate the alpha ourselves before using the `qnorm()` function like below

```
alpha=0.05
half_alpha=0.05/2 #this will divide alpha b 2
z==qnorm(p=half_alpha, lower.tail=FALSE)
print(z)
```
**Result>>**
```
1.959964
``` 

As you can see we did everything  b declaring variable names. we will still get the same result. I hope you understand that?

#### How to calculate critical value from t-table
To get the critical value from t-table you will use **qt()** function. this functions takes the following parameters.

**a) p:** the probability given(level of significant is written here in decimal)
**b) df**: this is the degree of freedom it is calculated b subtracting 1 from the sample size.
**d) lower.tail:** this is the tails ou are dealing with it is true b default, it can be assigned to False to walk in right tail. Hence when true and input level of significant our answer will surely be negative. otherwise it will be positive.

#### Example1 
what will be the critical value of sample of size 18 with confidence level of 95%??

#### Solution
CL=95% =0.95 α=1−CL=1−0.95=0.05
α/2==0.05/2(working in two tails)
α/2=0.5/100=0.025(toDecimal)
n=9,df=n−1
df=18−1=17

Now let's write the codes
**Codes>>**
```R
t=qt(p=0.025,df=17)
print(t)
```
you can also decide not to write each parameters and just input the value like the following but make sure *df* follows the alpha level immediately.

Codes
```R
t=qt(0.025,17)
print(t)
```

#### Example 3
what will be the critical value of sample of size 28 with alpha level of 1% assuming it is one tail test??

#### Solution
α=0.01(toDecimal)
n=9,df=n−1
df=28−1=27

Now let's input the codes
**Codes**
```R
t=qt(0.001,27)
print(t)
```

To avoid negative value if ou are working with right tail let lower.tail equal FALSE like the following

Codes
```R
t=qt(0.025,17,lower.tail=FALSE)
print(t)
```

Result>>
```
2.109816
```

you can compute the whole calculation to free yourself from working by declaring some variable name like below

```R
alpha=0.01
n=28
df=n-1
t=qt(alpha,df,lower.tail=FALSE)
print(t)
```

this will do all the calculation for you and print out the result. 

I hope you find this article helpful?? like and comment below. you can also message me on  [whatsApp](https://wa.me/2349153036869). feel free to ask any question it is our guy Max-wizard!