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

ideal_points.R 5.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
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
  1. #Load packages
  2. require(quanteda)
  3. require(quanteda.textmodels)
  4. require(quanteda.textplots)
  5. require(tidyverse)
  6. #Read in data
  7. tweets <-
  8. #Read in tweets
  9. read_csv('data/tweets.csv') %>%
  10. #Join elected officials
  11. left_join(
  12. read_csv('data/elected_officials.csv') %>%
  13. #Pivot so each individual account is a row
  14. pivot_longer(
  15. cols = c('officialTwitter',
  16. 'campaignTwitter',
  17. 'othertwitter'),
  18. names_to = 'account_type',
  19. values_to = 'twitter',
  20. values_drop_na = TRUE
  21. ) %>%
  22. #convert to lowercase so the join works
  23. mutate('twitter' = tolower(twitter)),
  24. by = c('username' = 'twitter')) %>%
  25. #Drop unused columns
  26. select(State:account_type,
  27. language,
  28. username,
  29. tweet) %>%
  30. #Filter just to english tweets
  31. filter(language == 'en') %>%
  32. #Concatenate tweets by individual name
  33. group_by(Name) %>%
  34. mutate('tweets' = paste0(tweet, collapse = "")) %>%
  35. #Drop unneeded columns again
  36. select(!c(tweet,account_type, username)) %>%
  37. #Select unique rows
  38. distinct()
  39. # Governor ------------------------------------------------------------
  40. #Running the whole dataset is too big, so filter by office
  41. governors <-
  42. tweets %>%
  43. filter(office == 'Governor')
  44. #Convert tweets into tokens
  45. tokens_tweets <- tokens(governors$tweets,
  46. remove_punct = TRUE,
  47. remove_symbols = TRUE,
  48. remove_numbers = TRUE,
  49. remove_url = TRUE)
  50. #Convert tokens into DTM
  51. dfmat_tweets <- dfm(tokens_tweets,
  52. tolower = TRUE)
  53. #Specify 2 rows where index[1] is to the left of index[2]
  54. #Ex. 37 = OR Gov Kate Brown, 1 = AL Gov Kay Ivey
  55. #Fit wordfish to dtm
  56. #NOTE: this line may need to be run more than once
  57. tmod_wf <- textmodel_wordfish(dfmat_tweets,
  58. dir = c(37, 1),
  59. sparse = TRUE)
  60. #Create a column of labels
  61. governors$label <-
  62. paste0(
  63. governors$Name,
  64. ' (',
  65. substr(governors$Party,1,1),
  66. '-',
  67. governors$StateAbbr,
  68. ')',
  69. sep = ''
  70. )
  71. #Set labels
  72. tmod_wf$docs <-
  73. governors$label
  74. #View summary of ideal points
  75. summary(tmod_wf)
  76. #Add ideal points to governor DF
  77. governors$ideal_point <-
  78. tmod_wf$theta
  79. #Drop unneeded columns
  80. governors <-
  81. governors %>%
  82. select(!c(tweets, language))
  83. #Write to csv
  84. #write_csv(governors, 'data/govs_ideal.csv')
  85. #Same procedure in other sections
  86. textplot_scale1d(tmod_wf)
  87. # Lt. Governor --------------------------------------------------------
  88. #NOTE: MODEL DID NOT CONVERGE
  89. lt_govs <-
  90. tweets %>%
  91. filter(office == 'LtGov')
  92. tokens_tweets <- tokens(lt_govs$tweets,
  93. remove_punct = TRUE,
  94. remove_symbols = TRUE,
  95. remove_numbers = TRUE,
  96. remove_url = TRUE)
  97. dfmat_tweets <- dfm(tokens_tweets,
  98. tolower = TRUE)
  99. tmod_wf <- textmodel_wordfish(dfmat_tweets,
  100. dir = c(8, 43),
  101. sparse = TRUE)
  102. summary(tmod_wf)
  103. # governors$ideal_point <-
  104. # tmod_wf$theta
  105. #
  106. # governors <-
  107. # governors %>%
  108. # select(!c(tweets, language))
  109. #
  110. # write_csv(governors, 'data/govs_ideal.csv')
  111. # Sec. State ----------------------------------------------------------
  112. #NOTE: Model did not converge
  113. secstate <-
  114. tweets %>%
  115. filter(office == 'SecState')
  116. tokens_tweets <- tokens(secstate$tweets,
  117. remove_punct = TRUE,
  118. remove_symbols = TRUE,
  119. remove_numbers = TRUE,
  120. remove_url = TRUE)
  121. dfmat_tweets <- dfm(tokens_tweets,
  122. tolower = TRUE)
  123. tmod_wf <- textmodel_wordfish(dfmat_tweets,
  124. dir = c(17, 7),
  125. sparse = TRUE)
  126. # summary(tmod_wf)
  127. #
  128. # governors$ideal_point <-
  129. # tmod_wf$theta
  130. #
  131. # governors <-
  132. # governors %>%
  133. # select(!c(tweets, language))
  134. #
  135. # write_csv(governors, 'data/govs_ideal.csv')
  136. #
  137. # State AG ------------------------------------------------------------
  138. #NOTE: Model did not converge
  139. stateag <-
  140. tweets %>%
  141. filter(office == 'StateAG')
  142. tokens_tweets <- tokens(stateag$tweets,
  143. remove_punct = TRUE,
  144. remove_symbols = TRUE,
  145. remove_numbers = TRUE,
  146. remove_url = TRUE)
  147. dfmat_tweets <- dfm(tokens_tweets,
  148. tolower = TRUE)
  149. tmod_wf <- textmodel_wordfish(dfmat_tweets,
  150. dir = c(1, 21),
  151. sparse = TRUE)
  152. # Treasurer -----------------------------------------------------------
  153. treasurer <-
  154. tweets %>%
  155. filter(office == 'Treasurer')
  156. tokens_tweets <- tokens(treasurer$tweets,
  157. remove_punct = TRUE,
  158. remove_symbols = TRUE,
  159. remove_numbers = TRUE,
  160. remove_url = TRUE)
  161. dfmat_tweets <- dfm(tokens_tweets,
  162. tolower = TRUE)
  163. tmod_wf <- textmodel_wordfish(dfmat_tweets,
  164. dir = c(3, 1),
  165. sparse = TRUE)
  166. treasurer <-
  167. treasurer %>%
  168. select(!c(language, tweets))
  169. treasurer$label <-
  170. paste0(
  171. treasurer$Name,
  172. ' (',
  173. substr(treasurer$Party,1,1),
  174. '-',
  175. treasurer$StateAbbr,
  176. ')',
  177. sep = ''
  178. )
  179. treasurer$ideal_point <-
  180. tmod_wf$theta
  181. tmod_wf$docs <-
  182. treasurer$label
  183. textplot_scale1d(tmod_wf)
  184. write_csv(treasurer, 'data/treasurer_ideal.csv')
Tip!

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

Comments

Loading...