 
---
title: "Lab 11: Inference for Quantitative Data"
author: "Insert Name"
output:
  pdf_document
 
---

## Instructions

**Due before lab Thursday April 29 or Friday April 30**

Read through the following document (it may be helpful to have both the .rmd and .pdf files open side-by-side). Interspersed throughout the text are 12 **Exercises**. Answer these questions in the .rmd file by inserting a code chunk or typing text between the stars directly below the exercise statement.


```{r, include=FALSE}
# Do not edit this code block/chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE, fig.width = 5, fig.height = 3)
```

## Load packages

For this lab we will read be using the `tidyverse` for data wrangling and visualization and `oilabs` for the data set. `infer` is included as well, in case you decide to use it.

```{r, include=FALSE, message = FALSE, warning = FALSE}
library(tidyverse)
library(knitr)
library(openintro)
library(infer)
```



 

## The data

Every two years, the Centers for Disease Control and Prevention conduct
the Youth Risk Behavior Surveillance System (YRBSS) survey, where it
takes data from high schoolers (9th through 12th grade), to analyze
health patterns. You will work with a selected group of variables from a
random sample of observations during one of the years the YRBSS was
conducted.

Load the `yrbss` data set into your workspace. Then type `View(yrbss)` in the console to explore it.

```{r}
data(yrbss)
```

There are observations on 13 different variables, some categorical and
some numerical. The meaning of each variable can be found by bringing up
the help file:

```{r}
?yrbss
```

### Exercise 1

What are the observations in this data set? How many observations are there in our
    sample?

Remember that you can answer this question by viewing the data in the
data viewer or by using the following command:

```{r}
glimpse(yrbss)
```


**********************************************************************

**********************************************************************

## Exploratory data analysis

You will first start with analyzing the weight of the participants in
kilograms: `weight`.

### Exercise 2

Using visualization and summary statistics, describe the
    distribution of weights. Also note how many missing observations
    there are (R codes missing data as `NA`). The `summary` function can
    be useful.

```{r}
summary(yrbss$weight)
```


**********************************************************************

**********************************************************************
 



Next, consider the possible relationship between a high schooler’s
weight and their physical activity. Plotting the data is a useful first
step because it helps us quickly visualize trends, identify strong
associations, and develop research questions.

First, we remove the NAs from weight and physical activity, and then create a new variable `physical_3plus`, which will be coded
as either “yes” if they are physically active for at least 3 days a
week, and “no” if not.

```{r}
yrbss <- yrbss %>% 
  drop_na(weight, physically_active_7d) %>% 
  mutate(physical_3plus = ifelse(physically_active_7d > 2, "yes", "no"))
```

### Exercise 3

Make a side-by-side boxplot of `physical_3plus` and `weight`. Is
    there a relationship between these two variables? What did you
    expect and why? (hint: `ggplot2` prefers the boxplots to be
    vertical).
    
**********************************************************************

**********************************************************************

The box plots show how the medians of the two distributions compare, but
we can also compare the means of the distributions using the following
to first group the data by the `physical_3plus` variable, and then
calculate the mean `weight` in these groups using the `mean` function
while ignoring missing values by setting the `na.rm` argument to `TRUE`.

```{r}
yrbss %>%
  group_by(physical_3plus) %>%
  summarise(mean_weight = mean(weight, na.rm = TRUE))
```

There is an observed difference, but is this difference statistically
significant? In order to answer this question we will conduct a
hypothesis test.



