---
title: "R code in Chapter 3"
author: "Andy P. Field"
date: 'Last updated `r format(Sys.Date(), "%d %B %Y")`'
output: html_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  warning = FALSE,
  message = FALSE
)

library(magrittr)
library(dplyr)
library(ggplot2)
```

***
## Usage

This file contains code relevant to chapter 1 of [Discovering Statistics Using R and RStudio](https://www.discovr.rocks) by [Andy Field](https://www.discoveringstatistics.com/about). These files contain abridged sections from the book so there are some copyright considerations but I offer them under a [Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License](http://creativecommons.org/licenses/by-nc-nd/4.0/).^[Basically you can use this material for teaching and non-profit activities but do not meddle with it or claim it as your own work.]

***

## Confidence intervals using `Hmisc` and `ggplot2`

```{r}

fb_tib <- tibble::tibble(
    friends = c(57, 40, 103, 234, 93, 53, 116, 98, 108, 121, 22)
  )

ggplot2::mean_cl_normal(fb_tib$friends)

```

Something other than a 95% interval:

```{r}
ggplot2::mean_cl_normal(fb_tib$friends, conf.int = 0.90)
```

Access individual values by using `$` to access the variable name (`y` for the estimate of the mean, `ymin` for the lower boundary of the CI and `ymax` for the upper boundary). Executing this gives us the lower boundary of the 95% confidence interval:

```{r}

ggplot2::mean_cl_normal(fb_tib$friends)$ymin
```

Use extracted value to create tables of summary statistics:

```{r}
# Tidyverse sumptuousness:

fb_tib %>%
  dplyr::summarize(
    Mean =  ggplot2::mean_cl_normal(friends)$y,
    `95% CI Lower` = ggplot2::mean_cl_normal(friends)$ymin,
    `95% CI Upper` = ggplot2::mean_cl_normal(friends)$ymax,
    ) %>% 
  round(., 2)
```
You can also combine these with other functions to get summary statistics:

```{r}
fb_tib %>%
  dplyr::summarize(
    Mean =  ggplot2::mean_cl_normal(friends)$y,
    `95% CI Lower` = ggplot2::mean_cl_normal(friends)$ymin,
    `95% CI Upper` = ggplot2::mean_cl_normal(friends)$ymax,
    IQR = IQR(friends),
    `Std. dev.` = sd(friends)
    ) %>% 
  round(., 2)

```


## Confidence intervals using `gmodels`

```{r}

gmodels::ci(fb_tib$friends)

```

Something other than a 95% interval:

```{r}
gmodels::ci(fb_tib$friends, confidence = 0.90)
```

Access individual values by appending their label in square brackets to the function. Executing this gives us the lower boundary of the 95% confidence interval:

```{r}

gmodels::ci(fb_tib$friends)["CI lower"]
```

Use extracted value to create tables of summary statistics:

```{r}
# Tidyverse sumptuousness:

fb_tib %>%
  dplyr::summarize(
    Mean =  gmodels::ci(friends)["Estimate"],
    `95% CI Lower` = gmodels::ci(friends)["CI lower"],
    `95% CI Upper` = gmodels::ci(friends)["CI upper"],
    ) %>% 
  round(., 2)
```
You can also combine these with other functions to get summary statistics:

```{r}
fb_tib %>%
  dplyr::summarize(
    Mean =  gmodels::ci(friends)["Estimate"],
    `95% CI Lower` = gmodels::ci(friends)["CI lower"],
    `95% CI Upper` = gmodels::ci(friends)["CI upper"],
    IQR = IQR(friends),
    `Std. dev.` = sd(friends)
    ) %>% 
  round(., 2)

```


