Skip to content

Archive for

Using R in/for Governments


Recently British government (by Office  of National Statistics: ONS) just published their version of R manual for analysis of the government survey. The links to PDF and MS word versions of the manual including the R syntax are as below.

Note: The R syntax link is not working now. I am contacting the ONS, hope they will fix it soon.

The R Guide to ESDS Large-Scale Government Surveys
PDFWord

For the US governemnt, there is an emerging awareness and recognition of the power of R in their Big Data Initiative. David Smith (Revolution Analytics) has summarised the application of R in the US governemnt in his post here.

How to post R code on WordPress blogs


Most WordPress BloggeRs are using this text highlight syntax, some are not.
I hope that this post would be a reference source for new WordPress BloggeRs for posting their R code on their blog posts.

According to an official guide by WordPress.com on “Posting Source Code“, To post R code in the WordPress.com, just wrap R code as follows (without “#” in both wrappers):

######################################################

[#sourcecode language="r"]
Your R code and comments
x <- rnorm(100)
y <- x + 10
[#/sourcecode]

######################################################

  • From above, before your R code put the command in line 1, or [#sourcecode language=”r”]but without #
  • Then, place your R code (line 2-4).
  • End the code box by put the command line as in line  5, but without # or “[/sourcecode]

Then the code will appear as following.

Your R code and comments
x <- rnorm(100)
y <- x + 10

Moreover, more options can be configured to better describe the code efficiently.

  • autolinks (true/false)
    TRUE: Makes all URLs in your posted code clickable.
    Defaults: TRUE
  • collapse (true/false) 
    TRUE: The code box will be collapsed when the page loads, requiring the visitor to click to expand it.
    Comment: Good for large code posts.
    Defaults: False.
  • firstline (number)
    Comments: Use this to change what number the line numbering starts at
    Defaults =  1
  • gutter (true/false)
    TRUE:  Show the line numbering on the left hand side.
    FALSE: The line numbering on the left side will be hidden.
    Defaults = TRUE
  • highlight (comma-seperated list of numbers)
    You can list the line numbers you want to be highlighted.
    Example = “4,7,19″.
  • htmlscript (true/false)
    TRUE: Any HTML/XML in your code will be highlighted.
    Comment: This is useful when you are mixing code into HTML, such as PHP inside of HTML.
    Defaults = FALSE (only work with certain code languages)
  • light (true/false)
    TRUE: The gutter (line numbering) and toolbar (see below) will be hidden.
    Comment: This is helpful when posting only one or two lines of code.
    Defaults = FALSE
  • padlinenumbers (true/false/integer)
    TRUE: Automatic padding
    FALSE: No padding, and entering a number will force a specific amount of padding.
    Comment: Allows you to control the line number padding.
  • toolbar (true/false)
    FALSE: The toolbar containing the helpful buttons that appears when you hover over the code will not be shown.
    Defaults = TRUE
  • wraplines (true/false)
    FALSE: Line wrapping will be disabled. This will cause a horizontal scrollbar to appear for long lines of code.

If you are using WordPress.org, here is a plugin.

Update: I just found a nice post by William K. Morris on How to update your WordPress.com blog from R

แหล่งข้อมูล ประชาคมเศรษฐกิจอาเซียน AEC


แหล่งข้อมูลสำหรับ AEC หรือ ASEAN Economic Community

Official website

Thailand

Malaysia

EURO 2012 Forecast: Spain will beat Germany in the Final again!? predicted Economists


(Featureก Image from The New York Times)

As EURO 2012 (European Football Championship) will kick starts today (in seconds)!

Recently, there is a working paper from Faculty of Economics and Statistics, University of Innsbruck predicting the winner of the tournament to be Spain (again). Achim Zeileis (achim.zeileis@r-project.org), Christoph Leitner (christoph.leitner@wu.ac.at) and Kurt Hornik(kurt.hornik@wu.ac.at) used data from odds provided by 23 online book makers (as the experts’ opinion) in their simulation for each match from the the group round to the final, which is predicted to be Spain vs Germany. I think that they used in their study as well.

EURO 2012 winning probabilities from the bookmaker consensus rating.

The figure below show the probability that Team i will beat Team j, calculated by this formula;

Pr (Team i beat Team j) = (Ability of Team i) / (Ability of Team i / Ability of  Team j)

Winning probabilities, that Team i will beat Team j, in pairwise comparisons of all EURO 2012 teams

As an Econometrician and a football fan, I really like this paper and wish I can replicate their work for the tournament related to my home country team, Thailand.

Probability for each team to survive in the EURO 2012 ,i.e., proceed from the group-phase to the quarter finals, semi-finals, the final and to win the tournament.


The paper details are as follows.

Achim Zeileis, Christoph Leitner, Kurt Hornik (2012). History Repeating: Spain Beats Germany in the EURO 2012 Final. Working Paper 2012-09, Working Papers in Economics and Statistics, Research Platform Empirical and Experimental Economics, Universität Innsbruck.


Abstract

Four years after the last European football championship (EURO) in Austria and Switzerland, the two finalists of the EURO 2008 – Spain and Germany – are again the clear favorites for the EURO 2012 in Poland and the Ukraine. Using a bookmaker consensus rating – obtained by aggregating winning odds from 23 online bookmakers – the forecast winning probability for Spain is 25.8% followed by Germany with 22.2%, while all other competitors have much lower winning probabilities (The Netherlands are in third place with a predicted 11.3%). Furthermore, by complementing the bookmaker consensus results with simulations of the whole tournament, we can infer that the probability for a rematch between Spain and Germany in the final is 8.9% with the odds just slightly in favor of Spain for prevailing again in such a final (with a winning probability of 52.9%). Thus, one can conclude that – based on bookmakers’ expectations – it seems most likely that history repeats itself and Spain defends its European championship title against Germany. However, this outcome is by no means certain and many other courses of the tournament are not unlikely as will be presented here.

All forecasts are the result of an aggregation of quoted winning odds for each team in the EURO 2012: These are first adjusted for profit margins (“overrounds”), averaged on the log-odds scale, and then transformed back to winning probabilities. Moreover, team abilities (or strengths) are approximated by an “inverse” procedure of tournament simulations, yielding estimates of all pairwise probabilities (for matches between each pair of teams) as well as probabilities to proceed to the various stages of the tournament. This technique correctly predicted the EURO 2008 final (Leitner, Zeileis, Hornik 2008), with better results than other rating/forecast methods (Leitner, Zeileis, Hornik 2010a), and correctly predicted Spain as the 2010 FIFA World Champion (Leitner, Zeileis, Hornik 2010b). Compared to the EURO 2008 forecasts, there are many parallels but two notable differences: First, the gap between Spain/Germany and all remaining teams is much larger. Second, the odds for the predicted final were slightly in favor of Germany in 2008 whereas this year the situation is reversed.

  • Link in EconPapers [url]
  • Paper [pdf]
  • Presentation [pdf]
  • Interview of  Achim Zeileis on EURO 2012 forecast  in ORF: ZIB 24
    (2012-06-06, 00:10) (in German, I guess)

Will the history repeat?

11 Jun 2012 Update: Diffuseprior Blog is updating the odd ratio on EURO 2012 weekly.

R Style Guide


useR! logo

Having a systematic and consistent guide is important for programming, so does in R.

Here are some recommended style guides for programming in R.

Pick one of them and strictly follow it.

[update] There is a research showing that underscore  allow programmers tracking the code easier. (link)

1. Google’s R Style Guide

  • Link to Google’s R Style Guide (official URL, pdf)
  • Providing 14 R Style Rules including filenames, identifiers, indentation, spacing etc.
    “These rules were designed in collaboration with the entire R user community at Google”

2. Henrik Bengtsson’s R Coding Conventions

  • Link to R Coding Conventions
  • Developed since 2002. It’s version 0.9 (January 2009)
  • Please read this message in the introduction part before applying this coding convention.

” Please note that this document is under construction since mid October 2002 and should still be seen as a first rought draft.  There is no well defined coding recommendations for the R language [1] and neither is there a de facto standard. This document will give some recommendations, which are very similar to the ones in the Java programming style [2][3], which have found to be helpful for both the developer as well as the end user of packages and functions written in R.

3. Hadley Wickham’s Style Guide

Here is what he mentions in the introduction.
“Good coding style is like using correct punctuation when writing: you can manage without it, but it sure makes things easier to read. As with punctuation, there are many possible variations, and the main thing is to be consistent. The following guide describes the style that I use – you don’t have to use it, but you need to have some consistent style that you do follow. My style is based on Google’s R style guide, with a few tweaks.Good style is important because while your code only has one author, it will usually have multiple readers, and when you know you will be working with multiple people on the same code, it’s a good idea to agree on a common style up-front.”

4. R Core’s coding standard

5. formatR package

  • Links to the package’s CRAN GitHub site
  • One can also use Yihui’s formatR package to tidy R codes with function tidy.source().
  • Package description is as follows

“The formatR package was designed to reformat R code to improve readability; the main workhorse is the function tidy.source(). Features include:

  • long lines of code and comments are reorganized into appropriately shorter ones
  • spaces and indent are added where necessary
  • comments are preserved in most cases
  • the number of spaces to indent the code (i.e. tab width) can be specified (default is 4)
  • an else statement in a separate line without the leading } will be moved one line back
  • = as an assignment operator can be replaced with <-"

6. The State of Naming Conventions in R

by Rasmus Bååth in The R Journal (pdf)
(Added on 15 December 2012)

More posts about R