---
title: "Practice Set 3"
author: "STUDENT NAME"
date: now
date-format: "YYYY-MM-DDTHH:mm:ssZ"
format:
  html:
    toc: true
    toc-depth: 2
    embed-resources: true
    code-tools: true
    df-print: paged
---

```{r setup}
#| include: false

# Do NOT modify this chunk
knitr::opts_chunk$set(
  collapse = TRUE, 
  warning = FALSE,
  message = FALSE,
  fig.height = 2.75, 
  fig.width = 4.25,
  fig.env = 'figure',
  fig.pos = 'h',
  fig.align = 'center')

# Use a color blind friendly color palette throughout doc
library(tidyverse)
cb_palette <- c("black", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
scale_colour_discrete <- function(...) scale_colour_manual(values = cb_palette, ...)
scale_fill_discrete   <- function(...) scale_fill_manual(values = cb_palette, ...)
theme_set(theme_bw())
```


# Purpose

The goal of this set of practice problems is to practice the following skills:

- Building appropriate multiple linear regression models to address research questions
- Interpreting linear regression model results with a mix of quantitative and categorical predictors




# Directions

1. Create a code chunk in which you load the `ggplot2`, `dplyr`, and `readr` packages. Include the following command in the code chunk to read in the data: `lifts <- read_csv("https://mac-stat.github.io/data/powerlifting.csv")`

2. Continue with the exercises below. You will need to create new code chunks to construct visualizations and models and write interpretations beneath. Put text responses in blockquotes as shown below:

> Response here. (The > at the start of the line starts a blockquote and makes the text larger and easier to read.)

3. Render your work for submission:
    - Click the "Render" button in the menu bar for this pane (blue arrow pointing right). This will create an HTML file containing all of the directions, code, and responses from this activity. A preview of the HTML will appear in the browser.
    - Scroll through and inspect the document to check that your work translated to the HTML format correctly.
    - Close the browser tab.
    - Go to the "Background Jobs" pane in RStudio and click the Stop button to end the rendering process.
    - Locate the rendered HTML file in the folder where this file is saved. Open the HTML to ensure that your work looks as it should (code appears, output displays, interpretations appear). Upload this HTML file to Moodle.



# Exercises

## Context 

