---
title: "Lab 13: Inference for MLR and ANOVA"
author: "Insert Name"
output:
  pdf_document
 
---

# Due: 12am night before lab

# Instructions
Work through the problems below, and submit this document as a knitted .pdf file with answers to the Problem Set assignment on Gradescope. 


**For each problem, put your solution between the bars of red stars.**


```{r, include=FALSE}
# Do not edit this code block/chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE, fig.width = 5, fig.height = 3)
```

## Load packages

For this lab we will be using the `tidyverse` for data wrangling and visualization, `moderndive` for linear modeling, `infer` for statistical inference, and a data set will come from the `oilabs` package.

```{r, include=FALSE, message = FALSE, warning = FALSE}
library(tidyverse)
library(knitr)
library(moderndive)
library(infer)
library(oilabs)
```



## Grading the professor

Many college courses conclude by giving students the opportunity to evaluate the course and the instructor anonymously. However, the use of these student evaluations as an indicator of course quality and teaching effectiveness is often criticized because these measures may reflect the
influence of non-teaching related characteristics, such as the physical appearance of the instructor. The article titled, “Beauty in the classroom: instructors’ pulchritude and putative pedagogical productivity” by Hamermesh and Parker found that instructors who are viewed to be better looking receive higher instructional ratings.

In this lab we will analyze the data from this study in order to learn what goes into a positive professor evaluation.

### The data

The data were gathered from end of semester student evaluations for a large sample of professors from the University of Texas at Austin. In addition, six students rated the professors’ physical appearance. The result is a data frame where each row contains a different course and columns represent variables about the courses and professors.

Let's load the data:

```{r}
data(evals)
```

We have observations on 21 different variables, some categorical and some numerical. The meaning of each variable can be found by bringing up the help file (select the help file in the oilabs package):

```{r eval = F}
?evals
```

## Exploring the data

### Exercise 1

Is this an observational study or an experiment? The original research question posed in the paper is whether beauty causes a difference in course evaluations. Given the investigation design, is it possible to answer this question as it is phrased? If not, rephrase the question.
    
************************************************************************

************************************************************************

### Exercise 2

Describe the shape, center and spread of the distribution of `score`. What does that tell you about how students rate courses? Is this what you expected to see? Explain.
    
    
************************************************************************


************************************************************************


## Multiple linear regression

In order to see if beauty is a significant predictor of professor score after we've accounted for the gender of the professor, we can build a model that includes both beauty and gender terms.

### Exercise 3


Use `lm` to create a multiple regression model predicting `score` as a function of `bty_avg` and `gender`. Investigate the results in the regression table and regression summary. Write down the equation for the multilinear model.
 

************************************************************************


************************************************************************


### Exercise 4

Interpret the intercept and the slope on `bty_avg` in context of the model.

************************************************************************


************************************************************************



### Exercise 5

Is `bty_avg` a significant predictor of `score` in a model that controls for `gender`? 

************************************************************************


************************************************************************


### Exercise 6

The following code chunk plots the regression lines from the multiple regression model corresponding to male and to female professors.

```{r}
ggplot(evals, aes(x = bty_avg, y = score, color = gender)) +
  geom_point() +
  geom_parallel_slopes(se = FALSE)
```


What is the equation of the line corresponding to male professors?  What is the equation of the line corresponding to female professors?

For two professors who received the same beauty rating, which gender tends to have the higher course evaluation score? 
    
************************************************************************


************************************************************************


### Exercise 7

Create a new model that includes `age`, as well as `bty_avg` and `gender`, to predict `score`. Compare the slope on `bty_avg` in this model to the slope on `bty_avg` in the model from exercise 3.   
    
************************************************************************

************************************************************************


### Exercise 8

The slope on `age` in the model constructed in the previous exercise is a relatively small number. Does this mean that `age` is not a useful predictor for this model? Explain.

************************************************************************

************************************************************************

### Exercise 9

The original paper describes how these data were gathered by taking a sample of professors from the University of Texas at Austin and including all courses that they have taught. Considering that each row represents a course, could this new information have an impact on any of the conditions of linear regression?


************************************************************************

************************************************************************
 
 
 
 

## Reed Theses

In this lab, we return to the Reed Thesis Tower data collected in Lab 6.

The following code chunk loads the data into R.

```{r}
theses <- read_csv("https://reed-statistics.github.io/math141s22-wells-website/data/theses_2022.csv") %>% filter(division != "G") %>% drop_na(division, n_pages, year)
```

The following abbreviations are used for divisions:

- `Arts`: The Arts

- `HSS`: History and the Social Sciences

- `ID`: Interdisciplinary

- `LL`: Literature and Languages

- `MNS`: Mathematical and Natural Sciences

- `PRPL`: Philosophy, Religion, Psychology, and Linguistics    


Thesis data was collected by in batches by groups of students. While no theses were duplicated within groups, it is possible that different groups of students happened to add the same theses to their own samples.

To determine whether a thesis was included more than once, we can treat `barcode` as an identification variable for each thesis, and determine how many times each `barcode` appears in the data set. 

### Exercise 10

Use a combination of `group_by` and `summarize` to create a dataframe `thesis_barcodes` which contains a column consisting of the unique barcodes, along with the count of how many times each barcode appears.


*********************************************************************************

*********************************************************************************



### Exercise 11

Use a combination of `filter`, `summarize` and `mutate` to determine how entries in the `theses` data set are duplicates (i.e. if the same thesis appears 4 times, there are 3 duplicates).


*********************************************************************************


*********************************************************************************


While several theses indeed show up more than once (and a few show up several times), they represent a relatively small fraction of the total number of observations and so should not significantly impact any of our analysis.

### Exercise 12

As noted in class last week, a few theses appear to have erroneously recorded dates of publications. Noting that Reed was founded in 1908, use the `filter` verb to identify and remove any theses that were published before this time. Save the resulting data frame as `theses`

*********************************************************************************

*********************************************************************************



### Exercise 13

We are interested in comparing page counts of theses across divisions. Create a boxplot showing the relationship between these two variables. Add the following layer (omit the backticks) to the end of your ggplot to include group means on your plot:
`+ stat_summary(fun = mean, colour = "red", geom = "point", size = 3)`

*********************************************************************************

*********************************************************************************

### Exercise 14

Compute the mean and standard deviations of page counts within each division, along with the number of theses in each division.

*********************************************************************************

*********************************************************************************


### Exercise 15

In the following exercises, we will perform an ANOVA test to determine "Do thesis page counts differ by division?"

State null and alternative hypotheses that could answer this question.


*********************************************************************************

*********************************************************************************



### Exercise 16

Use `infer` to compute the ANOVA test statistic.  What does this imply about the between group variability compared to the within group variability?

*********************************************************************************

*********************************************************************************


### Exercise 17

Use `infer` to generate the null distribution of the test statistic, visualize, and compute the p-value.
 

*********************************************************************************

*********************************************************************************


### Exercise 18

Make a conclusion to answer the research question in exercise 7.

*********************************************************************************

*********************************************************************************


### Exercise 19 

What are the effect sizes here?  In other words, what are sizes of the differences in average page counts between pairs of divisions?  As the domain expert (i.e. a current or future thesising student), are those effect sizes *practically significant*?  Justify your answer.


*********************************************************************************

*********************************************************************************





