Giter Site home page Giter Site logo

nominate's Introduction

This package is no longer maintained. I recommend using the dwnominate package for R instead of this gem. It should be more convenient in most cases.

nominate

The 'nominate' gem, to run W- and DW-NOMINATE from Ruby

W-NOMINATE uses rollcall votes to map legislators along a political spectrum. In American state and federal elections, it typically finds the traditional left-right spectrum. DW-NOMINATE uses legislators who have served in multiple legislative sessions to compare sessions and track changes through time.

Installation: sudo gem install nominate

Requirements

  • Linux
  • R
    In Ubuntu: sudo apt-get install r-base r-base-dev
  • GFortran (only for DW-NOMINATE)
    In Ubuntu: sudo apt-get install gfortran

Example

Let's say you have a file of voting data in the format

name1|party1|rollcall1 info|vote1
name2|party2|rollcall1 info|vote2
...
name1|party1|rollcall2 info|vote1
name2|party2|rollcall2 info|vote2
...

which is similar to what you can download from the New Hampshire state legislature. And these are collected in file '1999NHrollcalls.csv' and so forth.

The following code will read the rollcall data into Wnominate objects, and add the sessions to a Dwnominate object. chamber.dwnominate starts the nominate process, first calling wnominate on each Wnominate object, then using the results to write the input files for the DW-NOMINATE program, and then running DW-NOMINATE. All results, including output graphs from the R wnominate package, will be written to a folder called 'nominate'.

An alternative is to call wnominate(prefix) on Wnominate objects, which will run only the W-NOMINATE program, adding the specified prefix to each output file.

require 'nominate'

chamber = Dwnominate.new

# votes other than Yes or No are counted as missing ('M')
vote_key = Hash.new('M')
vote_key['Yes'] = 'Y'
vote_key['No'] = 'N'
# Wnominate object only accepts 'Y', 'N', and 'M' votes

['1999', '2001', '2003', '2005', '2007'].each do |year|
  file = year + 'NHrollcalls.csv'
  lines = IO.readlines(file)
  session = Wnominate.new
  # default party is 'unknown'
  parties = Hash.new('unknown')
  old_issue = 0
  # default vote is 'M' for 'missing'
  rollcall = Hash.new('M')
  lines.each_with_index do |line, i|
    data = line.split('|')
    name = data[0]
    party = data[1]
    issue = data[2]
    vote = vote_key[data[3].chomp]
    # every time we get to a new rollcall, add the hash with the old rollcall's votes to the
    # Wnominate object, and start a new rollcall hash.
    if issue != old_issue
      session.add_rollcall(rollcall) unless i == 0
      rollcall = Hash.new('M')
      old_issue = issue
    end
    rollcall[name] = vote
    parties[name] = party if not parties.has_key?(name)
    # get the last rollcall when you reach the end of the file
    session.add_rollcall(rollcall) if i == lines.length - 1
  end
  # add a hash of legislator names and parties if you want party info to be included
  session.parties = parties
  chamber.add_session(session)
end

chamber.dwnominate(prefix = 'dw_')

More Resources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.