Skip to content

Commit

Permalink
restructure prep folder so ready for importing other years of data
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisp committed Sep 30, 2017
1 parent 5f1a7d1 commit f3bdca4
Show file tree
Hide file tree
Showing 34 changed files with 1,325 additions and 68 deletions.
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ electorates <- c(1,0,0,27,0,1,41,1,1,0)
polls %>%
filter(MidDate > "2014-12-30" & MidDate < "2017-10-1") %>%
mutate(wt_p = weight_polls(MidDate, method = "pundit"),
wt_c = weight_polls(MidDate, method = "curia")) %>%
mutate(wt_p = weight_polls(MidDate, method = "pundit", refdate = as.Date("2017-09-22")),
wt_c = weight_polls(MidDate, method = "curia", refdate = as.Date("2017-09-22"))) %>%
group_by(Party) %>%
summarise(pundit_perc = round(sum(VotingIntention * wt_p, na.rm = TRUE) / sum(wt_p) * 100, 1),
curia_perc = round(sum(VotingIntention * wt_c, na.rm = TRUE) / sum(wt_c) * 100, 1)) %>%
Expand Down
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ GE2014 %>%
## 1 Gisborne Region 14342 0.351
## 2 Nelson Region 18754 0.398
## 3 Northland Region 53688 0.427
## 4 Wellington Region 165207 0.430
## 4 Wellington Region 164913 0.430
## 5 Manawatu-Wanganui Region 78841 0.447
## 6 Otago Region 75933 0.447
## 7 <NA> 934589 0.451
## 7 <NA> 935376 0.451
## 8 Hawke's Bay Region 53833 0.460
## 9 Tasman Region 17935 0.465
## 10 West Coast Region 12226 0.465
## 11 Bay of Plenty Region 89065 0.473
## 12 Auckland Region 478760 0.486
## 12 Auckland Region 478724 0.486
## 13 Waikato Region 134511 0.512
## 14 Canterbury Region 192577 0.520
## 14 Canterbury Region 192120 0.520
## 15 Marlborough Region 17474 0.520
## 16 Southland Region 36158 0.528
## 17 Taranaki Region 42586 0.552
Expand All @@ -168,16 +168,19 @@ GE2014 %>%
```

```
## # A tibble: 7 x 2
## VotingPlace TotalVotes
## <chr> <dbl>
## 1 Chatham Islands Council Building, 9 Tuku Road, Waitangi 90
## 2 Ordinary Votes BEFORE polling day 630775
## 3 Overseas Special Votes including Defence Force 38316
## 4 Special Votes BEFORE polling day 71362
## 5 Special Votes On polling day 151530
## 6 Votes Allowed for Party Only 40986
## 7 Voting places where less than 6 votes were taken 1530
## # A tibble: 10 x 2
## VotingPlace TotalVotes
## <chr> <dbl>
## 1 Chatham Islands Council Building, 9 Tuku Road, Waitangi 90
## 2 Mount Pleasant Community Centre, 3 Mccormacks Bay Road 457
## 3 Ordinary Votes BEFORE polling day 630775
## 4 Otaki Surf Lifesaving Club, Marine Parade 294
## 5 Overseas Special Votes including Defence Force 38316
## 6 Port Fitzroy Aotea Centre (Nurses Cottage) 36
## 7 Special Votes BEFORE polling day 71362
## 8 Special Votes On polling day 151530
## 9 Votes Allowed for Party Only 40986
## 10 Voting places where less than 6 votes were taken 1530
```

```r
Expand Down Expand Up @@ -301,8 +304,8 @@ electorates <- c(1,0,0,27,0,1,41,1,1,0)

