Topic 15: Hypothesis Tests and Confidence Intervals for Numerical Data
**A Detailed Introduction**
**A Shorter Introduction**
The variable `SentenceMonths` is a column within the `SDNYdrug` data frame. Remember that you can access columns in a data frame with the `$` operator.
### ```{r sentencing-ci-3, echo = FALSE} question_radio( "The standard error formula is", answer("$\\displaystyle{S_E = \\sqrt{\\frac{p\\left(1-p\\right)}{n}}}$"), answer("$\\displaystyle{S_E = s/\\sqrt{n}}$", correct = TRUE), answer("$\\displaystyle{S_E = \\sigma/\\sqrt{n}}$", message = "Ooops -- do you really know the population standard deviation for sentence lengths?"), answer("$\\displaystyle{S_E = \\sqrt{\\frac{p_1\\left(1 - p_1\\right)}{n_1} + \\frac{p_2\\left(1 - p_2\\right)}{n_2}}}$"), answer("$\\displaystyle{S_E = \\sqrt{\\frac{s_1^2}{n_1} + \\frac{s_2^2}{n_2}}}$"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### Use the code block below to compute the standard error ```{r sentencing-ci-4, exercise = TRUE} ``` ```{r sentencing-ci-4-check} grade_result( pass_if(~ (abs(.result - (sd(SDNYdrug$SentenceMonths)/sqrt(nrow(SDNYdrug)))) < 0.0005)) ) ``` Remember that you can compute the standard deviation in R with the `sd()` function. Additionally, you can find the number of rows in a data frame by passing the name of the data frame to R's `nrow()` function. The `sqrt()` function will also be useful to you.
### ```{r sentencing-ci-5, echo = FALSE} question_radio( "The distribution to be used is", answer("Normal"), answer("t-distribution with $df = n - 1$", correct = TRUE), answer("t-distribution with $df = \\min\\{n_1 - 1, n_2 - 1\\}$"), answer("t-distribution with $df = n_{\\text{diff}} - 1$"), answer("We should not use either the normal- or t- distributions"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ci-6, echo = FALSE} question_radio( "The desired level of confidence is", answer("90%"), answer("0.95%"), answer("95%", correct = TRUE), answer("98%"), answer("99%"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### Use the code block below to enter or compute the critical value associated with your confidence interval. ```{r sentencing-ci-7, exercise = TRUE} ``` ```{r sentencing-ci-7-check} grade_result( pass_if(~ (abs(.result - qt(0.975, df = nrow(SDNYdrug) - 1)) < 0.005)) ) ``` Did you try using 1.96? Remember that we can't use the normal distribution here. You can find the critical value you need by making use of the `qt()` function.
### Did you remember to use the `qt()` function to determine the critical value? With so many observations, the correct critical value differed very little from the 1.96 value used with the normal distribution. Remember that the critical values provided on the *standard error decision tree* are for use with the normal distribution only. Any time we use a sample standard deviation as a "stand-in" for the population standard deviation while computing standard error, we should be using critical values from a $t$-distribution. We can get the critical value using R's `qt()` function -- this will make a real difference if sample sizes are smaller. ### Use the code block below to compute the lower bound for the 95% confidence interval. ```{r sentencing-ci-8, exercise = TRUE} ``` ```{r sentencing-ci-8-check} grade_result( pass_if(~ (abs(.result - (mean(SDNYdrug$SentenceMonths) - ((qt(0.975, nrow(SDNYdrug) - 1))*(sd(SDNYdrug$SentenceMonths)/sqrt(nrow(SDNYdrug)))))) < 0.01)) ) ``` ### And use the code block below to compute the upper bound for the 95% confidence interval. ```{r sentencing-ci-9, exercise = TRUE} ``` ```{r sentencing-ci-9-check} grade_result( pass_if(~ (abs(.result - (mean(SDNYdrug$SentenceMonths) + ((qt(0.975, nrow(SDNYdrug) - 1))*(sd(SDNYdrug$SentenceMonths)/sqrt(nrow(SDNYdrug)))))) < 0.01)) ) ``` ### ```{r try-it-1a-question-10, echo = FALSE} question_radio( "The interpretations of the values you identified are:", answer("We are 95% confident that the true average sentence lengths for drug-related offenders in the Southern District of New York State is between the lower bound and upper bounds calculated.", correct = TRUE), answer("95% of sentence lengths for drug-related charges in the Southern District of New York State will fall between the lower and upper bounds calculated."), answer("We are 95% confident that the average sentence lengths for drug-related crimes within the Southern District of New York State in this sample fall between the lower and upper bounds calculated"), answer("There is a 95% chance a criminal charged with a drug-related offense in the Southern District of New York State will receive a sentence between the lower and upper bounds calculated."), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ci-11, echo = FALSE} question_radio( "Does this sample provide evidence to suggest that the average sentence length for drug-related charges in the Southern District of New York State exceeds three years (36 months) in jail?", answer("No. The confidence interval shows that an average sentence below 36 months is plausible."), answer("Yes. The confidence interval for the average sentence length includes only values exceeding 36 months.", correct = TRUE), answer("No. Some sentence lengths are below 36 months while others exceed 36 months."), answer("It is impossible to say, since a different sample would result in a different confidence interval."), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### **Question 2:** Conduct a hypothesis test at the $\alpha = 0.10$ level of significance to determine whether the sample data provides significant evidence to suggest that the average sentence length for white offenders and average sentence length for non-white offenders differs for drug-related cases in the Southern District of New York State. ### ```{r sentencing-ht-1, echo = FALSE} question_radio( "To answer the question as asked, we should", answer("Compute a confidence interval"), answer("Conduct a hypothesis test", correct = TRUE), answer("Compute a probability"), answer("Find a required sample size"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-2, echo = FALSE} question_radio( "What is the level of significance associated with this test?", answer("$\\alpha = 0.01$"), answer("$\\alpha = 0.10$", correct = TRUE), answer("$\\alpha = 0.05$"), answer("The $p$-value"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-3, echo = FALSE} question_radio( "Does this hypothesis test involve testing a statement about the mean ($\\mu$), a proportion ($p$), or something else?", answer("One or more means", correct = TRUE), answer("One or more proportions"), answer("Something else altogether"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-4, echo = FALSE} question_radio( "How many groups are being compared in this test?", answer("The test involves only a single group"), answer("The test compares two groups", correct = TRUE), answer("The test compares more than two groups"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-5, echo = FALSE} question_radio( "Which of the following are the hypotheses associated with this test?", answer("$\\begin{array}{ll} H_0: & \\mu_{\\text{white}} - \\mu_{\\text{non-white}} = 0\\\\ H_a: & \\mu_{\\text{white}} - \\mu_{\\text{non-white}} \\neq 0\\end{array}$", correct = TRUE), answer("$\\begin{array}{ll} H_0: & \\mu_{\\text{white}} - \\mu_{\\text{non-white}} = 0\\\\ H_a: & \\mu_{\\text{white}} - \\mu_{\\text{non-white}} > 0\\end{array}$"), answer("$\\begin{array}{ll} H_0: & \\mu_{\\text{white}} - \\mu_{\\text{non-white}} = 0\\\\ H_a: & \\mu_{\\text{white}} - \\mu_{\\text{non-white}} < 0\\end{array}$"), answer("$\\begin{array}{ll} H_0: & \\mu_{\\text{white}} \\neq \\mu_{\\text{non-white}}\\\\ H_a: & \\mu_{\\text{white}} = \\mu_{\\text{non-white}}\\end{array}$"), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-6, echo = FALSE} question_radio( "Do we know the population standard deviation(s), $\\sigma$, for sentence length in each group?", answer("Yes, we can compute the population standard deviation of sentence lenghts from the data with the `sd()` function"), answer("Yes, we are provided the population standard deviation of sentence lengths."), answer("No, we can compute the standard deviation in sentence lengths from the data, but this will give us sample standard deviations rather than the population standard deviations.", correct = TRUE), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-7, echo = FALSE} question_radio( "Are the observations in the two groups (sentences handed to white offenders and sentences handed to non-white offenders) paired?", answer("No. There is no reason to suggest that sentences are paired.", correct = TRUE), answer("Yes. Every sentence handed to a white offender can be naturally paired to a sentence handed to a non-white offender."), allow_retry = TRUE, random_answer_order = TRUE ) ``` ### ```{r sentencing-ht-8, echo = FALSE} quiz( question_radio( "Which standard error formula should be used in the computation of the test statistic?", answer("$\\displaystyle{S_E = \\sqrt{\\frac{s_1^2}{n_1} + \\frac{s_2^2}{n_2}}}$", correct = TRUE), answer("$\\displaystyle{S_E = s/\\sqrt{n}}$"), answer("$\\displaystyle{S_E = \\sqrt{\\frac{\\sigma_1^2}{n_1} + \\frac{\\sigma_2^2}{n_2}}}$"), answer("$\\displaystyle{S_E = \\sqrt{\\frac{p\\left(1 - p\\right)}{n}}}$"), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "Which distribution does the test statistic follow for this hypothesis test?", answer("The normal distribution"), answer("The $t$-distribution with $n-1$ degrees of freedom."), answer("The $t$-distribution with $\\min\\{n_1 - 1, n_2 - 1\\}$ degrees of freedom.", correct = TRUE), answer("The $t$-distribution with $n_{\\text{diff}} - 1$ degrees of freedom."), allow_retry = TRUE, random_answer_order = TRUE ) ) ``` ### I've stored the sentence lengths (in months) handed down to white offenders in an object called `whiteSentences` and the sentence lengths for non-white offenders in an object called `nonWhiteSentences`. Use the code blocks below to answer the corresponding questions. Find the number of white offenders sentenced in our dataset. ```{r sentencing-ht-9, exercise = TRUE} ``` ```{r sentencing-ht-9-check} grade_result( pass_if(~ (.result == length(whiteSentences))) ) ``` Since `whiteSentences` and `nonWhiteSentences` are vectors of sentence lengths rather than full data frames, the `nrow()` function won't work on them. Try using the `length()` function instead.
### Find the number of non-white offenders sentenced in our dataset. ```{r sentencing-ht-10, exercise = TRUE} ``` ```{r sentencing-ht-10-check} grade_result( pass_if(~ (.result == length(nonWhiteSentences))) ) ``` Compute the average sentence length in months for the white offenders. ```{r sentencing-ht-11, exercise = TRUE} ``` ```{r sentencing-ht-11-check} grade_result( pass_if(~ (abs(.result - mean(whiteSentences)) < 0.01)) ) ``` Compute the average sentence length in months for the non-white offenders. ```{r sentencing-ht-12, exercise = TRUE} ``` ```{r sentencing-ht-12-check} grade_result( pass_if(~ (abs(.result - mean(nonWhiteSentences)) < 0.01)) ) ``` ### Compute the standard deviation in sentence lengths for the white offenders. ```{r sentencing-ht-13, exercise = TRUE} ``` ```{r sentencing-ht-13-check} grade_result( pass_if(~ (abs(.result - sd(whiteSentences)) < 0.01)) ) ``` Compute the standard deviation in sentence lengths for the non-white offenders. ```{r sentencing-ht-14, exercise = TRUE} ``` ```{r sentencing-ht-14-check} grade_result( pass_if(~ (abs(.result - sd(nonWhiteSentences)) < 0.01)) ) ``` ### Okay, we've answered lots of questions that give us pieces of our analysis. Now, let's think about putting to pieces together to compute the test statistic, $p$-value, and complete the hypothesis test. ```{r sentencing-ht-15, echo = FALSE} quiz( question_radio( "The population parameter in question for this hypothesis test is", answer("$\\mu_{\\text{white}} - \\mu_{\\text{non-white}}$", correct = TRUE), answer("$\\mu_{\\text{white}}$"), answer("$\\mu_{\\text{non-white}}$"), answer("$\\bar{x}_{\\text{white}} - \\bar{x}_{\\text{non-white}}$", message = "Oops. This is the point estimate!"), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "The *null value* is", answer("0", correct = TRUE), answer("0.5"), answer("14"), answer("44.79"), answer("39.25"), allow_retry = TRUE, random_answer_order = TRUE ) ) ``` ### Compute the point estimate ```{r sentencing-ht-16, exercise = TRUE} ``` ```{r sentencing-ht-16-check} grade_result( pass_if(~ (abs(abs(.result) - abs(mean(whiteSentences) - mean(nonWhiteSentences))) < 0.01)) ) ``` ### Compute the standard error ```{r sentencing-ht-17, exercise = TRUE} ``` ```{r sentencing-ht-17-check} grade_result( pass_if(~ (abs(.result - (sqrt((sd(whiteSentences)^2/length(whiteSentences)) + (sd(nonWhiteSentences)^2/length(nonWhiteSentences))))) < 0.01)) ) ``` ### Compute the test statistic ```{r sentencing-ht-18, exercise = TRUE} ``` ```{r sentencing-ht-18-check} grade_result( pass_if(~ (abs(.result - ((mean(whiteSentences) - mean(nonWhiteSentences)) - 0)/((sqrt((sd(whiteSentences)^2/length(whiteSentences)) + (sd(nonWhiteSentences)^2/length(nonWhiteSentences)))))) < 0.01)) ) ``` ### Compute the degrees of freedom for this test. ```{r sentencing-ht-18_5, exercise = TRUE} ``` ```{r sentencing-ht-18_5-check} grade_result( pass_if(~ (.result - (min(length(whiteSentences), length(nonWhiteSentences)) - 1)) == 0) ) ``` ### Compute the p-value ```{r sentencing-ht-19, exercise = TRUE} ``` ```{r sentencing-ht-19-check} grade_result( pass_if(~ (abs(.result - (2*(1 - pt(abs(((mean(whiteSentences) - mean(nonWhiteSentences)) - 0)/((sqrt((sd(whiteSentences)^2/length(whiteSentences)) + (sd(nonWhiteSentences)^2/length(nonWhiteSentences)))))), df = min(length(whiteSentences) - 1, length(nonWhiteSentences) - 1))))) < 0.01)) ) ``` ### Answer the following to complete the hypothesis test. ```{r sentencing-ht-20, echo = FALSE} quiz( question_radio( "What is the result of the test?", answer("Since $p \\geq \\alpha$, we do not have enough evidence to reject the null hypothesis."), answer("Since $p \\geq \\alpha$, we accept the null hypothesis."), answer("Since $p < \\alpha$, we reject the null hypothesis and accept the alternative hypothesis.", correct = TRUE), answer("Since $p < \\alpha$, we fail to reject the null hypothesis."), answer("It is impossible to determine."), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "The result of the test means that", answer("The sample data did not provide significant evidence to suggest that there is difference in average sentence length handed to white versus non-white defendants for drug-related cases in the Southern District of New York State."), answer("The sample data proved that there is no difference in the average length of sentences handed down to white versus non-white defendants for drug-related cases in the Southern District of New York State."), answer("The sample data provided significant evidence to suggest a difference in the average length of sentences handed down to white versus non-white defendants for drug-related cases in the Southern District of New York State.", correct = TRUE), answer("Sample data cannot be used to test a claim about a population -- different samples yeild different results and so any claim about a population is nonsensicle unless a census was conducted."), allow_retry = TRUE, random_answer_order = TRUE ), question_radio( "Which of the following is an implication of our result", answer("We have proved that racial bias exists in sentencing for drug-related cases in the Southern District of New York State"), answer("We have found evidence to support the hypothesis that racial bias exists in sentencing for drug-related cases in the Southern District of New York State. Our result suggests that a more formal audit of sentencing recommendations should be undertaken.", correct = TRUE), answer("We have found evidence to support the hypothesis that racial bias exists in the courts across all case types."), answer("We have found evidence to suggest that racial minorities receive more lengthy sentences in drug-related cases in the Southern District of New York State.") ) ) ``` ### Submit ```{r context="server"} learnrhash::encoder_logic(strip_output = TRUE) ``` ```{r encode, echo=FALSE} learnrhash::encoder_ui( ui_before = shiny::div( "If you have completed this tutorial and are happy with all of your", "solutions, please click the button below to generate your hash and", "submit it using the corresponding tutorial assignment tab on Blackboard", shiny::tags$br() ), ui_after = shiny::tags$a(href = "https://blackboard.ncat.edu/", "NCAT Blackboard") ) ``` ### Summary Wow -- good work through all of that! I hope you found the application to sentence lengths in the SDNY to be interesting and eye-opening. The approaches to these problems were really drawn out, step-by-step, to help you identify the necessary steps for computing a confidence interval or conducting a hypothesis test with numerical data. Think about each individual step you were asked to complete and how that step relates to the larger process of completing the inference task. Doing this will help you become a better independent problem solver. Your next workbook will provide you an opportunity to practice with a mixture of inference tasks dealing with both numerical and categorical data. You'll become more comfortable with the content and applying our statistical techniques as you work through more and more problems.