Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: Programming Problem


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: Programming Problem
Date   Fri, 19 Jul 2002 12:28:19 -0400

I think this will do it.  But definitely test it.

I am assuming that you have the following variables in the dataset:

	district (district identifier)
	seats (the number of seats to be awarded in the district)
	v1 v2 v3 ... (the number of votes for each party)

A line-by-line explanation follows...

**************
*Assumes variables: district, seats, v*

local divisors "1 2 3 4 5 6 7 8"

reshape long v, i(district) j(party)
local cur_div : word 1 of `divisors'
gen divisor=`cur_div'
gen curvotes = .
gen m = 0

qui sum seats, meanonly
local toaward = `r(max)'
forval i=1/`toaward' {
	replace curvotes=-(v/(divisor))
	bysort district (curvotes): replace m=m+1 if _n==1 & seats>=`i'
	local next=`i'+1
	local cur_div : word `next' of `divisors'
	bysort district (curvotes): replace divisor=`cur_div' if _n==1 &
seats>=`i'
}
drop divisor curvotes
reshape wide v m, i(district) j(party)


*************************************
	local divisors "1 2 3 4 5 6 7 8"
This sets a local macro with the sequence of divisors, so different
systems can be implemented.

	reshape long v, i(district) j(party)
This reshapes the data to long form, so there is one observation per
party, rather than one per district.

	local cur_div : word 1 of `divisors'
	gen divisor=`cur_div'

This generates a variable with the current divisor for each party.  To
start, this is just the first divisor for everyone.

	gen curvotes = .
	gen m = 0

The m variable will hold the number of seats awarded to this party

	qui sum seats, meanonly
	local toaward = `r(max)'

This figures out the largest number of seats to award.  The next line
loops from 1 to the largest number to award

	forval i=1/`toaward' {
		replace curvotes=-(v/(divisor))

This calculates the negative of the "current votes" for each party.  The
negative means that the largest vote-getter will sort to the top, rather
than the bottom, in what follows.

		bysort district (curvotes): replace m=m+1 if _n==1 &
seats>=`i'

This increments the votes by one for the first observation in each
district -- ie, the one with the largest current votes.  It only does
this,howver, if the seats to be awarded exceeds the current seat we are
awarding.

		local next=`i'+1
		local cur_div : word `next' of `divisors'

This grabs the next divisor from the list

		bysort district (curvotes): replace divisor=`cur_div' if
_n==1 & seats>=`i'

This updates the divisor for the district we just awarded a seat to.

	}
	drop divisor curvotes
	reshape wide v m, i(district) j(party)

And put the data back in long form.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index