```{r}
#| echo: false
#| message: false
library(tidyverse)
```



# Inference for the Difference between Two Proportions

## Confidence Interval

### Recap

The general form of confidence interval based on CLT:

$[p_1-p_2\ \pm \ critical\ value\times\ SE]$, where $SE=\sqrt{\frac{p_1(1-p_1)}{n_1}+\frac{p_2(1-p_2)}{n_2}}$

Conditions to meet:

1. Independence: Within each group data have to be independent from each other. The two groups have to be independent from one another.

2. Sample Size: At least 10 successes and 10 failures in each group.

3. Sample Size vs. Population Size: For each group the sample size needs to be smaller than 10% of the population.

## Example

Question: Do Group A and B have different success rates? Suppose the independence assumption holds.

|    |  Success| Failure|
|:---|---:|---:|
|Group A  | 40|   18|
|Group B  | 50|   25|

To construct a 95% confidence interval for the difference of two proportions ($\pi_1-\pi_2$), first we check whether all the conditions are met, then follow the following steps:

### 1. Calculate point estimate $p_1-p_2$.

```{r}
p1 <- 40 / (40 + 18)
p2 <- 50 / (50 + 25)
n1 <- 40 + 18
n2 <- 50 + 25
point_estimate <- p1 - p2
point_estimate
```

### 2. Calculate critical value.

```{r}
cv <- qnorm(0.975)
cv
```

### 3. Calculate standard error.

```{r}
se <- sqrt( (p1 * (1-p1) / n1) + (p2 * (1-p2) / n2) ) 
se
```

### 4. Construct the 95% confidence interval.

```{r}
point_estimate - cv * se
point_estimate + cv * se
```

95% CI for difference of two proportions is (-0.137, 0.183).

## Hypothesis Testing

### 1. Set hypotheses.

$H_0:\pi_1=\pi_2$ vs. $H_A:\pi_1\neq\pi_2$

### 2. Identify Sampling Distribution of $H_0$.

If all the conditions are met, then according to CLT: 

$(p_1-p_2)\sim approx\ N(\pi_1-\pi_2,\ \frac{\pi_1(1-\pi_1)}{n_1}+\frac{\pi_2(1-\pi_2)}{n_2})$

Under $H_0$, $\pi_1=\pi_2$, so we use pooled proportion to calculate SE:

#### 2.1 Calculate pooled proportion.

$p_{pooled}=\frac{n_1p_1+n_2p_2}{n_1+n_2}$

```{r}
p_pooled <- (p1*n1 + p2*n2) / (n1+n2)
p_pooled
```

#### 2.2 Calculate SE.

$SE = \frac{p_{pooled}(1-p_{pooled})}{n_1}+\frac{p_{pooled}(1-p_{pooled})}{n_2}$

```{r}
se <- sqrt( (p_pooled * (1-p_pooled) / n1) + (p_pooled * (1-p_pooled) / n2) )
se
```

### 3. Calculate p-value.

How likely are we to observe a difference of proportions in samples that is at least as extreme as the observed sample proportion difference ($p_1-p_2=0.023$)?

```{r}
point_estimate
pnorm(-point_estimate, mean = 0, sd = se)
pnorm(-point_estimate, mean = 0, sd = se) * 2
```

```{r, echo=F}
x <- seq(-.3, .3, length = 1000)
y <- dnorm(x, mean = 0, sd = se)
plot(x, y, type = "l", lwd = 2, xlab = 'p1-p2', ylab = '')
abline(v = point_estimate, lwd = 2, lty = 1)
abline(v = -point_estimate, lwd = 2, lty = 1)
```

### 4. Make a Decision and a Conclusion.

If the null hypothesis were true, then the probability of observing a difference of proportions in the sample that is at least extreme as 0.023 would be 0.78, and this is very high (much higher than 0.05). Therefore, the observed value ( $p_1−p_2=0.023$ ) is not significantly different from 0. We fail to reject the null and conclude that we have not found any evidence against the null.

Questions: What if the p-value is 0.049?  What if the p-value is 0.000001? 

## Exercise

Exercise 6.22 and 6.24 on page 254 of [the book](https://www.openintro.org/go/?id=os4_for_screen_readers&referrer=/book/os/index.php).


# Inference for Single Mean

## Confidence Interval

We still follows the general form of CI: [point estimate $\pm$ critical value $\times$ standard error]

For single mean, CI is: $[\bar x\pm t_{n-1}^*\times SE]$, where $SE=\frac{s}{\sqrt n}$

Conditions to meet:

1. Independence

2. Normality: The sample size should be greater than 30.

## Example

Given the heights of 50 random students in UCI, construct a 95% confidence interval for the average student height in UCI, assuming that all the conditions are met.

5.75, 6.22, 5.52, 5.55, 5.48, 4.72, 5.84, 5.38, 5.74, 6.20, 6.78, 5.74, 5.21, 5.55, 6.70, 4.73, 5.86, 5.38, 4.98, 5.17, 6.17, 5.62, 5.98, 5.62, 5.96, 6.18, 5.12, 5.92, 5.38, 4.78, 4.46, 5.50, 5.22, 5.38, 5.54, 5.06, 5.66, 5.50, 6.33, 4.68, 5.45, 6.69, 5.60, 4.90, 5.18, 5.46, 5.47, 5.43, 5.58, 5.71


```{r}
x <- c(5.75, 6.22, 5.52, 5.55, 5.48, 4.72, 5.84, 5.38, 
       5.74, 6.20, 6.78, 5.74, 5.21, 5.55, 6.70, 4.73, 
       5.86, 5.38, 4.98, 5.17, 6.17, 5.62, 5.98, 5.62, 
       5.96, 6.18, 5.12, 5.92, 5.38, 4.78, 4.46, 5.50, 
       5.22, 5.38, 5.54, 5.06, 5.66, 5.50, 6.33, 4.68, 
       5.45, 6.69, 5.60, 4.90, 5.18, 5.46, 5.47, 5.43, 
       5.58, 5.71)

# sample size
n <- 50
# sample mean
x_bar <- mean(x)
# sample standard deviation
s <- sd(x)
# standard error
se <- s / sqrt(n)
# critical value
cv <- qt(0.975, df = n-1)
# lower bound
x_bar - cv * se
# upper bound
x_bar + cv * se
```

We are 95% confident that the average student height in UCI is between 5.414 and 5.707 feet.

## Hypothesis Testing

### 1. Set hypotheses.

$H_0:\mu=6$ vs. $H_A:\mu\neq 6$

### 2. Identify Sampling Distribution of $H_0$.

By assuming the null hypothesis is true, we have:

$\bar x\sim approx\ N(mean=6, sd=\frac{s}{\sqrt n})$

### 3. Calculate p-value.

```{r}
# t statistic
t <- (x_bar - 6) / se
t
```

```{r}
# p-value
pt(t, df = n-1) * 2
```


### 4. Make a Decision and a Conclusion.

If the null hypothesis were true ($\mu=6$) then probability of observing a sample statistic that is at least as extreme as the one that we have observed ($\bar x=5.56$) would be very close to 0. We consider this to be an evidence against the null since p-value is much less than 0.05, even close to 0. 

## Exercise

Exercise 7.7 on page 293 of [the book](https://www.openintro.org/go/?id=os4_for_screen_readers&referrer=/book/os/index.php).
