Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

LinkageStats.qmd 4.5 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...