## Inference Using Permutation
The classical method to address this question is to conduct a two-sample
t-test. (Discussed in Wednesday and Friday's lectures).

An alternative technique is to skip the t-distribution and use a
computational technique.


### Exercise 4

Write null and alternate hypotheses for this investigation (both in words and in symbols).

**********************************************************************

**********************************************************************

Let’s start by calculating and saving the observed test statistic, which
we’ll call `obs_diff`.

```{r}
obs_diff <- yrbss %>%
  specify(weight ~ physical_3plus) %>%
  calculate(stat = "diff in means", order = c("yes", "no"))
```

Notice how you have to specify the order of the difference in the same
manner that you did when looking at the difference in proportions.

Next you can use permutation to generate 500 data sets under the notion
that the `weight` is independent of `physical_3plus`.

```{r}
null <- yrbss %>%
  specify(weight ~ physical_3plus) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 500, type = "permute") %>%
  calculate(stat = "diff in means", order = c("yes", "no"))
```

We can visualize this null distribution with the following code:

```{r}
null %>%
  visualize()
```

### Exercise 5 

How many of these `null` permutations have a difference of at least
    `obs_diff`? Shade those statistics in your null distribution plot
    and calculate the precise proportion (the p-value). Make a conclusion about your original hypotheses.

**********************************************************************

**********************************************************************

## More Practice

### Exercise 6

Calculate a 95% confidence interval for the average height in meters
    (`height`) and interpret it in context. For this CI, use the 
    bootstrapping method with type = se. Additionally, find the standard error of the bootstrap 
    distribution. Then compare it with the theory-based SE of $$s / \sqrt{n}$$
    where $s$ is the standard deviation of the original sample.
    
**********************************************************************

**********************************************************************


### Exercise 7

Conduct a hypothesis test evaluating whether the average height is
    different for those who exercise at least three times a week and
    those who don’t.

**********************************************************************

**********************************************************************


### Exercise 8

Now, a non-inference question: How many unique answers are there in
    the dataset to the question regarding television viewing?
    (`hours_tv_per_school_day`). You can either manually examine the data
    using View (tedious) or use the `table` function which lists out each
    level of a variable and the number of occurences. To select just a 
    single variable named `var` from a data frame called `DD`, use the code 
    `DD$var`.

**********************************************************************

**********************************************************************


### Exercise 9

Come up with a research question evaluating the relationship between
    height or weight and sleep. Formulate the question in a way that it
    can be answered using a hypothesis test and/or a confidence interval
    concerning the difference in means. Report the statistical results,
    and also provide an explanation in plain language. Be sure to check
    all assumptions, state your alpha level, and conclude in context.

**********************************************************************

**********************************************************************

### Exercise 10

Consider the following data set listing the ages of a random selection of residents of four regions (A, B, C, D) in the United States. In each case, assume that the data represents a simple random sample drawn from all residents in each region.


```{r}
us_ages<-read_csv("/home/courses/math141s20/Data/us_ages.csv")
```


A.  Create a histogram of each sample. Then rank the samples (from least to greatest) corresponding to how appropriate it would be to use the $t$-prodecures for estimating the mean age of the region. Briefly justify your choice.

*********************************************************************************

*********************************************************************************

B.  For each sample, compute the sample mean and standard deviation, give the degrees of freedom for the corresponding $t$ distribution, and estimate the standard error.


*********************************************************************************

*********************************************************************************


C.  For the sample from Part A where $t$-procecures were most appropriate, use theory-based methods to create a $95\%$ confidence interval for the true mean age of the region. 

*********************************************************************************

*********************************************************************************




### Exercise 11 

In the mid-1990s, a Nabisco marketing campaign claimed that there were at least 1000 chips in every bag of Chips Ahoy! cookies. A group of Air Force adets collected samples of 42 bags of Chips Ahoy! cookies from locations across the country to verify this claim. The cookies were dissolved in water and the number of chips in each bag were counted by the cadets. The average number of chips per bag was 1261.6, with standard deviation 117.6 chips.

A.  Explain why it was important to purchase cookie bags from locations all across the country.

*********************************************************************************

*********************************************************************************

B.  Test whether the average number of chips per bag is greater than 1000. Be sure to show all steps in your process (hypotheses, test statistic, P-value, conclusion).

*********************************************************************************

*********************************************************************************

C.  Explain why it would not be possible to use the `infer` package to perform this hypothesis test using just the information provided in this problem.

*********************************************************************************

*********************************************************************************

### Exercise 12

Let's return to data from the thesis tower and determine if we have evidence that the number of pages varies by division.  The following code chunk loads a subset the data which only includes the three most common divisions (MNS = Mathematics and Natural Sciences, LL = Literature and Languages, and HSS = History and Social Science).

```{r}
# Load data
thesis_data <- read_csv("/home/courses/math141f19/Data/theses.csv") %>%
  drop_na(division, number_of_pages)
```

A.  State the null and alternative hypotheses in words.

*********************************************************************************

*********************************************************************************


B.  Create a boxplot to compare the number of pages across division. Add the following layer (omit the backticks) to the end of your ggplot to include group means on your plot:

```+ stat_summary(fun.y = mean, colour = "red", geom = "point", size = 3)```

Draw some initial conclusions based on your graphic.



*********************************************************************************

*********************************************************************************

C.  Compute the ANOVA test statistic.  What does this imply about the between group variability compared to the within group variability?

 

*********************************************************************************

*********************************************************************************

D.  Generate the null distribution of the test statistic.  Comparing your observed test statistic to the null distribution, what does this imply about the sample results?
 

*********************************************************************************

*********************************************************************************

E.  Check the conditions for whether or not we can approximate the null distribution with an F distribution.

 

*********************************************************************************

*********************************************************************************

F.  Use the function `aov(response_variable ~ explanatory_variable, data = your_data )` to produce an ANOVA table and save the result as a variable `thesis_aov` (i.e fill in `response_variabl`, `explanatory_variable` and `your_data` with the appropriate values.) Run the code `summary(thesis_aov)` to view the table.

*********************************************************************************

*********************************************************************************

G.  Do we have evidence that page lengths, on average, vary by division?  Justify your answer.

*********************************************************************************

*********************************************************************************

H.  What are the effect sizes here?  In other words, on average, how does page length differ between HSS and LL, between HSS and MNS, and between HSS and MNS?  As the domain expert (i.e. a current or future thesising student), are those effect sizes *practically significant*?  Justify your statement.


*********************************************************************************

*********************************************************************************
