---
title: "Inferencing Data worksheet"
subtitle: "MC 451 · Chapter 13 · Part 3 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/mc451/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 Describing Data you showed the manager how the numbers sit: gaming
chat against non-gaming chat, small audiences against large ones, reaction against commentary.

**Your job.** Tell the manager whether those differences are real or could be chance, and
whether they are big enough to put in a pitch. You test four relationships, one test each, and
say how big each one is. The manager does not read R, so every **Write:** prompt is a note to
them.

## Your assignment

| Part | What you do | What it tells the client |
|---|---|---|
| 0 | Write four hypotheses, before you run any code | What you expected, on record before the results |
| 1 | Fill the settings block | Nothing yet: it names the variables each test uses |
| 2 | Test 1, the chi-square test: viewer band and channel type | Whether audience size and channel type go together |
| 3 | Test 2, the t-test: message length for reaction and commentary | Whether reactions and commentary differ in length |
| 4 | Test 3, the one-way ANOVA: message length across viewer bands | Whether a bigger audience changes how people write |
| 5 | Test 4, the linear regression: message length and hour of the day | Whether how people write changes over the day |
| Last | Explain this stage to the client, in one short paragraph | The manager gets the results and their limits in plain language |

**The order matters.** Write the four hypotheses in Part 0 **before** you run any chunk below
it.

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, what each
function does, and what to do when R prints an error.

## What to submit

