-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path007-completeness-of-data.Rmd
More file actions
103 lines (77 loc) · 2.87 KB
/
Copy path007-completeness-of-data.Rmd
File metadata and controls
103 lines (77 loc) · 2.87 KB
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
---
title: "Checking completeness of data"
output: html_notebook
---
## Preliminary
```{r}
suppressPackageStartupMessages(library(tidyverse))
library(formattable)
```
Read in the relevant data
```{r, message=FALSE}
meae_maps <- read_csv("elections-data/maps.csv")
meae_maps_to_elections <- read_csv("elections-data/maps-to-elections.csv")
meae_elections <- read_csv("elections-data/elections.csv")
meae_congress <- read_csv("elections-data/congressional-counties.csv")
```
## Checking maps and maps-to-elections
Jordan has completed the list of maps to elections. How many maps are there? These two should be equal.
```{r}
meae_maps %>% nrow()
meae_maps_to_elections %>% count(meae_id) %>% nrow()
```
Also, an anti-join of these two tables should return 0 mismatches when run in both directions.
```{r}
meae_maps %>%
anti_join(meae_maps_to_elections, by = "meae_id")
meae_maps_to_elections %>%
anti_join(meae_maps, by = "meae_id")
```
## Checking completeness of maps by state and Congress
Now we want to check that there contiguous runs for each state. In other words, once a state has a congression election, it should keep having the congressional elections.
```{r}
meae_maps %>%
select(state, congress) %>%
mutate(available = "✔") %>%
spread(state, available, fill = "") %>%
knitr::kable()
```
## Checking maps-to-elections and elections
Now how many elections IDs that are present in the maps-to-elections table are not present in the elections table?
```{r}
meae_maps %>%
left_join(meae_maps_to_elections, by = "meae_id") %>%
anti_join(meae_elections, by = "election_id") %>%
select(election_id, meae_id, everything())
```
Previously there was a problem here but Jordan fixed it.
## Checking how many maps have data available
We will say that a map has data available if there is at least one non-zero vote recorded for that election (in the NNV sense).
```{r}
elections_present <- meae_congress %>%
filter(vote > 0) %>%
count(election_id)
meae_maps %>%
left_join(meae_maps_to_elections, by = "meae_id") %>%
left_join(meae_elections, by = "election_id") %>%
anti_join(elections_present, by = "election_id") %>%
rename(state = state.x, congress = congress.x) %>%
count(meae_id, state, congress) %>%
arrange(desc(n)) %>%
select(-meae_id) %>%
spread(state, n, fill = "") %>%
knitr::kable()
```
Now let's get a list of those missing NNV election IDs.
```{r}
elections_missing_data <- meae_maps %>%
left_join(meae_maps_to_elections, by = "meae_id") %>%
left_join(meae_elections, by = "election_id") %>%
anti_join(elections_present, by = "election_id") %>%
rename(state = state.x, congress = congress.x) %>%
select(-state.y, -congress.y) %>%
arrange(state, congress)
elections_missing_data
write_csv(elections_missing_data,
"~/dev/mapping-elections/data-cleaning/missing-congressional-counties/elections-missing-data.csv")
```