Week 12 · Chapter 12
| What | Due | Have done by |
|---|---|---|
| Data Wrangling in R, 50 points | Mon, Nov 16, 11:59 PM | Codes saved as a CSV tonight |
| Pre-registration file committed | Mon, Nov 16, 11:59 PM | Research Proposal revised first, if it needs it |
| Describing Data in R, 100 points | Mon, Nov 23, 11:59 PM | Started tonight, after Data Wrangling renders |
| Weekly reading journal | Mon, Nov 16 and Mon, Nov 23, 11:59 PM | The Week 12, then the Week 13 reading |
No class Nov 25: Thanksgiving Break.
| What | Due | Have done by |
|---|---|---|
| Inferencing Data in R, 100 points | Mon, Dec 7, 11:59 PM | Taught Nov 18 |
| Coded dataset pushed (White Paper dated step) | Mon, Dec 7, 11:59 PM | First coding pass done by Thu, Dec 3 |
| Week 15 reading journal | Mon, Dec 7, 11:59 PM | The Week 15 reading |
| White Paper and conference poster, 200 points | Fri, Dec 18, 11:59 PM | Results built from your own data |
list.files() lists what is in the folder R started in> list.files()
[1] "_templates" "00-check-your-setup.qmd"
[3] "00_Inbox" "01_Journal"
[5] "02_Literature" "03_Project"
[7] "04_Resources" "11-wrangling-data.qmd"
[9] "data" "LICENSE"
[11] "README.md"
data is in the list, beside README.mddata folderdata and press Enter (Return on a Mac)data sits beside README.md, not inside another folderData Wrangling worksheet, Task 0.3
CSV: a table saved as plain text, one row per line, with a comma between each column.
data/coding-form.csv, so the file must be a CSVdata folder and name it coding-form.csvid column exactly as it iscoding-form - Sheet1.csvcoding-form.csv inside data, click Delete, and confirmdata, click Rename…, type coding-form.csv, and press Enterread_csv() reads a CSV file into a tableglimpse() prints how many rows and columns it has, and each column’s type"data/coding-form.csv" means: start in the folder open in VS Code, go into data, open coding-form.csv
> read_csv("data/coding-form.csv") %>%
+ glimpse()
Rows: 30 Columns: 5
── Column specification ────────────────────────────────────────────────
Delimiter: ","
chr (2): channel, message
dbl (2): id, reaction_only
lgl (1): coder_notes
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 30
Columns: 5
$ id <dbl> 16240927, 182383, 19530370, 12102429, 7123505, 2…
$ channel <chr> "bobross", "bobross", "bobross", "bobross", "bob…
$ message <chr> "press F to bring back bob", "BIG TREE", "CoolSt…
$ reaction_only <dbl> 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, …
$ coder_notes <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
Rows: 30, Columns: 5, and reaction_only as <dbl> (a number): the sheet is right> read_csv("data/coding-form.csv") %>%
+ glimpse()
Error: 'data/coding-form.csv' does not exist in current working directory ('C:/Users/you/Documents/mc501-workspace').
does not exist: no file in data has exactly that namelist.files("data") lists every file in the data folder> list.files("data")
[1] "coding-form - Sheet1.csv" "twitch_analysis.RDS"
coding-form.csv in the VS Code Explorer, then run the check again"data" again means: start in the folder open in VS Code, go into data> list.files("data")
[1] "coding-form.csv" "twitch_analysis.RDS"
coding-form.csv: your thirty codes, saved as a CSVtwitch_analysis.RDS: the table Part 7 savedread_rds() loads the saved file back, and nrow() counts its rows> read_rds("data/twitch_analysis.RDS") %>%
+ nrow()
[1] 35267
35267: one row per chat message, nothing lost and nothing addedQuarto: Render Document and press EnterThe last line of the Terminal panel:
Output created: 11-wrangling-data.html
Output created means it worked11-wrangling-data.html appears beside the worksheet in the Explorer.qmd file is saved inREADME.md: both starts are the same folder03_Project: render looks for data inside that folder, and stopsThe same worksheet, saved in 03_Project and rendered:
Error:
! 'data/coding-form.csv' does not exist in current working directory ('C:/Users/you/Documents/mc501-workspace/03_Project').
Backtrace:
▆
1. ├─read_csv("data/coding-form.csv") %>% clean_names()
2. ├─janitor::clean_names(.)
3. ├─readr::read_csv("data/coding-form.csv")
4. │ └─vroom::vroom(...)
5. │ └─vroom:::vroom_(...)
6. └─vroom (local) `<fn>`("data/coding-form.csv")
7. └─vroom:::check_path(path)
Quitting from 11-wrangling-data.qmd:425-429 [read-codes]
Execution halted
03_Project: that is where render startedREADME.md, and render againData Wrangling worksheet, Part 8
Data Wrangling in R, Blackboard
11-wrangling-data.qmd, with no ___ left11-wrangling-data.html, the rendered pagedata/coding-form.csv, your thirty codesdata/twitch_analysis.RDS, the saved tableREADME.md, with an entry or the no-AI box checkedDescribing Data, 100 points, due Monday, November 23, 11:59 PM
Expect every first version to look wrong. The fix is usually one line.
data/twitch_analysis.RDS, with your thirty codes insideMC 501 worksheet · Describing Data risks guide
12-describing-data-mc501.qmd in your Downloads folderThree lines start every Describing session: two packages, then the table Data Wrangling saved.
library() switches a package on for this sessionread_rds() reads the saved file and names the table analysiscannot open the connection, the file is not in your data folder> library(tidyverse)
── Attaching core tidyverse packages ──────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.2.0
── Conflicts ────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
> library(v2v)
v2v 0.3.9 loaded.
setup() checks that R, Quarto and Git are ready
?v2v lists what this package gives you
> analysis <- read_rds("data/twitch_analysis.RDS")
masks lines are normal, not errorsread_rds() prints nothing: the table went into analysis quietlyglimpse() prints one line per column: its name, its type, and its first few values> glimpse(analysis)
Rows: 35,267
Columns: 16
$ id <dbl> 89, 551, 1033, 2094, 2803, 10406, 16104, 22571,…
$ channel <chr> "sodapoppin", "xqcow", "forsen", "sodapoppin", …
$ sender <chr> "madzee", "prometheanow", "spectre155", "itsjer…
$ message <chr> "???????????????????????", "TriEasy Clap TriEas…
$ date <dbl> 1.542578e+12, 1.542578e+12, 1.542578e+12, 1.542…
$ ts <dttm> 2018-11-18 21:55:27, 2018-11-18 21:55:35, 2018…
$ message_length <int> 23, 441, 8, 12, 206, 37, 6, 7, 4, 7, 3, 4, 12, …
$ snapshot_ts <dttm> NA, NA, NA, NA, NA, 2018-11-18 21:56:40, 2018-…
$ game <chr> NA, NA, NA, NA, NA, "Artifact", "Marble It Up!"…
$ viewers <int> NA, NA, NA, NA, NA, 15300, 28076, 4703, 19239, …
$ main_game <chr> "Hand Simulator", "Just Chatting", "The Elder S…
$ is_gaming <lgl> TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRU…
$ channel_type <fct> gaming, non-gaming, gaming, gaming, non-gaming,…
$ median_viewers <dbl> 22944, 16176, 10163, 22944, 16176, 10163, 22944…
$ viewer_band <fct> large, medium, medium, large, medium, medium, l…
$ reaction_only <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
reaction_only is NA except on your thirty coded messagesVisualization is not decoration applied afterwards. It is part of the analysis.
Three parts do most of the work (Wilkinson, 2005; Wickham, 2016):
Swap geom_line() for geom_col() and the same data is drawn as bars.
> viewers_over_time <- twitch_viewers_over_time()
> viewers_over_time
# A tibble: 97 × 3
six_hour category total_viewers
<dttm> <fct> <int>
1 2018-11-18 18:00:00 Art 238217
2 2018-11-18 18:00:00 Just Chatting 2034123
3 2018-11-18 18:00:00 Pokemon: Let's Go, Pikachu!/Eevee! 99909
4 2018-11-18 18:00:00 Other 4333423
5 2018-11-19 00:00:00 Art 870710
6 2018-11-19 00:00:00 Fortnite 258780
7 2018-11-19 00:00:00 Just Chatting 4312125
8 2018-11-19 00:00:00 Pokemon: Let's Go, Pikachu!/Eevee! 1991196
9 2018-11-19 00:00:00 Other 6933016
10 2018-11-19 06:00:00 Art 12444
# ℹ 87 more rows
count() makes one row per category and counts the rows in each> viewers_over_time %>%
+ count(category)
# A tibble: 6 × 2
category n
<fct> <int>
1 Art 21
2 Fortnite 17
3 Hearthstone 9
4 Just Chatting 18
5 Pokemon: Let's Go, Pikachu!/Eevee! 10
6 Other 22
n is how many six-hour windows each one has?twitch_viewers_over_time says how, and the package source holds the pipelineggplot(viewers_over_time,
aes(x = six_hour, y = total_viewers,
color = category)) +
geom_line(linewidth = 0.9) +
scale_y_continuous(labels = scales::label_comma()) +
labs(title = "Concurrent viewers by game category",
x = "Date (UTC, six-hour buckets)",
y = "Total viewers in bucket",
color = "Category") +
v2v::scale_color_v2v() +
v2v::theme_v2v()label_comma() writes 20,000,000 instead of 2.0e+07, and theme_v2v() applies publication-ready fonts, spacing, and gridlines in one call.
A dominant series crowds out the rest. Noticing that is reading the chart honestly.
hour() pulls the hour, 0 to 23, out of each message’s time stampcount() then counts the messages in each hour and names the count messages> chat_by_hour <- analysis %>%
+ mutate(hour = hour(ts)) %>%
+ count(hour, name = "messages")
> chat_by_hour
# A tibble: 24 × 2
hour messages
<int> <int>
1 0 1816
2 1 1959
3 2 1013
4 3 833
5 4 1384
6 5 1317
7 6 1393
8 7 1165
9 8 1016
10 9 1381
# ℹ 14 more rows
arrange() sorts the rows; desc() puts the biggest count first> chat_by_hour %>%
+ arrange(desc(messages))
# A tibble: 24 × 2
hour messages
<int> <int>
1 13 2201
2 16 2132
3 17 2084
4 1 1959
5 12 1915
6 0 1816
7 11 1775
8 18 1717
9 15 1578
10 10 1449
# ℹ 14 more rows
geom_col() draws a bar whose height is a value you already computed.
The chart shows all twenty-four hourly counts at once.
“Effect sizes are the most important outcome of empirical studies.”
Lakens (2013, p. 1)
Lakens (2013)
“Effect sizes are the most important outcome of empirical studies.”
Lakens (2013, p. 1)
Histogram: a bar chart of a number variable, where each bar counts the values in one range. Example: one bar counts the messages 0 to 5 characters long.
filter() drops the messages from channels with no gaming labelpmin() keeps each length, but shows anything over 120 as 120count(is_gaming) checks how many messages each group has left> msglen <- analysis %>%
+ filter(!is.na(is_gaming)) %>%
+ mutate(length_shown = pmin(message_length, 120))
> msglen %>%
+ count(is_gaming)
# A tibble: 2 × 2
is_gaming n
<lgl> <int>
1 FALSE 3457
2 TRUE 31309
FALSE is non-gaming: 3,457 messages, about one in tenTRUE is gaming: 31,309 messagesmax() finds the biggest value; na.rm = TRUE skips the empty messages> msglen %>%
+ summarize(
+ longest = max(message_length, na.rm = TRUE),
+ longest_shown = max(length_shown, na.rm = TRUE)
+ )
# A tibble: 1 × 2
longest longest_shown
<int> <dbl>
1 501 120
binwidth = 5 makes each bar five characters wide; alpha lets the overlap show throughThe terminal also prints this under the histogram code:
> ggplot(msglen,
+ aes(x = length_shown, fill = is_gaming)) +
+ geom_histogram(binwidth = 5, position = "identity",
+ alpha = 0.55) +
+ labs(x = "Message length (characters, capped at 120)",
+ y = "Messages",
+ fill = "Gaming channel") +
+ v2v::scale_fill_v2v() +
+ v2v::theme_v2v()
Warning message:
Removed 4 rows containing non-finite outside the scale range
(`stat_bin()`).
binwidth = 5 makes each bar cover five characters
pmin(message_length, 120) caps the display at 120 characters
An unannounced cap is a quiet distortion. The axis label states the cap.
Skew: a lopsided shape. Right-skewed means most values are small and a few are very large, like message length here.
The skew is the most important thing the histogram reveals, and no summary number can show it.
Descriptive statistics: numbers that summarize a set of data, such as how many cases there are, where the middle sits, and how spread out the values are. Example: n, mean, median, and sd of message length for each group.
channel_typegroup_by(is_gaming) splits the table into the two groups; summarize() makes one row per groupna.rm = TRUE leaves the four empty messages out of each number> msglen %>%
+ group_by(is_gaming) %>%
+ 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),
+ .groups = "drop"
+ )
# A tibble: 2 × 5
is_gaming n mean median sd
<lgl> <int> <dbl> <int> <dbl>
1 FALSE 3457 33.7 16 60.7
2 TRUE 31305 28.5 17 38.5
Mean: the average, the total divided by the count. Median: the middle value once the values are sorted. Example: the mean message is 29.0 characters long, the median 17.
Read the means and non-gaming chat is longer. Read the medians, 16 against 17, and the two groups are all but identical, pointing the other way.
Standard deviation (sd): how far values typically sit from the mean. Example: non-gaming chat’s sd of 60.7 says its lengths are more spread out than gaming chat’s 38.5.
Report the mean alone and you misreport what a typical message looks like.
Effect size: how big a difference or a relationship is, on a scale that does not grow with the sample. Example: Cramer’s V of 0.15 for viewer band by channel type.
effectsize::cohens_d() and effectsize::cramers_v() print each one with its 95% confidence intervalPut the effect size in the caption or in a companion table, never in a footnote.
message_length ~ is_gaming reads “message length, compared across gaming status”effectsize:: calls the function from the effectsize package without loading the whole package> effectsize::cohens_d(message_length ~ is_gaming,
+ data = msglen)
Cohen's d | 95% CI
------------------------
0.13 | [0.09, 0.16]
- Estimated using pooled SD.Warning message:
Missing values detected. NAs dropped.
Confidence interval: a range around an estimate, built by a method that captures the true value in a stated share of repeated samples, such as 95 in 100. Example: d with its 95% interval shows how far the true gap could plausibly sit from the measured one.
Categorical variable: a variable whose values are categories rather than measured amounts. Example: channel type, gaming or non-gaming.
Cross-tabulation: a table that counts how often each combination of two categorical variables occurs. Example: viewer band by channel type.
scale_color_v2v()#| fig-alt: "A histogram of message length..."alt = line inside labs()Example data: episodes_analysis.RDS, Power Rangers episodes coded for threat and focus
Cross-tabulation: a grid that counts every combination of two categories.
count() with two columns: one row per combinationpivot_wider() turns those rows into a grid, one column per focus code> episodes <- read_rds("data/episodes_analysis.RDS")
>
> episodes %>%
+ filter(!is.na(focus)) %>%
+ count(threat, focus) %>%
+ pivot_wider(names_from = focus,
+ values_from = n,
+ values_fill = 0)
# A tibble: 3 × 4
threat team one_ranger other
<fct> <int> <int> <int>
1 monster 15 21 1
2 villain 29 18 9
3 none 10 13 3
threat code, one column per focus codemonster and teamgroup_by(threat) makes sum(n) add up within each threat code> episodes %>%
+ filter(!is.na(focus)) %>%
+ count(threat, focus) %>%
+ group_by(threat) %>%
+ mutate(share = round(n / sum(n), 3))
# A tibble: 9 × 4
# Groups: threat [3]
threat focus n share
<fct> <fct> <int> <dbl>
1 monster team 15 0.405
2 monster one_ranger 21 0.568
3 monster other 1 0.027
4 villain team 29 0.518
5 villain one_ranger 18 0.321
6 villain other 9 0.161
7 none team 10 0.385
8 none one_ranger 13 0.5
9 none other 3 0.115
one_ranger: 0.568 of the monster episodes, 0.321 of the villain onesYou should now have:
figures/ and drawn with theme_v2v()#| fig-alt: sentence for each figure, with no YOUR TURN leftThe histogram left a precise question: is the gap between the groups real or chance? Week 13 answers it.