---
title: "Describing Data worksheet"
subtitle: "MC 501 · Chapter 12 · Part 2 of 3 of the Twitch job"
format:
  html:
    embed-resources: true
    toc: true
execute:
  echo: true
---

## Where this file goes

Save this worksheet in the top folder of your course workspace, the folder open in VS Code, beside `README.md`, not inside `00_Inbox` or any other folder. Keep `twitch_analysis.RDS` in the folder named `data` beside it, or render stops with `cannot open the connection`: [Put a Worksheet in Your Workspace](https://aura-lab.siue.edu/v2v-hub/guides/mc501/14-put-a-worksheet-in-your-workspace.html).

## The case

You are the data analyst on a small study. Your client is the channel manager at a small creator
agency that represents about a dozen Twitch streamers. The agency is about to pitch a sponsor on
how engaged its streamers' chats are. Before the pitch, the manager wants three questions
answered with data:

1. Does chat in gaming channels look different from chat in non-gaming channels?
2. Does a bigger audience change how people write?
3. How much of chat is reaction (an emote, a cheer, a single word) and how much is commentary?

**Where the job stands.** In Data Wrangling you built the table, `data/twitch_analysis.RDS`,
with your thirty hand-coded messages inside it. The manager has not seen a number from it yet.

**Your job.** Show the manager what is in the table before anything is tested: how often each
category occurs, how long messages run, how two categories line up, and what the patterns look
like in a chart. The manager does not read R, so every **Write:** prompt is a note to them.

## Your assignment

| Part | What you do | Why the client needs it |
|---|---|---|
| 1 | Frequency tables: count how often each category occurs | The manager sees how much of chat is gaming, large-audience, and reaction |
| 2 | The three numbers: the mean, median, and standard deviation of message length | "How people write" becomes numbers the manager can compare |
| 3 | The cross-tabulation: count viewer band against channel type | The manager sees whether audience size and channel type go together |
| 4 | Three figures, each with alt text, a disclosure sentence, and an interpretation | A chart is what the manager can put in front of a sponsor |
| 5 | Diagnostics behind each figure | A reviewer can check that each figure supports what you say about it |
| Last | Explain this stage to the client, in one short paragraph | The manager gets what you found in plain language |

**This is the MC 501 worksheet,** so Part 5 is part of your assignment.

The code sits in gray boxes called chunks, as in Data Wrangling. Work through them in order:

1. Replace each blank, shown as `___`, with the code it needs.
2. Click **Run Cell** at the top left of the gray box.
3. Compare what R prints with **What you should see**.

Where the worksheet says **Write:**, type your answer in your own words. Every task has a
**Help** link. It opens the page that explains that task: what the blank needs, where to click,
and what to do when R prints an error.

## What to submit

**100 points.** Due Monday, November 23, 11:59 PM, in Blackboard, under **Describing Data in R**.

You turn in an R assignment as one ZIP file: your whole course workspace, the folder you have open in VS Code, packed into a single file. [How to make the ZIP](https://aura-lab.siue.edu/v2v-hub/guides/mc501/11-render-a-quarto-document.html#submit-your-work-as-one-zip-file). The ZIP must hold:

- this worksheet, with every blank and every **Write:** filled
- the HTML file with the same name, which you make in **Before you submit**
- your [AI-use log](https://aura-lab.siue.edu/v2v-hub/resources/AI-use-log.docx)

Rubric: [Describing Data in R rubric](https://aura-lab.siue.edu/v2v-hub/resources/rubrics/MC501-rubric-describing-data-in-r.pdf).

## Setup

```{r}
#| label: setup
library(tidyverse)     # tables and charts
library(v2v)           # theme_v2v() and the course palette
options(scipen = 10)  # long numbers such as timestamps print in full; p-values keep the usual form

analysis <- read_rds("data/twitch_analysis.RDS")  # the table worksheet 1 saved
dir.create("figures", showWarnings = FALSE)        # where every chart is saved

analysis %>%
  count(reaction_only)  # your hand-coded variable made it through
```

**What you should see:** your two coded groups adding to `30`, and `NA` with `35237`.
[Help with Setup](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#setup)

## Part 1: Frequency tables

### Task 1.1: Channel type

A frequency table says how often each category occurs. This one is done for you as the model.

```{r}
#| label: freq-channel-type
analysis %>%
  count(channel_type) %>%                  # messages per category
  mutate(percent = round(100 * n / sum(n), 1))  # and as a share of all messages
```

**What you should see:** `gaming` `31309` (88.8%), `non-gaming` `3457` (9.8%), and `NA` `501`
(1.4%).
[Help with Task 1.1](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-1-1)

### Task 1.2: Viewer band

Build the same table for `viewer_band`.

```{r}
#| label: freq-viewer-band
analysis %>%
  ___(___) %>%
  mutate(percent = round(100 * n / sum(n), 1))
```

**What you should see:** `small` `27766` (78.7%), `medium` `5501` (15.6%), `large` `2000` (5.7%).
[Help with Task 1.2](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-1-2)

### Task 1.3: Your hand-coded variable

Only thirty messages carry a code. Keep those rows first, or the table is mostly `NA`.

```{r}
#| label: freq-reaction
analysis %>%
  filter(!is.na(___)) %>%  # only the thirty coded messages
  count(reaction_only) %>%
  mutate(percent = round(100 * n / sum(n), 1))
```

**What you should see:** two rows adding to `30`.
[Help with Task 1.3](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-1-3)

**Write:** two to three sentences for the client on the three tables: what they show, and what
they do not. Name the numbers.

*Your answer:*

## Part 2: The three numbers

### Task 2.1: Message length, all messages

```{r}
#| label: length-all
analysis %>%
  summarize(
    n      = sum(!is.na(message_length)),
    mean   = mean(message_length, na.rm = TRUE),
    median = median(message_length, na.rm = TRUE),
    sd     = sd(message_length, na.rm = TRUE)
  )
```

**What you should see:** `n` `35263`, `mean` `29.0`, `median` `17`, `sd` `41.2`.
[Help with Task 2.1](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-2-1)

### Task 2.2: By channel type, and by your code

Fill the grouping variable, then the filter that keeps only your coded messages.

```{r}
#| label: length-by-group
analysis %>%
  filter(!is.na(channel_type)) %>%
  ___(___) %>%
  summarize(
    n      = n(),
    mean   = mean(message_length, na.rm = TRUE),
    median = median(message_length, na.rm = TRUE),
    sd     = sd(message_length, na.rm = TRUE)
  )

analysis %>%
  filter(!is.na(___)) %>%
  group_by(___) %>%
  summarize(
    n      = n(),
    mean   = mean(message_length),
    median = median(message_length),
    sd     = sd(message_length),
    max    = max(message_length)
  )
```

**What you should see:** `gaming` n `31309`, mean `28.5`, median `17`, sd `38.5`; `non-gaming`
n `3457`, mean `33.7`, median `16`, sd `60.7`. Then one row for each of your two coded groups.
[Help with Task 2.2](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-2-2)

**Write:** two to three sentences for the client. Where do the mean and the median disagree, and
what does the gap tell you about the shape of message length?

*Your answer:*

## Part 3: The cross-tabulation

### Task 3.1: Viewer band by channel type

```{r}
#| label: crosstab
analysis %>%
  filter(!is.na(channel_type)) %>%
  count(___, ___) %>%                   # viewer band as the rows, channel type as the columns
  pivot_wider(names_from = ___, values_from = n, values_fill = 0)   # one column per channel type
```

**What you should see:** three rows of counts. `small`: `25309` gaming, `2457` non-gaming.
`medium`: `4000` and `1000`. `large`: `2000` and `0`.
[Help with Task 3.1](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-3-1)

### Task 3.2: As proportions of each band

The same counts as shares of each band.

```{r}
#| label: crosstab-shares
analysis %>%
  filter(!is.na(channel_type)) %>%
  count(viewer_band, channel_type) %>%
  ___(viewer_band) %>%                           # shares within each band, not across the table
  mutate(share = round(n / sum(n), 3))
```

**What you should see:** non-gaming is `0.088` of the small band and `0.2` of the medium band.
The large band has no non-gaming row.
[Help with Task 3.2](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-3-2)

**Write:** two to three sentences for the client on the cross-tabulation. What does it show, and
what can it not show yet?

*Your answer:*

## Part 4: Figures

Each figure is built, saved to `figures/` by the code that makes it, and carries alt text you
write on its `#| fig-alt:` line. After each one, **Write:** one sentence disclosing a choice you
made in it (a cap on an axis, which categories you kept, a bin width), and two to three
sentences of interpretation for the client.

### Task 4.1: Channel type within each viewer band

A proportion bar chart of the cross-tabulation. Fill the fill aesthetic and the position that
makes every bar the same height.

```{r}
#| label: fig-band-type
#| fig-alt: "YOUR TURN: one sentence saying what this chart shows."
fig_band_type <- analysis %>%
  filter(!is.na(channel_type)) %>%
  ggplot(aes(x = viewer_band, fill = ___)) +
  geom_bar(position = "___") +                        # every bar scaled to 100%
  scale_y_continuous(labels = scales::percent_format()) +
  scale_fill_v2v() +
  labs(title   = "Channel type within each viewer band",
       x       = "Viewer band (channel's median viewers)",
       y       = "Share of messages",
       fill    = "Channel type",
       caption = "Source: v2v Twitch corpus, November 18 to 24, 2018. 501 messages with no channel type left out.") +
  theme_v2v()

fig_band_type
ggsave("figures/band-by-type.png", fig_band_type, width = 7, height = 4.5, dpi = 300)
```

**What you should see:** three bars of equal height, and `band-by-type.png` in `figures/`.
[Help with Task 4.1](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-4-1)

**Write:** the disclosure sentence, then two to three sentences of interpretation.

*Your answer:*

### Task 4.2: Message length by your code

The thirty coded messages, as a boxplot. Fill the variable on the x axis and the geom.

```{r}
#| label: fig-length-reaction
#| fig-alt: "YOUR TURN: one sentence saying what this chart shows."
fig_length_reaction <- analysis %>%
  filter(!is.na(reaction_only)) %>%
  ggplot(aes(x = ___, y = message_length)) +
  ___(outlier.shape = NA) +   # no outlier dots here; the points below show every message
  geom_jitter(width = 0.15, alpha = 0.6) +   # every one of the thirty, shown as a point
  labs(title   = "Message length by message type",
       x       = "Hand-coded message type",
       y       = "Characters",
       caption = "Source: 30 hand-coded messages, 15 each from bobross and giantwaffle.") +
  theme_v2v()

fig_length_reaction
ggsave("figures/length-by-reaction.png", fig_length_reaction, width = 6, height = 4.5, dpi = 300)
```

**What you should see:** two boxes, and `length-by-reaction.png` in `figures/`.
[Help with Task 4.2](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-4-2)

**Write:** the disclosure sentence, then two to three sentences of interpretation.

*Your answer:*

### Task 4.3: A figure of your own choice

Choose the variable and the chart, and let the R Companion write it. Then fill in its title, axis
labels, and caption, and keep the `fig_own <-` and `ggsave()` lines so the figure is saved.
[Help with Task 4.3](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-4-3)

```{r}
#| label: fig-own
#| fig-alt: "YOUR TURN: one sentence saying what this chart shows."
fig_own <- analysis %>%
  ___  # YOUR TURN: the chart the menu writes, with its labs() filled in and + theme_v2v()

fig_own
ggsave("figures/own-choice.png", fig_own, width = 7, height = 4.5, dpi = 300)
```

**Write:** one sentence on why you chose this chart for this variable, the disclosure sentence,
and two to three sentences of interpretation.

*Your answer:*

## Part 5: Diagnostics behind each figure (MC 501)

For each figure, report the shape of the variable, whether the assumptions of the test it leads
to look reasonable, and where the outliers are, with the check shown.

### Task 5.1: The counts the chi-square will expect

```{r}
#| label: diag-expected
expected <- chisq.test(xtabs(~ ___ + ___, data = analysis))$expected   # the two categories of Task 3.1, in the same order
round(expected, 1)
```

**What you should see:** six numbers, the smallest `198.9` (large, non-gaming).
[Help with Task 5.1](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-5-1)

### Task 5.2: Spread and outliers behind the boxplot

```{r}
#| label: diag-groups
coded <- analysis %>%
  filter(!is.na(reaction_only))

coded %>%
  group_by(reaction_only) %>%
  summarize(n = n(), sd = sd(message_length), iqr = IQR(message_length))

coded %>%
  ___(reaction_only) %>%                                                                   # within each group
  filter(message_length > quantile(message_length, 0.75) + 1.5 * IQR(message_length)) %>%  # beyond the upper whisker
  select(reaction_only, message_length, message)
```

**What you should see:** the sd of each of your two coded groups, then the messages beyond the
upper whisker of their own box.
[Help with Task 5.2](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-5-2)

### Task 5.3: Shape across viewer bands

```{r}
#| label: diag-shape
analysis %>%
  group_by(viewer_band) %>%
  summarize(mean   = mean(message_length, na.rm = TRUE),
            median = median(message_length, na.rm = TRUE),
            sd     = sd(message_length, na.rm = TRUE),
            over_200 = sum(message_length > 200, na.rm = TRUE))
```

**What you should see:** the medium band's sd (`53.9`) is far larger than the small (`38.7`) or
large (`31.1`) band's, and in every band the mean is above the median.
[Help with Task 5.3](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#task-5-3)

**Write:** the diagnostics note for each of the three figures: shape, the assumption it bears
on, and the outliers.

*Your answer:*

## Explain this stage to the client

**Write:** one short paragraph, in plain language, that you would send to the channel manager.
Say what you did at this stage (what you counted, measured, and charted), what it shows about
the three questions they asked, and what it does not show yet: a description says how the
numbers sit, not whether a difference is bigger than chance, and that is worksheet 3. No R, no
column names, no jargon the manager would have to look up. Name the numbers that matter to
them.

*Your paragraph:*

## Before you submit

- [ ] No `___` and no `YOUR TURN` is left anywhere in the file.
- [ ] Every figure has its own alt text, a disclosure sentence, and an interpretation.
- [ ] `figures/` holds the three saved charts.
- [ ] The paragraph to the client is written, and it names what this stage does not show yet.
- [ ] The worksheet renders without an error.

[Help with rendering and submitting](https://aura-lab.siue.edu/v2v-hub/resources/help/describing-data.html#before-you-submit)
