
---
title: "M6: The model"
author: "Your group's names (and SUNet IDs)"
date: "`r Sys.Date()`"
output: pdf_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(coursekata)
library(here)
```

<!-- Fill in every line marked [...]. The step numbers match the steps on the
handout. Throughout, replace Thumb (the outcome) and Sex (the explanatory
variable) with your own variables. One of you should knit this to PDF
(Session -> Restart R, then Knit -> Knit to PDF) and submit this file and the
PDF on Gradescope, adding the other members of your group. -->

## Step 1. Your folder

- **Who will upload the changed files to the shared folder at the end of section:** [...]

## Step 2. Your data and your questions

```{r load-data}
# Replace this with:
#   processed <- read.csv(here("data", "processed", "processed-data.csv")) # this is the data you prepared in prepare-data.R
# (Fingers is only here so the file knits before you start.)
processed <- Fingers
```

- **Question 1:** [...]
- **Question 2:** [...]
- **What you predicted for question 1 in M4 (direction, and rough size in the outcome's units):** [...]

## Step 3. The empty model

```{r empty-model}
empty_model <- lm(Thumb ~ NULL, data = processed)
empty_model
```

**What `b0` is, and what the empty model predicts for every case:** [...]

## Step 4. The model for question 1

```{r q1-model}
q1_model <- lm(Thumb ~ Sex, data = processed)
q1_model

# For a categorical explanatory variable:
gf_jitter(Thumb ~ Sex, data = processed, width = 0.1) %>%
  gf_model(q1_model)
# For a quantitative explanatory variable, use instead:
#   gf_point(outcome ~ explanatory, data = processed) %>% gf_lm()

```

## Step 5. The estimates in units

- **`b0`, in a sentence with numbers and units (for a group model, name the reference group; for a regression model, say whether 0 lies inside the range of the explanatory variable):** [...]
- **`b1`, in a sentence with numbers and units:** [...]

## Step 6. The strength of the relationship

```{r strength}
supernova(q1_model)
# Only if your explanatory variable has exactly two groups:
cohensD(Thumb ~ Sex, data = processed)
```

- **PRE and F:** [...]
- **The meaning of the PRE for your question, in a sentence:** [...]
- **Cohen's d (two groups only):** [...]

## Step 7. Could chance alone produce a b1 of this size?

```{r shuffles}
sample_b1 <- b1(Thumb ~ Sex, data = processed)
shuffled_b1s <- do(1000) * b1(shuffle(Thumb) ~ Sex, data = processed)

gf_histogram(~ b1, data = shuffled_b1s) %>%
  gf_point(x = sample_b1, y = 0, color = "red", size = 3)

# The proportion of shuffled b1s at least as far from 0 as your real b1, in either direction:
mean(abs(shuffled_b1s$b1) >= abs(sample_b1))
```

- **The proportion of shuffled `b1`s at least as far from 0 as your real `b1`, in either direction:** [...]
- **The meaning of that proportion, in a sentence (if the outcome had no relationship with the explanatory variable, how often would a shuffle alone produce a `b1` at least as far from 0 as your real `b1`?):** [...]

## Step 8. The comparison with your M4 prediction

- **Direction: did it match the direction you predicted in M4?** [...]
- **Size: close to the size you predicted, larger, or smaller? Give both numbers, the predicted size and the fitted `b1`:** [...]

## Step 9. Question 2 so far

```{r question-2}
# If the other variable is in your data: look at the question 1 relationship
# within each of its groups, for example by faceting the plot from step 4.
# If it is not in your data, comment out this chunk by putting a # in front of each line
gf_jitter(Thumb ~ Sex, data = processed, width = 0.1) %>%
  gf_facet_grid(. ~ Job)
```

**Is `b1` about the same in every group, or does it differ between groups in its direction or size? (If the variable is not in your data: the pattern you would expect to see across its groups if you had the variable you described in M5.)** [...]

## Step 10. Which M4 DGPs could still be at work?

<!-- One line for each DGP you wrote in M4. For the evidence, use this
milestone's model and shuffles, your question 2 plot or table from M5 (step
10) and step 9 here, and the variables you listed in M4 as the ones that could
distinguish your DGPs. Add lines if you need them. -->

- **DGP:** [...] **Ruled out, consistent, or not distinguishable from the others, and the evidence (which plot, number, or milestone):** [...]
- **DGP:** [...] **Ruled out, consistent, or not distinguishable from the others, and the evidence (which plot, number, or milestone):** [...]
- **DGP:** [...] **Ruled out, consistent, or not distinguishable from the others, and the evidence (which plot, number, or milestone):** [...]
