Claim this site
Pick a password:

import csv
from random import shuffle

# get data
f = open('/Users/dmatt/Documents/Wonkblog/111dwnominate.csv','rU')
reader = csv.reader(f)

# get headers, and fill in dictionary with csv data for easy access
headers = reader.next()
column = {}
for h in headers:
    column[h] = []
for row in reader:
    for h, v in zip(headers,row):
        column[h].append(v)

# pull out DW-NOMINATE scores
dw = column['dimen1']

# store differences between pre-random and random scores
diffs = []
absolute_diffs = []
randoms = []

# copy the dataset
for i in range (len(dw)):
    randoms.append(dw[i])

# store the median/mean differences for each trial
median_diff = []
median_absolute_diff = []

# repeat 100,000 times
for i in range(1,100000):
    shuffle(randoms)

    # calculate diffs
    for i in range(0,434):
        diffs.append(float(randoms[i]) - float(dw[i]))
        absolute_diffs.append(abs(float(randoms[i]) - float(dw[i])))

    # median
    median_diff.append(sorted(diffs)[217])
    median_absolute_diff.append(sorted(absolute_diffs)[217])

    # clear diffs arrays for new trial
    diffs = []
    absolute_diffs = []

# print average of median swings, both directional and absolute
print "Median directional swing: " + str(sum([float (i) for i in median_diff])/len(median_diff))
print "Median absolute swing: " + str(sum([float (i) for i in median_absolute_diff])/len(median_absolute_diff))

# open file
write = csv.writer(open('/Users/dmatt/Documents/Wonkblog/Random Legislature/random.csv', 'wb'))

# write headers
write.writerow(['directional','absolute'])

for i in range (len(median_diff)):
    write.writerow([median_diff[i],median_absolute_diff[i]])

changed August 10, 2012 history edit