-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgu5.R
234 lines (175 loc) · 7.1 KB
/
cgu5.R
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
######################################
# Loren Collingwood #
# UC Riverside #
# CGU Text Presentation #
# Topic Modeling #
# Marijuana Topics News coverage #
# Date: 5/22/2019 #
######################################
rm(list=ls())
library(quanteda)
library(topicmodels)
library(tm)
library(descr)
#################
# Set Directory #
#################
setwd("~/Dropbox/collingwood_research/cgu_workshop/text_analysis"); list.files()
############################
# Loading & Pre-processing #
############################
load("mj_nyt.Rdata") #loads "final_out" object, which is the dataframe object from above loop
objects()
dim(final_out)
########################
# Convert to Dataframe #
########################
final_out <- as.data.frame(final_out)
##############################
# Take Random Sample for now #
# (ease of computing) #
##############################
set.seed(492847)
samp <- sample(1:nrow(final_out), 3000)
final_out <- final_out[samp,]; head(final_out)
######################################
# Subset to relevant items (for now) #
######################################
final_qe <- final_out[,c("uniq_id", "year", "texts")]
dim(final_qe)
#############################
# Text Mining Create Corpus #
#############################
nyt_corpus <- corpus(final_qe, text_field="texts")
############################
# Label Document Variables #
############################
docnames(nyt_corpus) <- final_qe$uniq_id
summary(nyt_corpus)
###############################
# Remove Non-alpha Characters #
###############################
nyt_corpus <- tokens(nyt_corpus, remove_punct=T, remove_numbers = T)
#########################################
# Create Document Term/Frequency Matrix #
#########################################
nyt_dfm <- dfm(nyt_corpus, stem=T, remove= stopwords("english"))
###########################
# Look at top 20 features #
###########################
topfeatures(nyt_dfm, n=20)
#################################
# Trim Matrix Based on Sparsity #
#################################
smalldfm <- dfm_trim(nyt_dfm,sparsity=.991)
smalldfm
topfeatures(smalldfm, n=20)
#########################################
# Convert to Matrix so can remove words #
#########################################
sdfm_mat <- as.matrix(smalldfm) # Turn into matrix format for easier access
#############################################################################
# Remove Words that don't tell us anything in Topic Model, but are frequent #
#############################################################################
remove <- c("play", "mr", "new", "show", "like", "first", "one",
"will", "said", "say","two", "home", "get","go", "just","want",
"use", "peopl", "think", "know", "time", "can", "make", "way", "thing", "now",
"even", "place", "around", "ms", "includ", "charact", "also",
"man", "ask", "come", "look", "back", "work", "see", "seem", "got", "day",
"year", "call", "plan", "open", "room" ,"water", "men","last", "good",
"never","us","talk", "much", "take", "road", "live", "s", "someth", "still",
"lot", "tell", "s", "word", "well", "mani", "along", "told", "went", "tri",
"live")
######################
# Remove those words #
######################
sdfm_mat <- sdfm_mat[,!colnames(sdfm_mat) %in% remove]
#############################################################
# Clear out text with none of the words, after the Trimming #
#############################################################
zeros <- apply(sdfm_mat, 1, function(x) ifelse(sum(x) == 0, FALSE, TRUE))
sdfm_mat <- sdfm_mat[zeros,] # Regular Matrix but seems to work with LDA
dim(sdfm_mat)
#####################################################
# Calculate marijuana usage in text, for subsetting #
#####################################################
smalldfm <- as.data.frame(sdfm_mat)
table(smalldfm$marijuana)
###############################################################
# Take only texts with word marijuana appearing at least once #
###############################################################
smalldfm <- smalldfm[smalldfm$marijuana > 0 ,]
dim(smalldfm)
##########################
# Convert Back to Matrix #
##########################
sdfm_mat <- as.matrix(smalldfm)
dim(sdfm_mat)
############################################
# Set up Parameters for LDA/Gibbs Sampling #
# 15 Topic Model #
############################################
burnin = 1000
iter = 1000
keep = 50
thin <- 500
k <- 15
alpha <- 1/k # This improves the probability separation; very important
seed <- 48790 # Seed for replication #
##################
# Estimate Model #
##################
fitted <- LDA(sdfm_mat, k = k, method = "Gibbs",
control = list(alpha=alpha, burnin = burnin,
iter = iter, keep = keep, seed=seed) ) # may take some time to run #
##########
# Assess #
##########
get_terms(fitted, k=10)
##############################
# Gather Topic Probabilities #
##############################
topicProbabilities <- as.data.frame(fitted@gamma)
topProb <- apply(topicProbabilities, 1, max)
hist(topProb) # Decent
########################
# Extract Topics, etc. #
########################
ldaOut.topics <- as.data.frame(as.matrix(topics(fitted)))
ldaOut.topics$uniq_id <- row.names(ldaOut.topics)
colnames(ldaOut.topics)[1] <- "topic_15"
ldaOut.terms <- as.matrix(terms(fitted,6))
#######################################
# Merge Topic Model with Exist Datars #
#######################################
final_out <- merge(final_out, ldaOut.topics, by.x="uniq_id", by.y="uniq_id", all.x=T)
table(final_out$topic_15)
###########################
# Get Proportions by Year #
###########################
tabs <- CrossTable(final_out$year, final_out$topic_15, prop.r=T, prop.c=F, prop.t=F, prop.chisq = F)$prop.row
# Clean #
tabs <- tabs[row.names(tabs)!="2007",]
###########################
# Initiate Plot #
###########################
plot(row.names(tabs), tabs[,5], type="n", ylim=c(0,.17), bty="n", lwd=3, # Legalization/medicinal
ylab="Topic Percent of all articles",
xlab= "Year",
main = "Marijuana Newspaper Topic Model Across Time\n(NYT marijuana-related articles)")
lines(lowess(row.names(tabs), tabs[,7]), lty=1, lwd=3, col="blue") # Mexican Drug/Border
lines(lowess(row.names(tabs), tabs[,4]), lty=2, lwd=3, col="red") # Addiction
lines(lowess(row.names(tabs), tabs[,14]), lty=3, lwd=3, col="pink") # Courts
lines(lowess(row.names(tabs), tabs[,2]), lty=4, lwd=3, col="black") # Police/shooting/murder
lines(lowess(row.names(tabs), tabs[,9]), lty=5, lwd=3, col="green") # State Revenue/Tax
lines(lowess(row.names(tabs), tabs[,5]), lty=7, lwd=3, col="orange") # Legalization/Medicinal
legend("topleft",
bty="n",
lty=1:7,
lwd=3,
cex=.7,
legend=c("Mexico/Border", "Addiction", "Law and Courts",
"Police/Shoot/Murder","State Revenue",
"Legalization/Medicinal"),
col=c("blue", "red", "pink", "black", "green", "orange")
)