Monday 3 December 2012

It's just the way I Roll...

This is data from Quebec on the number of children born every day. As you can see it's quite messy. There's alot of data in that plot, over 5000 day's figures.

By applying a 30 day rolling average a clear seasonal pattern emerges.

A 365 day rolling average produces a much more clear long term trend than either the daily or 30 day rolling figures.
 
Data: Number of daily births in Quebec, Jan. 01, 1977 to Dec. 31, 1990 from here.


Saturday 1 December 2012

ChRistmas with extRa R - XML & xtable

I like the R blog is.R(). They're doing an Advent CalendaR this Christmas looking at an R package everyday up until Christmas Eve. I thought I'd play along. Instead of doing exactly the same as them and using the US presidential election results I'll do a variation on a theme and scrape the last UK general election results data from Wikipedia.

> require(XML)
> myURL <-"http://en.wikipedia.org/wiki/United_Kingdom_general_election,_2010"
> allTables <-readHTMLTable(myURL)
> str(allTables)
#List of 29..... I just want the 11th
> stateTable <- allTables[[11]]
> head(stateTable)
#used the data editor in RGui to tidy it up a little
> fix(stateTable)
#Need to remove the first column as I'm not bothered about the #colours and adjust some on the column names
> stateTable <- stateTable[,-1]
> names(stateTable)
> colnames(stateTable)[7] <- 'Net Change in Seats'
> colnames(stateTable)[10] <- 'Change in % of Votes'
> require(xtable)
> resultsTable<-xtable(stateTable)
> print(resultsTable, type="html"))


Political Party Candidates Number of Votes Elected Seats Gained Seats Lost Net Change in Seats % of Seats % of Votes Change in % of Votes
1 Conservative 631 10,703,654 306 100 3 +97 47.1 36.1 +3.7
2 Labour 631 8,606,517 258 3 94 -91 39.7 29.0 -6.2
3 Liberal Democrat 631 6,836,248 57 8 13 -5 8.8 23.0 +1.0
4 UKIP 572 919,471 0 0 0 0 0 3.1 +0.9
5 BNP 338 564,321 0 0 0 0 0 1.9 +1.2
6 SNP 59 491,386 6 0 0 0 0.9 1.7 +0.1
7 Green 310 265,243 1 1 0 +1 0.2 0.9 -0.2
8 Sinn Féin 17 171,942 5 0 0 0 0.8 0.6 -0.1
9 Democratic Unionist 16 168,216 8 0 1 -1 1.2 0.6 -0.3
10 Plaid Cymru 40 165,394 3 1 0 +1 0.5 0.6 -0.1
11 SDLP 18 110,970 3 0 0 0 0.5 0.4 -0.1
12 Conservatives and Unionists 17 102,361 0 0 1 -1 0 0.3 -0.1
13 English Democrats 107 64,826 0 0 0 0 0 0.2 0.2
14 Alliance 18 42,762 1 1 0 +1 0.2 0.1 0.0
15 Respect 11 33,251 0 0 1 -1 0 0.1 -0.1
16 Traditional Unionist Voice 10 26,300 0 0 0 0 0 0.1 N/A
17 Speaker 1 22,860 1 0 0 0 0.2 0.1 0.0
18 Independent - Rodney Connor 1 21,300 0 0 0 0 0 0.1 N/A
19 Independent - Sylvia Hermon 1 21,181 1 1 0 +1 0.2 0.1 N/A
20 Christian 71 18,623 0 0 0 0 0 0.1 +0.1
21 Green 20 16,827 0 0 0 0 0 0.1 0.0
22 Health Concern 1 16,150 0 0 1 -1 0 0.1 0.0
23 Trade Unionist &amp Socialist 42 12,275 0 0 0 0 0 0.0 N/A
24 Independent - Bob Spink 1 12,174 0 0 1 -1 0 0.0 N/A
25 National Front 17 10,784 0 0 0 0 0 0.0 0.0
26 Buckinghamshire Campaign for Democracy 1 10,331 0 0 0 0 0 0.0 N/A
27 Monster Raving Loony 27 7,510 0 0 0 0 0 0.0 0.0
28 Socialist Labour 23 7,219 0 0 0 0 0 0.0 -0.1
29 Liberal 5 6,781 0 0 0 0 0 0.0 -0.1
30 Blaenau Gwent People's Voice 1 6,458 0 0 1 -1 0 0.0 -0.1
31 Christian Peoples 17 6,276 0 0 0 0 0 0.0 0.0
32 Mebyon Kernow 6 5,379 0 0 0 0 0 0.0 0.0
33 Lincolnshire Independents 3 5,311 0 0 0 0 0 0.0 N/A
34 Mansfield Independent Forum 1 4,339 0 0 0 0 0 0.0 N/A
35 Green (NI) 4 3,542 0 0 0 0 0 0.0 0.0
36 Socialist Alternative 3 3,298 0 0 0 0 0 0.0 0.0
37 Trust 2 3,233 0 0 0 0 0 0.0 N/A
38 Scottish Socialist 10 3,157 0 0 0 0 0 0.0 -0.1
39 People Before Profit 1 2,936 0 0 0 0 0 0.0 N/A
40 Local Liberals People Before Politics 1 1,964 0 0 0 0 0 0.0 N/A
41 Independent - Esther Rantzen 1 1,872 0 0 0 0 0 0.0 N/A
42 Alliance for Green Socialism 6 1,581 0 0 0 0 0 0.0 0.0
43 Social Democrat 2 1,551 0 0 0 0 0 0.0 N/A
44 Pirate 9 1,340 0 0 0 0 0 0.0 N/A
45 Communist 6 947 0 0 0 0 0 0.0 0.0
46 Democratic Labour 1 842 0 0 0 0 0 0.0 0.0
47 Democratic Nationalist Party 2 753 0 0 0 0 0 0.0 N/A
48 Workers Revolutionary 7 738 0 0 0 0 0 0.0 0.0
49 Peace 3 737 0 0 0 0 0 0.0 0.0
50 New Millennium Bean Party 1 558 0 0 0 0 0 0.0 0.0
51 - 29,687,604 650 - - - Turnout 65.1 -