How to calculate the critical value using R

how to calculate the critical value using R

one of the important aspects of analysis is doing the test. confidence interval, hypothesis, etc all need critical value either Ztest or t-test, one-tailed or two-tailed. whichever we can easily calculate them using R. let go into it.

How to calculate the critical value using Normal Distribution

to calculate the critical value using Z-test all that is needed is just one function provided that you know all the important parameters. Normal distribution function qnorm(): this is the function used to get the critical value, it can accept the following parameter.

a) sd: this is the standard deviation, it is equal to 1 b default standard deviations.

b) mean: this is the vector mean it is 0 b default.

c) p: the probability given(level of significance is written here in decimal).

d) lower.tail: this is the tail you are dealing with, it is true by default, it can be assigned to False to walk in the right tail. Hence when true and input level of significance our answer will surely 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 a 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 a one-tailed test)

Then we can proceed and assigned P to the function! like below

z=qnorm(p=0.02)
print(z)

Result>>

-2.053749

the above result is negative because it is the lower tail, to do the right tail then the code will look like the following Codes>>

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 a two-tailed test?

Solution

Step1

Here α=5

α/2=0.05/2=0.025 (since it is a two-tailed test)

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

Codes>>

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 by declaring variable names. we will still get the same result. I hope you understand that?

How to calculate the critical value from t-table

To get the critical value from the t-table you will use the qt() function. this function takes the following parameters.

a) p: the probability given(level of significance is written here in decimal)

b) df: this is the degree of freedom it is calculated b subtracting 1 from the sample size.

c) lower. tail: this is the tails you are dealing with it is true b default, it can be assigned to False to walk in the right tail. Hence when true and the input level of significance our answer will surely be negative. otherwise, it will be positive.

Example1 what will be the critical value of a sample of size 18 with a 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

t=qt(p=0.025,df=17)
print(t)

You can also decide not to write each parameter and just input the value like the following but make sure df follows the alpha level immediately.

Codes

t=qt(0.025,17)
print(t)

Example2

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

$$Solution$$

$$ \alpha =0.01(to Decimal), $$ $$n=9,df=n−1, df=28−1=27$$

Now let's input the codes Codes

t=qt(0.001,27)
print(t)

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

Codes

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

Result>>

 2.109816

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

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

this will do all the calculations 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 or Mail. feel free to ask any question it is your guy Maxwizard! Don't forget to follow me on Twitter so that you don't miss any of my articles.