polls %>%
filter(MidDate > "2014-12-30" & MidDate < "2017-10-1") %>%
mutate(wt_p = weight_polls(MidDate, method = "pundit"),
wt_c = weight_polls(MidDate, method = "curia")) %>%
mutate(wt_p = weight_polls(MidDate, method = "pundit", refdate = as.Date("2017-09-22")),
wt_c = weight_polls(MidDate, method = "curia", refdate = as.Date("2017-09-22"))) %>%
group_by(Party) %>%
summarise(pundit_perc = round(sum(VotingIntention * wt_p, na.rm = TRUE) / sum(wt_p) * 100, 1),
curia_perc = round(sum(VotingIntention * wt_c, na.rm = TRUE) / sum(wt_c) * 100, 1)) %>%
Expand Down Expand Up @@ -364,7 +367,7 @@ ggplot(Meshblocks2013, aes(x = WGS84Longitude, y = WGS84Latitude, colour = Media
```

```
## Warning: Removed 13 rows containing missing values (geom_point).
## Warning: Removed 642 rows containing missing values (geom_point).
```

![plot of chunk unnamed-chunk-10](figure/unnamed-chunk-10-1.png)
Expand Down
30 changes: 15 additions & 15 deletions build.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ library(grid)
# repeating expensive downloads

# About 1MB worth of voting results:
# source("prep/download_votingplace_results.R")
# source("prep/election_results/download_votingplace_results.R")

# About 130MB of shapefiles / maps, used for locating voting places in areas:
# source("prep/download_map_shapefiles.R")
# source("prep/census_related/download_map_shapefiles.R")

# source("prep/download_census2013.R")
# source("prep/census_related/download_census2013.R")

# download.file("http://www.electionresults.govt.nz/electionresults_2014/2014_Voting_Place_Co-ordinates.xls",
# destfile = "downloads/elect2014/vp_coordinates.xls", mode = "wb")

#----------tidying----------------
# import all the voting results CVS and amalgamate into a single object
source("prep/tidy_votingplace_results.R") # 30 seconds
source("prep/election_results/tidy_votingplace_results.R") # 30 seconds

# download and import the actual locations. Includes a 575KB download.
# This script also calls ./prep/match_locations_to_areas.R from within itself
# (takes a few minutes to run because of importing shapefiles, downloaded earlier):
source("prep/import_votingplace_locations.R") # 3 minutes
source("prep/election_results/import_votingplace_locations.R") # 3 minutes

# Import and tidy up census data
source("prep/import_census.R") # 30 seconds
source("prep/census_related/import_census.R") # 30 seconds

# Match census data to shapefiles so we have lat and long
source("prep/add_locations_census.R")
source("prep/census_related/add_locations_census.R")

# Create cartograms
# go to "prep/create-cartograms.R" and run by hand; requires some manual steps with ScapeToad
# go to "prep/census_related/create-cartograms.R" and run by hand; requires some manual steps with ScapeToad

#-------opinion polls and related-------------------
# Load in the existing version of the polls to facilitate checking and seeing
Expand All @@ -61,12 +61,12 @@ load("pkg1/data/polls.rda")
oldpolls <- polls
rm(polls)

source("prep/download_polls_2005.R")
source("prep/download_polls_2008.R")
source("prep/download_polls_2011.R")
source("prep/download_polls_2014.R")
source("prep/download_polls_2017.R")
source("prep/combine_polls.R")
source("prep/polls/download_polls_2005.R")
source("prep/polls/download_polls_2008.R")
source("prep/polls/download_polls_2011.R")
source("prep/polls/download_polls_2014.R")
source("prep/polls/download_polls_2017.R")
source("prep/polls/combine_polls.R")

expect_equal(oldpolls, polls[1:nrow(oldpolls), ])
polls_both <- cbind(oldpolls, polls[1:nrow(oldpolls), ])
Expand Down Expand Up @@ -99,7 +99,7 @@ knit("README.Rmd", "README.md")

# run CRAN checks
check("pkg1")
check("pkg2") # one note from exceeding 5MB
check("pkg2") # one note from exceeding 5MB, and the cartograms need helpfiles

# create vignettes for actual builds
build_vignettes("pkg1")
Expand Down
Binary file added examples/all-polls-and-years.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f3bdca4

Please sign in to comment.