---
title: "Difference Between Means with Paired Data"
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)
library(flextable)
library(dplyr)
library(tidyr)
```

```{r,echo=FALSE,message=FALSE}
x<-params$result
 # x=c(95,89,76,92,91,53,67,88,75,85,90,85,87,85,85,68,81,84,71,46,75,80)
 # y=c(90,85,73,90,90,53,68,90,78,89,95,83,83,83,82,65,79,83,60,47,77,83)
 # x=meanCI(x=x,y=y,paired=TRUE,alpha=0.1)
# x=meanCI(iris,paired=TRUE)

xresult=x$result[1,]
if(!is.null(attr(x,"form"))){
     vars=c(xresult$control,xresult$test)
     x$data %>% 
          dplyr::filter(name %in% vars) %>%
          pivot_wider(id="id") %>%
          select(all_of(vars)) -> wideData
     names(wideData)=c("x","y")
     wideData %>% mutate(d=x-y,sum=(d-mean(d))^2) ->wideData
     
} else{
     wideData=x$data
}

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

```{glue,results="asis",echo=FALSE}
**From a population of {xresult$n*100} students,{xresult$n} students were randomly selected. The sampling method was simple random sampling. All of the students were given a standardized English test and a standardized math test. Test results are summarized below.**
```
```{r,echo=FALSE}
cat("English")
wideData[[1]]
cat("math")
wideData[[2]]
```
```{glue,results="asis",echo=FALSE}
**Find the *{(1-xresult$alpha)*100}% confidence interval* for the mean difference between student scores on the math and English tests. Assume that the mean differences are approximately normally distributed.**
```

$$\sum(d-\bar{d})^2=`r sum(wideData$sum)`$$
$$\bar{d}=`r xresult$md`$$

## Solution

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**. This condition is satisfied; the problem statement says that we used simple random sampling.

- The **sampling distribution** should be approximately normally distributed. The problem statement says that the differences were normally distributed; so this condition is satisfied.


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

### Identify a sample statistic. 

#### Standard deviation($s_d$)

To solve the problem, we have to calculate standard deviation of the differences($s_d$) computed from  differences in English and math score from `r xresult$n` matched pairs. 

```{r}
df=wideData[1:10,]
names(df)[4]="(d-mean(d)^2"
flextable(df) %>% autofit()
```

$$s_d=\sqrt{\frac{\sum{(d_i-\bar{d})^2}}{n-1}}$$
 
 $$s_d=\sqrt{\frac{`r sum(wideData$sum)`}{`r xresult$n`-1}}=`r round(xresult$sd,2)`$$
 
where $d_i$ is the difference for pair i, $\bar{d}$ is the sample mean of the differences, and $n$ is the number of paired values.



#### standard error(SE)

Standard error. Compute the standard error (SE) of the sampling distribution of d.

$$SE = s_d \times \sqrt{ ( 1/n )\times [ (N - n) / ( N - 1 ) ] }$$

where $s_d$ is the standard deviation of the sample difference, $N$ is the number of matched pairs in the population, and $n$ is the number of matched pairs in the sample. When the population size is much larger (at least 20 times larger) than the sample size, the standard error can be approximated by:

$$SE = \frac{s_d}{\sqrt{n}}=\frac{`r round(xresult$sd,2)`}{\sqrt{`r xresult$n`}}=`r round(xresult$se,2)`$$

#### Select a confidence level. 

In this analysis, the confidence level is defined for us in the problem. We are working with a `r (1-xresult$alpha)*100`% confidence level. The critical probability(p*) is:

```{r,echo=FALSE}
if(xresult$alternative=="two.sided"){
    string=glue("$$p*=1-\\alpha/2=1-{xresult$alpha}/2={1- xresult$alpha/2}$$")
} else{
     string=glue("$$p*=1-\\alpha=1-{xresult$alpha}$$")
}
```
`r string`

#### Degrees of freedom(DF)

$$DF=n-1=`r xresult$n`-1=`r xresult$DF`$$

#### Find critical value

The critical value is the $t$ statistic having `r xresult$DF` degrees of freedom and a cumulative probability equal to 0.95. From the t Distribution table, we find that the critical value is `r round(xresult$critical,3)`.

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

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


#### Compute margin of error(ME): 

$$ME =critical\ value\times standard\ error$$

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


### Confidence level

```{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,side=FALSE)
```

### 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/mean-difference-pairs.aspx?tutorial=AP URL[Accessed Data: 1/23/2022]. 
