---
title: "Resampling Lab (stub)"
format: html
---

Stub of the Resampling Lab page: the exercises, without the solutions. Solutions: https://morrison-lab.github.io/lds/chapters/resampling-lab.html

#### Lab: the average fuel efficiency

Use the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).
What is the [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) of `mpg`?

::: {.callout-tip}

#### Hint

A [summary of a column](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) of the data frame returns its [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean).

:::

```{python}
# your code here
```

#### Lab: fuel efficiency and horsepower

Use the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).
What is the [sample correlation](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-pearson-r) between `mpg` and `horsepower`?

::: {.callout-tip}

#### Hint

The [summary functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more) of a data frame include its [correlation](https://morrison-lab.github.io/pds/variance-covariance.html#def-correlation) [matrix](https://morrison-lab.github.io/mds/linear-algebra-matrices.html#def-matrix),
from which you read one pair of columns.

:::

```{python}
# your code here
```

#### Lab: a histogram of fuel efficiency

Use the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).
Draw a histogram of `mpg`.

::: {.callout-tip}

#### Hint

The [plotting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) draw a histogram of one column.

:::

```{python}
# your code here
```

#### Lab: a scatterplot of fuel efficiency against horsepower

Use the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).
Draw a scatterplot of `mpg` against `horsepower`.

::: {.callout-tip}

#### Hint

The [plotting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) draw a scatterplot of one column against another.

:::

```{python}
# your code here
```

#### Lab: split the data into two halves

Randomly split the $392$ [observations](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-observation) of the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto) into a [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) of $196$ observations and a [validation set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-validation-set) of $196$.

::: {.callout-tip}

#### Hint

The [random number functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-random) draw whole numbers with or without repeats,
and a [selection by position](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-indexing) keeps those rows of the data frame.

:::

```{python}
# your code here
```

#### Lab: find the validation error of a linear fit

Fit a [linear regression](https://morrison-lab.github.io/lds/chapters/linear-regression.html#def-linear-model) of `mpg` on `horsepower` to the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) of Exercise 5,
and find the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of its [predictions](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) on the [validation set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-validation-set).

::: {.callout-tip}

#### Hint

[Fit a model with a formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) on the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set),
[predict](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) on the [validation set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-validation-set),
and take the [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) of the squared differences,
as the [error function of scikit-learn](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) does.

:::

```{python}
# your code here
```

#### Lab: find the validation error of a quadratic fit

Fit a quadratic [linear regression](https://morrison-lab.github.io/lds/chapters/linear-regression.html#def-linear-model) of `mpg` on `horsepower` to the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) of Exercise 5,
and find the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of its [predictions](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) on the [validation set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-validation-set).

::: {.callout-tip}

#### Hint

Wrap the square in `I(...)` inside the [formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels),
then compute the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) on the [validation set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-validation-set) as in Exercise 6.

:::

```{python}
# your code here
```

#### Lab: find the ten-fold cross-validation error

Find the ten-fold [cross-validation estimate](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-cv-estimate) of the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of [linear regression](https://morrison-lab.github.io/lds/chapters/linear-regression.html#def-linear-model) of `mpg` on `horsepower`, using all $392$ [observations](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-observation) of the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).

::: {.callout-tip}

#### Hint

Split the row numbers into ten groups;
each group is held out once
while the [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) is [fit](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) on the rest.

:::

```{python}
# your code here
```

#### Lab: find the leave-one-out cross-validation error

Using [leave-one-out cross-validation](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-leave-one-out-cross-validation), find the [cross-validation estimate](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-cv-estimate) of the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of [linear regression](https://morrison-lab.github.io/lds/chapters/linear-regression.html#def-linear-model) of `mpg` on `horsepower`, using all $392$ [observations](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-observation) of the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).

::: {.callout-tip}

#### Hint

Hold out one row at a time,
[fit](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) the [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) on the rest,
and average the squared errors of the [predictions](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction).

:::

```{python}
# your code here
```

#### Lab: estimate a standard error with the bootstrap

Draw $1000$ [bootstrap samples](https://morrison-lab.github.io/sds/bootstrap.html#def-bootstrap-sample) of the $392$ [observations](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-observation) of the [Auto data](https://morrison-lab.github.io/lds/chapters/course-overview.html#dat-auto).
In each, fit a [linear regression](https://morrison-lab.github.io/lds/chapters/linear-regression.html#def-linear-model) of `mpg` on `horsepower`.
Use the [slope](https://morrison-lab.github.io/mds/algebra-exponentials.html#def-slope) from each of the $1000$ fits to find the [bootstrap standard error](https://morrison-lab.github.io/sds/bootstrap.html#def-bootstrap-se) of the slope.

::: {.callout-tip}

#### Hint

Draw each of the [bootstrap samples](https://morrison-lab.github.io/sds/bootstrap.html#def-bootstrap-sample) with [`rng.choice` or `rng.integers`](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-random) in Python,
or `boot::boot()` or `rsample::bootstraps()` in R,
then take the [standard deviation](https://morrison-lab.github.io/pds/variance-covariance.html#def-sd) of the [slopes](https://morrison-lab.github.io/mds/algebra-exponentials.html#def-slope).

:::

```{python}
# your code here
```
