---
title: My first Quarto document
author:
  - name: Andrew Pua
date: October 5, 2022
format:
  html:
    code-fold: true
    self-contained: true
---

## Preamble

Make sure you have installed the Quarto CLI and the R packages `quarto` and `foreign`. To render this qmd file, you have to be in the R console/terminal, load quarto, and then use `quarto_render("applied-metrics-1-exercise-set-02-solutions-quarto.qmd")`. Ideally, put the external objects (like data) which need to be loaded in the same folder as your qmd file. Make sure to adjust the paths accordingly.

```{r}
library(foreign)
ratings <- read.dta("TeachingRatings.dta")
```

Another approach to adjust paths a bit more automatically is something like `paste(getwd(), "TeachingRatings.dta", sep="/")`. This creates a string first by getting the path of the current working directory `getwd()`, pasting it with the name of the file `"TeachingRatings.dta"`, and then making sure to put a `/` in between. Try running this command in the R console to see how it works. 

There are some new things you could learn from the qmd file, like how to incorporate results from your code into the text. 

## Item 2

```{r}
lm(course_eval ~ beauty, data = ratings)
plot(ratings$beauty, ratings$course_eval, xlab = "Beauty rating", ylab = "Course evaluation")
abline(lm(course_eval ~ beauty, data = ratings))
```

The average course evaluation score for instructors who scored 0 on the beauty measure is `r round(coef(lm(course_eval ~ beauty, data = ratings))[[1]],2)`. If we compare instructors whose beauty scores differ by 1, their average course evaluation scores will  differ by `r round(coef(lm(course_eval ~ beauty, data = ratings))[[2]],2)`.

## Item 3

```{r}
lm(course_eval ~ female, data = ratings)
plot(ratings$female, ratings$course_eval, xlab = "Sex (Male = 0, Female = 1)", ylab = "Course evaluation")
abline(lm(course_eval ~ female, data = ratings))
```

The average course evaluation scores for males and females separately are:

```{r}
tapply(ratings$course_eval, ratings$female, mean)
```

From the least squares regression, we find that the average course evaluation score for males is `r round(tapply(ratings$course_eval, ratings$female, mean)[[1]],2)` and this value matches the intercept of the least squares regression of course evaluations on beauty. In addition, female instructors score, on average, `r -round(coef(lm(course_eval ~ female, data = ratings))[[2]],2)` lower than male instructors. This matches what we see when we take the difference between the two group averages calculated by `tapply()`: `r round(tapply(ratings$course_eval, ratings$female, mean)[[2]] - tapply(ratings$course_eval, ratings$female, mean)[[1]],2)`. 

## Item 4

The difference this time is that `female` is a dummy variable, while `NetSalesMil` is not. Furthermore, there was just be one comparison between males and females. In the compensation-sales relationship, it might be difficult to find a subset of the data for which `NetSalesMil` differ by exactly  1 million dollars. If one uses `tapply()`, each possible value of `NetSalesMil` becomes a separate category.
