Coding the Codebook and First Contact in R

Week 9 · Chapter 9 · MC 501 Research Methods for Mass Communications

Dr. Alex Leith

Today’s Agenda

  • A project workspace that holds the whole study
  • Provenance as a methodological disclosure
  • The data loaded and inspected, column by column
  • Reading the data against your codebook

What We Build Today

Week 9 · Chapter 9 · working session

  • Due tonight: Extended Codebook and Reliability Protocol
  • A project workspace holding the study in one folder
    • Your codebook installed as the rendered rulebook
    • The Twitch data loaded and inspected
  • Assigned reading: Munafo et al. (2017)

The gentlest possible evening in R: you load data and look at it.

Why the Workspace Comes First

  • Reproducible research lives on traceability
    • Files in known places, not commands typed once
  • A project is one folder for one study
    • Data, code, codebook, report, travelling together
    • Nothing depends on a file only on your laptop
  • Your Week 15 White Paper renders from this folder

Scaffolding the Project

v2v::new_portfolio("twitch-portfolio")

Create a complete, correctly wired study folder in one call.

The v2v:: prefix is not decoration. It tells R, and you, that the function comes from this book’s package, so the package never becomes a hidden assumption.

What the Scaffold Contains

twitch-portfolio/
├── twitch-portfolio.Rproj
├── _quarto.yml
├── README.md
├── codebook.qmd
├── analysis.qmd
├── data/
└── .Rprofile

You did not decide where files go or what to name them. An hour lost to file layout is an hour not spent on the study.

Confirming the Stack

v2v::setup()

Check that R, the packages this book depends on, and the project environment are present and agree on versions.

  • You met setup() in Chapter 2, at installation
  • It returns as a habit: run it when you open a project
  • You learn about a missing piece before it interrupts you

The Rulebook Becomes a Document

  • One scaffolded file is codebook.qmd
    • Move your Chapter 8 draft in, with a version line and date
    • A codebook that renders has no broken structure
  • What changes today is the status, not the content
    • It stops being a sketch and becomes the instrument

Loading the Data

library(tidyverse)

chat <- v2v::twitch_chat()
streams <- v2v::twitch_streams()

Load the tidyverse, then pull in the two tables and name each one.

The arrow <- assigns, so chat and streams now refer to the tables for this session. Called with no arguments, each returns the full working sample.

Why a Call, Not a File Path

  • A new user’s first data encounter is a pile of path errors
    • These functions reach inside the package and hand the data back
    • There is no path for you to get wrong
  • “Everything” is not the entire 2018 collection
    • That runs to tens of millions of rows
    • What ships is a working sample: real, and fast

Provenance Is a Disclosure

  • First contact rests on documenting where data came from
    • Before the first line of analysis runs
  • “I collected Twitch chat data” is not a methods section
  • Replication requires exact knowledge of what was done

What a Provenance Sentence Looks Like

I collected 14 days of Twitch chat from the top 10 most-viewed streams in the Just Chatting category, using Twitch API v2, between 2024-01-15 and 2024-01-28, saving raw JSON responses to data/raw/ before any processing.

Every clause in that sentence is something a replicator would otherwise have to guess.

Munafo on Usability

“Poor usability reflects difficulty in evaluating what was done, in reusing the methodology to assess reproducibility, and in incorporating the evidence into systematic reviews and meta-analyses.”

Munafo et al. (2017, p. 4)

  • Discoverable is not the same as usable
  • A study can be public, cited, and impossible to evaluate

Usability, Not Discoverability

Munafo et al. (2017)

“Poor usability reflects difficulty in evaluating what was done, in reusing the methodology to assess reproducibility, and in incorporating the evidence into systematic reviews and meta-analyses.”

Munafo et al. (2017, p. 4)

  • Which is worse: unfindable, or unevaluable?
  • Your provenance statement owes a stranger what?
  • Name one thing in your workflow that fails today

What the Record Includes

For fresh API data the record must name:

  1. Data source and API version
  2. Date and time range of collection
  3. Any filters, keywords, or channel selectors applied
  4. The format in which raw data was saved
  5. The git commit at which raw data was frozen