**100 points.** Due Friday, December 4, 11:59 PM, in Blackboard, under **Inferencing 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/mc451/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: [Inferencing Data in R rubric](https://aura-lab.siue.edu/v2v-hub/resources/rubrics/MC451-rubric-inferencing-data-in-r.pdf).

## Part 0: Four hypotheses, before any code

The job tests these four pairs of variables:

| | First variable | Second variable |
|---|---|---|
| 1 | `viewer_band` (small, medium, large) | `channel_type` (gaming, non-gaming) |
| 2 | `message_length` (characters) | `reaction_only`, your thirty hand-coded messages |
| 3 | `message_length` | `viewer_band` |
| 4 | `message_length` | the hour of the day the message was sent (0 to 23) |

**Write:** for each pair, a hypothesis in plain English naming both variables and the
relationship you expect. Then name the test the two variables' types call for and say in one
sentence why that test and not another.

*H1:*

*H2:*

*H3:*

*H4:*

*Tests and reasons:*

[Help with Part 0](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#part-0)

## Part 1: The settings block

Every test below uses six short names instead of the Twitch column names. Give `group2` your
hand-coded variable.

```{r}
#| label: settings
library(tidyverse)     # data steps and hour()
library(v2v)           # the course package; the tests below are plain R
library(effectsize)    # cramers_v(), cohens_d(), eta_squared(): how big each result is
options(scipen = 10)  # long numbers such as timestamps print in full; p-values keep the usual form

# Load the analysis file, then keep the columns the tests use under the names they use.
# outcome is a number; group2 has two groups; group3 has three or more; predictor is a
# number; cat_a and cat_b are categories. factor() makes a column a category.
analysis <- read_rds("data/twitch_analysis.RDS") %>%
  transmute(
    outcome   = message_length,
    group2    = ___,        # your thirty hand-coded messages
    group3    = factor(viewer_band),
    predictor = hour(ts),             # hour of the day, 0 to 23, in UTC
    cat_a     = factor(viewer_band),
    cat_b     = factor(channel_type)
  )

glimpse(analysis)
```

**What you should see:** `Rows: 35,267` and six columns: `outcome` `<int>`, `group2`, `group3`,
`cat_a`, and `cat_b` `<fct>`, `predictor` `<int>`.
[Help with Part 1](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#part-1)

## Part 2: Test 1, the chi-square test

### Task 2.1: The test and its effect size

```{r}
#| label: chi-square
counts <- xtabs(~ ___ + ___, data = analysis)   # the two categories from the settings block
sum(counts)                                     # N: the messages the test used
chi <- chisq.test(counts)
chi
cramers_v(counts, adjust = FALSE)
```

**What you should see:** `34766`; then `X-squared = 822.66`, `df = 2`, `p-value < 2.2e-16`;
then `Cramer's V` `0.15`, with its 95% confidence interval beside it.
[Help with Task 2.1](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-2-1)

**Write:** the APA report of Test 1 (statistic, df, N, p, Cramér's V), then two to three sentences
for the client on what it means for H1, with the effect size in words.

*Your answer:*

## Part 3: Test 2, the t-test on your hand-coded variable

### Task 3.1: How many messages the test uses

The table has 35,267 rows, but only your thirty have a value of `group2`. Every other row is
dropped from this test.

```{r}
#| label: t-test-n
analysis %>%
  filter(!is.na(group2)) %>%
  count(group2)

analysis %>%
  filter(!is.na(outcome), !is.na(group2)) %>%   # the rows the test itself uses
  group_by(group2) %>%
  summarize(n = n(), mean = mean(outcome), sd = sd(outcome))
```

**What you should see:** your two groups adding to `30`, then the mean and sd of each.
[Help with Task 3.1](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-3-1)

### Task 3.2: The test and its effect size

```{r}
#| label: t-test
t.test(outcome ~ ___, data = analysis)     # your hand-coded variable
cohens_d(outcome ~ ___, data = analysis)
```

**What you should see:** `t`, `df`, and `p-value`, the mean of each group, then `Cohen's d` with
its 95% confidence interval. Your numbers depend on your codes.
[Help with Task 3.2](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-3-2)

**Write:** the APA report of Test 2 (both means and SDs, t, df, p, n, Cohen's d), then two to three
sentences for the client on what it means for H2. Say the size of the difference in characters.

*Your answer:*

## Part 4: Test 3, the one-way ANOVA

### Task 4.1: The test and its effect size

```{r}
#| label: anova
analysis %>%
  filter(!is.na(outcome)) %>%                   # the rows the test itself uses
  group_by(group3) %>%
  summarize(n = n(), mean = mean(outcome), sd = sd(outcome))

fit_anova <- aov(outcome ~ ___, data = analysis)   # the three viewer bands
summary(fit_anova)
eta_squared(fit_anova)
```

**What you should see:** n of `27765`, `5501`, and `1997` and means of `29.5`, `30.1`, and `18.8`
characters for small, medium, and large; `F value` `66.2`, `Df` `2` and `35260`, `Pr(>F)` `<2e-16`; then `Eta2` `3.74e-03`,
which is `0.004` when rounded.
[Help with Task 4.1](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-4-1)

**Write:** the APA report of Test 3 (F, both dfs, p, N, eta-squared), then two to three
sentences for the client on what it means for H3, with the effect size in words.

*Your answer:*

## Part 5: Test 4, the linear regression

### Task 5.1: The test and its effect size

```{r}
#| label: regression
fit_lm <- lm(outcome ~ ___, data = analysis)   # the hour of the day
summary(fit_lm)
```

**What you should see:** `Estimate` `0.05360` on the `predictor` row (the slope, `0.05` when
rounded); `Multiple R-squared` `7.603e-05` (`0.000` when rounded); `F-statistic` `2.681` on `1`
and `35261` DF; `p-value` `0.1016`.
[Help with Task 5.1](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-5-1)

**Write:** the APA report of Test 4 (b, t, df, p, R-squared), then two to three sentences for
the client on what it means for H4, with the slope in characters per hour.

*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 (four tests, one per question), what it shows (which differences
held up, and how big each one is in words the manager can repeat to a sponsor), and what it
does not show. Two limits belong in every version of this paragraph: a significant test on this
sample is not a claim about Twitch as a whole, and a variable that belongs to a channel (its
type, its audience) varies across about fifty channels, not tens of thousands of messages, so
it is weaker evidence than its message count suggests. No R, no column names, no jargon the
manager would have to look up.

*Your paragraph:*

## Before you submit

- [ ] The four hypotheses in Part 0 were written before you ran Part 1.
- [ ] No `___` is left anywhere in the file.
- [ ] Each test has its APA report, its effect size in words, and its interpretation.
- [ ] The paragraph to the client is written, and it names both limits.
- [ ] The worksheet renders without an error.

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