---
title: "Confidence interval for a mean"
output: html_document
params:
   result: "meanCI(mtcars$mpg)"
---

```{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(mtcars$mpg)

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

twoS="The null hypothesis will be rejected if the sample mean is too big or if it is too small."
lessS="The null hypothesis will be rejected if the sample mean is too small."
greaterS="The null hypothesis will be rejected if the sample mean is too big."
```

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("An inventor has developed a new, energy-efficient lawn mower engine. From his stock of  {xresult$n*100} engines, the inventor selects a simple random sample of {xresult$n} engines for testing. The engines run for an average of {round(xresult$m,2)} minutes on a single gallon of regular gasoline, with a standard deviation of {round(xresult$s,2)} minutes. What is the {(1-xresult$alpha)*100}% confidence interval for the average minutes? (Assume that run times for the population of engines are 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 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 first 10 rows of the provided data is as follows.")`

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

### Sample statistics

The sample size is `r xresult$n`, the sample mean is `r round(xresult$m,2)` and the standard error of sample is `r round(xresult$s,2)`. The confidence level is `r (1-xresult$alpha)*100` %.

### Find the margin of error

Since we do not know the standard deviation of the population, we cannot compute the standard deviation of the sample mean; instead, we compute the standard error (SE). Because the sample size is much smaller than the population size, we can use the "approximate" formula for the standard error.


$$ SE= \frac{s}{\sqrt{n}}$$
where **s** is the standard deviation of the sample, **n** is the sample size.


$$SE=\frac{`r round(xresult$s,2)`}{\sqrt{`r xresult$n`}}=`r round(xresult$se,2)`$$
Find the critical probability(p*):

```{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`

The **degree of freedom**(df) is:
$$df=n-1=`r xresult$n`-1=`r xresult$DF`$$


The **critical value** is the t statistic having `r xresult$DF` degrees of freedom and a cumulative probability equal to `r ifelse(xresult$alternative=="two.sided",1- xresult$alpha/2,1- xresult$alpha)`. 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,results='asis',echo=FALSE}
if(xresult$alternative=="two.sided"){
  string=glue("$$qt(p,df)=qt({1- xresult$alpha/2},{xresult$DF})={round(xresult$critical,3)}$$")
} else {
    string=glue("$$qt(p,df)=qt({1- xresult$alpha},{xresult$DF})={round(xresult$critical,3)}$$") 
} 
```

`r string`

The graph shows the $\alpha$ values are the tail areas of the distribution. 

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

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)`$$

```{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)."
}
```

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

### Confidence interval of the mean
 

```{glue,results='asis',echo=FALSE}
Therefore, the {(1-xresult$alpha)*100}% confidence interval is {round(xresult$lower,2)} to {round(xresult$upper,2)}. That is, we are {(1-xresult$alpha)*100}% confident that the true population mean is in the range {round(xresult$lower,2)} to {round(xresult$upper,2)}.
```


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