Item 5 is the audit trail: never re-run collection over a file a script uses.

Looking, the Chat Table

glimpse(chat)
Rows: 35,267
Columns: 5
$ id      <int> 89, 551, 1033, 2094, 2803, …
$ channel <chr> "sodapoppin", "xqcow", "forsen", …
$ sender  <chr> "madzee", "prometheanow", "spectre155", …
$ message <chr> "???????????????????????", "TriEasy Clap TriEasy Clap …
$ date    <dbl> 1542578127023, 1542578135562, 1542578143610, …

Print every column, its type, and its first values, without changing anything.

Reading the Chat Glimpse

  • 35,267 rows, one chat message per row
  • The values are real and not tidy
    • Twenty-three question marks, then repeated “TriEasy Clap”
    • Not errors: your codebook must meet them as they are
  • date is milliseconds since 1970, and unreadable as it stands

Converting it is Chapter 11’s headline.

Looking, the Stream Table

glimpse(streams)
Rows: 32,276
Columns: 6
$ id      <int> 4, 10, 18, 61, 105, …
$ channel <chr> "sodapoppin", "xqcow", "forsen", "giantwaffle", …
$ title   <chr> "Hello truckers\n44835158804929_2115841973", …
$ game    <chr> "Marble It Up!", "Just Chatting", "Artifact", …
$ viewers <int> 27934, 19381, 15300, 4697, 28076, …
$ date    <dbl> 1542578200214, 1542578200240, 1542578200273, …

Reading the Stream Glimpse

  • 32,276 rows, one snapshot of one stream each
  • Row 1 is the row Chapter 2 opened with, at 27,934 viewers
    • Row 5 is the same channel minutes later, at 28,076
  • title is as messy as chat
    • A line break inside the string, and a long run of digits

Noticing this before any analysis is the reason for first contact.

Counting

streams %>% count(game, sort = TRUE)
   game                 n
 1 Art               5298
 2 Fortnite          3682
 3 Just Chatting     2474
 4 Hearthstone       2157

The %>% is the tidyverse pipe: it feeds the thing on its left to the function on its right, so the line reads as an instruction in order.

What the Count Tells You

  • Sixty distinct games, more than a study can examine one by one
  • A handful hold most snapshots, and fifty more hold the rest
  • Art tops the list, on a platform mostly playing games
    • That shape matters when you draw a chart in Chapter 12

count() is looking, not changing. The table is exactly as loaded.

Checking the Corpus

n_distinct(chat$channel)
[1] 50
  • Chapter 1 described fifty channels, and here they are
  • n_distinct(streams$channel) returns the same fifty
  • The data you loaded is the corpus described, no more and no less

Reading Data Against the Codebook

Open the codebook beside the glimpse() output and ask a blunt question.

  • Message target: message text plus sender. Both exist, so supported
  • Message length: a character count of message, so supported
  • Contains emote: read from the same column, so supported

Far better to confirm now than to find a missing column mid-coding.

The Wrinkle Worth Carrying

  • A message begins “@xQcOW”, and channel reads “xqcow”
    • The display name typed against the stored login name
  • Your rule keys on the channel name, so it must allow for casing
  • Fix it in the codebook now, before a case is coded

When a Line Breaks

  • could not find function "glimpse": the tidyverse is not loaded
  • object 'chat' not found: the assignment has not run
  • no package called 'v2v': installation, not syntax
  • The Hub keeps a common-errors cheat sheet

An error is information, not a verdict. Run ?v2v::common_errors for this data’s own.

Checkpoint

You should now have:

  • A twitch-portfolio/ folder that v2v::setup() reports as sound
  • codebook.qmd versioned, dated, and rendering cleanly
  • chat and streams loaded in your session
  • A written provenance statement, four sentences
  • At least one codebook revision prompted by first contact

Before Week 10

  • Read Chapter 10, on sampling and the codebook on trial
    • Re-read Lakens (2022), with your reliability sample in mind
  • Due next week: the Sampling Plan and Pilot
    • Bring the revisions first contact prompted, committed
  • Write a journal entry, 450 to 500 words

Questions?