---
title: "Confidence intervals"
subtitle: "Notes and in-class exercises"
format: 
  html:
    embed-resources: true
    toc: true
---

# Notes

## Learning goals

By the end of this lesson, you should be able to:

- Construct (approximate) confidence intervals by hand using the 68-95-99.7 rule
- Construct exact confidence intervals in R
- Interpret confidence intervals in context by referring to the coefficient of interest
- Use confidence intervals to make statements about whether there appear to be true population relationships, changes, and differences

## Readings and videos

Please complete the following reading **before** class.

- Reading: Section 7 Introduction, Section 7.1, Section 7.2 (stop when you get to 7.2.4.3 Confidence Intervals for Prediction) in the [STAT 155 Notes](https://mac-stat.github.io/Stat155Notes/)

Optionally you can use the following videos as a companion to the reading (not in place of the reading):

- Video 1: [Introduction to Confidence Intervals](https://youtu.be/CCgpmFjENwA)
- Video 2: [Confidence Intervals: Construction and Interpretation](https://youtu.be/QAbRYk5g8D8)



# Exercises

For the first 2 exercises, we'll revisit the bikeshare dataset.

```{r warning=FALSE, message=FALSE}
# Load packages and import data
library(ggplot2)
library(dplyr)
library(readr)
library(stringr)
library(tidyr)
library(broom)

bikes <- read_csv("https://mac-stat.github.io/data/bikeshare.csv")
```

## Exercise 1

**Research question:** Is the relationship between wind speed (`windspeed`) (in miles per hour) and number of riders (`riders_total`) different across weekdays and weekends?

### Part a

Construct and interpret a visualization that would address this question.

```{r}
# Visualiation
```

> Your interpretation here

### Part b

Fit a regression model that would address our research question. (Should it be a linear or a logistic regression model?) Interpret **only** the coefficient of interest.

```{r}
mod_bikes <- ___
```

> Your interpretation here

### Part c

- Construct an approximate 95% confidence interval (CI) for the coefficient of interest by hand using the 68-95-99.7 rule.

```{r}
# Remember, R is a calculator! Use it as such here!
```


- Compare your confidence interval to the one given by `confint()` which gives an exact confidence interval. (The columns give the lower and upper ends of the CI for each coefficient.)

```{r}
# Using confint()
confint(mod_bikes, level = 0.95)
```

- Interpret the exact confidence interval in context.

> Your interpretation here

- Is zero in the interval? Do we have evidence for a real difference in the windspeed-riders relationship across weekends and weekdays?

> Your response here


### Part d

Let's see if these results agree when looking at adjusted R-squared.

Fit another regression model that does not have the coefficient of interest from your Part b model. Compare the adjusted R-squared values between this model and the Part b model. Explain your findings.

```{r}
# Your other regression model here

```


> Your comparison / explanation here

## Exercise 2

**Research question:** How different is holiday ridership from non-holidays, after accounting for confounding factors?

### Part a

We believe that weather category (`weather_cat`), temperature (`temp_actual`), and wind speed (`windspeed`) confound the relationship of interest.

- Draw a causal graph that shows the 5 variables of interest. Based on your graph do you believe that the 3 potential confounders are indeed confounders (and not mediators or colliders)?

> Feel free to use the white board! Ask for a white board marker! Otherwise, you can *describe* the arrows in your graph here.

> Your response here, as to whether they are confounders

- Construct visualizations that allow you how each potential confounder relates to `riders_total` and to `holiday`.

```{r}
# Visualizations

```


### Part b

Based on your Part a explorations, fit an appropriate regression model to answer our research question. Interpret only the coefficient of interest.

```{r}

```

> Your response here

**A note about scientific notation in R:** Sometimes you may see numbers with the letter `e` in the middle. This is R's way of expressing scientific notation. Whenever you see `e`, replace that with `10 to the power of ...`. So:

- 1.234e+02 is 1.234 x 10^2 = 123.4
- 1.234e-02 is 1.234 x 10^(-2) = 0.01234

### Part c

- Use `confint()` to construct a 95% confidence interval for the coefficient of interest.

```{r}

```


- Interpret this confidence interval in context.

> Your interpretation here

- Is zero in the interval? Do we have evidence for a real holiday effect on ridership?

> Your response here



## Exercise 3

The Western Collaborative Group Study (WCGS) was designed in order to investigate a possible link between Type A behavior and coronary heart disease (CHD), and to develop a framework to select patients for intervention in order to decrease risk of CHD. The study contained 3154 cis men between the ages of 39 and 59 in California who had no history of CHD. They were enrolled in the study in 1960 and 1961, underwent a medical examination and covered their medical history, and they were re-examined annually for interim cardiovascular history.

A full codebook is available [here](https://github.com/Mac-STAT/data/blob/main/wcgs_codebook.md). We will focus on the following variables:

- `chd`: Presence (1) or absence (0) of CHD over followup (outcome)
- `tabp`: Presence (1) or absence (0) of Type A behavior (main variable of interest)
- `age`: Age at time of enrollment in the study (years)
- `sbp`: Systolic blood pressure
- `dbp`: Diastolic blood pressure
- `chol`: Cholesterol (mg/dL)
- `ncigs`: Number of cigarettes smoked per day
- `arcus`: Presence (1) or absence (0) of arcus senilis (a colored ring around the cornea made up of lipids like cholesterol and believed to be a risk factor for CHD)
- `bmi`: BMI = weight * 703 / height^2

**Research question:** Is there a causal effect of Type A/B personality on developing coronary heart disease?

```{r}
wcgs <- read_csv("https://mac-stat.github.io/data/wcgs.csv")
```

### Part a

We believe that the following variables are confounders of the relationship between Type A/B personality `tabp` and coronary heart disease (`CHD`): `age + sbp + dbp + chol + ncigs + arcus + bmi`.

Fit a regression model that would address our research question. (Should it be a linear or a logistic regression model?) Interpret only the coefficient of interest.

```{r}
typea_mod <- ___
```

> Your interpretation here

### Part b

- Construct a 95% confidence interval for the odds ratio of interest.

```{r}
# Confidence interval code
```

- Interpret the confidence interval in context.

> Your interpretation here

- Is 1 contained in the interval? Why is 1 a relevant value to look for here?

> Your response here

### Part c

(On your own time)

The data context in this exercise has a fraught history with the smoking industry. Read [this article](https://www.thecut.com/2016/08/the-tobacco-industry-helped-create-the-type-a-personality.html) for some context about how the Type A personality came to be defined and studied. (One big takeaway: The smoking industry had a large incentive to find something to blame health problems on other than smoking!)





## Exercise 4

For each of the following **MISINTERPRETATIONS** of a 95% confidence interval (a,b), explain why the statement is a misinterpretation.

- Misinterpretation 1: "There is a 95% probability that the population parameter is within (a,b)."

> Your response here

- Misinterpretation 2: "There is a 5% probability that the population parameter is not within (a,b)."

> Your response here

- Misinterpretation 3: "There is a 95% chance that the sample estimate in (a,b)."

> Your response here



## Reflection

How are you feeling about your ability to translate research questions into appropriate statistical investigations and addressing those questions using output from those investigations? What has gotten easier? What remains challenging?

> **Reflection:** 






