---
title: 'STA 35C: Homework 7'
subtitle: 'Instructor: Akira Horiguchi'
author: "Student name: ABCDE FGHIJ; Student ID: 123456789"
date: "2025-11-19 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. Logistic Regression with the `titanic` dataset

Use the following codes to download the `titanic` data, which provide information on the fate of passengers of Titanic.

```{r eval = F}
install.packages("titanic")  # run just once, then comment out
library(titanic)  # run every time
```

(a) Fit a logistic regression model using the `titanic_train` dataset, with `Survived` as the response variable. Use passenger class, sex, age, and fare as predictors. Which predictors are significant, with significance level $\alpha = 0.05$?

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


```

<!-- ### Your Solution (Text) -->

\vskip 1in

(b) Find the confusion matrix of the train data. Calculate the accuracy.

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


```

\vskip 1in

(c) Predict the survival status (binary) using the `titanic_test` dataset. What portion of passengers are predicted to survive?

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


```

\newpage

# 2. Confusion matrix by hand

We assigned 80 individuals to a certain class based on their numbers of hours they regularly do sports a week. Afterwards, we asked them if they feel down or balanced, in other words, if they belong to class "I" (Null) or class "II" (Non-null). We obtained the confusion matrix:

    
| Predicted / True |   I |  II | Total |
|-----------------:|----:|----:|:-----:|
|                I |  35 |   8 |  43   |
|               II |   3 |  34 |  37   |
|            Total |  38 |  42 |  80   |


Calculate the accuracy, the false positive rate, the true positive rate, the positive predictive value, and the negative predictive value.
