---
title: 'Chapter 6, section 1: Comparing two groups: quantitative response'
author: "Dr. Paul Pearson, Hope College"
date: "Fall 2024"
#output: slidy_presentation
#output: html_document
output: word_document
#output: 
#  pdf_document: 
#    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "", warning = FALSE, message = FALSE)

library(ggplot2) 
# ggsave(ggplot_object) can save svg files without xml annotations

library(BrailleR) 
# VI(ggplot_object) for annotations for in text graph annotations
# MakeAccessibleSVG(ggplot_object) to save accessible standalone html (=svg + xml annotations)

library(tactileR) 
# Use braille in graphs for embossed printing
# brl_begin(file='tactile_histogram_SWL.pdf', pt=14, paper='special', font='SWL')
# base or lattice plot code here
# brl_end()

library(infer)
#library(tibble)

library(tidyr) # for reshaping data, etc.

set.seed(1)
options(digits = 4)
```

# Chapter 6: Comparing two means

## Section 6.1: Comparing two groups: quantitative response

We will compare groups of data on a quantitative variable. We still want to reveal patterns exhibited by the sample data including shape, center, and variability of the distributions of quantitative data. We will also investigate outliers.

# Example 6.1: Geyser eruptions

Here is a pair of dotplots for the times between eruptions (in minutes) of the Old Faithful geyser in Yellowstone National Park.  There are 107 eruptions occurring in August 1978 and95 eruptions from the same week in 2003.   Do the distributions of these inter-eruption times for these two years appear to have changed? 

<!--
Please paste the [data for Old Faithful Inter-eruption times grouped by year](http://www.isi-stats.com/isi/data/chap6/OldFaithful.txt) into the [Descriptive Statistics applet.]()
-->


```{r, echo=FALSE, fig.cap="Figure: Dot plot titled 'Old Faithful Geyser Inter-Eruption Times Grouped by Year'. The x-axis is 'inter-eruption time in minutes' with values between 40 and 110 minutes. The y-axis has two rows for the groups 'year 1978' in the bottom row and 'year 2003' in the top row.  Each row is its own dot plot.  The 'year 1978' dotplot has most of its dots between 45 and 90 minutes. The 'year 2003' dotplot has most of its dots between 80 and 105 minutes."}

geyser <- read.csv("OldFaithful.txt", sep="\t")
geyser$year <- as.character(geyser$year) # for dotplot: convert from int to chr so geom_dotplot() works
#str(geyser)

#mean_by_year <- c(
#  mean(subset(geyser, year == "1978")$time), 
#  mean(subset(geyser, year == "2003")$time)
#  )

dotPlot <- ggplot(geyser, aes(x=time, y=year)) + 
  geom_dotplot(binpositions = 'all', binwidth = 1, stackgroups = FALSE) +
  ggtitle("Old Faithful Geyser Inter-Eruption Time by Year") +
  ylab("Year") + xlab("Inter-eruption time (minutes)") +
  #annotate("text", x = mean_by_sex+c(-3,-3,3), y = c(1,1,1), label = c("Male mean","Female mean", "Both mean"), angle=90) +
  #geom_vline(xintercept = mean_by_sex, linewidth = 1) +
  theme_bw(base_size = 18)

