---
title: "Lab #1: Practice in R"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
set.seed(1)
```
The purpose of this lab is to get you comfortable working in R and trying a few things from the first lecture.
## Simulating data:
We will simulate weight vs. age data from a cubic function, add some noise, and do some simple analyses.
1. Write a function in R to input age $x$ and output the expected weight according to the following function: $f(x) = -0.0004x^3 + 0.025x^2 + 2x + 50$.
```{r}
```
2. Next we will simulate some values for $x$. Use the `sample` function to generate a random sample of integers between 40 and 80 of size 50 (you will need the `replace=TRUE` option to make sure the numbers can be resampled). Don’t forget that you can type `?sample` to get help as to how to use the function.
```{r}
```
3. Now generate a vector of the output of your function to generate the expected weights for each of the individuals.
```{r}
```
4. Use `rnorm` to generate some normally distributed noise with mean 0.0 and sd of 10.0 for each of the values.
```{r}
```
5. Add noise to the expected weight to get your simulated outcome data.
```{r}
```
6. Put these together in a dataframe with columns for age and observed weight.
```{r}
```
7. Generate a plot of weight against age.
```{r}
```
## Applying smoothing kernels:
Given the observed data, let's try to estimate the expected weight $f(x)$.
8. Fit a nearest neighbors curve with `ksmooth` using a bandwidth of 10 and the `box` kernel.
```{r}
```
9. Fit another curve this time using the `normal` kernel.
```{r}
```
10. Plot the data with the two fitted curves using the command `lines`. Also use the command `curve` to plot the true expected weight. How do these curves compare?
Hint: look at the object you have generated with ksmooth (i.e. type `name_of_the_object` or `print(name_of_the_object)`. Also, try `names(name_of_the_object)`. ?
```{r}
```
11. Fit curves using the "normal" kernel using bandwidths of size 5, 10, and 20. How do these compare to the true expected weight?
```{r}
```
## Fitting linear models:
12. Fit a linear regression to the data using the `lm` command.
```{r}
```
13. Run `summary(your_linear_model_name)` to see the estimated parameters and confidence intervals.
```{r}
```
14. What weight would this predict for someone whose age is 40? How about 10?
```{r}
```
15. Now fit quadratic and cubic models: you will need the `I` function to set quadratic and cubic terms in the regression, e.g. `I(x^2)`
```{r}
```
15. Compare the fitted models using `lm` in a plot. For the linear model, you can use the `abline` command. For the polynomial fits, you can use the `curve` command (you will need the option `add = TRUE`). Which curve do you prefer? How do these curves compare to those estimated using kernel smoothing?
```{r}
```
16. Among the curves fitted using linear regression, how would you select the curve given the data?
```{r}
```
## Practice installing an R package
17. R packages are easy to install using the `install.packages` command. Try installing the `e1071` package. We'll use this package later in the course for fitting support vector machines.
```{r}
```