---
title: "Homework 3.1"
author: "Your Name Here"
institute: "Old Dominion University"
format: 
  html:
    theme: lux # Check here for more themes: https://quarto.org/docs/output-formats/html-themes.html
    code-tools: true
    code-fold: true
    code-summary: "Code"
    code-copy: hover
    link-external-newwindow: true
    tbl-cap-location: top
    fig-cap-location: bottom

self-contained: true
editor: source
---

```{r setup, include=FALSE}
# DO NOT EDIT THIS

knitr::opts_chunk$set(fig.align = 'center')
knitr::opts_chunk$set(out.width = '90%')
knitr::opts_chunk$set(results = 'hold')
knitr::opts_chunk$set(fig.show = 'hold')
knitr::opts_chunk$set(error = TRUE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
par(mar = c(4.1, 4.1, 1.1, 4.1))

hooks = knitr::knit_hooks$get()
hook_foldable = function(type) {
  force(type)
  function(x, options) {
    res = hooks[[type]](x, options)
    
    if (is.na(as.logical(options[[paste0("fold.", type)]]))) {
      
      res
    } else if (isFALSE(as.logical(options[[paste0("fold.", type)]]))){
      
      # return(res)
      
      paste0(
      "<details open><summary>", gsub("^p", "P", gsub("^o", "O", type)), "</summary>\n\n",
      res,
      "\n\n</details>"
      )
    } else {
      
      paste0(
      "<details><summary>", gsub("^p", "P", gsub("^o", "O", type)), "</summary>\n\n",
      res,
      "\n\n</details>"
      )
    }
  }
}

knitr::knit_hooks$set(
  output = hook_foldable("output"),
  plot = hook_foldable("plot")
)
knitr::opts_chunk$set(fold.output=TRUE)
knitr::opts_chunk$set(fold.plot=TRUE)

Q <- 0
```

## Question `r Q <- Q+1; Q`

```{r include=FALSE}
q <- 0
```

For this question, you are going to analyze tipping culture. Consider these data ([data](https://vincentarelbundock.github.io/Rdatasets/csv/reshape2/tips.csv); [documentation](https://vincentarelbundock.github.io/Rdatasets/doc/reshape2/tips.html)) on tips received by a single waiter over a period of a few months.

`r q <- q+1; letters[q]`. Read in the data into R. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Create a table of summary statistics for `total_bill` and `tip`. (**2 Points**)

```{r, warning=FALSE, message=FALSE}
# Your code here...
```

`r q <- q+1; letters[q]`. Write down an econometric model (i.e. use $\beta_0$ and $\beta_1$) that explains tip amount based on the total bill amount. (**2 Points**)

*Your answer here...*

`r q <- q+1; letters[q]`. The rule-of-thumb for tipping is to give 18% of the total bill. If this rule of thumb were followed, what would we expect for values of $\beta_0$ and $\beta_1$? (**4 Points**)

*Your answer here...*

`r q <- q+1; letters[q]`. Plot `total_bill` vs `tip`. Make the figure presentable with labeled axes, colored points (with some opacity), etc. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Estimate the parameters in the model.  Interpret the value of each coefficient in words. (**2 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. Re-create the plot, but include the regression line *and* the rule-of-thumb for tipping.  Make each line a different color and include a legend.  Discuss how people tip in reality compared to the rule-of-thumb. (**6 Points**)

```{r}
# Your code here...
```

*Your answer here...*



## Question `r Q <- Q+1; Q`

```{r include=FALSE}
q <- 0
```

The American Housing Survey (AHS) collects information about individual dwelling choices.  These data ([data](https://vincentarelbundock.github.io/Rdatasets/csv/Stat2Data/MetroCommutes.csv); [documentation](https://vincentarelbundock.github.io/Rdatasets/doc/Stat2Data/MetroCommutes.html)), which are a subset of the 2007 AHS, contain information on distances and times individuals travel for their commutes to work. (**2 Points**)

`r q <- q+1; letters[q]`. Read the data into R.

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Estimate two models of commute time given commute distance. First, estimate a level-level model. Second, estimate a log-log model. Display the coefficients in a table. (**4 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Suppose you are a commuter who wants to live as far as possible from their job which is in the middle of a city center (and thus subject to pollution, crime, noise, and everything else you don't like). However, you also don't like commuting; each minute in traffic is torture to you. If you are willing to spend 30 minutes in traffic, how far from work, in miles, should you live? Use the parameters from the level-level model to inform your decision. (**2 Points**)

*Your answer here...*

`r q <- q+1; letters[q]`. Estimate the level-level model for each city in the data.  Display the coefficients in a single table. (**4 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Compare the coefficients for Boston, the highest density city in the sample, to Houston, the lowest density city in the sample.^[Persons (in 2016) per Square Mile: Boston (13,943), Houston (3,842), Minneapolis (7,664), and Washington (11,158).] (**2 Points**)

*Your answer here...*

`r q <- q+1; letters[q]`. Following from the previous question, why do you think a city's density appears to be related to its $R^2$ in the models above? (**2 Points**)

*Your answer here...*



## Question `r Q <- Q+1; Q`

```{r include=FALSE}
q <- 0
```

Infant birth weight is an often used measure in health economics, serving as a vital indicator of an infant's immediate and long-term health prospects. One of the key determinants of birth weight is the infant's gestation period, or the time spent in utero. Use these data ([data](https://vincentarelbundock.github.io/Rdatasets/csv/mosaicData/Gestation.csv); [documentation](https://vincentarelbundock.github.io/Rdatasets/doc/mosaicData/Gestation.html)) records of births from 1961 and 1962 to answer the following questions.

`r q <- q+1; letters[q]`. Read in the data. Convert birth weight to pounds and gestation to weeks, both as new variables. Use the `floor()` function to round gestation down to the nearest whole week. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Plot the distribution of gestation (in weeks). Be sure to label the axes, etc. What are some things you notice about the distribution? (**3 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. Create a visualization of average birth weight by weeks of gestation. What are some things you notice about the figure? (**3 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. Estimate models to explain birth weight in pounds using gestation in weeks. The first model should be level-level and the second model should be log-level. You should eliminate any outliers you might have identified from the data. Present the coefficients in a table. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Interpret each coefficient from each model in words. (**4 Points**)

*Your answer here...*







