---
title: 'Formative Exercise 08: GLM'
output: 
  html_document:
    df_print: kable
---

```{r setup, include=FALSE}
# please do not alter this code chunk
knitr::opts_chunk$set(echo = TRUE,
                      message = FALSE, 
                      error = TRUE)

library(broom)
library(tidyverse)
library(reprores)

# install the class package reprores to access built-in data
# devtools::install_github("psyteachr/reprores-v2)
# or download data from the website
# https://psyteachr.github.io/reprores/data/data.zip
```

## The `personality_scores` dataset

Load the dataset [reprores::personality_scores](https://psyteachr.github.io/reprores/data/personality_scores.csv).

```{r data}
personality_scores <- NULL
```


### Question 1

Use ggplot2 to visualise the relationship between extraversion (`Ex`) on the horizontal axis and neuroticism (`Ne`) on the vertical axis. 

```{r Q1}
ggplot()
```


### Question 2

Run a regression model that predicts neuroticism from extraversion, and store the model object in the variable `personality_mod`.  End the block by printing out the summary of the model.

```{r Q2}
personality_mod <- NULL
```


### Question 3

Make a histogram of the residuals of the model using ggplot2.

```{r Q3}
residuals <- NULL
```


### Question 4

Write code to predict the neuroticism score for the minimum, mean, and maximum  extraversion scores.  Store the vector of predictions in the variable `personality_pred`.

```{r Q4}
personality_pred <- NULL
```


## Simulating data from the linear model

### Question 5

*NOTE: You can knit this file to html to see formatted versions of the equations below (which are enclosed in `$` characters); alternatively, if you find it easier, you can hover your mouse pointer over the `$` in the code equations to see the formatted versions.* 

Write code to randomly generate 10 Y values from a simple linear regression model with an intercept of 3 and a slope of -7.  Recall the form of the linear model:

$Y_i = \beta_0 + \beta_1 X_i + e_i$

The residuals ($e_i$s) are drawn from a normal distribution with mean 0 and variance $\sigma^2 = 4$, and $X$ is the vector of integer values from 1 to 10. Store the 10 observations in the variable `Yi` below. (NOTE: the standard deviation is the *square root* of the variance, i.e. $\sigma$; `rnorm()` takes the standard deviation, not the variance, as its third argument).

```{r Q5}
X <- NULL
err <- NULL
Yi <- NULL
```

