Coding Hypothesis Tests


Start Coding

The first step in hypothesis testing is to calculate the test statistic. When the population standard deviation is known, the formula for hte test statistic uses the population standard deviation and is denoted by z. The test statistic is written z because it follows a standard normal, or z, distribution. The formula for the test statistic uses four values: the sample mean, the hypothesized value of the population mean, the population standard deviation and the sample size. So a function that calculates the the test statistic will require four arguments.

 1 test <- function(mean, mu, sd, size) { # Four Inputs
 2     (mean - mu) / (sd / sqrt(size)) # Output Test Statistic
 3 }
 4 test(mean = 5.5, mu = 6, sd = 2, size = 40) # -1.58

There are two ways to use the test statistic to conduct a hypothesis test. One way is with the p-value approach and the other is the critical value approach. In the p-value approach, we calculate a p-value, where the "p" stands for probability. In a lower tail test, the p-value is the probability of getting a value of the test statistic as small as or smaller than the value from the sample. So to calculate this in R, we can use the pnorm() function and put the test statistic as the argument. This will give us the area under the standard normal distribution to the left of the test statistic.

 5 pval <- function(test) { # Input Test Statistic
 6     pnorm(test) # Output p-Value
 7 }
 8 pval(test = -1.25) # .0571

Once we've calculate the p-value, we can use the rejection rule for the p-value approach to determine whether or not to reject the null hypothesis. The rejection rule for the p-value approach says to reject the null hypothesis if the p-value is less than or equal to the level of significance, $\alpha$. In order to program this into R, we need to use an if-else statement. The condition for our if-else statement is that the p-value is less than or equal to the level of significance. If this condition is met, our function prints "reject null". If the condition is not satisfied, meaning the p-value is greater than the level of significance, our function prints "do not reject null".

 9 reject <- function(pval, alpha) { # Input p-Value and Level of Significance
 10     if (pval <= alpha) { # Condition
 11          "reject null" # Output if Condition Satisfied
 12     } else {
 11          "do not reject null" # Output if Condition Not Satisfied
 12     }
 13 }
 14 reject(pval = .0571, alpha = .05) # "do not reject null"

The second way of using the test statistic to conduct a hypothesis test is through the critical value approach. In the critical value approach, we calculate a critical value and compare its value to the test statistic. The critical value in a lower tail test is the value of the test statistic corresponding to an area of $\alpha$ (level of significance) in the lower tail of the sampling distribution of the test statistic. Since the test statstic follows a z-distribution, the critical value is the z-value corresponding to an area of $\alpha$ in the lower tail of the z-distribution. So, in our function, we can use the qnorm function and use the level of significance as the argument.

 5 crit <- function(alpha) { # Input Test Statistic
 6     qnorm(alpha) # Output p-Value
 7 }
 8 crit(alpha = .05) # -1.64

Once we've calculate the critical value, we can use the rejection rule for the critical value approach to determine whether or not to reject the null hypothesis. The rejection rule for the p-value approach says to reject the null hypothesis if the p-value is less than or equal to the level of significance, $\alpha$. In order to program this into R, we need to use an if-else statement. The condition for our if-else statement is that the p-value is less than or equal to the level of significance. If this condition is met, our function prints "reject null". If the condition is not satisfied, meaning the p-value is greater than the level of significance, our function prints "do not reject null".

 9 reject <- function(pval, alpha) { # Input p-Value and Level of Significance
 10     if (pval <= alpha) { # Condition
 11          "reject null" # Output if Condition Satisfied
 12     } else {
 11          "do not reject null" # Output if Condition Not Satisfied
 12     }
 13 }
 14 reject(.0571, .05) # "do not reject null"

The first step in hypothesis testing is to calculate the test statistic. When the population standard deviation is known, the formula for hte test statistic uses the population standard deviation and is denoted by z. The test statistic is written z because it follows a standard normal, or z, distribution. The formula for the test statistic uses four values: the sample mean, the hypothesized value of the population mean, the population standard deviation and the sample size. So a function that calculates the the test statistic will require four arguments.

 1 test <- function(x, mu) { # Four Inputs
 2     (mean(x) - mu) / (sd(x) / sqrt(length(x))) # Output Test Statistic
 3 }
 4 test(x, mu = 6) # -1.58

There are two ways to use the test statistic to conduct a hypothesis test. One way is with the p-value approach and the other is the critical value approach. In the p-value approach, we calculate a p-value, where the "p" stands for probability. In a lower tail test, the p-value is the probability of getting a value of the test statistic as small as or smaller than the value from the sample. So to calculate this in R, we can use the pnorm() function and put the test statistic as the argument. This will give us the area under the standard normal distribution to the left of the test statistic.

 5 pval <- function(test) { # Input Test Statistic
 6     pt(test, lower.tail = FALSE) # Output p-Value
 7 }
 8 pval(test = -1.25) # .0571

Once we've calculate the p-value, we can use the rejection rule for the p-value approach to determine whether or not to reject the null hypothesis. The rejection rule for the p-value approach says to reject the null hypothesis if the p-value is less than or equal to the level of significance, $\alpha$. In order to program this into R, we need to use an if-else statement. The condition for our if-else statement is that the p-value is less than or equal to the level of significance. If this condition is met, our function prints "reject null". If the condition is not satisfied, meaning the p-value is greater than the level of significance, our function prints "do not reject null".

 9 reject <- function(pval, alpha) { # Input p-Value and Level of Significance
 10     if (pval <= alpha) { # Condition
 11          "reject null" # Output if Condition Satisfied
 12     } else {
 11          "do not reject null" # Output if Condition Not Satisfied
 12     }
 13 }
 14 reject(pval = .0571, alpha = .05) # "do not reject null"

The second way of using the test statistic to conduct a hypothesis test is through the critical value approach. In the critical value approach, we calculate a critical value and compare its value to the test statistic. The critical value in a lower tail test is the value of the test statistic corresponding to an area of $\alpha$ (level of significance) in the lower tail of the sampling distribution of the test statistic. Since the test statstic follows a z-distribution, the critical value is the z-value corresponding to an area of $\alpha$ in the lower tail of the z-distribution. So, in our function, we can use the qnorm function and use the level of significance as the argument.

 5 crit <- function(alpha) { # Input Test Statistic
 6     qnorm(alpha) # Output p-Value
 7 }
 8 crit(alpha = .05) # -1.64

Once we've calculate the critical value, we can use the rejection rule for the critical value approach to determine whether or not to reject the null hypothesis. The rejection rule for the p-value approach says to reject the null hypothesis if the p-value is less than or equal to the level of significance, $\alpha$. In order to program this into R, we need to use an if-else statement. The condition for our if-else statement is that the p-value is less than or equal to the level of significance. If this condition is met, our function prints "reject null". If the condition is not satisfied, meaning the p-value is greater than the level of significance, our function prints "do not reject null".

 9 reject <- function(pval, alpha) { # Input p-Value and Level of Significance
 10     if (pval <= alpha) { # Condition
 11          "reject null" # Output if Condition Satisfied
 12     } else {
 11          "do not reject null" # Output if Condition Not Satisfied
 12     }
 13 }
 14 reject(.0571, .05) # "do not reject null"