---
title: |
  Answers to Exercise Set 02
author:
  - name: Andrew Pua
date: September 30, 2022
format:
  html:
    code-fold: true
    self-contained: true

---
## Preamble

This template is a radically simplified version of Andrew Heiss's quarto [templates](https://github.com/andrewheiss/hikmah-academic-quarto). 

Here you can organize the commands needed to load packages and do other formatting options (if needed). 

```{r}
# To clean R's memory, leave this alone
rm(list=ls())
```

## Item 2

You can weave R code into your document.

```{r}
n_lights <- 2 + 2
n_lights
```


You can also type math, but this is one equation at a time for now. 

$$
\widehat{y} = \text{Something} + \beta_1 x_1 + \beta_2 x_1^2 + \beta_3 \log x_3 +\beta_4 \frac{1}{x_4}
$$
qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.

## Item 3

Recall fasteR? 

```{r}
Nile
hist(Nile)
which(Nile > 1200)
```

qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.

## Item 4

You can also make plots. 

```{r}
#| fig-align: "center"
# Here I downloaded directly from the course website.
# This will take longer if the files are larger.
# If you want to use a local file on your computer, then adjust the paths.
dataset1 <- read.csv("https://applied-metrics.neocities.org/datasets/sia1_ceo_comp.csv")
dataset2 <- read.csv("https://applied-metrics.neocities.org/datasets/04_4m_exec_comp_2003.csv")
names(dataset1) <- c("Name", "Company", "Industry", "TotalCompMil", 
                     "NetSalesMil", "Log10TotalComp")
names(dataset2) <- c("SalaryThou", "Name", "Company", "Industry", 
                     "TotalCompMil")
execComp <- merge(dataset1, dataset2, 
                  by=c("Name","Company", "Industry", "TotalCompMil")) 
eCsub <- subset(execComp, (TotalCompMil>0 & NetSalesMil>0))
lm(TotalCompMil ~ NetSalesMil, data=eCsub)
plot(eCsub$NetSalesMil, eCsub$TotalCompMil, xlim=c(0,280000), ylim=c(0,90), 
     xlab = "Net Sales in Millions", ylab = "Total Compensation in Millions")
```

Plots can be more complicated:

```{r}
#| label: fig-n-lights
#| fig-cap: There are four lights
#| fig-width: 5
#| fig-height: 1
#| fig-align: "center"
par(mar = c(0, 0, 0, 0))
plot(x = seq(1:n_lights), y = rep(1, 4), col = "darkgoldenrod1",
     pch = 8, cex = 4, axes = TRUE, ann = FALSE, xlab = FALSE, ylab = FALSE)
```
