Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# Parse a data table from pollster.com
# Get data via copy-and-paste from http://www.pollster.com/polls/us/08-us-pres-ge-mvo.php
# yields a messy tab-separated thingamajigger (i'm using firefox 3 on mac)
# This script normalizes, in an R-friendly way
require 'date'
def numclean(x)
x =~ /^-$/ ? "NA" : x.to_i
end
data = open("data.orig").readlines.map{|l| l.split("\t").map{|x| x.strip}}.reject{|x| x.size<8}
records = data[1..-1].map do |parts|
date = parts[1]
if date =~ %r[^(\d+)/(\d+)-(\d+)/(0\d)$]
m1=m2 = $1
d1,d2 = $2,$3
y1=y2 = $4
elsif date =~ %r[^(\d+)/(\d+)/(0\d)$]
m1=m2 = $1
d1=d2 = $2
y1=y2 = $3
elsif date =~ %r[^(\d+)/(\d+)-(\d+)/(\d+)/(0\d)$]
m1,d1,m2,d2 = $1,$2,$3,$4
y1=y2 = $5
else
raise "cant parse date: #{date}"
end
date1 = Date.new(2000+y1.to_i, m1.to_i, d1.to_i)
date2 = Date.new(2000+y2.to_i, m2.to_i, d2.to_i)
n_pop = parts[2].split
if n_pop.size == 2
n = n_pop[0].to_i
pop = n_pop[1]
elsif n_pop.size == 1
if n_pop[0] =~ /^\d+/
n = n_pop[0].to_i; pop="NA"
else
n = "NA"; pop=n_pop[0]
end
else
raise "wtf: #{parts[2]}"
end
{
:pollster => parts[0],
:date1 => date1.to_s,
:date2 => date2.to_s,
:n => n,
:pop => pop,
:mccain => numclean(parts[3]),
:obama => numclean(parts[4]),
:other => numclean(parts[5]),
:undec => numclean(parts[6]),
:nv => numclean(parts[7])
}
end
fields = [:pollster,:date1,:date2,:n,:pop,:mccain,:obama,:other,:undec,:nv]
puts fields.join("\t")
records.each{|r| puts fields.map{|f| r[f]}.join("\t")}
pollster date1 date2 n pop mccain obama other undec nv
Zogby (Internet) 2008-08-12 2008-08-14 3339 LV 42 44 NA 14 NA
Gallup 2008-08-12 2008-08-14 2690 RV 44 44 6 6 NA
Economist/YouGov 2008-08-11 2008-08-13 1000 A 40 41 4 12 2
Rasmussen 2008-08-10 2008-08-12 3000 LV 46 48 NA NA NA
Gallup 2008-08-09 2008-08-11 2656 RV 42 47 1 10 NA
Pew 2008-07-31 2008-08-10 2414 RV 43 46 NA 11 NA
Rasmussen 2008-08-07 2008-08-09 3000 LV 46 48 NA NA NA
IBD/TIPP 2008-08-04 2008-08-09 925 RV 38 43 NA 19 NA
Gallup 2008-08-06 2008-08-08 2686 RV 42 47 1 10 NA
Harris Interactive 2008-08-01 2008-08-07 2488 RV 38 47 8 7 NA
Rasmussen 2008-08-04 2008-08-06 3000 LV 46 47 NA NA NA
Economist/YouGov 2008-08-04 2008-08-06 1000 A 39 42 4 13 1
Gallup 2008-08-03 2008-08-05 2691 RV 44 46 1 10 NA
CBS 2008-07-31 2008-08-05 851 RV 39 45 NA 15 NA
AP-Ipsos 2008-07-31 2008-08-04 833 RV 41 47 10 3 NA
Time 2008-07-31 2008-08-04 808 LV 41 46 3 10 NA
Rasmussen 2008-08-01 2008-08-03 3000 LV 47 46 NA NA NA
Gallup 2008-07-31 2008-08-02 2684 RV 44 45 1 11 NA
Zogby 2008-07-31 2008-08-01 1011 LV 42 41 4 13 NA
Rasmussen 2008-07-29 2008-07-31 3000 LV 46 47 NA NA NA
Gallup 2008-07-28 2008-07-30 2679 RV 44 45 1 12 NA
Economist/YouGov 2008-07-28 2008-07-29 1000 A 37 44 5 14 1
CNN 2008-07-27 2008-07-29 914 RV 44 51 1 4 NA
Rasmussen 2008-07-26 2008-07-28 3000 LV 46 47 NA NA NA
Research 2000 2008-07-25 2008-07-27 1100 LV 39 51 5 5 NA
Gallup 2008-07-25 2008-07-27 2674 RV 40 48 1 10 NA
USA Today/Gallup 2008-07-25 2008-07-27 791 LV 49 45 2 4 NA
Pew 2008-07-23 2008-07-27 1241 RV 42 47 NA 11 NA
Rasmussen 2008-07-23 2008-07-25 3000 LV 43 49 NA NA NA
Economist/YouGov 2008-07-22 2008-07-24 1000 A 38 41 5 14 2
Gallup 2008-07-22 2008-07-24 2695 RV 41 47 1 11 NA
Democracy Corps (D) 2008-07-21 2008-07-24 735 LV 43 49 5 2 NA
FOX 2008-07-22 2008-07-23 900 RV 37 40 3 17 NA
Rasmussen 2008-07-20 2008-07-22 3000 LV 45 47 NA NA NA
Gallup 2008-07-19 2008-07-21 2645 RV 42 45 1 12 NA
NBC/WSJ 2008-07-18 2008-07-21 1003 RV 41 47 4 8 NA
Rasmussen 2008-07-17 2008-07-19 3000 LV 45 47 NA NA NA
Gallup 2008-07-16 2008-07-18 2657 RV 43 45 1 10 NA
Economist/YouGov 2008-07-15 2008-07-17 1000 A 36 41 4 13 5
Rasmussen 2008-07-14 2008-07-16 3000 LV 46 46 NA NA NA
Gallup 2008-07-13 2008-07-15 2638 RV 44 47 1 9 NA
CBS/Times 2008-07-07 2008-07-14 1534 RV 39 45 5 11 NA
Rasmussen 2008-07-11 2008-07-13 3000 LV 46 47 NA NA NA
ABC/Post 2008-07-10 2008-07-13 NA LV 46 49 3 2 NA
Zogby/Reuters 2008-07-09 2008-07-13 1039 LV 40 47 3 10 NA
Quinnipiac 2008-07-08 2008-07-13 1725 LV 41 50 2 7 NA
Gallup 2008-07-10 2008-07-12 2635 RV 43 46 1 10 NA
IBD/TIPP 2008-07-07 2008-07-11 854 RV 37 40 4 18 NA
Harris Interactive 2008-07-03 2008-07-11 NA RV 35 44 7 16 NA
Newsweek 2008-07-09 2008-07-10 1037 RV 41 44 NA 15 NA
Rasmussen 2008-07-08 2008-07-10 3000 LV 45 47 NA NA NA
Gallup 2008-07-07 2008-07-09 2647 RV 43 46 1 10 NA
Economist/YouGov 2008-07-07 2008-07-09 1000 A 38 39 4 14 5
Rasmussen 2008-07-02 2008-07-07 3000 LV 43 49 NA NA NA
Gallup 2008-07-03 2008-07-06 2650 RV 43 47 1 10 NA
Economist/YouGov 2008-07-01 2008-07-02 1000 A 34 37 3 17 9
Gallup 2008-06-30 2008-07-02 2641 RV 43 47 1 10 NA
Rasmussen 2008-06-29 2008-07-01 3000 LV 44 49 NA NA NA
Gallup 2008-06-26 2008-06-29 2656 RV 42 47 1 10 NA
CNN 2008-06-26 2008-06-29 906 RV 45 50 NA 5 NA
McLaughlin (R) 2008-06-26 2008-06-29 1000 LV 38 46 NA 16 NA
Pew 2008-06-18 2008-06-29 1574 RV 40 48 NA 12 NA
Rasmussen 2008-06-26 2008-06-28 3000 LV 43 49 NA NA NA
Rasmussen 2008-06-23 2008-06-25 3000 LV 45 49 NA NA NA
Gallup 2008-06-23 2008-06-25 2605 RV 44 44 1 11 NA
Economist/YouGov 2008-06-23 2008-06-25 1000 A 34 36 4 17 8
Democracy Corps (D) 2008-06-22 2008-06-25 2000 LV 45 49 2 3 NA
Time 2008-06-18 2008-06-25 805 LV 43 47 2 8 NA
Times/Bloomberg 2008-06-19 2008-06-23 1115 RV 37 49 4 10 NA
Rasmussen 2008-06-20 2008-06-22 3000 LV 43 49 NA NA NA
Gallup 2008-06-20 2008-06-22 2608 RV 43 46 1 9 NA
Franklin and Marshall/Hearst-Argyle 2008-06-16 2008-06-22 1501 RV 36 42 7 15 NA
Newsweek 2008-06-18 2008-06-19 896 RV 36 51 NA 13 NA
Rasmussen 2008-06-17 2008-06-19 3000 LV 44 48 NA NA NA
Gallup 2008-06-16 2008-06-19 2648 RV 44 46 1 9 NA
USA Today/Gallup 2008-06-15 2008-06-19 1310 LV 44 50 NA NA NA
FOX 2008-06-17 2008-06-18 900 RV 39 42 6 14 NA
Economist/YouGov 2008-06-16 2008-06-17 1000 A 34 37 5 16 8
Rasmussen 2008-06-14 2008-06-16 3000 LV 44 48 NA NA NA
Gallup 2008-06-13 2008-06-15 2607 RV 42 46 1 12 NA
Cook/RT 2008-06-12 2008-06-15 880 RV 40 44 1 15 NA
ABC/Post 2008-06-12 2008-06-15 NA LV 48 47 3 2 NA
Zogby/Reuters 2008-06-12 2008-06-14 1113 LV 42 47 NA 12 NA
Rasmussen 2008-06-11 2008-06-13 3000 LV 43 49 NA NA NA
Harris Interactive 2008-06-05 2008-06-13 2327 A 32 42 14 12 NA
Gallup 2008-06-10 2008-06-12 2714 RV 43 46 1 10 NA
Economist/YouGov 2008-06-10 2008-06-11 1000 A 33 37 5 15 10
AP-Ipsos 2008-06-05 2008-06-11 467 LV 43 50 5 1 NA
Rasmussen 2008-06-08 2008-06-10 3000 LV 44 49 NA NA NA
Gallup 2008-06-07 2008-06-09 2633 RV 41 48 1 10 NA
NBC/WSJ 2008-06-06 2008-06-09 1000 RV 41 47 4 8 NA
Diageo/Hotline 2008-06-05 2008-06-08 806 RV 42 44 NA 11 NA
IBD/TIPP 2008-06-02 2008-06-08 916 RV 40 43 NA 17 NA
Rasmussen 2008-06-05 2008-06-07 3000 LV 43 50 NA NA NA
Gallup 2008-06-02 2008-06-06 4408 RV 45 46 1 8 NA
CNN 2008-06-04 2008-06-05 921 RV 46 49 NA 5 NA
Rasmussen 2008-06-02 2008-06-04 3000 LV 45 47 NA NA NA
CBS 2008-05-30 2008-06-03 930 RV 42 48 3 7 NA
USA Today/Gallup 2008-05-30 2008-06-01 803 LV 44 49 NA NA NA
Rasmussen 2008-05-29 2008-06-01 1600 LV 45 45 NA NA NA
Gallup 2008-05-28 2008-06-01 4378 RV 46 46 1 7 NA
Cook/RT 2008-05-29 2008-05-31 802 RV 43 44 3 10 NA
Economist/YouGov 2008-05-27 2008-05-28 1000 A 48 38 NA 15 NA
Lombardo (R) 2008-05-26 2008-05-28 1000 RV 40 44 NA NA NA
Rasmussen 2008-05-25 2008-05-28 1600 LV 47 42 NA NA NA
Gallup 2008-05-22 2008-05-27 4411 RV 46 45 1 8 NA
Democracy Corps (D) 2008-05-19 2008-05-26 1600 LV 47 47 2 3 NA
Pew 2008-05-21 2008-05-25 1242 RV 44 47 NA 9 NA
Rasmussen 2008-05-21 2008-05-24 1600 LV 46 44 NA NA NA
Newsweek 2008-05-21 2008-05-22 1205 RV 46 46 NA 8 NA
Economist/YouGov 2008-05-20 2008-05-21 1000 A 41 43 NA 16 NA
Gallup 2008-05-17 2008-05-21 4446 RV 44 47 1 7 NA
Rasmussen 2008-05-17 2008-05-20 1600 LV 45 44 NA NA NA
Harris Interactive 2008-05-08 2008-05-19 2317 A 35 39 13 11 NA
Zogby 2008-05-15 2008-05-18 1076 LV 40 48 NA NA NA
IBD/TIPP 2008-05-12 2008-05-18 876 RV 37 48 NA 15 NA
Rasmussen 2008-05-13 2008-05-16 1600 LV 45 44 NA NA NA
Gallup 2008-05-12 2008-05-16 4385 RV 47 44 1 8 NA
Economist/YouGov 2008-05-14 2008-05-15 1000 A 42 39 NA 19 NA
GWU (Lake/Tarrance) 2008-05-11 2008-05-14 1018 LV 47 49 NA 5 NA
Democracy Corps (D) 2008-05-13 2008-05-13 1014 LV 46 48 2 4 NA
Rasmussen 2008-05-09 2008-05-12 1600 LV 46 47 NA NA NA
Quinnipiac 2008-05-08 2008-05-12 1245 RV 40 47 2 8 4
ABC/Post 2008-05-08 2008-05-11 1122 A 44 51 3 1 1
Gallup 2008-05-07 2008-05-11 4370 RV 43 47 1 8 NA
NPR 2008-05-07 2008-05-10 800 LV 43 48 1 5 NA
Rasmussen 2008-05-05 2008-05-08 1600 LV 44 47 NA NA NA
Times/Bloomberg 2008-05-01 2008-05-08 1986 RV 40 46 NA 9 NA
Economist/YouGov 2008-05-05 2008-05-06 1000 A 46 39 NA 15 NA
Gallup 2008-05-02 2008-05-06 4382 RV 45 46 1 9 NA
Rasmussen 2008-05-01 2008-05-04 1600 LV 47 43 NA NA NA
AP-Ipsos 2008-04-30 2008-05-04 755 RV 42 46 9 3 NA
USA Today/Gallup 2008-05-01 2008-05-03 803 LV 48 47 4 1 NA
CBS/Times 2008-05-01 2008-05-03 601 RV 38 50 3 9 NA
Diageo/Hotline 2008-04-30 2008-05-03 803 RV 43 47 NA 10 NA
Economist/YouGov 2008-04-30 2008-05-02 1000 A 41 43 NA 16 NA
Gallup 2008-04-27 2008-05-01 4381 RV 48 42 1 10 NA
CNN 2008-04-28 2008-04-30 906 RV 45 49 NA 7 NA
Rasmussen 2008-04-27 2008-04-30 1600 LV 46 43 NA NA NA
FOX 2008-04-28 2008-04-29 900 RV 46 43 NA 11 NA
CBS/Times 2008-04-25 2008-04-29 956 RV 45 45 2 8 NA
NBC/WSJ 2008-04-25 2008-04-28 1006 RV 43 46 5 6 NA
AP-Ipsos 2008-04-23 2008-04-27 1001 A 44 46 8 2 NA
Pew 2008-04-23 2008-04-27 1323 RV 44 50 NA 6 NA
Rasmussen 2008-04-23 2008-04-26 1600 LV 46 46 NA NA NA
Gallup 2008-04-22 2008-04-26 4383 RV 45 45 1 9 NA
Newsweek 2008-04-24 2008-04-25 1203 RV 44 47 NA 9 NA
Economist/YouGov 2008-04-21 2008-04-22 1000 A 45 42 NA 13 NA
Rasmussen 2008-04-19 2008-04-22 1600 LV 47 44 NA NA NA
Gallup 2008-04-17 2008-04-21 4367 RV 45 45 1 5 NA
USA Today/Gallup 2008-04-18 2008-04-20 832 LV 44 49 4 3 NA
Cook/RT 2008-04-17 2008-04-20 802 RV 44 45 2 9 NA
Rasmussen 2008-04-15 2008-04-18 1600 LV 48 41 NA NA NA
Newsweek 2008-04-16 2008-04-17 1209 RV 44 48 NA 8 NA
Democracy Corps (D) 2008-04-15 2008-04-17 1000 LV 47 48 2 3 NA
Economist/YouGov 2008-04-15 2008-04-16 1000 A 45 42 NA 13 NA
Gallup 2008-04-12 2008-04-16 4413 RV 44 46 1 9 NA
Rasmussen 2008-04-11 2008-04-14 1600 LV 47 43 NA NA NA
ABC/Post 2008-04-10 2008-04-13 NA RV 44 49 3 2 3
Harris Interactive 2008-04-11 2008-04-12 2869 A 35 41 13 11 NA
Zogby 2008-04-10 2008-04-12 1049 LV 45 45 NA NA NA
Gallup 2008-04-07 2008-04-11 4400 RV 43 46 1 10 NA
Rasmussen 2008-04-07 2008-04-10 1600 LV 47 44 NA NA NA
AP-Ipsos 2008-04-07 2008-04-09 1005 A 45 45 7 3 NA
Economist/YouGov 2008-04-07 2008-04-08 1000 A 44 44 NA 13 NA
Economist/YouGov 2008-04-04 2008-04-07 1000 A 44 43 NA 13 NA
Rasmussen 2008-04-03 2008-04-06 1600 LV 46 45 NA NA NA
Gallup 2008-04-02 2008-04-06 4380 RV 45 45 1 9 NA
IBD/TIPP 2008-04-01 2008-04-06 NA RV 44 44 NA 12 NA
Rasmussen 2008-03-30 2008-04-02 1600 LV 48 41 NA NA NA
CBS/Times 2008-03-28 2008-04-02 1196 RV 42 47 3 8 NA
Gallup 2008-03-28 2008-04-01 4431 RV 46 44 1 8 NA
Diageo/Hotline 2008-03-28 2008-03-31 799 RV 46 44 NA 8 NA
Rasmussen 2008-03-26 2008-03-29 1600 LV 47 44 NA NA NA
Voter/Consumer (R-RNC) 2008-03-25 2008-03-27 800 RV 48 42 NA NA NA
Democracy Corps (D) 2008-03-25 2008-03-27 500 LV 49 46 2 3 NA
Gallup 2008-03-22 2008-03-27 4398 RV 46 44 1 10 NA
Economist/YouGov 2008-03-24 2008-03-26 1000 A 44 42 NA 14 NA
NBC/WSJ 2008-03-24 2008-03-25 700 RV 42 44 4 10 NA
Rasmussen 2008-03-22 2008-03-25 1600 LV 51 41 NA NA NA
Economist/YouGov 2008-03-21 2008-03-24 1000 A 45 41 NA 14 NA
Harris Interactive 2008-03-14 2008-03-24 2549 A 33 39 17 12 NA
Pew 2008-03-19 2008-03-22 1248 RV 43 49 NA 8 NA
Rasmussen 2008-03-18 2008-03-21 1600 LV 49 41 NA NA NA
Gallup 2008-03-17 2008-03-21 4376 RV 46 44 1 8 NA
FOX 2008-03-18 2008-03-19 900 RV 44 43 NA 13 NA
CBS 2008-03-15 2008-03-18 952 RV 43 48 NA 6 NA
Rasmussen 2008-03-14 2008-03-17 1600 LV 48 42 NA NA NA
USA Today/Gallup 2008-03-14 2008-03-16 685 LV 47 49 NA NA NA
CNN 2008-03-14 2008-03-16 950 RV 46 47 1 6 NA
Gallup 2008-03-12 2008-03-16 4390 RV 46 44 1 9 NA
Zogby 2008-03-13 2008-03-14 1001 LV 46 40 NA NA NA
Rasmussen 2008-03-10 2008-03-13 1600 LV 45 44 NA NA NA
Gallup 2008-03-07 2008-03-11 4372 RV 44 46 1 9 NA
NBC/WSJ 2008-03-07 2008-03-10 1012 RV 44 47 3 6 NA
Rasmussen 2008-03-06 2008-03-09 1600 LV 45 44 NA NA NA
Cook/RT 2008-03-06 2008-03-09 802 RV 45 43 2 11 NA
Newsweek 2008-03-05 2008-03-06 1215 RV 45 46 NA 9 NA
Rasmussen 2008-03-02 2008-03-05 1600 LV 46 44 NA NA NA
SurveyUSA 2008-03-04 2008-03-04 1041 RV 46 46 NA NA NA
Cook/RT 2008-02-28 2008-03-02 802 RV 38 47 3 12 NA
ABC/Post 2008-02-28 2008-03-02 NA LV 42 53 2 3 1
Rasmussen 2008-02-27 2008-03-01 1600 LV 48 43 NA NA NA
Economist/YouGov 2008-02-25 2008-02-26 1000 A 44 43 NA 13 NA
Democracy Corps (D) 2008-02-24 2008-02-26 1956 LV 47 48 2 4 NA
Rasmussen 2008-02-23 2008-02-26 1600 LV 46 43 NA NA NA
Times/Bloomberg 2008-02-21 2008-02-25 1246 RV 44 42 5 9 NA
AP-Ipsos 2008-02-22 2008-02-24 1011 A 41 51 6 2 NA
USA Today/Gallup 2008-02-21 2008-02-24 1653 LV 48 47 3 2 NA
CBS/Times 2008-02-20 2008-02-24 1115 RV 38 50 3 9 NA
Pew 2008-02-20 2008-02-24 1240 RV 43 50 NA 7 NA
Rasmussen 2008-02-19 2008-02-22 1600 LV 46 43 NA NA NA
Research 2000 2008-02-18 2008-02-21 802 LV 44 50 NA 6 NA
FOX 2008-02-19 2008-02-20 900 RV 43 47 NA 10 NA
Economist/YouGov 2008-02-19 2008-02-20 1000 A 44 45 NA 14 NA
Rasmussen 2008-02-15 2008-02-18 1600 LV 43 46 NA NA NA
Diageo/Hotline 2008-02-14 2008-02-17 801 RV 40 48 NA 12 NA
Reuters/Zogby 2008-02-13 2008-02-16 1015 LV 40 47 4 8 NA
Rasmussen 2008-02-11 2008-02-14 1600 LV 43 46 NA NA NA
Economist/YouGov 2008-02-11 2008-02-12 1000 A 39 49 NA 13 NA
USA Today/Gallup 2008-02-08 2008-02-10 706 LV 46 50 2 1 NA
AP-Ipsos 2008-02-07 2008-02-10 1029 A 42 48 8 2 NA
Rasmussen 2008-02-07 2008-02-10 1600 LV 40 44 NA NA NA
Rasmussen 2008-02-03 2008-02-06 1600 LV 43 46 NA NA NA
Economist/YouGov 2008-02-04 2008-02-05 1000 A 38 44 NA 18 NA
Time 2008-02-01 2008-02-04 958 LV 41 48 2 9 NA
CNN 2008-02-01 2008-02-03 947 RV 44 52 1 4 NA
Cook/RT 2008-01-31 2008-02-02 855 RV 43 45 2 9 NA
ABC/Post 2008-01-30 2008-02-01 NA RV 48 47 2 2 1
FOX 2008-01-30 2008-01-31 900 RV 43 44 NA 13 NA
NPR 2008-01-29 2008-01-31 1000 LV 48 47 3 2 NA
Democracy Corps (D) 2008-01-29 2008-01-31 500 LV 48 47 3 2 NA
NBC/WSJ 2008-01-20 2008-01-22 1008 A 42 42 6 10 NA
Times/Bloomberg 2008-01-18 2008-01-22 1541 RV 42 41 5 12 NA
USA Today/Gallup 2008-01-10 2008-01-13 1598 LV 50 45 3 2 NA
Diageo/Hotline 2008-01-10 2008-01-12 803 RV 42 43 NA 14 NA
Reuters/Zogby 2008-01-10 2008-01-11 1006 LV 45 43 NA NA NA
CNN 2008-01-08 2008-01-10 840 RV 48 49 NA 3 NA
FOX 2007-12-18 2007-12-19 900 RV 44 40 NA 17 NA
Rasmussen 2007-12-14 2007-12-16 800 LV 45 43 9 3 NA
Zogby 2007-12-12 2007-12-14 1000 LV 43 47 NA NA NA
CNN 2007-12-06 2007-12-09 912 RV 48 48 NA 4 NA
Gallup 2007-11-11 2007-11-14 897 RV 44 47 6 4 NA
Quinnipiac 2007-10-23 2007-10-29 1636 RV 43 43 3 8 3
Times/Bloomberg 2007-10-19 2007-10-22 1209 RV 36 44 7 13 NA
Rasmussen 2007-10-17 2007-10-18 800 LV 45 44 7 4 NA
FOX 2007-09-25 2007-09-26 900 RV 38 40 NA 22 NA
Rasmussen 2007-09-19 2007-09-20 800 LV 41 46 10 3 NA
Rasmussen 2007-08-15 2007-08-16 800 LV 40 46 10 4 NA
Quinnipiac 2007-08-07 2007-08-13 1545 RV 39 43 3 11 4
FOX 2007-07-17 2007-07-18 900 RV 37 47 NA 16 NA
Rasmussen 2007-07-16 2007-07-17 800 LV 38 47 10 5 NA
Zogby 2007-07-12 2007-07-14 1012 LV 42 45 5 8 NA
CNN 2007-06-22 2007-06-24 907 RV 44 48 NA 8 NA
Newsweek 2007-06-20 2007-06-21 831 RV 41 51 NA 8 NA
Gallup 2007-06-11 2007-06-14 927 RV 46 48 3 2 NA
Quinnipiac 2007-06-05 2007-06-11 1711 RV 41 43 3 10 4
Times/Bloomberg 2007-06-07 2007-06-10 1056 RV 35 47 10 8 NA
Zogby 2007-05-17 2007-05-20 993 LV 43 46 3 8 NA
Diageo/Hotline 2007-05-16 2007-05-20 800 RV 39 42 NA 19 NA
Newsweek 2007-05-02 2007-05-03 831 RV 39 52 NA 9 NA
Rasmussen 2007-05-02 2007-05-03 800 LV 42 46 7 4 NA
WNBC/Marist 2007-04-26 2007-05-01 823 RV 46 39 NA 15 NA
Quinnipiac 2007-04-25 2007-05-01 1166 RV 42 42 2 12 2
Diageo/Hotline 2007-04-26 2007-04-30 801 RV 37 48 NA 15 NA
NBC/WSJ 2007-04-20 2007-04-23 1004 A 39 45 5 11 NA
Rasmussen 2007-04-09 2007-04-10 800 LV 42 48 6 4 NA
Times/Bloomberg 2007-04-05 2007-04-09 1246 RV 40 48 5 7 NA
Time 2007-04-05 2007-04-09 1102 RV 42 47 1 11 NA
Time 2007-03-23 2007-03-26 1102 RV 45 43 1 11 NA
McLaughlin (R) 2007-03-21 2007-03-25 1000 LV 38 39 NA 23 NA
Dem Corps (D) 2007-03-20 2007-03-25 763 LV 43 50 2 4 NA
Rasmussen 2007-03-14 2007-03-15 800 LV 44 44 8 3 NA
Time 2007-03-09 2007-03-12 1918 RV 41 44 1 13 NA
ARG 2007-03-02 2007-03-05 2104 LV 46 42 NA 12 NA
Newsweek 2007-02-28 2007-03-01 1202 RV 43 45 NA 12 NA
Time 2007-02-23 2007-02-26 1144 RV 42 46 1 11 NA
Zogby 2007-02-22 2007-02-24 1078 LV 40 44 3 13 NA
Quinnipiac 2007-02-13 2007-02-19 1536 RV 43 43 1 10 4
WNBC/Marist 2007-02-12 2007-02-15 978 RV 44 41 NA 15 NA
USA Today/Gallup 2007-02-09 2007-02-11 936 RV 48 48 2 2 NA
Rasmussen 2007-02-07 2007-02-08 800 LV 44 44 7 5 NA
Newsweek 2007-01-24 2007-01-25 837 RV 42 48 NA 10 NA
Time 2007-01-22 2007-01-23 1064 RV 49 42 NA 8 NA
ABC/Post 2007-01-16 2007-01-19 1000 A 45 47 3 3 1
Newsweek 2007-01-17 2007-01-18 1003 RV 44 46 NA 10 NA
Rasmussen 2007-01-17 2007-01-18 800 LV 44 47 6 4 NA
Diageo/Hotline 2007-01-11 2007-01-14 800 RV 43 38 NA 20 NA
GWU (Lake/Tarrance) 2007-01-08 2007-01-11 1000 LV 51 39 NA 10 NA
IBD/TIPP 2007-01-02 2007-01-04 951 A 48 36 6 10 NA
Pollster Dates N/Pop McCain Obama Other Undecided Not Voting
Zogby (Internet) 8/12-14/08 3339 LV 42 44 - 14 -
Gallup 8/12-14/08 2690 RV 44 44 6 6 -
Economist/YouGov 8/11-13/08 1000 A 40 41 4 12 2
Rasmussen 8/10-12/08 3000 LV 46 48 - - -
Gallup 8/9-11/08 2656 RV 42 47 1 10 -
Pew 7/31-8/10/08 2414 RV 43 46 - 11 -
Rasmussen 8/7-9/08 3000 LV 46 48 - - -
IBD/TIPP 8/4-9/08 925 RV 38 43 - 19 -
Gallup 8/6-8/08 2686 RV 42 47 1 10 -
Harris Interactive 8/1-7/08 2488 RV 38 47 8 7 -
Rasmussen 8/4-6/08 3000 LV 46 47 - - -
Economist/YouGov 8/4-6/08 1000 A 39 42 4 13 1
Gallup 8/3-5/08 2691 RV 44 46 1 10 -
CBS 7/31-8/5/08 851 RV 39 45 - 15 -
AP-Ipsos 7/31-8/4/08 833 RV 41 47 10 3 -
Time 7/31-8/4/08 808 LV 41 46 3 10 -
Rasmussen 8/1-3/08 3000 LV 47 46 - - -
Gallup 7/31-8/2/08 2684 RV 44 45 1 11 -
Zogby 7/31-8/1/08 1011 LV 42 41 4 13 -
Rasmussen 7/29-31/08 3000 LV 46 47 - - -
Gallup 7/28-30/08 2679 RV 44 45 1 12 -
Economist/YouGov 7/28-29/08 1000 A 37 44 5 14 1
CNN 7/27-29/08 914 RV 44 51 1 4 -
Rasmussen 7/26-28/08 3000 LV 46 47 - - -
Research 2000 7/25-27/08 1100 LV 39 51 5 5 -
Gallup 7/25-27/08 2674 RV 40 48 1 10 -
USA Today/Gallup 7/25-27/08 791 LV 49 45 2 4 -
Pew 7/23-27/08 1241 RV 42 47 - 11 -
Rasmussen 7/23-25/08 3000 LV 43 49 - - -
Economist/YouGov 7/22-24/08 1000 A 38 41 5 14 2
Gallup 7/22-24/08 2695 RV 41 47 1 11 -
Democracy Corps (D) 7/21-24/08 735 LV 43 49 5 2 -
FOX 7/22-23/08 900 RV 37 40 3 17 -
Rasmussen 7/20-22/08 3000 LV 45 47 - - -
Gallup 7/19-21/08 2645 RV 42 45 1 12 -
NBC/WSJ 7/18-21/08 1003 RV 41 47 4 8 -
Rasmussen 7/17-19/08 3000 LV 45 47 - - -
Gallup 7/16-18/08 2657 RV 43 45 1 10 -
Economist/YouGov 7/15-17/08 1000 A 36 41 4 13 5
Rasmussen 7/14-16/08 3000 LV 46 46 - - -
Gallup 7/13-15/08 2638 RV 44 47 1 9 -
CBS/Times 7/7-14/08 1534 RV 39 45 5 11 -
Rasmussen 7/11-13/08 3000 LV 46 47 - - -
ABC/Post 7/10-13/08 LV 46 49 3 2 -
Zogby/Reuters 7/9-13/08 1039 LV 40 47 3 10 -
Quinnipiac 7/8-13/08 1725 LV 41 50 2 7 -
Gallup 7/10-12/08 2635 RV 43 46 1 10 -
IBD/TIPP 7/7-11/08 854 RV 37 40 4 18 -
Harris Interactive 7/3-11/08 RV 35 44 7 16 -
Newsweek 7/9-10/08 1037 RV 41 44 - 15 -
Rasmussen 7/8-10/08 3000 LV 45 47 - - -
Gallup 7/7-9/08 2647 RV 43 46 1 10 -
Economist/YouGov 7/7-9/08 1000 A 38 39 4 14 5
Rasmussen 7/2-7/08 3000 LV 43 49 - - -
Gallup 7/3-6/08 2650 RV 43 47 1 10 -
Economist/YouGov 7/1-2/08 1000 A 34 37 3 17 9
Gallup 6/30-7/2/08 2641 RV 43 47 1 10 -
Rasmussen 6/29-7/1/08 3000 LV 44 49 - - -
Gallup 6/26-29/08 2656 RV 42 47 1 10 -
CNN 6/26-29/08 906 RV 45 50 - 5 -
McLaughlin (R) 6/26-29/08 1000 LV 38 46 - 16 -
Pew 6/18-29/08 1574 RV 40 48 - 12 -
Rasmussen 6/26-28/08 3000 LV 43 49 - - -
Rasmussen 6/23-25/08 3000 LV 45 49 - - -
Gallup 6/23-25/08 2605 RV 44 44 1 11 -
Economist/YouGov 6/23-25/08 1000 A 34 36 4 17 8
Democracy Corps (D) 6/22-25/08 2000 LV 45 49 2 3 -
Time 6/18-25/08 805 LV 43 47 2 8 -
Times/Bloomberg 6/19-23/08 1115 RV 37 49 4 10 -
Rasmussen 6/20-22/08 3000 LV 43 49 - - -
Gallup 6/20-22/08 2608 RV 43 46 1 9 -
Franklin and Marshall/Hearst-Argyle 6/16-22/08 1501 RV 36 42 7 15 -
Newsweek 6/18-19/08 896 RV 36 51 - 13 -
Rasmussen 6/17-19/08 3000 LV 44 48 - - -
Gallup 6/16-19/08 2648 RV 44 46 1 9 -
USA Today/Gallup 6/15-19/08 1310 LV 44 50 - - -
FOX 6/17-18/08 900 RV 39 42 6 14 -
Economist/YouGov 6/16-17/08 1000 A 34 37 5 16 8
Rasmussen 6/14-16/08 3000 LV 44 48 - - -
Gallup 6/13-15/08 2607 RV 42 46 1 12 -
Cook/RT 6/12-15/08 880 RV 40 44 1 15 -
ABC/Post 6/12-15/08 LV 48 47 3 2 -
Zogby/Reuters 6/12-14/08 1113 LV 42 47 - 12 -
Rasmussen 6/11-13/08 3000 LV 43 49 - - -
Harris Interactive 6/5-13/08 2327 A 32 42 14 12 -
Gallup 6/10-12/08 2714 RV 43 46 1 10 -
Economist/YouGov 6/10-11/08 1000 A 33 37 5 15 10
AP-Ipsos 6/5-11/08 467 LV 43 50 5 1 -
Rasmussen 6/8-10/08 3000 LV 44 49 - - -
Gallup 6/7-9/08 2633 RV 41 48 1 10 -
NBC/WSJ 6/6-9/08 1000 RV 41 47 4 8 -
Diageo/Hotline 6/5-8/08 806 RV 42 44 - 11 -
IBD/TIPP 6/2-8/08 916 RV 40 43 - 17 -
Rasmussen 6/5-7/08 3000 LV 43 50 - - -
Gallup 6/2-6/08 4408 RV 45 46 1 8 -
CNN 6/4-5/08 921 RV 46 49 - 5 -
Rasmussen 6/2-4/08 3000 LV 45 47 - - -
CBS 5/30-6/3/08 930 RV 42 48 3 7 -
USA Today/Gallup 5/30-6/1/08 803 LV 44 49 - - -
Rasmussen 5/29-6/1/08 1600 LV 45 45 - - -
Gallup 5/28-6/1/08 4378 RV 46 46 1 7 -
Cook/RT 5/29-31/08 802 RV 43 44 3 10 -
Economist/YouGov 5/27-28/08 1000 A 48 38 - 15 -
Lombardo (R) 5/26-28/08 1000 RV 40 44 - - -
Rasmussen 5/25-28/08 1600 LV 47 42 - - -
Gallup 5/22-27/08 4411 RV 46 45 1 8 -
Democracy Corps (D) 5/19-26/08 1600 LV 47 47 2 3 -
Pew 5/21-25/08 1242 RV 44 47 - 9 -
Rasmussen 5/21-24/08 1600 LV 46 44 - - -
Newsweek 5/21-22/08 1205 RV 46 46 - 8 -
Economist/YouGov 5/20-21/08 1000 A 41 43 - 16 -
Gallup 5/17-21/08 4446 RV 44 47 1 7 -
Rasmussen 5/17-20/08 1600 LV 45 44 - - -
Harris Interactive 5/8-19/08 2317 A 35 39 13 11 -
Zogby 5/15-18/08 1076 LV 40 48 - - -
IBD/TIPP 5/12-18/08 876 RV 37 48 - 15 -
Rasmussen 5/13-16/08 1600 LV 45 44 - - -
Gallup 5/12-16/08 4385 RV 47 44 1 8 -
Economist/YouGov 5/14-15/08 1000 A 42 39 - 19 -
GWU (Lake/Tarrance) 5/11-14/08 1018 LV 47 49 - 5 -
Democracy Corps (D) 5/13/08 1014 LV 46 48 2 4 -
Rasmussen 5/9-12/08 1600 LV 46 47 - - -
Quinnipiac 5/8-12/08 1245 RV 40 47 2 8 4
ABC/Post 5/8-11/08 1122 A 44 51 3 1 1
Gallup 5/7-11/08 4370 RV 43 47 1 8 -
NPR 5/7-10/08 800 LV 43 48 1 5 -
Rasmussen 5/5-8/08 1600 LV 44 47 - - -
Times/Bloomberg 5/1-8/08 1986 RV 40 46 - 9 -
Economist/YouGov 5/5-6/08 1000 A 46 39 - 15 -
Gallup 5/2-6/08 4382 RV 45 46 1 9 -
Rasmussen 5/1-4/08 1600 LV 47 43 - - -
AP-Ipsos 4/30-5/4/08 755 RV 42 46 9 3 -
USA Today/Gallup 5/1-3/08 803 LV 48 47 4 1 -
CBS/Times 5/1-3/08 601 RV 38 50 3 9 -
Diageo/Hotline 4/30-5/3/08 803 RV 43 47 - 10 -
Economist/YouGov 4/30-5/2/08 1000 A 41 43 - 16 -
Gallup 4/27-5/1/08 4381 RV 48 42 1 10 -
CNN 4/28-30/08 906 RV 45 49 - 7 -
Rasmussen 4/27-30/08 1600 LV 46 43 - - -
FOX 4/28-29/08 900 RV 46 43 - 11 -
CBS/Times 4/25-29/08 956 RV 45 45 2 8 -
NBC/WSJ 4/25-28/08 1006 RV 43 46 5 6 -
AP-Ipsos 4/23-27/08 1001 A 44 46 8 2 -
Pew 4/23-27/08 1323 RV 44 50 - 6 -
Rasmussen 4/23-26/08 1600 LV 46 46 - - -
Gallup 4/22-26/08 4383 RV 45 45 1 9 -
Newsweek 4/24-25/08 1203 RV 44 47 - 9 -
Economist/YouGov 4/21-22/08 1000 A 45 42 - 13 -
Rasmussen 4/19-22/08 1600 LV 47 44 - - -
Gallup 4/17-21/08 4367 RV 45 45 1 5 -
USA Today/Gallup 4/18-20/08 832 LV 44 49 4 3 -
Cook/RT 4/17-20/08 802 RV 44 45 2 9 -
Rasmussen 4/15-18/08 1600 LV 48 41 - - -
Newsweek 4/16-17/08 1209 RV 44 48 - 8 -
Democracy Corps (D) 4/15-17/08 1000 LV 47 48 2 3 -
Economist/YouGov 4/15-16/08 1000 A 45 42 - 13 -
Gallup 4/12-16/08 4413 RV 44 46 1 9 -
Rasmussen 4/11-14/08 1600 LV 47 43 - - -
ABC/Post 4/10-13/08 RV 44 49 3 2 3
Harris Interactive 4/11-12/08 2869 A 35 41 13 11 -
Zogby 4/10-12/08 1049 LV 45 45 - - -
Gallup 4/7-11/08 4400 RV 43 46 1 10 -
Rasmussen 4/7-10/08 1600 LV 47 44 - - -
AP-Ipsos 4/7-9/08 1005 A 45 45 7 3 -
Economist/YouGov 4/7-8/08 1000 A 44 44 - 13 -
Economist/YouGov 4/4-7/08 1000 A 44 43 - 13 -
Rasmussen 4/3-6/08 1600 LV 46 45 - - -
Gallup 4/2-6/08 4380 RV 45 45 1 9 -
IBD/TIPP 4/1-6/08 RV 44 44 - 12 -
Rasmussen 3/30-4/2/08 1600 LV 48 41 - - -
CBS/Times 3/28-4/2/08 1196 RV 42 47 3 8 -
Gallup 3/28-4/1/08 4431 RV 46 44 1 8 -
Diageo/Hotline 3/28-31/08 799 RV 46 44 - 8 -
Rasmussen 3/26-29/08 1600 LV 47 44 - - -
Voter/Consumer (R-RNC) 3/25-27/08 800 RV 48 42 - - -
Democracy Corps (D) 3/25-27/08 500 LV 49 46 2 3 -
Gallup 3/22-27/08 4398 RV 46 44 1 10 -
Economist/YouGov 3/24-26/08 1000 A 44 42 - 14 -
NBC/WSJ 3/24-25/08 700 RV 42 44 4 10 -
Rasmussen 3/22-25/08 1600 LV 51 41 - - -
Economist/YouGov 3/21-24/08 1000 A 45 41 - 14 -
Harris Interactive 3/14-24/08 2549 A 33 39 17 12 -
Pew 3/19-22/08 1248 RV 43 49 - 8 -
Rasmussen 3/18-21/08 1600 LV 49 41 - - -
Gallup 3/17-21/08 4376 RV 46 44 1 8 -
FOX 3/18-19/08 900 RV 44 43 - 13 -
CBS 3/15-18/08 952 RV 43 48 - 6 -
Rasmussen 3/14-17/08 1600 LV 48 42 - - -
USA Today/Gallup 3/14-16/08 685 LV 47 49 - - -
CNN 3/14-16/08 950 RV 46 47 1 6 -
Gallup 3/12-16/08 4390 RV 46 44 1 9 -
Zogby 3/13-14/08 1001 LV 46 40 - - -
Rasmussen 3/10-13/08 1600 LV 45 44 - - -
Gallup 3/7-11/08 4372 RV 44 46 1 9 -
NBC/WSJ 3/7-10/08 1012 RV 44 47 3 6 -
Rasmussen 3/6-9/08 1600 LV 45 44 - - -
Cook/RT 3/6-9/08 802 RV 45 43 2 11 -
Newsweek 3/5-6/08 1215 RV 45 46 - 9 -
Rasmussen 3/2-5/08 1600 LV 46 44 - - -
SurveyUSA 3/4/08 1041 RV 46 46 - - -
Cook/RT 2/28-3/2/08 802 RV 38 47 3 12 -
ABC/Post 2/28-3/2/08 LV 42 53 2 3 1
Rasmussen 2/27-3/1/08 1600 LV 48 43 - - -
Economist/YouGov 2/25-26/08 1000 A 44 43 - 13 -
Democracy Corps (D) 2/24-26/08 1956 LV 47 48 2 4 -
Rasmussen 2/23-26/08 1600 LV 46 43 - - -
Times/Bloomberg 2/21-25/08 1246 RV 44 42 5 9 -
AP-Ipsos 2/22-24/08 1011 A 41 51 6 2 -
USA Today/Gallup 2/21-24/08 1653 LV 48 47 3 2 -
CBS/Times 2/20-24/08 1115 RV 38 50 3 9 -
Pew 2/20-24/08 1240 RV 43 50 - 7 -
Rasmussen 2/19-22/08 1600 LV 46 43 - - -
Research 2000 2/18-21/08 802 LV 44 50 - 6 -
FOX 2/19-20/08 900 RV 43 47 - 10 -
Economist/YouGov 2/19-20/08 1000 A 44 45 - 14 -
Rasmussen 2/15-18/08 1600 LV 43 46 - - -
Diageo/Hotline 2/14-17/08 801 RV 40 48 - 12 -
Reuters/Zogby 2/13-16/08 1015 LV 40 47 4 8 -
Rasmussen 2/11-14/08 1600 LV 43 46 - - -
Economist/YouGov 2/11-12/08 1000 A 39 49 - 13 -
USA Today/Gallup 2/8-10/08 706 LV 46 50 2 1 -
AP-Ipsos 2/7-10/08 1029 A 42 48 8 2 -
Rasmussen 2/7-10/08 1600 LV 40 44 - - -
Rasmussen 2/3-6/08 1600 LV 43 46 - - -
Economist/YouGov 2/4-5/08 1000 A 38 44 - 18 -
Time 2/1-4/08 958 LV 41 48 2 9 -
CNN 2/1-3/08 947 RV 44 52 1 4 -
Cook/RT 1/31-2/2/08 855 RV 43 45 2 9 -
ABC/Post 1/30-2/1/08 RV 48 47 2 2 1
FOX 1/30-31/08 900 RV 43 44 - 13 -
NPR 1/29-31/08 1000 LV 48 47 3 2 -
Democracy Corps (D) 1/29-31/08 500 LV 48 47 3 2 -
NBC/WSJ 1/20-22/08 1008 A 42 42 6 10 -
Times/Bloomberg 1/18-22/08 1541 RV 42 41 5 12 -
USA Today/Gallup 1/10-13/08 1598 LV 50 45 3 2 -
Diageo/Hotline 1/10-12/08 803 RV 42 43 - 14 -
Reuters/Zogby 1/10-11/08 1006 LV 45 43 - - -
CNN 1/8-10/08 840 RV 48 49 - 3 -
FOX 12/18-19/07 900 RV 44 40 - 17 -
Rasmussen 12/14-16/07 800 LV 45 43 9 3 -
Zogby 12/12-14/07 1000 LV 43 47 - - -
CNN 12/6-9/07 912 RV 48 48 - 4 -
Gallup 11/11-14/07 897 RV 44 47 6 4 -
Quinnipiac 10/23-29/07 1636 RV 43 43 3 8 3
Times/Bloomberg 10/19-22/07 1209 RV 36 44 7 13 -
Rasmussen 10/17-18/07 800 LV 45 44 7 4 -
FOX 9/25-26/07 900 RV 38 40 - 22 -
Rasmussen 9/19-20/07 800 LV 41 46 10 3 -
Rasmussen 8/15-16/07 800 LV 40 46 10 4 -
Quinnipiac 8/7-13/07 1545 RV 39 43 3 11 4
FOX 7/17-18/07 900 RV 37 47 - 16 -
Rasmussen 7/16-17/07 800 LV 38 47 10 5 -
Zogby 7/12-14/07 1012 LV 42 45 5 8 -
CNN 6/22-24/07 907 RV 44 48 - 8 -
Newsweek 6/20-21/07 831 RV 41 51 - 8 -
Gallup 6/11-14/07 927 RV 46 48 3 2 -
Quinnipiac 6/5-11/07 1711 RV 41 43 3 10 4
Times/Bloomberg 6/7-10/07 1056 RV 35 47 10 8 -
Zogby 5/17-20/07 993 LV 43 46 3 8 -
Diageo/Hotline 5/16-20/07 800 RV 39 42 - 19 -
Newsweek 5/2-3/07 831 RV 39 52 - 9 -
Rasmussen 5/2-3/07 800 LV 42 46 7 4 -
WNBC/Marist 4/26-5/1/07 823 RV 46 39 - 15 -
Quinnipiac 4/25-5/1/07 1166 RV 42 42 2 12 2
Diageo/Hotline 4/26-30/07 801 RV 37 48 - 15 -
NBC/WSJ 4/20-23/07 1004 A 39 45 5 11 -
Rasmussen 4/9-10/07 800 LV 42 48 6 4 -
Times/Bloomberg 4/5-9/07 1246 RV 40 48 5 7 -
Time 4/5-9/07 1102 RV 42 47 1 11 -
Time 3/23-26/07 1102 RV 45 43 1 11 -
McLaughlin (R) 3/21-25/07 1000 LV 38 39 - 23 -
Dem Corps (D) 3/20-25/07 763 LV 43 50 2 4 -
Rasmussen 3/14-15/07 800 LV 44 44 8 3 -
Time 3/9-12/07 1918 RV 41 44 1 13 -
ARG 3/2-5/07 2104 LV 46 42 - 12 -
Newsweek 2/28-3/1/07 1202 RV 43 45 - 12 -
Time 2/23-26/07 1144 RV 42 46 1 11 -
Zogby 2/22-24/07 1078 LV 40 44 3 13 -
Quinnipiac 2/13-19/07 1536 RV 43 43 1 10 4
WNBC/Marist 2/12-15/07 978 RV 44 41 - 15 -
USA Today/Gallup 2/9-11/07 936 RV 48 48 2 2 -
Rasmussen 2/7-8/07 800 LV 44 44 7 5 -
Newsweek 1/24-25/07 837 RV 42 48 - 10 -
Time 1/22-23/07 1064 RV 49 42 - 8 -
ABC/Post 1/16-19/07 1000 A 45 47 3 3 1
Newsweek 1/17-18/07 1003 RV 44 46 - 10 -
Rasmussen 1/17-18/07 800 LV 44 47 6 4 -
Diageo/Hotline 1/11-14/07 800 RV 43 38 - 20 -
GWU (Lake/Tarrance) 1/8-11/07 1000 LV 51 39 - 10 -
IBD/TIPP 1/2-4/07 951 A 48 36 6 10 -
source("~/dlanalysis/util.R") # github.com/brendano/dlanalysis
load_data <- function(filename) {
d = read.table(filename, sep="\t", header=T)
d$date1 = as.Date(d$date1)
d$date2 = as.Date(d$date2)
# d$date1 = as.POSIXct(d$date1)
# d$date2 = as.POSIXct(d$date2)
# d$date = midtime(d$date1, d$date2)
d$date = d$date2
d$o = d$obama / (d$obama+d$mccain)
d = d[order(d$date),]
d
}
midtime.POSIXt <- function(t1, t2) {
stop("fixme")
stopifnot(all(t2 >= t1))
t1 + as.double(difftime(t2,t1,units='secs')) / 2
}
midtime.Date <- function(d1, d2) {
# test: data.frame(d$date1, midtime(d$date1,d$date2), d$date2)
stopifnot(all(d2 >= d1))
ret = d1 + as.double(difftime(d2,d1,units='days')) / 2
# wacky: "ret" is a Date, but can have a fractional day value [[print(as.numeric(ret))]]. So identity testing is different depending whether you look at the day value underneath, vs. the string representation (which is what you see at the console).
# So let's force the tie resolution right here then.
ret = floor(as.numeric(ret))
class(ret) = 'Date' # gees, why doesnt as.Date(ret) work?!
ret
}
plots <- function(d, f=.1, k=10, ylim=c(0,60), main="", xlim=c(min(d$ate),max(d$date))) {
plot(rep(0,nrow(d)) ~ date, data=d, ylim=ylim, type='n', ylab="Percentage", main=main, xlim=xlim)
par(cex=.2)
points((undec)~date, data=d, col='purple')
points((nv)~date, data=d, col='green')
points(obama~date, data=d, col='blue')
points(mccain~date, data=d, col='red')
par(cex=1, lwd=.9)
# lines(with(d[nna(d$nv),], lowess(date,nv, f=f)), col='green')
# lines(with(d[nna(d$undec),], lowess(date,undec, f=f, diff=range(date))), col='purple')
lines(with(d,lowess(date,undec, f=f)), col='purple')
lines(with(d,lowess(date,mccain, f=f)), col='red')
lines(with(d,lowess(date,obama, f=f)), col='blue')
# assumptions that no NA's everywhere...
# m = data.frame(
# mccain=windowmean(d$mccain,d$date,k),
# obama=windowmean(d$obama,d$date,k),
# undec=windowmean(d$undec,d$date,k),
# date=seq.Date(min(d$date), max(d$date), 1 ))
# lines(mccain~date, data=m, col='red')
# lines(obama~date, data=m, col='blue')
# lines(undec~date, data=m, col='purple')
par(lwd=1)
}
windowmean <- function(items, dates, k, min_points=k, weights=rep(1,length(items))) {
weights[is.na(weights)] = mean(weights, na.rm=T)
# dates = as.Date(dates)
days = seq.Date(min(dates), max(dates), 1)
means = rep(NA, length(days))
for (i in (1+k):length(days)) {
window = dates >= days[i-k] & dates <= days[i]
# print(table(window))
if (sum(window) < min_points) {
now_j = last(which(dates<=days[i]))
prev = max(now_j-10, 1)
window = window | unwhich( prev:now_j, len=length(window))
}
means[i] = weighted.mean( items[window], w=weights[window] )
}
means
}
## for blog post, 4 smoother kernel plots
# for (f in c(0.01,.1,.3,.5)) plots(d[is.na(d$nv) & nna(d$undec),], ylim=c(35,55), f=f, main=sprintf("Lowess smoother f=%.2f",f))
## for blog post, the day-by-day smoothed version
# the blogpost also subsetted the data before running this -->
# > x=perday_breakout(d[nna(d$undec) & is.na(d$nv),])
# > dayplot(x)
perday_breakout <- function(d) {
# SLOW
x=data.frame(date="hi",pollster=NA,n=NA,o=NA)
for(i in 1:nrow(d)) {
span=seq(d$date1[i],d$date2[i],1)
for(day in as.c(span)) {
x=lax_rbind(x, data.frame(date=day, n=d$n[i]/length(span), o=d$o[i], pollster=d$pollster[i]))
}
}
x=x[2:nrow(x),]
x$date = as.Date(x$date)
x[order(x$date),]
}
dayplot <- function(x, ...) {
y=dfagg(x,x$date,function(x) list(o=weighted.mean(x$o,x$n), n=sum(x$n)))
plot(as.Date(row.names(y)), y$o, ...)
}
## 3 versions
# > d$date=midtime(d$date1,d$date2)
# > plot(o~date, data=d, main="One point per poll, at midinterval dates", ylab="obama/(obama+mccain)")
# > dayplot(d, main="Every day that's on a poll midinterval, average if multiple")
# > dayplot(x, main="Stretch each poll across its own interval, then average by day")
## with loesses, using span=.2 as charles franklin does on the pollster.com graph
# (copy and paste, not real functions, below)
bla1 <- function(){
dates=seq(min(d$date),max(d$date),1)
plot(o~date, data=d, main="One point per poll, at midinterval dates", ylab="obama/(obama+mccain)", cex=.2)
lines(dates,predict(loess(o~as.numeric(date), data=d, span=.2), newdata=dates))
dayplot(d, main="Every day that's on a poll midinterval, average if multiple",cex=.2)
y=dfagg(d,d$date,function(x) list(o=weighted.mean(x$o,x$n), n=sum(x$n)))
y$date=as.Date(row.names(y))
lines(dates,predict(loess(o~as.numeric(date), data=y, span=.2), newdata=dates))
dayplot(x, main="Stretch each poll across its own interval, then average by day",cex=.2)
lines(dates, predict(loess(o~as.numeric(date), data=x, span=.2), newdata=dates))
}
## Residuals for the above
bla2 <- function() {
plot(d$date,d$o-predict(loess(o~as.numeric(date), data=d, span=.2), newdata=d$date), cex=.8, ylim=c(-.07,.07), main="Residuals, one point per poll vs its loess w/ span=.2")
plot(y$date,y$o-predict(loess(o~as.numeric(date), data=y, span=.2), newdata=y$date), cex=.8, ylim=c(-.07,.07), main="Residuals, every day with a poll point (average if multiple) vs its loess")
plot(x$date,x$o-predict(loess(o~as.numeric(date), data=x, span=.2), newdata=x$date), cex=.8, ylim=c(-.07,.07), main="Residuals, stretch polls then average every day, vs its loess")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment