---
title: "Difference Between Means"
output: html_document
params:
   result: !r meanCI(n1=500,n2=1000,m1=20,s1=3,m2=15,s2=2,alpha=0.01)
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,comment=NA)
library(interpretCI)
library(glue)
```

```{r,echo=FALSE,message=FALSE}
x<-params$result
#x=meanCI(n1=500,n2=1000,m1=20,s1=3,m2=15,s2=2,alpha=0.01,alternative="greater")
#x=meanCI(acs,Dx,age)
xresult=x$result[1,]
two.sided<-greater<-less<-FALSE
if(xresult$alternative=="two.sided") two.sided=TRUE
if(xresult$alternative=="less") less=TRUE
if(xresult$alternative=="greater") greater=TRUE
```


This document is prepared automatically using the following R command.

```{r,echo=FALSE}
call=paste0(deparse(x$call),collapse="")
x1=paste0("library(interpretCI)\nx=",call,"\ninterpret(x)")
textBox(x1,italic=TRUE,bg="grey95",lcolor="grey50")
```


## Problem

```{r,echo=FALSE}
string=glue("The local baseball team conducts a study to find the amount spent on refreshments at the ball park. Over the course of the season they gather simple random samples of {xresult$n1} men and {xresult$n2} women. For men, the average expenditure was $ {round(xresult$m1,2)}, with a standard deviation of $ {round(xresult$s1,2)}. For women, it was ${round(xresult$m2,2)}, with a standard deviation of ${round(xresult$s2,2)}.

What is the {(1-xresult$alpha)*100}% confidence interval for the spending difference between men and women? Assume that the two populations are independent and normally distributed.")

textBox(string)
```
## Confidence interval of mean

The approach that we used to solve this problem is valid when the following conditions are met.

- The sampling method must be **simple random sampling**. 

- The samples must be **independent**.

- The **sampling distribution** should be approximately normally distributed. 

Since the above requirements are satisfied, we can use the following four-step approach to construct a confidence interval of mean.

### Raw data

`r ifelse(is.na(x$data[[1]][1]),"Raw data is not provided.","The raw data are as follows.")`

```{r,echo=FALSE}
if(!is.na(x$data[[1]][1])) {
     x$data
}
```

### Identify sample statistics

Since we are trying to estimate the difference between population means, we choose the difference between sample means as the sample statistic. Thus, 

$$\bar{x1}-\bar{x2}=`r round(xresult$m1,2)`-`r round(xresult$m2)`=`r round(xresult$md,)`$$

### Select a confidence level. 

In this analysis, the confidence level is defined for us in the problem. We are working with a 99% confidence level.


### Find the margin of error


#### 1. Find standard error. 

The standard error is an estimate of the standard deviation of the difference between population means. We use the sample standard deviations to estimate the standard error (SE).

$$SE=\sqrt{\frac{s^2_1}{n_1}+\frac{s^2_2}{n_2}}$$
$$SE=\sqrt{\frac{`r round(xresult$s1,2)`^2}{`r xresult$n1`}+\frac{`r round(xresult$s2,2)`^2}{`r xresult$n2`}}$$
$$SE=`r round(xresult$se,3)`$$

#### 2. Find the degree of freedom(df)

 $$DF=\frac{(\frac{s_1^2}{n_1}+\frac{s_2^2}{n_2})^2}{\frac{(s_1^2/n_1)^2}{n_1-1}+\frac{(s_2^2/n_2)^2}{n_2-1}}$$
$$DF=\frac{(\frac{`r round(xresult$s1,2)`^2}{`r xresult$n1`}+\frac{`r round(xresult$s2,2)`^2}{`r xresult$n2`})^2}{\frac{(`r round(xresult$s1,2)`^2/`r xresult$n1`)^2}{`r xresult$n1`-1}+\frac{(`r round(xresult$s2,2)`^2/`r xresult$n2`)^2}{`r xresult$n2`-1}}$$


$$DF=`r round(xresult$DF,2)`$$

#### 3. Find the critical value

The **critical value** is the t statistic having `r round(xresult$DF,2)` degrees of freedom and a cumulative probability equal to `r 1- xresult$alpha/ifelse(two.sided,2,1)`. From the t Distribution table, we find that the critical value is `r round(xresult$critical,3)`.

```{r,echo=FALSE}
show_t_table(DF=round(xresult$DF,2),p=xresult$alpha,alternative=xresult$alternative)
```


$$ qt(p,df)=qt(`r 1- xresult$alpha/ifelse(two.sided,2,1)`,`r round(xresult$DF,2)`)=`r round(xresult$critical,3)`$$
The graph shows that $\alpha$ values are the tail areas of the distribution. 

```{r,echo=FALSE}
draw_t(DF=round(xresult$DF,2),p=xresult$alpha,alternative=xresult$alternative)
```

#### 4. Compute margin of error(ME)

$$ME=critical\ value \times SE$$
$$ME=`r round(xresult$critical,3)` \times `r round(xresult$se,3)`=`r round(xresult$ME,3)`$$

#### 5. Specify confidence interval 

```{r,results='asis',echo=FALSE}
if(two.sided) {
     string="The range of the confidence interval is defined by the sample statistic $\\pm$margin of error."
} else if(less){
     string="The range of the confidence interval is defined by the -$\\infty$(infinite) and the sample statistic + margin of error."
} else{
     string="The range of the confidence interval is defined by the sample statistic - margin of error and the $\\infty$(infinite)."
}
```

`r string` And the uncertainty is denoted by the confidence level.


### Confidence interval of the mean difference
 
```{glue,results="asis",echo=FALSE}
Therefore, the {(1-xresult$alpha)*100}% confidence interval is {round(xresult$lower,2)} to {round(xresult$upper,2)}. Here's how to interpret this confidence interval. Suppose we repeated this study with different random samples for men and women. Based on the confidence interval, we would expect the observed difference in sample means to be between {round(xresult$lower,2)} and {round(xresult$upper,2)} {(1-xresult$alpha)*100}% of the time.
```

### Plot

You can visualize the mean difference:

```{r}
plot(x)
```

### Result of meanCI()

```{r,echo=FALSE}
print(x)
```

### Reference

The contents of this document are modified from StatTrek.com.
Berman H.B., "AP Statistics Tutorial", [online] Available at:  https://stattrek.com/estimation/difference-in-means.aspx?tutorial=AP URL[Accessed Data: 1/23/2022]. 

