File:NZ opinion polls 2005-2008 -PPM.png
From Wikimedia Commons, the free media repository
Jump to navigation
Jump to search
NZ_opinion_polls_2005-2008_-PPM.png (778 × 487 pixels, file size: 13 KB, MIME type: image/png)
File information
Structured data
Captions
Summary
[edit]DescriptionNZ opinion polls 2005-2008 -PPM.png |
English: Preferred Prime Minister Poll, New Zealand General Elections |
Date | |
Source | Own work |
Author | Mark Payne, Denmark |
Figure is produced using the R statistical package, using the following code. It first reads the HTML directly from the website, then parses the data and saves the graph into your working directory. It should be able to be run directly by anyone with R.
#Load the complete HTML file into memory
html <- readLines(url("http://en.wikipedia.org/wiki/Opinion_polling_for_the_New_Zealand_general_election,_2008"),encoding="UTF-8")
closeAllConnections()
#The fourth table is the opinion poll data
tbl <- html[(grep("<table.*",html)[4]):(grep("</table.*",html)[4])]
#Now split it into the rows, based on the <tr> tag
tbl.rows <- split(tbl,cumsum(tbl=="<tr>"))
#Now extract the data
survey.dat <- lapply(tbl.rows,function(x) {
#Start by only considering where we have <td> tags
td.tags <- x[grep("<td",x)]
#Polling data appears in columns 3-6
dat <- td.tags[3:6]
#Now strip the data and covert to numeric format
dat <- gsub("<td>|</td>","",dat)
dat <- gsub("%","",dat)
dat <- gsub("-","0",dat)
dat <- as.numeric(dat)
#Getting the date strings is a little harder. The approach we will take is to take advantage
#of the title="date" hyperlinks to generate a set of dates
date.str <- td.tags[2] #Dates are in the second column
date.str <- gsub("<sup.*</sup>","",date.str) #Throw out anything between superscript tags, as its an reference to the source
titles <- gregexpr("(?U)title=\".*\"",date.str,perl=TRUE)[[1]] #Find the location of the title tags
#Now, extract the actual date strings
date.strings <- rep(NULL,length(titles))
for(i in 1:length(titles)) {
date.strings[i] <- substr(date.str,titles[i]+7,titles[i]+attr(titles,"match.length")[i]-2)
}
yr <- rev(date.strings)[1]
dates <- rep(as.POSIXct(Sys.time()),length(date.strings)-1)
for(i in 1:(length(date.strings)-1)) {
dates[i] <- as.POSIXct(strptime(paste(date.strings[i],yr),"%B %d %Y"))
}
survey.time <- mean(dates)
#And now return results
# if(is.na(survey.time)) browser()
return(data.frame(Date=survey.time,t(dat)))
})
polls <- do.call(rbind,survey.dat)
#Now make the graph!
polls <- subset(polls,!is.na(polls$Date))
polls$Date <- as.double(polls$Date)
colnames(polls) <- c("Date","Helen Clark","Don Brash","John Key","Winston Peters")
parties <- colnames(polls)[-1]
cols <- c("red","purple","blue","black")
ticks <- ISOdate(c(2005,rep(2006,3),rep(2007,3),rep(2008,3)),c(9,rep(c(1,5,9),3)),1)
xlims <- range(as.double(c(ticks,ISOdate(2009,2,1))))
png("NZ_opinion_polls_2005-2008 -PPM.png",width=778,height=487,pointsize=16)
par(mar=c(3,4,1,1))
matplot(polls$Date,polls[,parties],pch=NA,xlim=xlims,ylab="Party support (%)",xlab="",col=cols,xaxt="n",ylim=c(0,60))
abline(h=seq(0,95,by=5),col="lightgrey",lty=3)
abline(v=as.double(ticks),col="lightgrey",lty=3)
#Now add loess smoothers
smoothed <- list()
for(i in 1:length(parties)) {
smoother <- loess(polls[,i+1] ~ polls[,"Date"],span=0.33)
smoothed[[i]] <- predict(smoother,se=TRUE)
# polygon(c(polls[,"Date"],rev(polls[,"Date"])),
# c(smoothed[[i]]$fit+smoothed[[i]]$se.fit*1.96,rev(smoothed[[i]]$fit-smoothed[[i]]$se.fit*1.96)),
# col=rgb(0.5,0.5,0.5,0.5),border=NA)
lines(polls[,"Date"],pmax(0,smoothed[[i]]$fit),col=cols[i],lwd=2) #Constraints it to be positive
}
matpoints(polls$Date,polls[,parties],pch=20,col=cols)
legend("topleft",legend=parties,col=cols,pch=20,bg="white",lwd=2)
axis(1,at=as.double(ticks),labels=format(ticks,format="%b\n%Y"),cex.axis=0.8)
axis(4,at=axTicks(4),labels=rep("",length(axTicks(4))))
#Add best estimates
for(i in 1:length(smoothed)) {
lbl <- sprintf("%4.0f%% ± %2.0f",abs(round(rev(smoothed[[i]]$fit)[1],0)),round(1.96*rev(smoothed[[i]]$se.fit)[1],0))
text(rev(polls$Date)[1],rev(smoothed[[i]]$fit)[1],labels=lbl,pos=4,col=cols[i])
}
dev.off()
Licensing
[edit]I, the copyright holder of this work, hereby publish it under the following licenses:
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.http://www.gnu.org/copyleft/fdl.htmlGFDLGNU Free Documentation Licensetruetrue |
You may select the license of your choice.
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 08:52, 6 November 2008 | 778 × 487 (13 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | |
07:58, 28 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
07:44, 24 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
07:16, 20 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
09:51, 18 October 2008 | 778 × 487 (13 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date=18 October 2008 |Author=Mark Payne, Copenhagen, Denmark |Permission= |other_versions= }} | ||
19:44, 12 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
09:37, 10 October 2008 | 778 × 487 (13 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
06:48, 10 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
19:29, 8 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} | ||
19:27, 8 October 2008 | 778 × 487 (12 KB) | Trevva (talk | contribs) | {{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }} |
You cannot overwrite this file.
File usage on Commons
There are no pages that use this file.
File usage on other wikis
The following other wikis use this file:
- Usage on en.wikipedia.org