---
title: "Exercise 4.3"
author: "Put Your Name Here"
output:
  html_document:
    toc: true
---

<!-- You can knit this document at any time to see the draft of your answer. 
     An html file will be produced in the same folder as the Rmd file. -->

<!-- When you have completed the exercise, knit the document for the final time. -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  comment = NA,
  fig.width = 7,
  fig.height = 4,
  dpi = 130
)
```

In this exercise, we will be using functions from the `tidyverse`, `performance`,
`gtsummary`, `broom`, and `emmeans` packages.

```{r packages, message = FALSE, warning = FALSE}
library(tidyverse)
library(performance)
library(gtsummary)
library(emmeans)
library(broom)
```

### (a) Fit a one-way ANOVA to a wine-growing experiment

> The file `pinot.csv` contains the results of an experiment by winemaker
> Vincent Lakey, comparing a standard herbicide with two greener alternatives:
> straw mulch and compost.
>
> The experiment used these three treatments in each of six different
> areas of the vineyard, recorded in the variable called `block` in
> the data set, `pinot.csv`.
>
> For this exercise, we will fit a one-way ANOVA to the response variable
> `Weight2001` (the weight of grapes harvested in 2001, in kg) using the
> explanatory variable `Treatment`.
>
> Make a residual plot using `check_model()`. (To make the residual plot
> look nicer in the knitted output, you may want to change fig.width and
> fig.height - look at the solution for an example.)
> 
> Use `joint_tests()` to test the hypothesis that all treatments resulted in
> equal mean harvest weights.
>
> Use `emmeans()` to produce a table of means and confidence intervals for the
> three treatments.
>
> Use `pairs()` to produce a table of pairwise comparisons, with confidence
> intervals and p-values. (Note that this would not normally be reported, as
> the ANOVA result provides no reason to believe treatment means differ.)
>
> Extension: produce a plot of the means of the three treatments, with error bars
> showing 95% confidence intervals.




### (b) Fit a linear regression to women's Olympic 100m gold medal times vs time

> Read in the file `olympic_100m_results.csv` and use `filter()` to select
> the results for women (Gender is `W`) gold medalists (Medal is `G`).
>
> Fit a linear regression to result as a function of year.
>
> Use `model_performance()` to obtain model summary statistics, e.g. R-squared.
>
> Make a residual plot using `check_model()`.
>
> Obtain an estimate, confidence interval and p-value for the slope using
> `tidy()` or `tbl_summary()`. (Is the rounding appropriate? Look at the solutions
> for a way to control the number of decimal places.)
>
> Plot the original data and use `geom_smooth()` to add a line of best fit.
>
> Does this model appear to be a good fit to the data? Is it plausible for this
> relationship to continue into the future?




### (c) Extension: fit an interaction between gender and time

> Fit a linear model to both men and women's 100m sprint gold medal times,
> with gender, year, and the interaction in the model.
>
> Are the parameter estimates (from `tbl_regression()` or `tidy()`) easy to interpret?
>
> Can you use `emmeans()` to obtain the mean for each gender?
>
> This continuous-by-categorical interaction is best analysed using emmeans
> features we haven't seen yet:
> `emmeans(m, "Gender", at = list(Year = 2020))` obtains estimated means for
> each gender in the year 2020.
> `emtrends(m, "Gender", "Year")` obtains estimated slopes for each gender.
>
> Use `pairs()` to obtain an estimate and confidence interval for the difference
> in slopes between genders.




---

&copy; 2021 Statistical Consulting Centre, The University of Melbourne.
