---
title: "Homework 8"
subtitle: "Psychology 2812B FW23"
output: html_document
---

# Lab Component

Experiments were carried out on six commercial goat farms to determine whether the standard worm drenching program was adequate. Forty goats were used in each experiment. Twenty of these, chosen at random, were drenched according to the standard program, while the remaining twenty were drenched more frequently. The goats were individually tagged and weighed at the start and end of the year-long study. For the first farm in the study the resulting live weight gains (`gain`), along with the initial live weights (`wt`), are given for the first farm in the study. In each experiment the main interest was in the comparison of the live weight gains between the two treatments (`treatment`).

## 1. Goats

Load the Goats dataset

```{r, warning=FALSE}
options(contrasts=c("contr.sum","contr.poly")) # set contrasts to sum-to-zero
library(tidyverse)
goats <- read_csv(url("https://www.gribblelab.org/2812/data/goats.csv"),
                      col_types = "fnn")
glimpse(goats)
```

## 2. Means

Compute the mean and sd weight gain for each treatment group, and also report the number of observations in each group.

```{r}
# your code goes here
```

## 3. Graphics

Make a graphical plot showing the marginal means of weight gain (`gain`) by treatment (`treatment`).

```{r}
# your code goes here
```

## 4. ANOVA

Use simple one-way ANOVA to evaluate the effect of treatment (`treatment`) on weight gain (`wt`). Show the ANOVA table and interpret the results.

```{r}
# your code goes here
```

> your text goes here


## 5. Linear Regression

Use linear regression to assess whether there is a linear relationship between weight gain (`gain`) and starting weight (`wt`).

Interpret the results---is there evidence of a linear relationship between weight gain and starting weight? What is the nature of the relationship?

```{r}
# your code goes here
```

> your text goes here

Generate a scatterplot of weight gain as a function of starting weight, showing the linear regression lines and the correlation coefficients separated by treatment group.
 
```{r}
# your code goes here
```

Interpret the plot. What does it tell you about the relationship between weight gain and starting weight, and how does this relationship appear to differ (or not) between the two treatment groups?

> your text goes here


## 6. ANCOVA

Use ANCOVA to evaluate the effect of treatment (`treatment`) on weight gain (`gain`) while accounting for the linear association between weight gain and initial weight (`wt`). Interpret the results of the ANCOVA.

```{r}
# your code goes here
```

> your text goes here

## 7. Adjusted Means

Calculate and show the raw means and the adjusted means of weight gain (`gain`) for each treatment group (`treatment`). Feel free to use either the `predict()` approach or the `emmeans()` approach to do this (as shown in the ANCOVA notes); either way is fine.

What is the conceptual difference between these two sets of means? Is there a substantial numerical difference between the two sets of means? Why or why not?

```{r}
# your code goes here
```

> your text goes here


## 8. Interpretation

Compare the outcome of the simple one-way ANOVA with the outcome of the ANCOVA. Explain why this happened in the context of (a) the raw means vs adjusted means, and (b) the relationship between weight gain and initial weight.

> your text goes here


# Home Component

You are Dean of the Faculty of Science. You have just met with one of the new Assistant Professors in the Department of Mathematics, for their annual performance evaluation. Professor X claims that their salary is much too low and they want you to give them a raise. They claim they are particularly good at teaching and thus deserves a raise. As evidence of this, they say that among the three sections of calculus taught in first year, their students (Section I) get the highest grades on the Calculus final exam, which is common to all three sections (see Table 1 below). You are the kind of Dean who likes to make decisions based on facts, and so you ask the Chair of the Department of Mathematics to gather some data for you. The Chair gives you final exam scores for a random sample of 50 students chosen from each of the three sections of Calculus 101.

## 9. Calculus Grades

Load in the dataset:

```{r, warning=FALSE}
options(contrasts=c("contr.sum","contr.poly")) # set contrasts to sum-to-zero
library(tidyverse)
calculus_data <- read_csv(url("https://www.gribblelab.org/2812/data/calculus1.csv"),
                          col_types = "nnf")
glimpse(calculus_data)
```

Compute and show the mean and sd of Calculus grade for each of the three Calculus 101 sections, and report the number of observations in each section. Do the data suggest Professor X’s claim may be true or not?

```{r}
# your code goes here
```

> your text goes here

## 10. ANOVA

Conduct a standard one-way ANOVA with follow-up tests (corrected for multiple comparisons) to assess whether the three sections differ in terms of final exam scores. What would you conclude based on the simple one-way ANOVA?

```{r}
# your code goes here
```

> your text goes here

## 11. High School Grades

The Chair of the Mathematics Department calls you the next day and tells you that she also has available high-school mathematics grades for each of the students in the three sections of Calculus 101. She advises you that Section I (Professor X’s section) appears to contain a disproportionately high number of students from the same private school in Toronto, compared to sections II and III which contain a broader representation of students overall. She advises that you might want to take this information into consideration when comparing final grades in the three sections.

Load the new dataset:

```{r}
library(tidyverse)
calculus_data <- read_csv("http://www.gribblelab.org/2812/data/calculus2.csv",
                          col_types = "nnfn")
glimpse(calculus_data)
```

Compute and show the mean and sd of Calculus grade and the mean and sd of High School Math grade for each of the three Calculus 101 sections, and report the number of observations in each section. What do the High School Math grades suggest about the three sections of Calculus 101, and how might this information be relevant to Professor X’s claim?

```{r}
# your code goes here
```

> your text goes here

Using a one-way ANOVA with appropriate follow-up tests, assess whether the average high-school math grades differ between the three sections of Calculus 101. Interpret the results.

```{r}
# your code goes here
```

> your text goes here

Fit a linear model to assess whether there appears to be a linear relationship between High School Math grades (`HighSchoolMath`) and grades in Calculus 101 (`CalcGrade`). Interpret the results.

```{r}
# your code goes here
```

> your text goes here

Generate a scatterplot of calculus grade as a function of high school math, showing the linear regression lines and the correlation coefficients, separated by calculus section.
 
```{r}
# your code goes here
```


## 12. ANCOVA

Conduct an ANCOVA in which you take into account the high-school grades of students and re-assess whether there is a statistically reliable difference between Calculus grades across the three sections. Report adjusted means and regardless of the outcome of the ANCOVA, perform follow-up tests and interpret them.

```{r}
# your code goes here
```

> your text goes here


What can you conclude about Professor X’s claim? Would you give them a raise based on your ANCOVA analysis? Why or why not?

> your text goes here
