Political piece is here. My attempt to develop a comprehensive suite to look at all aspects of a voter database.
require(plyr)
require(lattice)
# jpeg_create()
jpeg_create <- function() {
systime <- as.numeric(Sys.time())
# dev.new()
jpeg(filename = systime,
width = 1024, height = 768, units = "px", pointsize = 12,
quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
type = c("windows"))
Sys.sleep(2)
}
VDB_08.01.2014 <- read.delim("08.01.2014.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
VDB <- VDB_08.01.2014
LD42 <- read.csv('Precinct42.csv') # 119 Precincts for 2014
VDB <- subset(VDB, PrecinctID %in% LD42$PrecinctID42)
par(mfrow=c(1,2))
ddf <- arrange(count(VDB,"PrecinctID"),desc(freq))
jpeg_create()
barplot(ddf$freq,names.arg = (ddf$PrecinctID) ,xlab="PrecinctID",ylab="Precinct Count",col=rgb(1,0,0,.75))
ddf
PrecinctID freq
1 201 1290
2 208 1227
3 140 1110
4 229 1078
5 108 1063
6 302 1049
7 206 1034
8 146 1025
9 134 1012
10 601 1007
11 126 998
12 213 984
13 127 983
14 133 981
15 222 975
16 301 971
17 137 961
18 505 958
19 148 955
20 166 948
21 151 942
22 101 939
23 107 934
24 225 930
25 131 913
26 145 901
27 609 901
28 141 900
29 227 896
30 150 894
31 211 894
32 118 891
33 508 889
34 203 884
35 610 871
36 135 849
37 136 847
38 506 844
39 144 842
40 147 841
41 503 840
42 216 839
43 210 833
44 115 831
45 234 826
46 153 825
47 233 824
48 602 821
49 125 815
50 606 815
51 111 790
52 209 787
53 129 786
54 215 781
55 219 775
56 221 772
57 152 769
58 106 767
59 142 765
60 502 750
61 220 745
62 138 742
63 303 738
64 608 738
65 701 736
66 501 732
67 218 727
68 120 719
69 604 708
70 507 692
71 103 685
72 110 685
73 607 685
74 801 683
75 113 680
76 132 672
77 603 672
78 402 651
79 102 635
80 214 631
81 509 617
82 204 602
83 149 596
84 202 588
85 205 584
86 611 584
87 401 574
88 217 565
89 143 557
90 117 554
91 223 547
92 155 544
93 121 543
94 122 532
95 154 527
96 130 524
97 123 519
98 207 513
99 116 493
100 212 489
101 124 480
102 605 463
103 114 460
104 157 432
105 504 426
106 104 420
107 165 418
108 119 414
109 105 409
110 112 392
111 158 386
112 139 363
113 156 360
114 304 324
115 224 311
116 167 134
117 267 111
118 268 61
119 183 35
ddf <- arrange(count(VDB,"ResidenceCity"),desc(freq))
jpeg_create()
barplot(ddf$freq, names.arg = (ddf$ResidenceCity),xlab="Cities",ylab="City identified as Voter Residence",col=rgb(0,0,1,.75))
ddf
ResidenceCity freq
1 Bellingham 34523
2 Ferndale 13747
3 Lynden 12663
4 Blaine 10162
5 Everson 4946
6 Custer 1909
7 Deming 1798
8 Maple Falls 1755
9 Sumas 1415
10 Pt Roberts 939
11 Lummi Island 741
12 Nooksack 741
13 Acme 268
14 Glacier 189
15 Rockport 33
ddf <- arrange(count(VDB,"ResidenceZipCode"),desc(freq))
jpeg_create()
barplot(ddf$freq, names.arg = (ddf$ResidenceZipCode),xlab="Zip Codes",ylab="Zip Code Count",col=rgb(1,0,0,.75))
ddf
ResidenceZipCode freq
1 98226 20043
2 98225 14140
3 98248 13747
4 98264 12663
5 98230 10162
6 98247 4946
7 98244 1987
8 98240 1909
9 98266 1755
10 98295 1415
11 98281 939
12 98262 741
13 98276 741
14 98229 340
15 98220 268
16 98283 33
ddf <- arrange(count(VDB,"Gender"),desc(freq))
jpeg_create()
barplot(ddf$freq,names.arg = (ddf$Gender),xlab="Genders",ylab="Gender Count",col=rgb(0,0,1,.75))
ddf
Gender freq
1 F 44569
2 M 41259
3 1
ddf <- data.frame(table(substr(VDB$BirthDate,7,10)))
colnames(ddf) <- c("BirthYear","freq")
jpeg_create()
barplot(ddf$freq,names.arg = (ddf$BirthYear),xlab="Birth Years",ylab="Birth Year Counts",col=rgb(1,0,0,.75))
ddf
BirthYear freq
1 1905 1
2 1907 1
3 1910 1
4 1911 2
5 1912 4
6 1913 3
7 1914 10
8 1915 11
9 1916 31
10 1917 37
11 1918 57
12 1919 57
13 1920 109
14 1921 132
15 1922 131
16 1923 193
17 1924 218
18 1925 245
19 1926 279
20 1927 287
21 1928 337
22 1929 368
23 1930 458
24 1931 439
25 1932 460
26 1933 458
27 1934 544
28 1935 580
29 1936 632
30 1937 645
31 1938 717
32 1939 755
33 1940 803
34 1941 904
35 1942 1028
36 1943 1112
37 1944 1069
38 1945 1164
39 1946 1371
40 1947 1649
41 1948 1510
42 1949 1517
43 1950 1603
44 1951 1614
45 1952 1636
46 1953 1605
47 1954 1681
48 1955 1576
49 1956 1606
50 1957 1633
51 1958 1546
52 1959 1619
53 1960 1545
54 1961 1585
55 1962 1544
56 1963 1445
57 1964 1436
58 1965 1405
59 1966 1243
60 1967 1284
61 1968 1392
62 1969 1461
63 1970 1486
64 1971 1378
65 1972 1237
66 1973 1180
67 1974 1270
68 1975 1307
69 1976 1231
70 1977 1388
71 1978 1433
72 1979 1493
73 1980 1504
74 1981 1518
75 1982 1445
76 1983 1399
77 1984 1497
78 1985 1436
79 1986 1403
80 1987 1381
81 1988 1362
82 1989 1430
83 1990 1506
84 1991 1444
85 1992 1348
86 1993 1179
87 1994 1031
88 1995 599
89 1996 156
ddf <- data.frame(table(substr(VDB$RegistrationDate,7,10)))
colnames(ddf) <- c("RegistrationYear","freq")
jpeg_create()
barplot(ddf$freq,names.arg = (ddf$RegistrationYear),xlab="Registration by Year",ylab="Registration Year Counts",col=rgb(0,0,1,.75))
ddf
RegistrationYear freq
1 1936 1
2 1938 2
3 1940 4
4 1941 1
5 1942 8
6 1943 2
7 1944 19
8 1946 22
9 1947 5
10 1948 62
11 1949 3
12 1950 46
13 1951 3
14 1952 116
15 1953 1
16 1954 53
17 1955 3
18 1956 187
19 1957 2
20 1958 68
21 1959 6
22 1960 192
23 1961 4
24 1962 82
25 1963 22
26 1964 266
27 1965 28
28 1966 126
29 1967 82
30 1968 347
31 1969 68
32 1970 304
33 1971 259
34 1972 674
35 1973 107
36 1974 207
37 1975 220
38 1976 609
39 1977 137
40 1978 240
41 1979 282
42 1980 852
43 1981 157
44 1982 441
45 1983 416
46 1984 978
47 1985 151
48 1986 380
49 1987 301
50 1988 1204
51 1989 398
52 1990 674
53 1991 1014
54 1992 2678
55 1993 1642
56 1994 1554
57 1995 1787
58 1996 1579
59 1997 1005
60 1998 1037
61 1999 1182
62 2000 1821
63 2001 918
64 2002 1106
65 2003 1227
66 2004 5876
67 2005 2282
68 2006 4350
69 2007 3059
70 2008 10830
71 2009 3353
72 2010 5237
73 2011 5507
74 2012 8719
75 2013 4802
76 2014 2442
DF1 <- as.data.frame(xtabs(2014 - as.numeric(substr(BirthDate,7,10)) ~ PrecinctID,VDB))
DF2 <- as.data.frame(table(VDB$PrecinctID))
ddf <- data.frame(DF2, "AverageBirthYear"=DF1$Freq/DF2$Freq)
jpeg_create()
barplot(ddf$AverageBirthYear,names.arg=ddf$Var1,xlab="Average Age by Precinct",col=rgb(1,0,0,.75))
ddf
Var1 Freq AverageBirthYear
1 101 939 56.98296
2 102 635 54.70394
3 103 685 59.08321
4 104 420 61.28095
5 105 409 54.73105
6 106 767 50.11995
7 107 934 56.13062
8 108 1063 46.96707
9 110 685 51.03066
10 111 790 50.70886
11 112 392 49.68878
12 113 680 52.26471
13 114 460 52.35870
14 115 831 51.35138
15 116 493 46.46045
16 117 554 47.72924
17 118 891 47.33109
18 119 414 55.08937
19 120 719 50.45341
20 121 543 48.95764
21 122 532 52.31203
22 123 519 51.50482
23 124 480 51.13542
24 125 815 48.24417
25 126 998 54.83768
26 127 983 51.12208
27 129 786 51.79771
28 130 524 49.55344
29 131 913 52.36911
30 132 672 49.97917
31 133 981 48.71254
32 134 1012 51.85474
33 135 849 48.77150
34 136 847 48.28099
35 137 961 47.51093
36 138 742 57.89353
37 139 363 46.23967
38 140 1110 47.33514
39 141 900 49.52222
40 142 765 47.36863
41 143 557 49.70197
42 144 842 49.65083
43 145 901 54.48946
44 146 1025 48.75902
45 147 841 49.33175
46 148 955 48.72356
47 149 596 49.82886
48 150 894 49.89709
49 151 942 49.83652
50 152 769 51.21066
51 153 825 50.85818
52 154 527 52.09867
53 155 544 51.93934
54 156 360 50.94167
55 157 432 47.94213
56 158 386 51.36269
57 165 418 50.18182
58 166 948 50.06751
59 167 134 53.26119
60 183 35 49.65714
61 201 1290 51.51860
62 202 588 60.54592
63 203 884 52.52149
64 204 602 42.68605
65 205 584 50.32363
66 206 1034 44.55513
67 207 513 50.04094
68 208 1227 47.79951
69 209 787 49.46379
70 210 833 47.18968
71 211 894 51.52461
72 212 489 50.02658
73 213 984 55.47358
74 214 631 47.09192
75 215 781 46.06786
76 216 839 45.44696
77 217 565 57.56460
78 218 727 51.64924
79 219 775 47.76516
80 220 745 45.55034
81 221 772 51.06606
82 222 975 48.12821
83 223 547 42.59598
84 224 311 44.29904
85 225 930 43.42796
86 227 896 44.89955
87 229 1078 41.11874
88 233 824 46.69782
89 234 826 52.34988
90 267 111 45.20721
91 268 61 53.50820
92 301 971 49.28733
93 302 1049 50.79409
94 303 738 63.06233
95 304 324 51.24691
96 401 574 48.62195
97 402 651 44.20584
98 501 732 46.03825
99 502 750 45.07733
100 503 840 47.84167
101 504 426 48.95775
102 505 958 50.50209
103 506 844 47.73460
104 507 692 48.02168
105 508 889 48.98763
106 509 617 48.91896
107 601 1007 59.23833
108 602 821 54.61023
109 603 672 44.86310
110 604 708 45.56215
111 605 463 49.66523
112 606 815 50.14479
113 607 685 49.24818
114 608 738 51.26423
115 609 901 48.14872
116 610 871 56.08152
117 611 584 52.66781
118 701 736 45.60326
119 801 683 47.38946
DF1 <- as.data.frame(xtabs(2014 - as.numeric(substr(RegistrationDate,7,10)) ~ PrecinctID,VDB))
DF2 <- as.data.frame(table(VDB$PrecinctID))
ddf <- data.frame(DF2, "AverageRegistrationAge"=DF1$Freq/DF2$Freq)
jpeg_create()
barplot(ddf$AverageRegistrationAge,names.arg=ddf$Var1,xlab="Average Registration Age by Precinct",col=rgb(0,0,1,.75))
ddf
Var1 Freq AverageRegistrationAge
1 101 939 9.059638
2 102 635 11.066142
3 103 685 10.442336
4 104 420 12.073810
5 105 409 10.633252
6 106 767 8.411995
7 107 934 9.031049
8 108 1063 7.743180
9 110 685 12.392701
10 111 790 11.427848
11 112 392 11.775510
12 113 680 12.241176
13 114 460 13.008696
14 115 831 12.158845
15 116 493 12.363083
16 117 554 15.505415
17 118 891 13.913580
18 119 414 11.161836
19 120 719 12.431154
20 121 543 11.987109
21 122 532 13.567669
22 123 519 12.784200
23 124 480 14.479167
24 125 815 13.665031
25 126 998 11.497996
26 127 983 11.900305
27 129 786 13.017812
28 130 524 13.022901
29 131 913 12.392114
30 132 672 12.837798
31 133 981 10.818552
32 134 1012 14.532609
33 135 849 10.036514
34 136 847 11.044864
35 137 961 10.547347
36 138 742 12.760108
37 139 363 14.691460
38 140 1110 14.554955
39 141 900 13.917778
40 142 765 8.520261
41 143 557 10.321364
42 144 842 16.260095
43 145 901 14.579356
44 146 1025 14.043902
45 147 841 14.063020
46 148 955 9.965445
47 149 596 14.734899
48 150 894 14.259508
49 151 942 14.149682
50 152 769 14.230169
51 153 825 14.990303
52 154 527 14.593928
53 155 544 15.145221
54 156 360 15.233333
55 157 432 12.481481
56 158 386 13.945596
57 165 418 13.889952
58 166 948 12.967300
59 167 134 16.671642
60 183 35 4.857143
61 201 1290 7.756589
62 202 588 11.875850
63 203 884 9.864253
64 204 602 6.931894
65 205 584 13.982877
66 206 1034 9.386847
67 207 513 14.836257
68 208 1227 10.494703
69 209 787 11.691233
70 210 833 11.345738
71 211 894 9.907159
72 212 489 10.177914
73 213 984 13.465447
74 214 631 11.082409
75 215 781 11.172855
76 216 839 10.492253
77 217 565 14.697345
78 218 727 8.371389
79 219 775 10.837419
80 220 745 8.217450
81 221 772 11.123057
82 222 975 11.489231
83 223 547 9.488117
84 224 311 7.758842
85 225 930 6.541935
86 227 896 8.776786
87 229 1078 7.352505
88 233 824 11.983010
89 234 826 12.593220
90 267 111 9.666667
91 268 61 14.081967
92 301 971 10.975283
93 302 1049 9.878932
94 303 738 8.172087
95 304 324 8.858025
96 401 574 12.090592
97 402 651 10.374808
98 501 732 8.489071
99 502 750 10.649333
100 503 840 9.714286
101 504 426 11.035211
102 505 958 8.624217
103 506 844 10.252370
104 507 692 9.449422
105 508 889 9.424072
106 509 617 9.833063
107 601 1007 12.679245
108 602 821 13.855055
109 603 672 9.459821
110 604 708 10.209040
111 605 463 12.164147
112 606 815 12.298160
113 607 685 14.592701
114 608 738 13.054201
115 609 901 12.135405
116 610 871 15.526980
117 611 584 13.893836
118 701 736 10.179348
119 801 683 11.269400
DF1 <- as.data.frame(with(VDB,(table(Gender,PrecinctID))))
DFF <- subset(DF1,Gender=="F")
DFM <- subset(DF1,Gender=="M")
ddf <- data.frame("Male"=DFM,"Female"=DFF)
jpeg_create()
barplot(ddf$Female.Freq,names.arg=ddf$Female.PrecinctID,xlab="Girls by Precinct",ylim=c(0,800),col=rgb(1,0,0,.75))
barplot(ddf$Male.Freq,names.arg=ddf$Male.PrecinctID,xlab="Boys by Precinct",ylim=c(0,800),col=rgb(0,0,1,.75))
ddf
Male.Gender Male.PrecinctID Male.Freq Female.Gender Female.PrecinctID Female.Freq
3 M 101 488 F 101 451
6 M 102 314 F 102 321
9 M 103 339 F 103 346
12 M 104 203 F 104 217
15 M 105 200 F 105 209
18 M 106 375 F 106 392
21 M 107 435 F 107 499
24 M 108 519 F 108 544
27 M 110 346 F 110 339
30 M 111 384 F 111 406
33 M 112 196 F 112 196
36 M 113 343 F 113 337
39 M 114 225 F 114 235
42 M 115 422 F 115 409
45 M 116 267 F 116 226
48 M 117 284 F 117 270
51 M 118 449 F 118 442
54 M 119 215 F 119 199
57 M 120 363 F 120 356
60 M 121 268 F 121 275
63 M 122 262 F 122 270
66 M 123 256 F 123 263
69 M 124 229 F 124 251
72 M 125 388 F 125 427
75 M 126 495 F 126 503
78 M 127 476 F 127 507
81 M 129 382 F 129 404
84 M 130 260 F 130 264
87 M 131 445 F 131 468
90 M 132 327 F 132 345
93 M 133 461 F 133 520
96 M 134 498 F 134 514
99 M 135 419 F 135 430
102 M 136 415 F 136 432
105 M 137 452 F 137 509
108 M 138 359 F 138 383
111 M 139 189 F 139 174
114 M 140 582 F 140 528
117 M 141 458 F 141 442
120 M 142 380 F 142 385
123 M 143 293 F 143 264
126 M 144 416 F 144 426
129 M 145 435 F 145 466
132 M 146 518 F 146 507
135 M 147 400 F 147 441
138 M 148 483 F 148 472
141 M 149 297 F 149 299
144 M 150 464 F 150 430
147 M 151 473 F 151 469
150 M 152 385 F 152 384
153 M 153 426 F 153 399
156 M 154 270 F 154 257
159 M 155 272 F 155 272
162 M 156 177 F 156 183
165 M 157 206 F 157 226
168 M 158 190 F 158 196
171 M 165 199 F 165 219
174 M 166 451 F 166 497
177 M 167 64 F 167 70
180 M 183 17 F 183 18
183 M 201 531 F 201 759
186 M 202 223 F 202 365
189 M 203 382 F 203 502
192 M 204 282 F 204 320
195 M 205 283 F 205 301
198 M 206 485 F 206 549
201 M 207 262 F 207 251
204 M 208 554 F 208 673
207 M 209 381 F 209 406
210 M 210 392 F 210 441
213 M 211 392 F 211 502
216 M 212 238 F 212 251
219 M 213 449 F 213 535
222 M 214 277 F 214 354
225 M 215 374 F 215 407
228 M 216 391 F 216 448
231 M 217 254 F 217 311
234 M 218 302 F 218 425
237 M 219 347 F 219 428
240 M 220 327 F 220 418
243 M 221 357 F 221 415
246 M 222 441 F 222 534
249 M 223 261 F 223 286
252 M 224 155 F 224 156
255 M 225 461 F 225 469
258 M 227 421 F 227 475
261 M 229 534 F 229 544
264 M 233 439 F 233 385
267 M 234 407 F 234 419
270 M 267 54 F 267 57
273 M 268 33 F 268 28
276 M 301 461 F 301 510
279 M 302 520 F 302 529
282 M 303 363 F 303 375
285 M 304 162 F 304 162
288 M 401 269 F 401 305
291 M 402 311 F 402 340
294 M 501 359 F 501 373
297 M 502 346 F 502 404
300 M 503 415 F 503 425
303 M 504 192 F 504 234
306 M 505 404 F 505 553
309 M 506 398 F 506 446
312 M 507 328 F 507 364
315 M 508 412 F 508 477
318 M 509 308 F 509 309
321 M 601 450 F 601 557
324 M 602 386 F 602 435
327 M 603 318 F 603 354
330 M 604 342 F 604 366
333 M 605 227 F 605 236
336 M 606 385 F 606 430
339 M 607 319 F 607 366
342 M 608 324 F 608 414
345 M 609 436 F 609 465
348 M 610 375 F 610 496
351 M 611 259 F 611 325
354 M 701 356 F 701 380
357 M 801 341 F 801 342
# Looking By Age (Decade)
library(RPostgreSQL)
library(lattice)
con <- dbConnect(PostgreSQL(), user= "postgres", password="password", port="2345", host="localhost", dbname="WC2014_08_01_2014")
Decade_BD_1980_All <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from Birthdate) from voterdb where extract(DECADE from Birthdate)::numeric > 198 Group By PrecinctID,date_part Order by Count DESC;")
Decade_BD_1980_42 <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from Birthdate) from voterdb where extract(DECADE from Birthdate)::numeric > 198 and PrecinctID in (Select * from LD42) Group By PrecinctID,date_part Order by Count DESC;")
Decade_RD_1980_All <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from RegistrationDate) from voterdb where extract(DECADE from RegistrationDate)::numeric > 198 Group By PrecinctID,date_part Order by Count DESC;")
> Decade_RD_1980_42 <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from RegistrationDate) from voterdb where extract(DECADE from RegistrationDate)::numeric > 198 and PrecinctID in (Select * from LD42) Group By PrecinctID,date_part Order by Count DESC;")
Decade_BD_1990_All <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from Birthdate) from voterdb where extract(DECADE from Birthdate)::numeric > 199 Group By PrecinctID,date_part Order by Count DESC;")
Decade_BD_1990_42 <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from Birthdate) from voterdb where extract(DECADE from Birthdate)::numeric > 199 and PrecinctID in (Select * from LD42) Group By PrecinctID,date_part Order by Count DESC;")
Decade_RD_1990_All <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from RegistrationDate) from voterdb where extract(DECADE from RegistrationDate)::numeric > 199 Group By PrecinctID,date_part Order by Count DESC;")
Decade_RD_1990_42 <- dbGetQuery(con,"Select PrecinctID,Count(PrecinctID),extract(DECADE from RegistrationDate) from voterdb where extract(DECADE from RegistrationDate)::numeric > 199 and PrecinctID in (Select * from LD42) Group By PrecinctID,date_part Order by Count DESC;")
sum(Decade_BD_1980_All$count)
[1] 14054
sum(Decade_BD_1980_42$count)
[1] 7263
sum(Decade_RD_1980_All$count)
[1] 112266
> sum(Decade_RD_1980_42$count)
[1] 75681
sum(Decade_BD_1990_All$count)
[1] 0
sum(Decade_BD_1990_42$count)
[1] 0
sum(Decade_RD_1990_All$count)
[1] 92322
sum(Decade_RD_1990_42$count)
[1] 61529
jpeg_create()
plot.new()
with(Decade_BD_1980_42,(barplot(count,names.arg=precinctid,xlab="42LD Birthdays later than 1980 by Precinct")))
mtext(sum(Decade_BD_1980_42$count),side=1,line=2)
jpeg_create()
plot.new()
with(Decade_RD_1980_42,(barplot(count,names.arg=precinctid,xlab="42LD Registration Dates later than 1980 by Precinct")))
mtext(sum(Decade_RD_1980_42$count),side=1,line=2)
jpeg_create()
plot.new()
with(Decade_BD_1980_42,(barchart(~count | as.factor(precinctid))))
mtext("42LD Birthdays later than 1980 by Precinct",side=1,line=0)
jpeg_create()
plot.new()
with(Decade_RD_1980_42,(barchart(~count | as.factor(precinctid))))
mtext("42LD Registration Dates later than 1980 by Precinct",side=1,line=0)
graphics.off()
No comments:
Post a Comment