---
title: 'Hwk #1: Regression'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
For this homework we will use NHANES data that exists in a package for R.
NHANES consists of survey data collected by the US National Center for Health Statistics (NCHS) which has conducted a series of health and nutrition surveys since the early 1960's. Since 1999 approximately 5,000 individuals of all ages are interviewed in their homes every year and complete the health examination component of the survey. The health examination is conducted in a mobile examination center (MEC).
Note that there is the following warning on the NHANES website:
“For NHANES datasets, the use of sampling weights and sample design variables is recommended for all analyses because the sample design is a clustered design and incorporates differential probabilities of selection. If you fail to account for the sampling parameters, you may obtain biased estimates and overstate significance levels.”
For this homework, please ignore this warning and just apply our analyses to the data as if they were randomly sampled! We will be using the data called `NHANESraw`.
For questions that ask for your comments, it suffices to answer with one or two sentences in each case.
## Data Preparation
1. Install the package `NHANES` into R, load the `NHANES` package, and then run the command `data(NHANES)` which will load the NHANES data. Type `?NHANES` and read about the dataset.
```{r}
```
2. Make an object `nhanes` that is a subset version `NHANESraw` that does not include any missing data for `Diabetes`, `BPSysAve`, `BPDiaAve`
```{r}
```
3. (1 point) Plot `BPSysAve` against `BPDiaAve`. Comment on what is going on.
Further subset the data such the observations with `BPDiaAve` equal to zero are removed.
```{r}
```
4. (1 point)
Make an object `nhanes09` that is a subset of `nhanes` to only the 2009_10 data. This will be your training dataset. Also make an object `nhanes11` that is a subset of `nhanes` to only the 2011_12 data. This will be your test dataset.
```{r}
```
## Linear regression
5. (1 point) Plot `BPSysAve` against `BPDiaAve` for the training data.
```{r}
```
6. (2 points) Fit a linear model using the training data with `BPSysAve` as outcome and `BPDiaAve` as the single predictor.
```{r}
```
7. (1 point) Use the `summary` command to examine the resulting fitted model.
```{r}
```
8. (1 point) Generate 95% confidence intervals for the parameters of the fitted model.
```{r}
```
9. (1 point) Also, generate 99% confidence intervals for the parameters of the fitted model.
```{r}
```
10. (2 points) Comment on the difference between the 95% and 99% confidence intervals and whether or not the difference is what you would expect.
11. (3 points) Now fit models with quadratic and cubic terms in the predictor `BPDiaAve` in addition to the linear term. Look at the output of each model with summary and generate 95% confidence intervals for each.
```{r}
```
12. (2 points) Plot the training data along with the linear, quadratic and cubic fit lines in different colors.
```{r}
```
13. (1 point) Which would be your preferred model based on the visual fits?
14. (3 points) Perform an anova test comparing the 3 models. Does the result seem in line with what you were expecting from the visual fits? Why/why not?
```{r}
```
15. (1 point) Now plot `BPSysAve` against `BPDiaAve` for the test data and overlay the fitted linear, quadratic, and cubic models.
```{r}
```
16. (3 points) Does this change your opinion at all about which is the best fit? Why/why not?
## Smoothing kernels:
17. (3 points) Fit a nearest neighbors curve with `ksmooth` using the "normal" kernel to the `nhanes09` data with bandwidths of 3, 10, and 20.
```{r}
```
18. (2 points) Plot the test data as well as the fitted curves from kernel smoothing and the best model fitted in the previous section using linear regression. Use different colors for each model.
```{r}
```
19. (1 point) Based on the above results, which model would you pick? In the next section, we'll evaluate the model based on its test error.
## Evaluating error on a test set
20. (5 points) Evaluate the mean squared error of the fitted models from the "Linear Regression" section on the test data. Also provide standard errors.
```{r}
```
21. (2 points) Using ANOVA, did we pick the model with the lowest mean squared error on the test data? Why do you think this happened?
22. (5 points) Evaluate the mean squared error of the fitted models from kernel smoothing on the test data.
```{r}
```
23. (1 point) Among all the methods we tried, which one had the lowest test error? Was the test error of your selected model within one standard error of the minimum test error?