```{r, load packages, echo=F}
# Comment on the lesson plan: Sections up to 6 Figures script done in lesson 3, 7-10 done in lesson4 in the skills training 2025. 

library(cowplot)
library(ggplot2)
library(dplyr)
library(readr)
library(svglite)
```

## Goals

-   Repeat setting up your work space with R-Projects, Git & GitHub
-   Clean data using dplyr (`filter` and `mutate`)
-   Make a histogram, scatter plot, and box plot
-   Repeat the math behind linear regression
-   Run a linear regression
-   Summarise results in a Quarto Document

## Set up your work space

1.  On [https://github.com]():
    1.  Make a remote git repository (e.g. “rintro_lesson3”)
    2.  Copy the SSH link.
2.  In Rstudio: Clone the remote repository via the Terminal (quicker) or the Rstudio interface:
    1.  Terminal: navigate to your folder for this R course using `cd`. Type `git clone` and paste your SSH link.
    2.  Rstudio interface: Create a new project & choose `Version Control`, then `Git`. Under `Clone Git Repository`, enter your SSH link.
3.  Then organize your repository:
    1.  In the now local folder of the repository, create sub folders (e.g. “data”, “figures”, “code”, “write_up”).
    2.  Put the [data file](https://github.com/hannahmetzler/R_intro_lesson3/blob/main/data/lesson2_data_babynames.txt) for this lesson in your “data” folder.
    3.  Make a new R Project based in your main directory folder (e.g. “rintro_lesson3”).
    4.  Commit & Push to GitHub.

## Analysis project for Lesson 3

Data from the USA Social Security Administration on baby names

**2 research questions:**

1.  Continuous Predictor: Does your name get more or less popular between the years of 1901 and 2000?
2.  Categorical Predictor: Is your name more or less popular with females or males?

## Organizing your scripts

-   Separate scripts for each step can be useful, we will use this structure today:
    -   cleaning
    -   figures
    -   statistics
-   Alternatively, these can be sections in a Quarto Document.

::: callout-caution
#### To do

Create a script and save it in the `code` folder as `01_cleaning.R`.
:::

## Cleaning script

-   Start again with a header (# TITLE ####) for loading packages.
-   We only need the packages `dplyr` and `readr` for now (or the meta-package tidyverse)
-   Shortcut to run a line of code from a script: `Ctrl/Strg/Cmd + Enter`

```{r, eval=F}
## LOAD PACKAGES ####
library(dplyr)
library(readr)
```

-   Next, we read in data, with the same function as last time but for .txt files. You'll see a message because we did not specify the variable type for the columns. 

```{r}
data = readr::read_tsv("data/lesson2_data_babynames.txt")
```

- To check if the file was read correctly you can use `head(data)` in the Console. 

```{r}
head(data)
```
- Use `summary(data`) and `glimpse(data)` to see how the output changes before and after changing the column type. 

```{r, eval=F}
summary(data)
glimpse(data)
```


- To change a columns' variable type, you can use the argument `col_types` when reading in the data (see ?read_tsv): 

```{r}
data = readr::read_tsv("data/lesson2_data_babynames.txt", col_types = 'iffin')
```

```{r, eval=F}
summary(data)
glimpse(data)
```

-   Check if your own name exists in the data set using `filter()` from dplyr:
    -   Check the help entry: `?dplyr::filter` (Description, Usage & Examples).
        -   The first argument is for the data frame.
        -   The second argument (`...`) is for the expression that defines our condition for rows we want to keep.
        -   The Examples section shows you how to use the function.
-   I'll be using the name "Page" - in honor of the colleague who taught me R with exactly this same data set.

```{r}
data_clean = dplyr::filter(data, name == "Page")
```

- Check the filtered data frame, for example using `head()`. 

```{r}
head(data_clean)
```

- If this is empty, your name does not exist in the US names data set. Simply stay with the name "Page".

```{r, results = F}
dplyr::filter(data, name == "Lav")
```

```{r, echo=FALSE, fig.width=20}
knitr::include_graphics('images/empty_tibble.jpg')
```

- Now, we'll use the same code to assign only rows with your name to the data frame `data_clean`.
- To make our code easier to read and add to, we're going to use something called a pipe (%>%).  Imagine it like a series of steps: the pipe helps you move from one step to the next, passing the data along as you go. 
  - In the first step, `filter()` will be run on `data` before assigning the output to `data_clean`.
    -   Shortcut for %>%: `Shift + Ctrl/Cmd + M`
-   To use something different, I check if it worked with `xtabs()` this time.

```{r}
## CLEAN DATA ####
data_clean = data %>%
             filter(name == "Page")
xtabs(~name, data_clean)
```

We only want to look at the 20th century, so we filter out the years between 1900 and 2000 using the `filter()` function:

-   The first new filter call selects all years after 1900, the second all years up to and including 2000.

```{r, results = FALSE}
data_clean = data %>%
             filter(name == "Page") %>%
             filter(year > 1900) %>%
             filter(year <= 2000)
```

To confirm this worked, we can check the minimum and maximum year in the new data frame. For the name "Page", this shows the first year in which it was given to a baby was 1909.

```{r}
min(data_clean$year)
max(data_clean$year)
```

::: callout-caution
#### To do

-   Save the cleaning script.
-   Commit the changes to Git. The commit message could be "Made cleaning script".
-   Open a new script `02_figures.R`
:::

## Figures Script

We'll learn a new function now: `source()` tells R to run another script.

-   We can use it to run the cleaning script from our figures script, to read in our clean data.

```{r, eval = F, results = F}
## READ IN DATA ####
source("code/01_cleaning.R")
```

-   Enter the exact path and name of your script.
-   To be sure it works, first delete all objects from the **Environment** (broom symbol or `rm(list = ls)`)

All variables should now reappear in your Environment:

```{r, echo=F}
knitr::include_graphics('images/source_environment.png')
```

Now load all packages not yet loaded in the cleaning script. We need ggplot2 to make a figure.

```{r, eval=F}
## LOAD PACKAGES ####
library(ggplot2)
```

We may want to change names or levels of variables a bit for our figures. It's good to create an extra data set for this:

```{r}
## ORGANIZE DATA ####
data_figs = data_clean
```

### Checking for normal distribution: Histogram

To run a linear regression, our dependent variable needs to be normally distributed. A perfect normal distribution is symmetrical, for example, here are 2 distributions with n = 1 million observations:

```{r, histogram nv,  echo=F, fig.height=3.5}
data_normal = data.frame(narrow = rnorm(1000000, sd = 1.5), broad = rnorm(1000000, mean = 0, sd = 3.5))
p1 = ggplot(data_normal, aes(x = narrow)) +
            geom_histogram(aes(y=after_stat(density)), bins = 40)+
  geom_density(colour = "blue")+
  geom_vline(aes(xintercept=0), colour="blue", linetype="dashed")+
  xlim(c(-10, 10))+
  ylim(c(0, 0.3))

p2 = ggplot(data_normal, aes(x = broad)) +
            geom_histogram(aes(y=after_stat(density)), bins = 40)+
  geom_density(colour = "blue")+
  geom_vline(aes(xintercept=0), colour="blue", linetype="dashed")+
  xlim(c(-10, 10))+
  ylim(c(0, 0.3))
plot_grid(p1, p2)
```

-   With less observations, the shape will be less perfect, but still roughly symmetrical (n = 100) with most values around the mean:

```{r, histogram less observations, echo=F, fig.height=3.5}
data_normal = data.frame(narrow = rnorm(100, sd = 1.5), broad = rnorm(100, mean = 0, sd = 3.5))
p1 = ggplot(data_normal, aes(x = narrow)) +
            geom_histogram(aes(y=after_stat(density)), bins = 40)+
  geom_density(colour = "blue")+
  geom_vline(aes(xintercept=0), colour="blue", linetype="dashed")+
   xlim(c(-10, 10))+
  ylim(c(0, 0.3))

p2 = ggplot(data_normal, aes(x = broad)) +
            geom_histogram(aes(y=after_stat(density)), bins = 40)+
  geom_density(colour = "blue")+
  geom_vline(aes(xintercept=0), colour="blue", linetype="dashed")+
   xlim(c(-10, 10))+
  ylim(c(0, 0.3))
plot_grid(p1, p2)
```

Let's check the distribution of our dependent variable with a histogram:

```{r, histogram names, fig.asp=1, fig.width=3, warning=T}
## MAKE FIGURES ####
# Histogram of dependent variable (proportion of 'Page's)
name.plot = ggplot(data_figs, aes(x = prop)) +
            geom_histogram()
name.plot
```

-   Proportion of babies with the name "Page" is clearly not normally distributed.
-   Note that aes() only needs an x-variable for histograms, because we plot only one variable.
-   Information message about bins: R picks one by default, but notifies you that you can enter a better one, as the binwidth can really change the shape of a distribution.

```{r, histogram binwidth, fig.asp=1, fig.width=3}
# Histogram with specific binwidth
ggplot(data_figs, aes(x = prop)) +
            geom_histogram(binwidth = 0.0000005)
```

-   One usual way of making data normal is a log transformation. In R, you can take the logarithm with base 10 with `log10()`.
-   Go back to the cleaning script to do the log transform. To make any transformation to a variable, we can use `mutate()` from dplyr. `mutate()` is used to make a new column or change an existing one.

```{r, results = FALSE}
data_clean = data %>%
  filter(name == "Page") %>%
  filter(year > 1900) %>%
  filter(year <= 2000) %>% 
  mutate(prop_log10 = log10(prop))
```

```{r, echo=F}
data_figs = data_clean
```

-   Run this code block, or save the script and rerun `source()` in the figures script.
-   Back in the figures script, rerun the line to make the "data_figs" data frame.
-   Now let's have a look at the distribution again:

```{r, histogram names log, fig.asp=1, fig.width=3.5}
# Histogram of log transformed dependent variable (proportion of 'Page's)
name_log10.plot = ggplot(data_figs, aes(x = prop_log10)) +
            geom_histogram()
name_log10.plot
```

This distribution is more or less normal. We can go ahead and focus on figures for the regression.

### Continuous predictor: Scatter plot

Our first research question was: How did the popularity of the name, “Page” change over time?

::: callout-caution
#### Exercise

-   Which 2 variables do we need to plot to look at this?
:::

-   To make a scatter plot we use `geom_point()`.
-   To add a regression line we use `geom_smooth()` with the method set to `lm`.

```{r, prop per year scatterplot, fig.asp=1, fig.width=3.5}
# Proportion of 'Page's by year (continuous predictor)
year.plot = ggplot(data_figs, aes(x = year, y = prop_log10)) +
            geom_point() +
            geom_smooth(method="lm")
year.plot
```

::: callout-caution
#### Exercise

-   Is there an effect of year on time?
:::

For a prettier version of the figure, we can again change a few settings:

```{r}
# Proportion of 'Page's by year (continuous predictor)
year.plot <- ggplot(data_figs, aes(x = year, y = prop_log10)) +
  # Make the figure a scatterplot
  geom_point() +
  # Add a regression line, 'lm' call makes it linear regression
  geom_smooth(method="lm")+
  # Add a title
  ggtitle("Proportion of People with\nthe Name 'Page' Over Time")+
  # Customize the x-axis
  xlab("Year") +
  # Customize the y-axis
  ylab("Proportion of People\n(log base 10 transformed)") +
  # Remove dark background
  theme_classic() +
  # Additional paramaters for displaying plot
  theme(text=element_text(size=10), title=element_text(size=12))

# Call plot
year.plot
```

-   For detailed tutorials on different figures and design features, check out Chapter 3 of [this R course](https://psyteachr.github.io/reprores-v3/ggplot.html) (de Bruine & Barr, 2022).

-   To save the figure to a file, you can use `ggsave()` like in Lesson 2.

```{r, eval = F}
# save the plot
ggsave('figures/scatterplot_proportion_year.pdf', dpi = 300)
```

Below, I have changed the figure above slightly by adding a separate colour for each sex:

```{r}
plot.interaction <-  ggplot(data_figs, 
                            aes(x = year, y = prop_log10, colour = sex)) +
            geom_point() +
            geom_smooth(method="lm")
plot.interaction
```

This shows a different trend over time for men and women: Page became more popular in women, less popular in men. This is called an interaction effect between sex and time.

### Categorical predictor: Boxplot

-   Now let's make a figure for the second question: Is there a sex difference in popularity of the name?
    -   We will use a box plot for this. Which variable should be on the x and y axis?

```{r, prop per sex boxplot, fig.asp=1, fig.width=3.5}
# Proportion of 'Page's by sex (categorical predictor)
sex.plot = ggplot(data_figs, aes(x = sex, y = prop_log10)) +
geom_boxplot()
sex.plot
```

-   You might want more explicit labels for "F" and "M" in a figure. We can update the code to create the `data_figs` data frame at the top of our script.
-   We'll use `mutate()` to change the names of the variable levels of the variable sex.
-   We'll use `factor()`, a function to make categorical variables with labels. The arguments we need are (from the help section):
    -   levels: "an optional vector of the unique values (as character strings) that x might have taken" 
    -   labels: "an optional character vector of labels for the levels"
-   What is a vector? A list of items of the same type (e.g., numbers or characters).
    -   If you want to assign numbers to a variable, you use a vector in R.
    -   Notation in R: `c()`

```{r}
my_vector <- c(1,7,5,3, 3)
my_character_vector <- c("string1", "string2", "string3")
my_vector
my_character_vector
```

-   We use vectors for levels and labels within `factor()` below.

```{r}
## ORGANIZE DATA ####
data_figs = data_clean %>% 
      mutate(sex = factor(sex, 
                          levels=c("F", "M"), 
                          labels=c("female", "male")))
```

-   If you run the figure code again, you will get this figure:

```{r, prop per sex boxplot better labels, fig.asp=1, fig.width=3.5}
# Proportion of 'Page's by sex (categorical predictor)
sex.plot = ggplot(data_figs, aes(x = sex, y = prop_log10)) +
geom_boxplot()
sex.plot
```

-   To save the plot, uncomment (short cut: `Ctrl+Shift+C`) and run:

```{r}
# ggsave('figures/boxplot_propotion_by_sex.pdf', dpi = 300)
```

-   To make the figure prettier, there are various elements you can change. Have a look at this at home.

```{r, prop per sex boxplot pretty, fig.asp=1, fig.width=3.5}
# Proportion of 'Page's by sex (categorical predictor)
sex.plot = ggplot(data_figs, aes(x = sex, y = prop_log10, fill=sex))+
  geom_boxplot()+
  # choose a design
  theme_classic()+
  # Add a title
  ggtitle("Popularity of the name 'Page' by sex")+
  # Customize the y-axis (with a line break)
  ylab("Proportion of People\n(log base 10 transformed)")+
  # Customize the x-axis
  xlab("Sex") +
  # Custom colours for the fill variable (sex)
  scale_fill_manual(values = c("orange", "blue"))+
  # Additional paramaters for displaying plot
  theme(text=element_text(size=10), # text size
        title=element_text(size=10), # text size for title
        legend.position="none") # no legend

sex.plot

# to save the plot: 
# ggsave('figures/boxplot_propotion_by_sex.pdf', dpi = 300)
```

::: callout-caution
#### To do

The figure script is done. Save it, commit to Git, and push to GitHub!
:::

End of lesson 3 LBG Career Center Skills Training.

## Math of linear regression

The basic equation of linear regression is:

**y~i~ = a + bx~i~ + e~i~**

-   y~i~ = a specific y value (dependent variable)
-   a = intercept (where the regression line crosses the y-axis, y when x = 0)
-   b = slope (change in y for one change in x)
-   x~i~ = specific x value (independent variable, predictor)
-   e~i~ = random variance, residual, noise

### Continuous predictor

Let's see an example for a continuous predictor: How does the weight of chicks change over time? As more time passes, the weight increases.

```{r, chickweight scatterplot, echo = F, fig.width = 4, fig.asp = 1}
#ChickWeight dataset
data(package = "datasets", ChickWeight)

ggplot(ChickWeight, aes(y = weight, x =  Time))+
  geom_point()+
  theme_bw()+
  theme(text = element_text(size = 12))+
  ggtitle("Chick Weight over Time")
```

We can add a line to show the increase:

```{r, chickweight scatterplot line, echo = F, fig.width = 4, fig.asp = 1}
ggplot(ChickWeight, aes(y = weight, x =  Time))+
  geom_point()+
  geom_smooth(method = 'lm')+
  theme_bw()+
  theme(text = element_text(size = 12))+
  ggtitle("Chick Weight over Time")
# ggsave('images/scatterplot.pdf', width = 4, height = 4)
```

Let's pick a specific data point (red square in the Figure below).

-   In red, you see the x and y values for the data point (y=200g, x = 12 days).
-   In yellow, you see the intercept a: the weight (y) when days (x) are 0.
-   In purple, you see the slope b, telling you how much weight a chick gains on average on 1 day (here from day 5 to 6)
-   In green, you see the residual for the data point: The difference between the model prediction and the actual data point.

```{r, echo = F, fig.height=7}
knitr::include_graphics('images/scatterplot_labels.svg')
```

### Running linear regression in R

-   The notation for a linear regression in R is: `lm (y ~ x)`
-   In the chick weight example above, this would be: `lm(weight ~ Time)`
-   To see the output of the model, we use `summary(model)`.

```{r}
chicks.lm = lm(weight ~ Time, data = ChickWeight)
summary(chicks.lm)
```

-   The intercept a is at 27 gram: where the blue line crosses the y-axis in the Figure above.
-   The slope b for the predictor Time is 8.8 gram: chicks grow by 8.8 g on average per day (the vertical part of the purple triangle).

### Categorical predictor

What if x is categorical?

**a = bx**

| -- | Continuous Predictor | Categorical Predictor |
|------------------------|------------------------|:-----------------------|
| x | a set of continuous data points | a set of binary/categorical data points |
| a | the value of y when x is 0 | the value of y when x is the default level |
| b | the change in y for one change in x | the change in y when x is the non-default level |

As an example, let's say chicks get 2 different diets:

```{r, chickweight boxplot diet, echo = F, fig.width=4, fig.asp = 1}
data_chicks = ChickWeight %>% 
  filter(Diet %in% c(1,2)) %>% 
  mutate(Diet = factor(Diet, levels = c(1,2), labels = c("Diet 1", "Diet 2")))
ggplot(data_chicks, aes(y = weight, x =  Diet, fill = Diet))+
  geom_boxplot()+
  theme_bw()+
  theme(text = element_text(size = 12), legend.position = "none")+
  ggtitle("Chick Weight by Diet")
```

-   We can look at this as a collection of points.
-   I have also added a line like before, from the mean of Diet 1, to the mean of Diet 2.
-   Finally, we need to change the labels of diets to values for our statistical analysis: 0 for the default, and 1 for the next level.

```{r, chickweight scatterplot diet, echo = F, fig.width=4, fig.asp = 1}

data_chicks = ChickWeight %>% 
  filter(Diet %in% c(1,2)) %>% 
  mutate(Diet = factor(Diet, levels = c(1,2), labels = c("0", "1")))
ggplot(data_chicks, aes(y = weight, x =  Diet))+
  geom_point()+
  stat_summary(fun.y=mean, geom="line", aes(group = 1))+
  theme_bw()+
  theme(text = element_text(size = 12), legend.position = "none")+
  ggtitle("Chick Weight by Diet")
# ggsave('images/scatterplot_categorical.svg', width = 4, height = 4)
```

Below, you see

-   x and y for a specific data point in red
-   the intercept a in yellow: the value of y (just above 100) when x is the default level (= 0, or Diet 1)

```{r, echo = F, fig.height=7}
knitr::include_graphics('images/scatterplot_categorical_intercept_yx.svg')
```

-   The slope b in purple: the average change in weight from Diet 1 (default level 0) to Diet 2 (level 1).
-   The residual e of the specific data point in green.

```{r, echo = F, fig.height=7}
knitr::include_graphics('images/scatterplot_categorical_slope_residual.svg')
```

#### Running this regression in R

```{r}
diet.lm = lm(weight ~ Diet, data_chicks)
summary(diet.lm)
```

-   The intercept a is 102 g: the mean weight of chicks eating Diet 1.
-   The slope b is 19.97 g: the mean weight difference between the Diets. On average, Diet 2 chicks are this much heavier.

## Statistics script

As always, start with a section to read in data, then one to load packages.

-   We don't need any package at the moment, but you usually want to add some later while doing the analysis.

```{r, eval=F}
## READ IN DATA ####
source("code/01_cleaning.R")

## LOAD PACKAGES #####
# [none currently needed]
```

As before, we make a new data frame for statistics data:

```{r}
## ORGANIZE DATA ####
data_stats = data_clean
```

### Continuous predictor: Year

```{r}
## BUILD MODEL - PROPORTION OF 'PAGE'S BY YEAR (CONTINUOUS PREDICTOR) ####
year.lm = lm(prop_log10 ~ year, data = data_stats)
```

-   Store the model in an object: `year.lm`
-   Using names like "sex.plot", "name.plot", "year.lm" is a useful convention to know what objects are.
-   The model itself is lm(prop_log10 \~ year), with prop_log10 as the dependent variable and year as the predictor.
-   To look at the output, we call `summary()`:

```{r}
year.lm_sum <- summary(year.lm)
year.lm_sum
```

-   Exact numbers of intercept and slope are not very informative, because they are log transformed values.
-   Effect of year not significant: t =`r round(year.lm_sum$coefficients[2,3], 2)`, p = `r round(year.lm_sum$coefficients[2,4], 3)`
-   We can look at the residuals with the call `resid()`. Here, I store them and then look at the fist values:

```{r}
year.lm_resid <- resid(year.lm_sum)
head(year.lm_resid)
```

### Categorical predictor: Sex

-   Copy your code just above from the regression with `year`, then change the predictor `year` to `sex`.

```{r}
## BUILD MODEL - PROPORTION OF 'PAGE'S BY SEX (CATEGORICAL PREDICTOR) ####
sex.lm = lm(prop_log10 ~ sex, data = data_stats)

sex.lm_sum = summary(sex.lm)
sex.lm_sum
```

-   The line "sexM" shows the effect for sex.
    -   It is significant: t = `r round(sex.lm_sum$coefficients[2,3], 2)`, p = `r round(sex.lm_sum$coefficients[2,4], 3)`.
-   How do you know in which direction the effect is?
    -   R by default codes variables alphabetically, so our default level is "F" for female.
    -   Coefficients indicate the change from the default to the second level (here: from F to M)
    -   You can also see this because R adds "M" to the variable name: "sexM", so it's the effect for men (compared to women).
    -   The **Estimate** ($\beta$) `r round(sex.lm_sum$coefficients[2,1], 2)` is negative: Less men (a lower proportion of the population) are called "Page" than women.

::: callout-caution
#### To do

Save the script, commit to Git (message: "Made statistics script"), and push to GitHub.
:::

## Write up: Quarto Report

We repeat the steps from the last lesson (see Lesson 2 for more explanation):

-   Save your current working environment to your write-up folder as `lesson3_environment`.
-   Open a Quarto Document.
-   Delete everything below the chunk at the top (the `YAML`) that starts and ends with `---`.
-   Save the Quarto document in your write_up folder as `Lesson3_Report`.
-   After this, insert a code chunk (Shortcut: `Ctrl/Cmd + Alt + I`) and enter the code below to load your environment.

```{=html}
<pre><code class="r">
&#96;&#96;&#96;{r, echo=F}
load('lesson3_environment.RData')
&#96;&#96;&#96;
</code></pre>
```

-   Add `echo = FALSE` (or only the F) in the curly brackets after r: \`\`\``{r, echo = FALSE}`. This let's R know you do not want to print out this code chunk in the report, but only its results.

Now add sections to your report, for example:

-   Introduction
-   Results
    -   Prevalence by Year
    -   Prevalence by Sex
-   Conclusions

Now you can write something in each section. For example:

```{Markdown, eval = F}
# Introduction

We analysed how common the name "Page" was in the United States in between 1900 and
2000. We used data from the USA Social Security Administration on baby names. 
We used linear regression to analyse the change of the name over time, and the 
difference of the name's popularity between men and women. 
```

In the Results sections, you can include the figures and model results by calling the objects within a code chunk:

```{Markdown, eval = F}
# Results

## Prevalence by Year

We built a linear model to test the effect of year on the proportion of people 
with the name "Page". Proportion was log base 10 transformed, because it was 
not normally distributed. 
There was no significant change in the popularity of the name over time. The
positive coefficients indicates a slightly increasing slope over time. 
```

```{=html}
<pre><code class="r">
&#96;&#96;&#96;{r, echo=F}
year.plot
year.lm_sum
&#96;&#96;&#96;
</code></pre>
```

```{Markdown, eval = F}
## Prevalence by Sex

The name is more popular among women then men. 
```

```{=html}
<pre><code class="r">
&#96;&#96;&#96;{r, echo=F}
sex.plot
sex.lm_sum
&#96;&#96;&#96;
</code></pre>
```

-   You can continue describing your results and the conclusion at home.
-   To compile the Quarto report, click `Render` at the top of the Quarto document.
-   You can change the format from html, to docx, or pdf.
- At the end, don't forget to commit and push to GitHub. 

## References & Resources

This lesson closely follows: Page Piccinini (2018). "R for Publication", [Lesson 2: Linear Regression](https://pagepiccinini.com/r-course/lesson-2-linear-regression/).

-   The completed code for the lesson can be found [here](https://github.com/hannahmetzler/R_intro_lesson3).
-   Use git clone to download it into a folder on your computer, or download just the file you need.
-   For Quarto tips and tricks, check the online documentation: [https://quarto.org/](https://quarto.org/docs/visual-editor/options.html)
    -   Shortcuts and options: <https://quarto.org/docs/visual-editor/options.html>
-   For more on figures with ggplot (and many other R skills): Lisa DeBruine & Dale Barr. (2022). [Data Skills for Reproducible Research: Chapter 3](https://psyteachr.github.io/reprores-v3/ggplot.html) (3.0) Zenodo. \<www.doi.org/10.5281/zenodo.6527194\>.
