Package 'nzelect'

Title: New Zealand Election Data
Description: Convenient access to New Zealand election results by voting place. Voting places have been matched to Regional Council, Territorial Authority, and Area Unit, to facilitate matching with additional data. Opinion polls since 2002 and some convenience analytical function are also supplied.
Authors: Peter Ellis [aut, cre]
Maintainer: Peter Ellis <[email protected]>
License: GPL-3
Version: 0.4.0
Built: 2024-11-14 04:44:50 UTC
Source: https://github.com/cran/nzelect

Help Index


New Zealand Election Data

Description

Convenient access to New Zealand election results by voting place. Voting places have been matched to Regional Council, Territorial Authority, and Area Unit, to facilitate matching with additional data. Opinion polls since 2002 and some convenience analytical function are also supplied.

Author(s)

Maintainer: Peter Ellis [email protected]


Allocate seats after election

Description

Allocates seats in Parliament after an election using the Sainte-Lague allocation formula

Usage

allocate_seats(votes, parties = names(votes), nseats = 120,
  threshold = 0.05, electorate = rep(0, length(votes)))

Arguments

votes

vector of vote proportions or counts.

parties

vector of names of parties.

nseats

number of seats to allocate. Note that in mixed systems such as New Zealand's where electorate is not all zero, there may be a "hangover" and total number of seats ends up larger than nseats due to parties that win an electorate seat but received less than 1/nseats of the vote.

threshold

minimum proportion of votes needed to be allocated a seat.

electorate

a numeric vector of same length as parties, for use in mixed-member proportional systems such as New Zealand's. If the ith electorate > 0, the ith party is allocated seats regardless of whether the party exceeded threshold. This is needed in a mixed-member proportional system such as New Zealand's.

Value

A list with two elements: a data frame seats_df and a vector seats_v, each of which provides information on the number of seats allocated to each party.

The data frame has four columns: proportionally_allocated, electorate_seats, final and party. In New Zealand, the number of "list MPs" for each party is final - electorate_seats.

The vector is the same as final in the data frame, with names equal to party.

Author(s)

Peter Ellis

References

http://www.elections.org.nz/voting-system/mmp-voting-system/sainte-lague-allocation-formula

See also Wikipedia on the Webster/Sainte-Lague method.

Examples

# From Wikipedia; should return 3, 2, 2:
allocate_seats(c(53000, 24000, 23000), nseats = 7, threshold = 0)

# From 2014 New Zealand election
votes <- c(National = 1131501, Labour = 604535, Green = 257359,
           NZFirst = 208300, Cons = 95598, IntMana = 34094, 
           Maori = 31849, Act = 16689, United = 5286,
           Other = 20411)
electorate = c(41, 27, 0, 
               0, 0, 0, 
               1, 1, 1,
               0)
               
# Actual result:               
allocate_seats(votes, electorate = electorate)

# Result if there were no 5% minimum threshold:
allocate_seats(votes, electorate = electorate, threshold = 0)$seats_v

General Election Results 2014

Description

New Zealand 2014 general election results by voting place

Usage

GE2014

Format

A data frame with 136,195 rows and 7 variables.

Details

  • ApproxLocation. Approximate location of voting place

  • VotingPlace. Exact location of voting place

  • Party. Party voted for (party vote) or party of candidate voted for (candidate vote)

  • Votes. Number of votes

  • Electorate. Electorate in which voters were enrolled. Note that this is not necessarily the physical location of the VotingPlace, so VotingPlace and Electorate have a many to many relationship

  • VotingType. Party (proportional representation) or Candidate (first past the vote). In New Zealand each voter has to vote for both an individual candidate to represent their electorate, and a party vote for the overall makeup of Parliament.

  • Candidate. If VotingType == "Candidate", the name of the candidate; otherwise NA

GE2014 is deprecated; please use nzge instead - it has results back to 2002 and is the one we will update in future.

Source

http://www.electionresults.govt.nz/electionresults_2014/e9/html/statistics.html

See Also

Locations2014

Examples

# the following matches the results published at
# http://www.electionresults.govt.nz/electionresults_2014/e9/html/e9_part1.html
library(tidyr)
library(dplyr)
GE2014 %>%
    mutate(VotingType = paste0(VotingType, "Vote")) %>%
    group_by(Party, VotingType) %>%
    summarise(Votes = sum(Votes)) %>%
    spread(VotingType, Votes) %>%
    select(Party, PartyVote, CandidateVote) %>%
    ungroup() %>%
    arrange(desc(PartyVote))

General Election Voting Places 2014

Description

Voting places for the New Zealand 2014 general election

Usage

Locations2014

Format

A data frame with 2,568 rows and 9 variables.

