---
title: "Inferencing Data worksheet"
subtitle: "MC 501 · 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/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 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 |
| 6 | Significance against size: one paragraph across all four results | A reviewer sees which results are big, not only which are significant |
| Last | Explain this stage to the client, in one short paragraph | The manager gets the results and their limits in plain language |

**This is the MC 501 worksheet,** so the tasks marked (MC 501) are part of your assignment.

**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 Monday, December 7, 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/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: [Inferencing Data in R rubric](https://aura-lab.siue.edu/v2v-hub/resources/rubrics/MC501-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.

**MC 501:** for each, also name one alternative test you rejected, and why.

*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)

### Task 2.2: The assumption and the cells driving it (MC 501)

```{r}
#| label: chi-square-501
round(chi$expected, 1)   # expected counts
round(chi$stdres, 2)     # standardized residuals
```

**What you should see:** the smallest expected count `198.9`, and residuals of `-25.68` and
`25.68` in the medium row.
[Help with Task 2.2](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-2-2)

**Write:** one sentence naming the cell that drives the result.

*Your answer:*

**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:*

### Task 3.3: Power for this focal test (MC 501)

What difference could thirty messages have detected? Put in your own group sizes and leave `d`
out: `pwr` solves for it.

```{r}
#| label: power
pwr::pwr.t2n.test(n1 = ___, n2 = ___,      # your two group sizes from Task 3.1
                  sig.level = .05,
                  power = .80)                   # the usual target: an 80% chance of detecting it

pwr::pwr.t.test(d = 0.52,                        # the d you observed
                sig.level = .05,
                power = .80)                     # n is the messages needed in EACH group
```

**What you should see,** with 17 and 13: `d = 1.069343`. Then `n = 59.03` per group for the d of
0.52.
[Help with Task 3.3](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-3-3)

**Write:** what effect this sample could detect, and what that means for the null result in
Task 3.2.

*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)

### Task 4.2: Welch's ANOVA, and which groups differ (MC 501)

```{r}
#| label: anova-501
oneway.test(outcome ~ group3, data = analysis)   # Welch's ANOVA
omega_squared(fit_anova)
TukeyHSD(fit_anova)
```

**What you should see:** Welch's `F = 109.83`, `p-value < 2.2e-16`; `Omega2` `3.68e-03`, which
is `0.004` when rounded; and under Tukey, `large-small` `diff` `-10.75`, `large-medium` `diff`
`-11.38`, and `medium-small` `diff` `0.63`.
[Help with Task 4.2](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-4-2)

**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.

**MC 501:** add the spread across the groups, Welch's result, and which bands differ.

*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)

### Task 5.2: The standardized slope, the interval, and the residuals (MC 501)

```{r}
#| label: regression-501
standardize_parameters(fit_lm)   # the standardized slope, with its interval
```

```{r}
#| label: regression-501-residuals
#| fig-alt: "Residuals plotted against predicted message length for the regression on hour of day. The points stack in a vertical stripe at each of the 24 hours and stretch far above the zero line, up to about 470, while none fall below about minus 30."
plot(fit_lm, which = 1)   # residuals against the predicted values
```

```{r}
#| label: regression-501-qq
#| fig-alt: "Normal Q-Q plot of the standardized residuals. The points follow the line only in the middle, sit flat above minus 1 at the left, and curve sharply upward at the right end, to about 11."
plot(fit_lm, which = 2)   # Q-Q plot: the residuals against a normal shape
```

**What you should see:** `Std. Coef.` `8.72e-03` for `predictor`, which is `0.009`; and two
plots.
[Help with Task 5.2](https://aura-lab.siue.edu/v2v-hub/resources/help/inferencing-data.html#task-5-2)

**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.

**MC 501:** add the standardized slope and what the residual plots show.

*Your answer:*

## Part 6: Significance against size (MC 501)

**Write:** one paragraph, across all four results, that separates statistical significance from
practical size, the way a reviewer would want it.

*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)
