---
title: "Mean Inference"
author: "David Gerbing"
output: 
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteIndexEntry{mean inference}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r}
library("lessR")
```

First read the Employee data included as part of __lessR__.

```{r read}
d <- Read("Employee")
```


## One-Sample t-test

Obtain the summary statistics and 95% confidence interval for a single variable by specifying that variable with `ttest()`.

```{r}
ttest(Salary)
```

Add a hypothesis test to the above.

```{r, fig.height=3.75, fig.width=5}
ttest(Salary, mu=52000)
```

Analysis of the above from summary statistics only.
 
```{r}
ttest(n=37, m=73795.557, s=21799.533, Ynm="Salary", mu=52000)
```



## Two-Samples t-test


### Independent groups

Full analysis with `ttest()` function, abbreviated as `tt()`, with formula mode.

```{r, fig.height=4.25, fig.width=5}
ttest(Salary ~ Gender)
```

Brief version of the output contains just the basics.

```{r, fig.height=4.25, fig.width=5}
tt_brief(Salary ~ Gender)
```



### Dependent groups

```{r, fig.height=4.25, fig.width=5}
tt_brief(Pre, Post)
```


## ANOVA


Analysis of variance applies to the inferential analysis of means across groups.  The __lessR__ function `ANOVA()`, abbreviated `av()`, provides this analysis, based on the base R function `aov()`.

The data for these examples is the _warpbreaks_ data set included with the R __datasets__ package. The data are from a weaving device called a loom for a fixed length of yarn. The response variable is the number of times the yarn broke during the weaving. Independent variables are the type of wool -- A or B --and the level of tension -- L, M, or H.

Because _warpbreaks_ is not the default data frame, specify with the `data` parameter (or set _d_ equal to _warpbreaks_). 


### One-way independent groups

First, for illustrative purposes, ignore the type of wool and only examine the impact of tension on breaks.

The output includes descriptive statistics, ANOVA table, effect size indices, Tukey's multiple comparisons of means, and residuals, as well as the scatterplot of the response variable with the levels of the independent variable, and a visualization of the mean comparisons.

```{r}
ANOVA(breaks ~ tension, data=warpbreaks)
```

The brief version forgoes the multiple comparisons and the residuals.

```{r}
av_brief(breaks ~ tension, data=warpbreaks)
```



### Two-way independent groups

Specify the second independent variable preceded by a `*` sign.

```{r}
ANOVA(breaks ~ tension * wool, data=warpbreaks)

```


### Randomized block design


```{r}
d <- read.csv(header=TRUE, text="
Person,sup1,sup2,sup3,sup4
p1,2,4,4,3
p2,2,5,4,6
p3,8,6,7,9
p4,4,3,5,7
p5,2,1,2,3
p6,5,5,6,8
p7,2,3,2,4")
```

Reshape data from wide form to long form with base R `reshape()` according to the following parameters. (R refers to time, which is only one specific application.)

- `idvar`: Identify the blocking variable in the wide form data
- `varying`: Identify the variables in wide format gathered into a single variable in long format
- `v.names`: Name the response variable in the long form
- `timevar`: Name the corresponding long form variable
- `times`: Name the values of the corresponding long form variable, otherwise numbered consecutively

Do not need the row names.

```{r}
d <- reshape(d, direction="long",
        idvar="Person", v.names="Reps",
        varying=list(2:5), timevar="Supplement", times=names(d)[2:5])
row.names(d) <- NULL
```


```{r}
d[1:10,]
```

Specify the blocking variable preceded by a `+` sign.

```{r}
ANOVA(Reps ~ Supplement + Person)
```


## Full Manual

Use the base R `help()` function to view the full manual for `ttest()` or `ANOVA()`. Simply enter a question mark followed by the name of the function.

```
?ttest
?ANOVA
```