Details

  • ElectorateNumber

  • ElectorateName. Name of electorate in which the voting place is physically located.

  • VotingPlaceID.

  • NZTM2000Northing

  • NZTM2000Easting

  • WGS84Latitude

  • WGS84Longitude

  • VotingPlace. Address of the VotingPlace. Cross-references to GE2014$VotingPlace.

  • TA2014_NAM. Name of the Territorial Authority in which the voting place is physically located.

  • REGC2014_n. Name of the Regional Council in which the voting place is physically located.

  • AU2014. Number of the Area Unit in which the voting place is physically located.

  • MB2014. Number of the Mesh Block in which the voting place is physically located.

Locations2014 is deprecated; please use voting_places instead - it has locations back to 2008 and is the one we will update in future.

Source

http://www.electionresults.govt.nz/electionresults_2014/e9/html/statistics.html for the voting place locations. http://www.stats.govt.nz/browse_for_stats/Maps_and_geography/Geographic-areas/digital-boundary-files.aspx for the 2014 shapefiles for Regional Council, Territorial Authority, and Area Unit. See https://github.com/ellisp/nzelect/tree/master/prep for the code linking the two.

See Also

GE2014


General Election Results 2002 and onwards

Description

New Zealand general election results by voting place for 2002, 2005, 2008, 2011 and 2014

Usage

nzge

Format

A data frame with 728,602 rows and 9 variables.

Details

  • approx_location. Approximate location of voting place

  • vorting_place. Description of exact location of voting place

  • party. Party voted for (party vote) or party of candidate voted for (candidate vote)

  • votes. Number of votes

  • electorate. Electorate in which voters were enrolled. Note that this is not necessarily the physical location of the voting_place, so voting_place and electorate have a many to many relationship

  • voting_type. Party (proportional representation) or Candidate (first past the vote). In New Zealand each voter has to vote for both an individual candidate to represent their electorate, and a party vote for the overall makeup of Parliament.

  • candidate. If voting_type == "Candidate", the name of the candidate; otherwise NA

  • election_year. Year of the election.

  • electorate_number. Number of the electorate.

Source

http://www.electionresults.govt.nz/electionresults_2014/e9/html/statistics.html

See Also

voting_places

Examples

# the following matches the results published at
# http://www.electionresults.govt.nz/electionresults_2014/e9/html/e9_part1.html
library(tidyr)
library(dplyr)
nzge %>%
    mutate(voting_type = paste0(voting_type, " vote")) %>%
    group_by(party, voting_type, election_year) %>%
    summarise(votes = sum(votes)) %>%
    spread(voting_type, votes) %>%
    ungroup() %>%
    arrange(election_year, desc(`Party vote`))

New Zealand political parties

Description

Metadata associated with New Zealand political parties in the early 21st century

Usage

parties_df

Format

A data frame of colours, shades, short and long names

Source

https://en.wikipedia.org/wiki/Wikipedia:Index_of_New_Zealand_political_party_meta_attributes


New Zealand political party colours

Description

Colours associated with New Zealand political parties in the early 21st century

Usage

parties_v

Format

A named vector of colours

Source

https://en.wikipedia.org/wiki/Wikipedia:Index_of_New_Zealand_political_party_meta_attributes

Examples

# Example use of parties_v in a colour scale for ggplot2
if(require(ggplot2) & require(scales) & require(dplyr) & require(forcats)){
polls %>%
    filter(MidDate > as.Date("2014-11-20") & !is.na(VotingIntention)) %>%
    filter(Party %in% c("National", "Labour", "Green", "NZ First")) %>%
    mutate(Party = fct_reorder(Party, VotingIntention, .desc = TRUE),
           Party = fct_drop(Party)) %>%
    ggplot(aes(x = MidDate, y = VotingIntention, colour = Party, linetype = Pollster)) +
    geom_line(alpha = 0.5) +
    geom_point(aes(shape = Pollster)) +
    geom_smooth(aes(group = Party), se = FALSE, colour = "grey15", span = .4) +
    scale_colour_manual(values = parties_v) +
    scale_y_continuous("Voting intention", label = percent) +
    scale_x_date("") +
    facet_wrap(~Party, scales = "free_y") 
    }

New Zealand Opinion Polls

Description

Opinion polling of voting intention for New Zealand general elections

Usage

polls

Format

A data frame of 7 columns

Details

Intended party vote. Note the original source says 'Refusals are generally excluded from the party vote percentages, while question wording and the treatment of "don't know" responses and those not intending to vote may vary between survey organisations.'

EndData and StartDate refer to the data collection period. These dates were entered by hand, use with caution and check against the WikipediaDates column which is the version from the original source.

Where the date in Wikipedia is given only as "released on X", the start and end dates have been deemed to be two days before release.

The data for the 2005 election are particularly unreliable and in some cases it is not clear whether some parties have been omitted. For example, the Digipoll from 22 March to 30 March 2005 has figures only for National and Labour (47.5 and 34.5).

Source