Powerlifting is a sport in which athletes compete to lift as much as possible in 3 events: bench press, squat, and deadlift.
[Open Powerlifting](https://www.openpowerlifting.org/) maintains a database of competition results for powerlifters across the world.
We have information on 100,000 lifters from this database.
Check out the codebook [here](https://github.com/Mac-STAT/data/blob/main/powerlifting_codebook.md).




**Research question:**

Are lighter or heavier lifters proportionately stronger?



**Variables of interest in this analysis:**

- `TotalKg`: total amount that an athlete lifted across all 3 events (the squat, bench, and deadlift), in kilograms
- `BodyweightKg`: the athlete's body weight, in kilograms
- `Age`: the athlete's age, in years
- `Sex`: the athlete's sex as recorded by Open Powerlifting, including `M` (male), `F` (female), and `Mx` (gender-neutral). These designations are [detailed here](https://openpowerlifting.gitlab.io/opl-csv/bulk-csv-docs.html))
- `Equipment`: the type of equipment that an athlete used for their lifts (this may require some Googling!)



\
\



## Exercise 1: Exploratory visualizations

**Guiding question:**   
How are age, sex, body weight, and equipment usage related to strength (as measured by `TotalKg`)?

For each of these 4 explanatory variables of strength, in parts a-d, do the following:

- Construct a visualization of the relationship of `TotalKg` with this variable. Thus each visualization should include only *2* variables.
- Write a 1-2 sentence summary of what you learn from the plot. Remember to discuss trend, variability/dispersion about the trend, and any notable outliers, *in context*.

### Part a

Complete the 2 steps above for the relationship of `TotalKg` with age.
NOTE: Using `alpha = 0.1` inside your `geom` line will improve this visualization!

**Put your plot here:**


**Write your discussion here:**



### Part b

Complete the 2 steps above for the relationship of `TotalKg` with sex.

**Put your plot here:**


**Write your discussion here:**



### Part c

Complete the 2 steps above for the relationship of `TotalKg` with body weight.

**Put your plot here:**


**Write your discussion here:**


### Part d

Complete the 2 steps above for the relationship of `TotalKg` with equipment usage.

**Put your plot here:**


**Write your discussion here:**



## Exercise 2: More exploratory visualizations

Let's practice some visualizations with more than 2 variables.
For each of the 3 sets of variables below, construct an appropriate visualization.
Take mental note of what you observe, but no need to write out a discussion.

```{r}
# Plot 1: TotalKg vs BodyweightKg and Age

```


```{r}
# Plot 2: TotalKg vs Sex and Equipment

```

```{r}
# Plot 3: TotalKg vs BodyweightKg and Sex

```



## Exercise 3: Causal diagram

We are interested in the relationship of `TotalKg` with `BodyweightKg`, but are concerned about `Age`, `Sex`, and `Equipment` as potential confounders.

### Part a

Draw a causal diagram that shows how these 5 variables might be related. Draw this by hand or software and save the file as `ps3_dag.jpg` or `ps3_dag.png` in the same folder as this `.qmd` file. You can then insert the diagram as below (delete whichever line you don't use):

![](ps3_dag.jpg)
![](ps3_dag.png)

### Part b

Construct some visualizations that explore whether `Age`, `Sex`, and `Equipment` have a relationship with `BodyweightKg` (ignore `TotalKg` in these visualizations!).


### Part c

Explain how your visualizations in Part b relate to your causal diagram.
For example, what do these visualizations indicate about the various relationships under investigation and is this consistent with your original causal diagram?


### Part d

Identify another possible confounder that would be important to consider but is missing from our data set.



## Exercise 4: Linear regression modeling

**Guiding question:**   
Are lighter or heavier lifters stronger?
Put another way, what's the causal effect of body weight on `TotalKg`?

### Part a

Fit an appropriate linear regression model that answers our *causal* research question, and include a summary table for this model.
Make sure that the predictors used in your model are consistent with your causal diagram from Exercise 4.
(Specifically, in your model of `TotalKg`, you should include body weight and any potential *confounders*.)



### Part b

Interpret the ONE coefficient that answers the research question about whether lighter or heavier lifters are stronger.
*Remember: Use appropriate causation vs. association language, include units, talk about averages rather than individual cases, and interpret the coefficient in context.*





## Exercise 5: More model interpretations

Let's continue to dig into the messages from the model you built in Exercise 5.

### Part a

Interpret the intercept (and comment on whether the intercept is meaningful in this context).




### Part b

Interpret *2* of the remaining coefficients, at least *1* of which corresponds to a categorical predictor.





# Disclosures & citations

In this final section, please share whether you worked with others on this PS, whether you attended office hours to discuss this PS, and whether and how you used AI.
This is here to both help *you* reflect on your approach to learning / assignment completion, and to help the *instructor / preceptors* understand what resources are being utilized.

## Working with others

You're encouraged to work with others on PSs, though all submitted work must be in your own words / code and you must be able to explain everything therein.
Did you discuss this PS / work on this PS with any other STAT 155 students?
If so, include their name(s) here.
NOTE: No worries if you put somebody's name and they don't put yours, or vice versa.

**Your response:**

## Attending office hours

Did you attend any office hours to get help on / discuss this PS?
If so, include the name of the preceptor or instructor whose office hours you attended and roughly how much time you spent in office hours.

**Your response:**

## AI

You're encouraged to AVOID the use of AI and to NEVER use it as your first approach to an exercise.
Learning comes from you doing the puzzling, not from you producing a correct answer.
Did you use AI for any part of this PS?
If so, describe: where you used it (on which exercises), how long you worked on the exercises before turning to AI, and what prompts you used / typed into AI.

**Your response:**





