Hypothesis testing “by hand” involves computing the test statistic and finding the appropriate p-value for a test directly. This is the only means of hypothesis testing if a function for performing some desired test does not exist (which is probably not the case unless you’re working with a very obscure test).
Let’s start by considering a simple \(z\)-test, where the hypotheses are:
\[H_0: \mu = \mu_0\] \[H_A: \left\{\begin{array}{l} \mu < \mu_0 \\ \mu \neq \mu_0 \\ \mu > \mu_0 \end{array}\right.\]
The test statistic is \(z = \frac{\bar{x} - \mu_0}{\frac{\sigma}{\sqrt{n}}}\) and the p-value (denoted \(p_{\text{val}}\)) is:
\[p_{\text{val}} = \left\{\begin{array}{lr} \Phi(z) & \text{if } H_A \text{ is } \mu < \mu_0 \\ 2\left(1 - \Phi(|z|)\right) & \text{if } H_A \text{ is } \mu \neq \mu_0 \\ 1 - \Phi(z) & \text{if } H_A \text{ is } \mu > \mu_0 \end{array}\right.\]
This test is unrealistic since it assumes \(\sigma\) is known, but it is simple to analyze.
I demonstrate these procedures by testing whether the diameter of black cherry trees is 12 in. I test the hypotheses:
\[H_0: \mu = 12\] \[H_A: \mu \neq 12\]
I use the data set trees
and assume that the population standard deviation is 3. I will base my test on the significance level of \(\alpha = .05\).
# Some basic numbers
xbar <- mean(trees$Girth)
xbar
## [1] 13.24839
n <- nrow(trees)
mu_0 <- 12
sigma <- 3
# Test statistic
z <- (xbar - mu_0)/(sigma/sqrt(n))
z
## [1] 2.316908
# Get p-value, using pnorm
pval <- 2 * (1 - pnorm(abs(z)))
pval
## [1] 0.02050872
Since 0.02 is less than my significance level of .05, I reject the null hypothesis; the mean diameter of black cherry trees is not 12. That said, if I were to use a significance level of \(\alpha = .01\), I would not reject the null hypothesis. Remember that the p-value represents the largest level of significance at which I would fail to reject the null hypothesis. Thus the p-value measures how unlikely my data is if the null hypothesis were true, with smaller p-values indicating more evidence against the null hypothesis.
Testing hypotheses “by hand” follows a similar format to the one demonstrated here, so I do not demonstrate this approach any more in this lecture.