-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtortexploration.Rmd
286 lines (216 loc) · 8.5 KB
/
tortexploration.Rmd
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
---
title: "R Notebook"
output: html_notebook
---
#An Exploration of Tort & Railroad Law
In this notebook, I am going to explore the tort data and compare it to the railroad data. My goal is to check for similarity. In order to do this, I am going to combine the two sets of data and check for clusters.
##Libraries
```{r}
library(tokenizers)
library(text2vec)
library(readr)
library(dplyr)
library(stringr)
library(purrr)
library(forcats)
library(ggplot2)
library(glmnet)
library(doParallel)
library(Matrix)
library(broom)
library(tidyr)
library(tibble)
library(devtools)
library(wordVectors)
library(ggrepel)
library(apcluster)
library(caret)
library(tidyverse)
library(textreuse)
library(devtools)
```
#Metadata Cleaning
```{r}
us_items <- read_csv("C:/Users/Joshua/Documents/rdata/data/us-items.csv",
col_types = cols(
.default = col_character(),
document_id = col_character(),
publication_date = col_date(format = ""),
release_date = col_date(format = ""),
volume_current = col_integer(),
volume_total = col_integer(),
page_count = col_integer()
))
us_authors <- read_csv("C:/Users/Joshua/Documents/rdata/data/us-authors.csv",
col_types = cols(
document_id = col_character(),
author = col_character(),
birth_year = col_character(),
death_year = col_integer(),
marc_dates = col_character(),
byline = col_character()
))
us_subjects <- read_csv("C:/Users/Joshua/Documents/rdata/data/us-subjects.csv", col_types = cols(
document_id = col_character(),
subject_source = col_character(),
subject_type = col_character(),
subject = col_character()
))
us_authors <- read_csv("C:/Users/Joshua/Documents/rdata/data/us-authors.csv",
col_types = cols(.default = col_character()))
get_year <- function(x) { as.integer(str_extract(x, "\\d{4}")) }
pick <- function(x, y) { ifelse(!is.na(x), x, y) }
us_authors <- us_authors %>%
mutate(birth_year = get_year(birth_year),
death_year = get_year(death_year),
creator = pick(author, byline))
us_subjects_moml <- us_subjects %>%
filter(subject_source == "MOML",
subject != "US") %>%
distinct(document_id, subject)
us_subjects_loc <- us_subjects %>%
filter(subject_source == "LOC")
rm(us_subjects)
us_items <- read_csv("C:/Users/Joshua/Documents/rdata/data/us-items.csv",
col_types = cols(
.default = col_character(),
publication_date = col_date(format = ""),
release_date = col_date(format = ""),
volume_current = col_integer(),
volume_total = col_integer(),
page_count = col_integer()
))
clean_place <- function(x) {
str_split(x, ",", n = 2) %>%
map_chr(1) %>%
str_replace_all("[[:punct:]]", "")
}
us_items <- us_items %>%
mutate(city = clean_place(imprint_city),
city = fct_recode(city,
"Unknown" = "Sl",
"Unknown" = "US",
"New York" = "NewYork",
"Boston" = "Boston New York",
"Cambridge" = "Cambridge Mass",
"New York" = "New York City",
"Washington" = "Washington DC"),
publication_year = lubridate::year(publication_date)) %>%
filter(publication_year > 1795,
publication_year < 1925)
```
##Loading Files
First I need to load my tort data.
```{r}
tort_par <- read_csv("C:/Users/Joshua/Documents/rdata/data/torts-paragraphs.csv",
col_types = cols(
.default = col_character(),
page_id = col_character(),
record_id = col_character(),
para_num = col_character()
))
```
###Manipulating Files to Load
This is organized by the paragraph level, so it needs organized by the document level.
```{r}
tort_doc <- tort_par %>%
group_by(document_id) %>%
summarize(text = str_c(text, collapse = " "))
```
Now I need to create individual files out of each line. First I am going to create a csv of my data at the document level.
[ write.csv(tort_doc, file = "tort_doc.csv") ]
Then I needed to create multiple text files out of that csv.I used code found online for the function [csv2txt](https://gist.github.com/benmarwick/9266072#file-csv2txts-r):
csv2txt("C:/Users/Joshua/Documents/rdata/tortdata", labels = 2).
This created an individual text file for each document.
Next, I loaded the tort and railroad data and combined them.
```{r}
tort_files <- list.files("C:/Users/Joshua/Documents/rdata/tortdata",
pattern = "*.txt",
full.names = TRUE)
rr_files <- list.files("C:/Users/Joshua/Documents/rdata/railroaddata/railroads_documents",
pattern = "*.txt",
full.names = TRUE)
rr_filesnotxt <- sapply(rr_files, FUN = function(eachPath) {file.rename(from = eachPath, to = sub(pattern = ".txt", replacement = "", eachPath))})
rr_filesnotxt <- str_replace(rr_files, "\\.txt", "")
basename(comb_files)
comb_files <- c(rr_files,tort_files)
```
##Tokenization
The next step was tokenization.
```{r createvocab, cache=TRUE}
reader <- function(f) {
require(stringr)
n <- basename(f) %>% str_replace("\\.txt", "")
doc <- readr::read_file(f)
names(doc) <- n
doc
}
it_files <- ifiles(comb_files, reader = reader)
it_tokens <- itoken(it_files,
tokenizer = tokenizers::tokenize_words)
vocab <- create_vocabulary(it_tokens)
pruned_vocab <- prune_vocabulary(vocab, term_count_min = 10,
term_count_max = 50000)
vectorizer <- vocab_vectorizer(pruned_vocab)
dtm <- create_dtm(it_tokens, vectorizer)
rownames(dtm) <- basename(comb_files)
comb_files
```
##Cosinesimilarity
After creating the dtm, I examined the cosinesimilarity. The histogram shows that the similarity is pretty high.
```{r}
distances <- dist2(dtm[1, , drop = FALSE], dtm[1:367, ])
distances2 <- distances[1, ] %>% sort()
head(distances2)
tail(distances2)
range(distances2)
similarities <- wordVectors::cosineSimilarity(dtm[1:367, , drop = FALSE],
dtm[1:367, , drop = FALSE])
similarities %>% View
```
##Principle Component Analysis
Performing a principle component analysis.
```{r}
dtm2 <- as.matrix(dtm)
pca <- prcomp(dtm2, scale. = FALSE)
plot(pca)
augment(pca) %>% select(1:6) %>% as_tibble() %>% View
augment(pca) %>%
ggplot(aes(.fittedPC1, .fittedPC2)) +
geom_point()
```
##K Means
Now I will see if using Kmeans reveals anything.Here I used two clusters to see if it would divide evenly between railroads and torts.This resulted in one cluster having 118 documents and the other 249. This indicates that there is some overlap as there are only 70 tort documents.
To check, I needed to compare the 118 files in cluster 1 with the 70 tort documents.
However, this revealed that the dtm files still contained the .txt extension. So I removed that and tried comparing the vectors containing the file names using == but that yeidled only one result.
That seemed odd so I tried using %in% and that yeilded 42 matches. This means that 42 tort documents are included in kluster 1 and 28 tort documents were clustered with railroad documents instead. Although this is not definative, it does show that some tort documents more closely resembled railraod documents.
This also indicates that the opposite is true. If there were 118 documents in cluster 1 and only 42 of them were tort documents, then 76 railroad documents were clustered with tort documents.
```{r}
# Kmeans
km <- kmeans(dtm, centers = 2)
k_clusters <- tibble(document_id = rownames(dtm),
cluster = km$cluster) %>%
left_join(us_items, by = "document_id")
k_clusters %>% arrange(cluster) %>% View
k1 <- k_clusters %>% filter(cluster == 1)
k1vector <- c(k1$document_id)
k1vector <- strsplit(k1vector, "\\.txt")
tortvector <- c(tort_doc$document_id)
tortvector == k1vector
matches <- (which(tortvector%in%k1vector))
tortvectorm <- tortvector[c(matches)]
mixedup <- (which(!tortvector%in%k1vector))
tortvectormx <- tortvector[c(mixedup)]
```
##Affinity propogation clustering
```{r}
# Affinity propagation clustering
clu <- apcluster(negDistMat(r = 2), dtm2, details = TRUE)
ap_clusters <- clu@clusters
names(ap_clusters) <- names(clu@exemplars)
ap_clusters <- lapply(ap_clusters, names)
ap_clusters <- map_df(names(ap_clusters), function(x) {
tibble(exemplar = x, document_id = ap_clusters[[x]])
})
ap_clusters %>% left_join(us_items, by = "document_id") %>% View
```