https://en.wikipedia.org/wiki/Opinion_polling_for_the_New_Zealand_general_election,_2005 https://en.wikipedia.org/wiki/Opinion_polling_for_the_New_Zealand_general_election,_2008 https://en.wikipedia.org/wiki/Opinion_polling_for_the_New_Zealand_general_election,_2011 https://en.wikipedia.org/wiki/Opinion_polling_for_the_New_Zealand_general_election,_2014 https://en.wikipedia.org/wiki/Opinion_polling_for_the_New_Zealand_general_election,_2017

Examples

if(require(ggplot2) & require(scales) & require(dplyr) & require(forcats)){
election_dates <- polls %>%
    filter(Pollster == "Election result") %>%
    select(MidDate) %>%
    distinct()

polls %>%
    filter(Party %in% c("National", "Labour", "Green", "NZ First")) %>%
    mutate(Party = fct_reorder(Party, VotingIntention, .desc = TRUE),
           Pollster = fct_relevel(Pollster, "Election result")) %>%
    ggplot(aes(x = MidDate, y = VotingIntention, linetype = Pollster)) +
    geom_line(alpha = 0.5) +
    geom_point(aes(colour = Client), size = 0.7) +
    geom_smooth(aes(group = Party), se = FALSE, colour = "grey15", span = .20) +
    scale_y_continuous("Voting intention", label = percent) +
    scale_x_date("") +
    facet_wrap(~Party, scales = "free_y") +
    geom_vline(xintercept = as.numeric(election_dates$MidDate), colour = "grey80") 
}

General Election Voting Places 2008 and onwards

Description

Voting places for the New Zealand general elections in 2008, 2011 and 2014

Usage

voting_places

Format

A data frame with 7,909 rows and 15 variables.

Details

  • electorate_number

  • electorate. Name of electorate in which the voting place is physically located.

  • voting_place_id.

  • voting_place_suburb

  • northing Coordinates, on one of two incompatible systems - either NZTM2000 or NZMG - see the coordinate_system variable for which.

  • easting as per northing

  • longitude Use with caution.

  • latitude Use with caution.

  • voting_place. Address of the VotingPlace. Cross-references to nzge$voting_place.

  • election_year. Year in which this was a voting place for the election.

  • coordinate_system. Either NZMG (New Zealand Map Grid) or NZTM2000 (New Zealand Transverse Mercator Projection)

  • TA2014_NAM. Name of the Territorial Authority in which the voting place is physically located. Use with caution.

  • REGC2014_n. Name of the Regional Council in which the voting place is physically located. Use with caution.

  • AU2014. Number of the Area Unit in which the voting place is physically located. Use with caution.

  • MB2014. Number of the Mesh Block in which the voting place is physically located. Use with caution.

There are problems with the locations of many voting places which will be resolved in future versions.

Source

http://www.electionresults.govt.nz/electionresults_2014/e9/html/statistics.html (and similar for earlier years) for the voting place locations. http://www.stats.govt.nz/browse_for_stats/Maps_and_geography/Geographic-areas/digital-boundary-files.aspx for the 2014 shapefiles for Regional Council, Territorial Authority, and Area Unit. See https://github.com/ellisp/nzelect/tree/master/prep for the code linking the two.

See Also

nzge


Weight polls

Description

Create a vector of weights to use in calculating a weighted average

Usage

weight_polls(polldates, n = rep(1, length(polldates)), method = c("pundit",
  "curia"), refdate = Sys.Date(), electiondate = as.Date("2017-09-23"))

Arguments

polldates

a vector of dates of polls.

n

a vector of sample sizes of polls.

method

which weighting method to use; either that used in 2017 by Curia or Pundit (two New Zealand poll aggregators).

refdate

date against which to compare polling dates (both methods give more weight to more recent polls).

electiondate

date of the next election (the Curia weighting method gives more weight to polls close to the election).

Details

This function is experimental and so far it has not been possible to match published results. Use with caution.

This function is to facilitate reproduction of existing poll aggregation methods.

Both methods provide weights proportional to the sample sizes.

The Pundit Poll of Polls states its method is an adaptation of that used by fivethirtyeight. It gives polls a half life of 30 days, so a poll that is 120 days old gets 0.125 the weight of one conducted today.

The Curia method gives weight of 1 to all polls that are seven or less days old; 0 to polls older than 38 days; and a linear interpolation for inbetween. Note that this method gives zero weight to many older surveys that would get a weight in the pundit method.

Caution - as at March 2017, this function had failed to exactly replicate results on the webpages of Curia and Pundit.

Value

a vector of weights, adding up to one, for use in calculating a weighted average of opinion polls

References

http://www.curia.co.nz/methodology/ http://www.pundit.co.nz/content/poll-of-polls

Examples

polldates <- tail(unique(polls$MidDate), 20)
weight_polls(polldates, method = "curia", refdate = as.Date("2017-09-22"))
weight_polls(polldates, method = "pundit", refdate = as.Date("2017-09-22"))