-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-se.r
170 lines (119 loc) · 5.89 KB
/
build-se.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
bills = subset(bills, type == "PROPOSITION DE LOI")
for (k in 53:49) { # too few bills in S. 54
cat("Sénat, législature", k,
years[ as.character(k) ], "to",
years[ as.character(k + 1) ])
data = subset(bills, legislature == k & n_au > 1)
cat(":", nrow(data), "cosponsored documents, ")
# ============================================================================
# DIRECTED EDGE LIST
# ============================================================================
edges = lapply(data$authors, function(d) {
w = unlist(strsplit(d, ";")) %>% as.integer
# remove missing sponsors; if first author is missing, first nonmissing
# cosponsor is used in replacement (happens only in very few cases)
w = w[ w %in% b$sid ]
if (length(w) > 0) {
e = expand.grid(i = b$nom[ b$sid %in% w ],
j = b$nom[ b$sid == w[1]], stringsAsFactors = FALSE)
return(data.frame(e, w = length(w) - 1)) # number of cosponsors
} else {
return(NULL)
}
}) %>% bind_rows
# ============================================================================
# EDGE WEIGHTS
# ============================================================================
# first author self-loops, with counts of cosponsors
self = subset(edges, i == j)
# count number of bills per first author
n_au = table(self$j)
# remove self-loops from directed edge list
edges = subset(edges, i != j)
# count number of bills cosponsored per sponsor
n_co = table(edges$i)
# identify directed ties
edges$ij = apply(edges[, 1:2 ], 1, paste0, collapse = "///")
# raw edge counts
raw = table(edges$ij)
# Newman-Fowler weights (weighted quantity of bills cosponsored)
edges = aggregate(w ~ ij, function(x) sum(1 / x), data = edges)
# expand to edge list
edges = data_frame(i = gsub("(.*)///(.*)", "\\1", edges$ij),
j = gsub("(.*)///(.*)", "\\2", edges$ij),
raw = as.vector(raw[ edges$ij ]), # raw edge counts
nfw = edges$w)
# Gross-Shalizi weights (weighted propensity to cosponsor)
edges = merge(edges, aggregate(w ~ j, function(x) sum(1 / x), data = self))
edges$gsw = edges$nfw / edges$w
# sanity check
stopifnot(edges$gsw <= 1)
# final edge set: cosponsor, first author, weights
edges = select(edges, i, j, raw, nfw, gsw)
cat(nrow(edges), "edges, ")
# ============================================================================
# DIRECTED NETWORK
# ============================================================================
n = network(edges[, 1:2 ], directed = TRUE)
n %n% "country" = meta[ "cty" ]
n %n% "country" = meta[ "cty" ] %>% as.character
n %n% "lang" = meta[ "lang" ] %>% as.character
n %n% "years" = paste0(years[ as.character(k) ], "-", years[ as.character(k + 1) ])
n %n% "legislature" = as.character(k)
n %n% "chamber" = meta[ "se" ] %>% as.character
n %n% "type" = meta[ "type-se" ] %>% as.character
n %n% "ipu" = meta[ "ipu-se" ] %>% as.integer
n %n% "seats" = meta[ "seats-se" ] %>% as.integer
n %n% "n_cosponsored" = nrow(data)
n %n% "n_sponsors" = table(subset(bills, legislature == k)$n_au)
# ============================================================================
# VERTEX-LEVEL ATTRIBUTES
# ============================================================================
n_au = as.vector(n_au[ network.vertex.names(n) ])
n %v% "n_au" = ifelse(is.na(n_au), 0, n_au)
n_co = as.vector(n_co[ network.vertex.names(n) ])
n %v% "n_co" = ifelse(is.na(n_co), 0, n_co)
n %v% "n_bills" = n %v% "n_au" + n %v% "n_co"
cat(network.size(n), "nodes\n")
rownames(b) = b$nom
n %v% "url" = b[ network.vertex.names(n), "url" ]
n %v% "sex" = b[ network.vertex.names(n), "sex" ]
n %v% "born" = b[ network.vertex.names(n), "born" ]
n %v% "party" = b[ network.vertex.names(n), "parti" ]
n %v% "partyname" = groups[ n %v% "party" ] %>% as.character
n %v% "lr" = scores[ n %v% "party" ] %>% as.numeric
n %v% "photo" = b[ network.vertex.names(n), "photo" ]
# mandate years done up to start year of legislature
b$nyears = sapply(b$mandate, function(x) {
sum(unlist(strsplit(x, ";")) <= as.numeric(years[ as.character(k) ]))
})
n %v% "nyears" = b[ network.vertex.names(n), "nyears" ] %>% as.integer
set.edge.attribute(n, "source", as.character(edges[, 1])) # cosponsor
set.edge.attribute(n, "target", as.character(edges[, 2])) # first author
set.edge.attribute(n, "raw", edges$raw) # raw edge counts
set.edge.attribute(n, "nfw", edges$nfw) # Newman-Fowler weights
set.edge.attribute(n, "gsw", edges$gsw) # Gross-Shalizi weights
# ============================================================================
# SAVE PLOTS
# ============================================================================
if (plot) {
save_plot(n, paste0("plots/net_be_se", years[ as.character(k) ], "-", years[ as.character(k + 1) ]),
i = colors[ b[ n %e% "source", "parti" ] ],
j = colors[ b[ n %e% "target", "parti" ] ],
mode, colors)
}
# ============================================================================
# SAVE OBJECTS
# ============================================================================
assign(paste0("net_be_se", years[ as.character(k) ]), n)
assign(paste0("edges_be_se", years[ as.character(k) ]), edges)
assign(paste0("bills_be_se", years[ as.character(k) ]), data)
# ============================================================================
# SAVE GEXF
# ============================================================================
if (gexf)
save_gexf(n, paste0("net_be_se", years[ as.character(k) ], "-", years[ as.character(k + 1) ]),
mode, colors)
}
if (gexf)
zip("net_be_se.zip", files = dir(pattern = "^net_be_se\\d{4}-\\d{4}\\.gexf$"))