---
title: "Skills Lab 10"
author: "Jennifer Mankin"
date: "01/04/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

# Essential packages
while(!require(remotes)) install.packages("remotes")
library(kableExtra)

## At home
library(tidyverse)

## On uni computers
library(magrittr)
library(dplyr)
library(tidyr)

# Helper packages
while(!require(ViewPipeSteps)) install.packages("ViewPipeSteps")
while(!require(fcuk)) install.packages("fcuk")
while(!require(introverse)) remotes::install_github("sjspielman/introverse")
```

## As Usual...

- Load required packages in the `setup` code chunk
- Read in the data

URL: https://and.netlify.app/datasets/gensex_2022.csv
Codebook: https://and.netlify.app/docs/gensex_codebook.html

```{r}
gensex <- readr::read_csv("https://and.netlify.app/datasets/gensex_2022.csv")
```

```{r}
gensex %>% 
  dplyr::select(starts_with("gender"))
```


## Decision Time!

What **outcome** would you like to investigate?: `gender_comfort`

What **predictor** would you like to start with?: `age`

## Taking a Look

```{r}
gensex %>% 
  ggplot2::ggplot(aes(x = gender_comfort)) +
  geom_histogram()

gensex %>% 
  ggplot2::ggplot(aes(x = age)) +
  geom_histogram()

gensex %>% 
  ggplot2::ggplot(aes(x = age, y = gender_comfort)) +
  geom_point(position = "jitter") +
  geom_smooth(method = "lm")
```

## Creating the Model

```{r}
# model creation and summary()

m1 <- lm(gender_comfort ~ age, data = gensex)

summary(m1)
```
## Second Predictor

Second predictor: `gender_stable`

```{r}
m2 <- lm(gender_comfort ~ age + gender_stable, data = gensex)

summary(m2)
```

## Tidying Up

```{r}
# tidy() and glance()

m2_tidy <- m2 %>% broom::tidy()
m2_glance <- m2 %>% broom::glance()
```

## Using the Model for Predictions

### One Predictor

```{r}
b0 <- m1_tidy %>% 
  dplyr::filter(term == ) %>% 
  pull(estimate)

b1 <- m1_tidy %>% 
  dplyr::filter(term == ) %>% 
  pull(estimate)
```

The linear model with one predictor is y = b0 + b1*x

```{r}
x <- 45
b0 + b1*x
```

### Two Predictors

```{r}
b0_2 <- m1_tidy %>% 
  dplyr::filter(term == ) %>% 
  pull(estimate)

b1_2 <- m1_tidy %>% 
  dplyr::filter(term == ) %>% 
  pull(estimate)

b2_2 <- m1_tidy %>% 
  dplyr::filter(term == ) %>% 
  pull(estimate)
```

The linear model with two predictors is y = b0 + b1*x2 + b2*x2

```{r}
x1 <- 37
x2 <- 7
b0_2 + b1_2*x2 + b2_2*x2
```

## Comparing Predictors

```{r}
## don't forget to install the parameters package!
m2 %>% parameters::model_parameters(standardize = "refit")

```

## Centring

If time permits!
