---
title: 'STA 35C: Homework 8'
subtitle: 'Instructor: Akira Horiguchi'
author: "Student name: ABCDE FGHIJ; Student ID: 123456789"
date: "2025-11-26 23:59:00 PDT"
date-format: "MMM D, YYYY (dddd), HH:mm z"
format: 
    pdf: 
        include-in-header:
          text: |
            \usepackage{fancyhdr}
            \usepackage{lastpage}
            \pagestyle{fancy}
            \fancyhf{} % sets both header and footer to nothing
            \renewcommand{\headrulewidth}{0pt}
            \fancyhead[RO,RE]{}
            \fancyhead[LO,LE]{}
            \fancyfoot[LE,LO]{}
            \fancyfoot[LE,CO]{Page \thepage\ of \pageref{LastPage}}
editor: source
geometry:
  - top=5mm
  - bottom=20mm
  - left=5mm
  - right=5mm
urlcolor: blue
---

The assignment must be done in an [R Markdown](https://rmarkdown.rstudio.com) or [Quarto](https://quarto.org/) document. The assignment must be submitted by the due date above by uploading:

-   a .pdf file in GRADESCOPE (if you can knit/compile your .rmd to a .html file only, please save the created .html file as a .pdf file (by opening the .html file -\> print -\> save to .pdf)).

Email submissions will not be accepted.

Each answer has to be based on `R` code that shows how the result was obtained. The code has to answer the question or solve the task. For example, if you are asked to find the largest entry of a vector, the code has to return the largest element of the vector. If the code just prints all values of the vector, and you determine the largest element by hand, this will not be accepted as an answer. No points will be given for answers that are not based on `R`. This homework already contains chunks for your solution (you can also create additional chunks for each solution if needed, but it must be clear to which tasks your chunks belong).

There are many possible ways to write `R` code that is needed to answer the questions or do the tasks, but for some of the questions or tasks you might have to use something that has not been discussed during the lectures or the discussion sessions. You will have to come up with a solution on your own. Try to understand what you need to do to complete the task or to answer the question, feel free to search the Internet for possible solutions, and discuss possible solutions with other students. It is perfectly fine to ask what kind of an approach or a function other students use. However, you are not allowed to share your code or your answers with other students. Everyone has to write the code, do the tasks and answer the questions on their own.

During the discussion sessions, you may be asked to present and share your solutions.

\newpage

# 1. Applied

ISLR Chapter 7, exercise 6. In this exercise, you will further analyze the `Wage` data set considered throughout this chapter.

Perform polynomial regression to predict `wage` using `age`. Use cross-validation to select the optimal degree $d$ for the polynomial. What degree was chosen? Make a plot of the resulting polynomial fit to the data.

```{r}
### Your Solution (Code)


```

\newpage

# 2. Counting subsets

In lecture (Monday 11/24), we mentioned that 

- $2^n$ is the number of possible subsets of a set with $n$ items;
- $\binom{n}{K}$ is the number of possible subsets **of size $K$** of a set with $n$ items.

The formula for "$n$ choose $K$" is $$\binom{n}{K} = \frac{n!}{K! (n-K)!}$$ and can be computed using the R function `choose()`. The exclamation point is called "factorial", so $n!$ is "$n$ factorial"; this can be computed using the R function `factorial()`.

(These are identities/quantities that are fundamental to counting, which itself is fundamental to probability and hence will be helpful for when you take either MAT 135A or STA 131A. Thus I want to make sure you understand them.)

(a) Compute the number of possible subsets of size $0$ of a set with $50$ items. How many digits are there in this number? *Hint: you do not need to use R to compute this.* Verify your solution using R.

(b) Compute the number of possible subsets of size $1$ of a set with $50$ items. How many digits are there in this number? *Hint: you do not need to use R to compute this.* Verify your solution using R.

(c) Compute the number of possible subsets of size $2$ of a set with $50$ items. How many digits are there in this number? Do this calculation by hand (or by TeX), and verify your solution using R. For examples of doing this calculation by hand, you can watch this video [https://www.youtube.com/watch?v=nMQQUaa3d2c](https://www.youtube.com/watch?v=nMQQUaa3d2c). 

(d) Compute the number of possible subsets of size $3$ of a set with $50$ items. How many digits are there in this number? Do this calculation by hand (or by TeX), and verify your solution using R.

(e) Compute the number of possible subsets of a set with $50$ items. How many digits are there in this number? No need to do this calculation by hand -- you can just use R for this.

(f) Use the `sapply()` function to compute $\sum_{K=0}^{50} \binom{n}{K}$. Use R to verify that it equals your solution to part (e).
