-
Notifications
You must be signed in to change notification settings - Fork 0
/
NYS-assembly.R
177 lines (131 loc) · 4.2 KB
/
NYS-assembly.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
# Load libraries
library(rvest)
library(stringr)
library(dplyr)
library(quanteda)
library(quanteda.textplots)
# install.packages("seededlda")
library(seededlda)
library(ggplot2)
# NYS Assembly Scraping ---------------------------------------------------
# NYS Assembly
homepage <- "https://nyassembly.gov/mem/"
directory <- read_html(homepage)
# Find all links to assembly members
leg_urls <-
directory |> html_nodes(".mem-name a") |> html_attr("href")
scrape <- function(url) {
url <- paste0("https://assembly.state.ny.us", url, "/")
page <- read_html(url)
name <-
page |> html_node('#head-mem-name') |> html_text(trim = TRUE)
district <-
page |> html_node('#head-mem-dist') |> html_text(trim = TRUE)
bioUrl <- paste0(url, "bio")
bioPage <- read_html(bioUrl)
biography <-
bioPage |> html_node('#biotext') |> html_text(trim = TRUE)
contactUrl <- paste0(url, "contact")
contactPage <- read_html(contactUrl)
email <-
contactPage |> html_node('.member-email') |> html_text(trim = TRUE)
results <- data.frame(name, district, biography, email)
return (results)
}
# create data frame
assembly <- data.frame(stringsAsFactors = F)
# cycle through all MPs
for (url in leg_urls) {
print(url)
assembly <- rbind(assembly, scrape(url))
}
# Ballotpedia Scraping & Merge -------------------------------------------
# Get additional data from Ballotpedia
bpUrl <- "https://ballotpedia.org/New_York_State_Assembly"
bpPage <- read_html(bpUrl)
bpTable <-
bpPage |> html_node('table#officeholder-table') |> html_table()
# Merge dataframes
bpTable$Office <- bpTable$Office |> str_remove("New York State ")
bpTable <-
bpTable |> rename(district = Office,
memberSince = `Date assumed office`,
party = Party)
fullAssembly <- merge(assembly, bpTable, by = "district")
# Cleaning & basic analysis -----------------------------------------------
# Convert Date joined into year for simplicity
fullAssembly$yearSince <-
fullAssembly$memberSince |> str_extract("\\d{4}") |> as.numeric()
# Check party composition
fullAssembly$party <- as.factor(fullAssembly$party)
summary(fullAssembly$party)
summary(fullAssembly$yearSince)
hist(fullAssembly$yearSince)
arrange(fullAssembly, yearSince) |> head()
# Create party subsets
assemblyDem <- fullAssembly |> filter(party == "Democratic")
assemblyRep <- fullAssembly |> filter(party == "Republican")
# Compare how long Dems & Rs are in the Assembly
summary(assemblyDem$yearSince)
summary(assemblyRep$yearSince)
# Visualize this as a boxplot
ageBox <-
fullAssembly |> filter(party == "Republican" |
party == "Democratic") |> ggplot(aes(x = yearSince)) +
geom_boxplot(position = "identity") +
facet_wrap( ~ party, ncol = 1)
ageBox
# or a violin plot
ageViolin <-
fullAssembly |> filter(party == "Republican" |
party == "Democratic") |> ggplot(aes(y = yearSince, x = party)) +
coord_flip()+
geom_violin(trim=FALSE)+
geom_boxplot(width=0.1) +
theme_minimal()
ageViolin
# Topic modeling ----------------------------------------------------------
tokenize_bios <- function (data){
bioTokens <-
tokens(
data$biography,
remove_punct = TRUE,
remove_numbers = TRUE,
remove_symbols = TRUE
)
bioTokens <-
tokens_remove(bioTokens, pattern = c(stopwords("en"), stopwords("es")))
return(bioTokens)
}
# Word clouds
bio_wordCloud <- function(data){
bioTokens <- tokenize_bios(data)
dfmat_bio <- dfm(bioTokens) |>
dfm_trim(
min_termfreq = 0.8,
termfreq_type = "quantile",
max_docfreq = 0.1,
docfreq_type = "prop"
)
plot(textplot_wordcloud(dfmat_bio))
}
bio_wordCloud(fullAssembly)
bio_wordCloud(assemblyDem)
bio_wordCloud(assemblyRep)
# Run topic models
topicTerms <- function(data, k = 6) {
# From https://tutorials.quanteda.io/machine-learning/topicmodel/
bioTokens <- tokenize_bios(data)
dfmat_bio <- dfm(bioTokens) |>
dfm_trim(
min_termfreq = 0.8,
termfreq_type = "quantile",
max_docfreq = 0.3,
docfreq_type = "prop"
)
tmodBio <- textmodel_lda(dfmat_bio, k)
return (terms(tmodBio, 10))
}
topicTerms(fullAssembly)
topicTerms(assemblyDem)
topicTerms(assemblyRep)