---
title: 'CSSS/POLS 510 MLE Lab'
subtitle: "Lab 4. Prediction and Quantities of Interest"
author: Minji Jeong
output:
  beamer_presentation:
    colortheme: seahorse
    fonttheme: structurebold
    theme: CambridgeUS
    highlight: default
linkcolor: blue
editor_options: 
  chunk_output_type: console
header-includes:
 \usepackage{float}
 \usepackage{xcolor}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,message = FALSE,warning = FALSE,error = FALSE)
library(tidyverse);library(MASS);library(simcf);library(tile)
```


# Housekeeping 

- Problem set 1 grades will be released after the lab

- Problem set 2 is due October 24 3:30pm

- Agenda

  - Review Problem set 1

  - Last lab review

  - Quantities of Interest


# 1. Review HW1: simulation precision

\tiny
```{r}
dbinom(x=16, size=30, prob=0.49)


set.seed(12345)

sims <- 100       # 100 simulations

nmen <- vector(mode = "numeric", length = sims)
for (i in 1:sims) {
  nmen[i] <- sum(sample(x = c(0,1),
                        size = 30,
                        replace = TRUE,
                        prob = c(0.51, 0.49) ))
}
sum(nmen==16)/length(nmen) # sum of trials with 16 males

```



# 1. Review HW1: simulation precision

\tiny
```{r}
dbinom(x=16, size=30, prob=0.49)


set.seed(12345)

sims <- 1000       # 1000 simulations

nmen <- vector(mode = "numeric", length = sims)
for (i in 1:sims) {
  nmen[i] <- sum(sample(x = c(0,1),
                        size = 30,
                        replace = TRUE,
                        prob = c(0.51, 0.49) ))
}
sum(nmen==16)/length(nmen) # sum of trials with 16 males

```



# 1. Review HW1: simulation precision

\tiny
```{r}
dbinom(x=16, size=30, prob=0.49)


set.seed(12345)

sims <- 10000       # 10000 simulations

nmen <- vector(mode = "numeric", length = sims)
for (i in 1:sims) {
  nmen[i] <- sum(sample(x = c(0,1),
                        size = 30,
                        replace = TRUE,
                        prob = c(0.51, 0.49) ))
}
sum(nmen==16)/length(nmen) # sum of trials with 16 males

```


# 1. Review HW1: simulation precision

\tiny
```{r}
dbinom(x=16, size=30, prob=0.49)


set.seed(12345)

sims <- 100000      # 100000 simulations

nnmen <- vector(mode = "numeric", length = sims)
for (i in 1:sims) {
  nmen[i] <- sum(sample(x = c(0,1),
                        size = 30,
                        replace = TRUE,
                        prob = c(0.51, 0.49) ))
}
sum(nmen==16)/length(nmen) # sum of trials with 16 males

```


# 1. Review HW1: Probability **Mass** Functions


```{r, echo=FALSE, out.width="100%", fig.align='center'}
knitr::include_graphics("images/binomial_pmf1.pdf")
```



# 1. Review HW1: Probability **Mass** Functions


```{r, echo=FALSE, out.width="100%", fig.align='center'}
knitr::include_graphics("images/binomial_pmf2.pdf")
```


# 1. Review HW1: Probability **Mass** Functions


```{r, echo=FALSE, out.width="100%", fig.align='center'}
knitr::include_graphics("images/binomial_pmf3.pdf")
```



# 1. Review HW1: summary

- When displaying P**M**Fs, provide visuals with a discrete sample space.

- **Sanity checks**:
  - Use R built-in or packages programs/functions to **double-check**.
  - Increase simulations/sample **size**.
    - Convergence in probability ($N \rightarrow \infty$ )



# 2. Last lab review: MLE

- How do we estimate the MLE?
  1. Define a probability model (PDF): $Y_i \sim N(\mu_i, \sigma^2)$.
  2. Derive the log-likelihood function.
  3. Reduce to sufficient statistics and substitute systematic component.
  4. Use `optim()` or any other function to find the maxima.



# 2. Normal homoskedastic

Two **different** notations for the **same** model.

\vspace{5mm}

::: columns
:::: column
LS notation:
\scriptsize
$$
\begin{aligned}
\varepsilon & \sim N(0, \sigma^2) & (stochastic) \\
Y_i & = x_i \mathcal{\beta} & (systematic) \\
Y_i & = x_i \mathcal{\beta}  + \varepsilon  & (stochastic + systematic) \\
\end{aligned}
$$


::::
:::: column
MLE notation:
\scriptsize
$$
\begin{aligned}
Y_i & \sim N(\mu_i, \sigma^2) & (stochastic) \\
\mu_i & = x_i \beta & (systematic) \\
Y_i & \sim N( x_i \beta, \sigma^2)  & (stochastic + systematic) \\
\end{aligned}
$$
::::
:::





# 2. MLE general notation



$$
\begin{aligned}
Y_i & \sim f(\theta_{i}, \alpha)  & \text{(stochastic)}\\
\theta_{i} & = g(\boldsymbol{x}_{i} \boldsymbol{\beta}) & \text{(systematic})
\end{aligned}
$$
where

  - $Y_i$ is a random outcome variable.
  - $f(.)$ is a probability density function.
  - $\theta_i$ is a systematic feature of the PDF that varies over i.
  - $\alpha$ is an ancillary parameter (feature of f that we treat as constant).
  - $g(.)$ functional form for reparametrization of the data model.
  - $x_i$ explanatory variables vector.
  - $\beta$ vector of effect parameters.




# 2. Last lab review: MLE - Heteroskedastic normal


```{r, echo=FALSE, out.width="100%", fig.align='center'}
knitr::include_graphics("images/mle_heterosknorm.pdf")
```



# 3. Quantities of Interest: prediction

Once we have estimated the model, we can compare the predictive performance of each model. Refer to [Chris's MLE lecture (slide 80)](https://faculty.washington.edu/cadolph/index.php?page=102&loc=mle&pdf=topic2.pw) for more details.


```{r, echo=FALSE, out.width="100%", fig.align='center'}
knitr::include_graphics("images/chris_pred.png")
```



# 3. Quantities of Interest
**Motivation**: We want to study how the change in a particular explanatory variable affects the outcome variable, *all else being equal*.

We will focus on how to simulate predictions from *estimands* or **quantities of interest**.

Let's open $\texttt{RStudio}$ and Lab4.html file jointly with the Lab4.Rmd file with the code and contents of today's lab!


# 

\Huge

\centerline{F I N}