dotPlot
```

After this paragraph is a frequency table that conveys exactly the same information as the dotplot.  The top row of the table has time values between 42 and 100 minutes. The middle row has count data for the year 1978 eruptions. The bottom row has count data for the year 2003 eruptions.

```{r, echo=FALSE}
table(geyser)
```


```{r, include=FALSE}
brl_begin('tactile-old-faithful-intereruption-times-grouped-by-year-dotplot.pdf', 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
stripchart(geyser$time ~ geyser$year,
           group.names = c("1978","2003"),
           method = "stack",
           main = "Inter-Eruption Times by Year",
           xlab = "Inter-eruption time (minutes)",
           ylab = "Year",
           pch = 20, # use dots instead of squares
           #pch = 15, # use filled squares
           cex = 1, # scale size of filled squares
           at = c(0,1), # aligns dots along axis
           xlim = c(min(geyser$time),max(geyser$time)),
           ylim = c(0,2),
           offset = 0.35 # vertical separation of dots
)
#axis(1, at = seq(60, 160, by = 5))
#abline(v = mean(subset(superbowl, sex == "Male")$song_length), col = 1, pch = 7, cex = 2, lwd = 2)
#abline(v = mean(subset(superbowl, sex == "Female")$song_length), col = 1, pch = 7, cex = 2, lwd = 2)
brl_end()
```


# Order statistics

Quantitative data (think a list of numbers) can be ordered from smallest to largest. Using this ordered list, we can find the smallest or largest numbers in the list called the minimum and maximum, a number called the median that splits the list into two equal length parts (50% of the list below it and 50% above it), and numbers called lower and upper quartiles that split the the list into four equal length parts (25% below or 75% below). In fact, we can define any percentile we like, such as the 90th percentile is a number that has 90% of the list items below it.

## Five number summary 

The five-number summary for the distribution of a quantitative variable consists of the minimum, lower quartile, median, upper quartile, and maximum.

1. The min in a data set is the smallest number in the data set.
2. The lower quartile (or 25th percentile) is the number in a data set at which 25% of the data lie below it. 
3. The median (or 50th percentile) is a number in a data set at which 50% of the data lie below it when the list has odd length. When the list has even length, the median is the average of the two data points in the middle of the ordered list and is often not a number in the data set. 
4. The upper quartile (or 75th percentile) is the number in a data set at which 25% of the data lie above it. 
5. The max in a data set is the largest number in the data set.

## Remarks

- Lower and upper quartiles can be found by finding the overall median and then determining the median of the values below or above it. 
- The inter-quartile range (IQR) is the difference between the upper and lower quartiles, it is another measure of variability like standard deviation. IQR is less sensitive (more resistant) to extreme values and skewness than standard deviation.
- A box plot is a visual display of the five-number summary.
    - It is also called a “box-and-whiskers” plot
    - The box displays the middle 50% of the distribution (between the lower and upper quartiles) and 
    - Its width (which is the IQR) helps us see the variability shown as the spread of most numbers in the list
- An outlier is more than 1.5 * IQR from the edge of the box
    - The whiskers extend to 1.5 * IQR from the edge of the box
    - Outliers are shown as dots that lie outside the range defined by the whiskers

# Five number summary, dotplot, and boxplot

Please [click here to open the Descriptive Statistics applet](https://www.rossmanchance.com/applets/2021/descstats/Dotplot.htm?hideExtras=1) and follow the instructions below to load the Old Faithful data to compute the five number summary, and to create dotplots and boxplots that display and summarize the data.

![Old Faithful Geyser data put into the Descriptive Statistics applet to find a five number summary, a dotplot of the data, and a boxplot of the data.](screenshot-old-faithful-dotplot-boxplot.png)

## Input to the Descriptive Statistics applet

- Select data "Old Faithful"
- Press the "Use data" button
- Verify that quantitative variable is set to "time"
- Select Separate by "year"
- Under choose statistics, next to median select the checkbox "actual"
- Under choose statistics, next to IQR select the checkbox "actual"
- Select the checkbox for "Add Boxplot"
- Select the checkbox for "Show outliers"

## Output from the Descriptive Statistics applet

- Sample size: n = 95 for year 2003, n = 107 for year 1978
- Medians: 91 minutes for year 2003, 75 minutes for year 1978
- Inter-Quartile Ranges (IQRs): 98 - 87 = 11 minutes for year 2003, 81 - 58 = 23 minutes for year 1978
- A dotplot and boxplot for year 2003 in the top half of the figure
- A dotplot and boxplot for year 1978 in the top half of the figure


<!--

```{r, echo=FALSE}
# https://www.tutorialspoint.com/how-to-create-boxplot-of-vectors-having-different-lengths-in-r
# boxplot( values ~ group, data=dataframe)
boxplot(time ~ year, 
        data=geyser, 
        horizontal = TRUE,
        main = "Inter-Eruption Times by Year",
        xlab = "Inter-eruption time (minutes)",
        ylab = "Year",
        boxwex = 0.5, # Box width
        boxlty = 1,   # Box line type
        boxlwd = 3,   # Box line width
        whisklty = 1,       # Line type of the whiskers
        whisklwd = 3,       # Line width of the whiskers
        staplelty = 1,      # Line type of the staples
        staplelwd = 3       # Line width of the staples        
)
```
-->

```{r, include=FALSE}
brl_begin('tactile-old-faithful-intereruption-times-grouped-by-year-boxplot.pdf', 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
boxplot(time ~ year, 
        data=geyser, 
        horizontal = TRUE,
        main = "Inter-Eruption Times by Year",
        xlab = "Inter-eruption time (minutes)",
        ylab = "Year",
        boxwex = 0.5, # Box width
        boxlty = 1,   # Box line type
        boxlwd = 3,   # Box line width
        whisklty = 1,       # Line type of the whiskers
        whisklwd = 3,       # Line width of the whiskers
        staplelty = 1,      # Line type of the staples
        staplelwd = 3       # Line width of the staples        
)
  brl_end()
```


# Exploration 6.1: Cancer pamphlets

Researchers in Philadelphia investigated whether pamphlets containing information for cancer patients are written at a level that the cancer patients can comprehend (Short, Moriarty, and Cooley, 1995). 
First, they measured the readability of a sample of cancer pamphlets based on factors such as the length of sentences and the number of polysyllabic words, assigning each pamphlet a grade level. 

The results shown in the table below are presented as a frequency table, reporting the number of pamphlets at each grade level.

| Cancer Pamphlets Frequency table     |   |   |   |   |    |    |    |    |    |    |    |
|:-------------------------------------|:--|:--|:--|:--|:---|:---|:---|:---|:---|:---|:---|
| Pamphlets' readability (grade) level | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| Count (number of pamphlets)          | 3 | 3 | 8 | 4 | 1  | 1  | 4  | 2  | 1  | 2  | 1  |

Let's work the exploration together in small groups. Please 

- [share this google document with your neighbors](https://docs.google.com/document/d/1q7HxdcmJUoJulTJNO3OwFExrHeMd6qEbYGh1Uk6LdVU/copy)
- [share this google sheet with your neighbors](https://docs.google.com/spreadsheets/d/1VB0QolrO57Sla2MYgZh7kl_KVPXl8J3HAyWNsvHtZFI/copy)

and get started.

