1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
- ---
- title: Book Data Linkage Statistics
- jupyter:
- jupytext:
- text_representation:
- extension: .qmd
- format_name: quarto
- format_version: '1.0'
- jupytext_version: 1.14.7
- kernelspec:
- display_name: R (IRkernel)
- language: R
- name: ir
- ---
- This notebook presents statistics of the book data integration.
- ## Setup
- ```{r}
- library(tidyverse, warn.conflicts=FALSE)
- library(arrow, warn.conflicts=FALSE)
- library(jsonlite)
- ```
- I want to use `theme_minimal()` by default:
- ```{r}
- theme_set(theme_minimal())
- ```
- And default image sizes aren't great:
- ```{r}
- options(repr.plot.width = 7,
- repr.plot.height = 4)
- ```
- ## Load Link Stats
- We compute dataset linking statistics as `gender-stats.csv` as part of the integration. Let's load those:
- ```{r}
- link_stats = read_csv("book-links/gender-stats.csv")
- glimpse(link_stats)
- ```
- Now let's define variables for our variou codes. We are first going to define our gender codes. We'll start with the resolved codes:
- ```{r}
- link_codes = c('female', 'male', 'ambiguous', 'unknown')
- ```
- We want the unlink codes in order, so the last is the first link failure:
- ```{r}
- unlink_codes = c('no-author-rec', 'no-book-author', 'no-book')
- ```
- ```{r}
- all_codes = c(link_codes, unlink_codes)
- ```
- ## Processing Statistics
- Now we'll pivot each of our count columns into a table for easier reference.
- ```{r}
- book_counts = link_stats %>%
- pivot_wider(id_cols=dataset, names_from=gender, values_from=n_books) %>%
- replace(is.na(.), 0) %>%
- mutate(total=rowSums(across(-dataset)))
- glimpse(book_counts)
- ```
- ```{r}
- act_counts = link_stats %>%
- filter(dataset != "LOC-MDS") %>%
- pivot_wider(id_cols=dataset, names_from=gender, values_from=n_actions) %>%
- replace(is.na(.), 0) %>%
- mutate(total=rowSums(across(-dataset)))
- glimpse(act_counts)
- ```
- We're going to want to compute versions of this table as fractions, e.g. the fraction of books that are written by women. We will use the following helper function:
- ```{r}
- fractionalize = function(data, columns, unlinked=NULL) {
- fracs = select(data, dataset | all_of(columns))
- if (!is.null(unlinked)) {
- fracs = mutate(fracs, unlinked=rowSums(select(data, all_of(unlinked))))
- }
- totals = rowSums(select(fracs, !dataset))
- fracs %>% mutate(across(!dataset, ~ .x / totals))
- }
- fractionalize(book_counts, link_codes) %>% glimpse()
- ```
- And a helper function for plotting bar charts:
- ```{r}
- plot_bars = function(data, what="UNSPECIFIED") {
- tall = data %>%
- pivot_longer(!dataset, names_to="status", values_to="fraction")
- codes = c(all_codes, "unlinked")
- codes = intersect(codes, unique(tall$status))
- tall = tall %>% mutate(status=ordered(status, codes))
- ggplot(tall) +
- aes(y=dataset, x=fraction, fill=status) +
- geom_col(position=position_stack(reverse=TRUE), width=0.5) +
- geom_text(aes(label=if_else(fraction >= 0.1,
- sprintf("%.1f%%", fraction * 100),
- "")),
- position=position_stack(reverse=TRUE, vjust=0.5),
- colour="white", fontface="bold") +
- scale_fill_brewer(type="qual", palette="Dark2") +
- ylab("Dataset") +
- xlab(paste("Fraction of", what)) +
- labs(fill="Author Gender")
- }
- ```
- ## Resolution of Books
- What fraction of *unique books* are resolved from each source?
- ```{r}
- book_counts %>% fractionalize(all_codes)
- ```
- ```{r}
- book_counts %>% fractionalize(all_codes) %>% plot_bars("Books")
- ```
- ```{r}
- book_counts %>% fractionalize(link_codes, unlink_codes)
- ```
- ```{r}
- book_counts %>% fractionalize(link_codes, unlink_codes) %>% plot_bars("Books")
- ```
- ```{r}
- book_counts %>% fractionalize(c('female', 'male')) %>% plot_bars("Books")
- ```
- ## Resolution of Ratings
- What fraction of *rating actions* have each resolution result?
- ```{r}
- act_counts %>% fractionalize(all_codes)
- ```
- ```{r}
- act_counts %>% fractionalize(all_codes) %>% plot_bars("Actions")
- ```
- ```{r}
- act_counts %>% fractionalize(link_codes, unlink_codes)
- ```
- ```{r}
- act_counts %>% fractionalize(link_codes, unlink_codes) %>% plot_bars("Actions")
- ```
- ```{r}
- act_counts %>% fractionalize(c('female', 'male')) %>% plot_bars("Actions")
- ```
- ## Metrics
- Finally, we're going to write coverage metrics.
- ```{r}
- book_linked = eval(quote(male + female + ambiguous), envir=book_counts)
- book_coverage = book_linked / book_counts$total
- book_coverage = setNames(book_coverage, book_counts$dataset)
- book_coverage
- ```
- ```{r}
- json = toJSON(
- as.list(book_coverage),
- auto_unbox=TRUE,
- )
- write_file(json, "book-coverage.json")
- ```
|