Thursday, December 7, 2017

International Comparison Program 2011 results

Playing around with excel bubble plot, and ICP data http://siteresources.worldbank.org/…/Resources/ICP_2011.html
Things are likely to be more expensive in richer countries, but not always. Market exchange rates tend to underestimate income.
 

Sunday, August 6, 2017

My slides for Econ 4415

Last week I presented one of my previous slides in the first class of Econ 4415, as part of my teaching assistant work. 


This is a considerably (I hope) updated version of the slides I presented last year at the EMERGE conference. The animations should work better here, although they are not as ideal as those shown in an actual PowerPoint session. Hopefully we'll see the improvement of Google slides sometimes soon.


Friday, July 21, 2017

Interactive Trade Flows Chord Diagram (with R)

Chord Diagram is becoming more and more popular in offering an elegant way to capture the complexity of big data, and more so if you can make them "user-driven", i.e. interactive. This post suggests a few tips to do it.

 

After one full day tinkering with Javascripts I was able to produce a very nice interactive plot using the 2013 East Asian intra-regional export flows data provided by one of my fellow PhD students at UWA (Son Nguyen) and the code in the developing R package "chorddiag". I got great help from Son in publishing this plot also.
Note that different from the one-way diagram in my previous post, here the chords are bi-directional, and their colors match those countries with lower bilateral export values, which are also at the smaller ends of these chords. That is, each chord represents a trade deficit for the colored countries. Not the best arrangement I'd say, since it would feel more natural to have the chord colors matched with countries with larger export (to give more of a "flowing" feel?), as shown in a very similar plot (for Australian trade data only) featured in a recent blog by World Bank's Siddhesh Kaushik. For more prominent Javascript developers, there is a blog showing how to create a trade diagram directly with D3 here

Wednesday, July 19, 2017

Migration Chord Diagram

This is the plot done using Guy Abel's code in his "migest" R package. It shows the average inter- and intra-regional flows of migration from 2005 to 2010, based on place of birth data. Unit is in millions. The arrows indicate direction of flows (as do their colors), while their sizes indicate the magnitude of flows. The width of a region's arch indicates the total migration flow for that region. It seems that accept for Latin America & Caribbean, intra- dominates inter-regional migration in this 5-year period. I will probably use this setting to plot a similar figure with my bilateral trade cost data in the future.

See more about the guiding steps in Guy's blog here and the blog of his co-author Nikola Sander here. The full data (with country-level data) as well as an visually stunning interactive figure are accessible in the authors' migration project home page.


Update: After one day tinkering I was able to produce an interactive plot of bilateral export flows, using the 2013 East Asian intra-regional data provided by one of my fellow PhD Student at UWA (Son Nguyen) and the code in the developing R package "chorddiag". There is an issue with the colors, as they don't show up properly in some computers. See the plot in my subsequent blog here, or on Rstudio live server at http://rpubs.com/ranma1202/292965


Wednesday, July 12, 2017

My working paper on Law of one price

This paper was recently listed (12 Jul 2017) on SSRN's Top Ten download list for Macroeconomics: Consumption, Saving, & Wealth, Global Commodity Issues, Food Industry, AARN: Globalization & Transnationalism and Cross-Cultural Studies eJournals.

How to plot and publish 3D plot online with R

In this post I show how to plot a 3D object and put it into an HTML-supported platform (as an example, see my previous post here). Being horrible with IT related matters, it took me quite a while and tediously long hours of website browsing (and groaning!) to figure this out, so I figure I'll put the code here and perhaps save some time for anyone who interests in this.

OK, first, the plot of a 3D globe itself is done simple enough, as the example code is provided in the R package "rgl":

library(rgl)
###plot the globe
open3d()
options(rgl.useNULL=FALSE)
par3d(windowRect = c(100, 100, 900, 900))
# Show the Earth with a cutout by using clipplanes in subscenes
lat <- matrix(seq(90, -90, len = 50)*pi/180, 50, 50, byrow = TRUE)
long <- matrix(seq(-180, 180, len = 50)*pi/180, 50, 50)
r <- 6378.1 # radius of Earth in km
x <- r*cos(lat)*cos(long)
y <- r*cos(lat)*sin(long)
z <- r*sin(lat)
#
obj <- surface3d(x, y, z, col = "white",
       texture = system.file("textures/worldsmall.png", package = "rgl"),
       specular = "black", axes = FALSE, box = FALSE, xlab = "", ylab = "", zlab = "",
       normal_x = x, normal_y = y, normal_z = z)
cols <- c(rep("chocolate4", 4), rep("burlywood1", 4), "darkgoldenrod1")
rs <- c(6350, 5639, 4928.5, 4207, 3486,
                         (3486 + 2351)/2, 2351, (2351 + 1216)/2, 1216)
for (i in seq_along(rs))
  obj <- c(obj, spheres3d(0, 0, col = cols[i], radius = rs[i]))

###put it in a local host HTML page
browseURL(paste("file://",writeWebGL(dir=file.path(tempdir(), "webGL"), width=500), sep=""))

In case you are new to R, find out more about it here. It short it is a powerful statistical programming language of which vast capabities extend well beyond the field of statistics and into fields such as designing and engineering, to name but a few. The power of R is humbly illustrated here with this 3D plot. No need to say this, but I've fallen in love with R since my days doing Master thesis, over my favourite Eviews and Stata packages.

Right, after plotting, the hassle is on how to publish it online. The code above allows you to open the plot of a 3D globe in a browser. If you have access to an HTML-supported platform (like a Blogger account), all you need is an HTML representation of the plot. To get this, you can play around with another R package called "knitr" which has very extensive interaction with "rgl".

However, I found another way which is arguably faster (in a most "hackish" manner): suppose you are using Firefox, go to "Open menu" (the button at the top right corner, as of my current version 45). Then open the "Developer" tab and go to "Page source". This opens the immediate page's source code, in HTML form. This is what you need. I'm sure other browsers allow you to do the same thing, it might require some research and tinkering though.

Finally, simply copy all the codes and put them into the platform you are using. In my case, Blogger posts have an HTML section where I can add these 3D pictures anywhere in the posts.

That's it.

Monday, June 26, 2017

3d plot of the globe (plotted by R "rgl" package)

RGL model
.

Drag mouse to rotate model. Use mouse wheel or middle button to zoom it.

Object written from rgl 0.98.1 by writeWebGL.