Thursday, May 21, 2020

How to plot COVID-19 epidemic curves with R

It's been a while since I have had time to update my blog. Given the current situation of the COVID-19 outbreak, I guess it'll be useful to provide a step-by-step guide on how to extract and plot those epidemic curves that everyone seems to be plotting, with the help of R. To run these code, you need to have the latest version of R and RStudio installed.

Note that since I write these codes for one of my current projects, not all of them are crucial in creating the plots.
  1. First, we need to load the following packages and define a function to compute differences and logarithmic differences (this is to compute the growth rates):
  2. Then, we extract the latest data from the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE)'s Github repository. There are three main variables of interest: The daily cumulative numbers of confirmed cases, recovered cases and death tolls:   
  3. Next, we can do some reshaping the panel data from wide to long format:  
  4. The next step is to compute the growth rates of these variables:  
  5. I then create a nice theme for our ggplot2 plots:  
  6. Now we can begin plotting. The first thing to do is to get the data for some representative countries. I include the big players that are featured heavily in the news and two developing ones (my home country and its neighbor, can you guess which one I am from?). You can modify this country list anyway you like:  
  7. Then plot the daily cumulative epidemic curves using the confirmed cases and deaths. This result in the following plots:  
  8. And finally, we can plot different varieties of these curves using the changes in cases and in death tolls:  
That's it for now folks. If I find some more time, I'll discuss these plots and perhaps update them with new features. If you have any questions or comments please leave them below or contact me via email. 

Friday, July 27, 2018

Fun fact about Singapore's time zone

A fun fact which I noticed, but haven't thought about until now 
According to this map, Singapore is in the "wrong" time zone: Since she's westward of VN, the time zone should be one hour lag to SG's. 
The short answer I found in the net is "Sometime in 1981, Malaysia declared that West / Peninsular Malaysia would move their clocks ahead by 30 min to match the time in use in East Malaysia (8 hrs ahead of GMT). This meant that Singapore would be in a rather awkward position in time if she didn't follow suit, especially with the amount of traffic and trade across the causeway." (https://www.quora.com/Why-do-Peninsular-Malaysia-and-Singap…
East Malay is in the same time zone as Perth.
Perth is eastward of VN, so she leads one hour.


Tuesday, June 5, 2018

Tuesday, March 20, 2018

Reading list 20/03/18

1- Shining a Light on Purchasing Power Parities
http://www.nber.org/papers/w24419.pdf
2- Income Differences and Prices of Tradables: Insights from an Online Retailer
by Ina Simonovska
http://www.nber.org/papers/w16233.pdf
3- Trying to Understand the PPPs in ICP 2011: Why Are the Results So Different?
by Angus Deaton and Bettina Aten
https://pubs.aeaweb.org/doi/pdfplus/10.1257/mac.20150153

Friday, February 16, 2018

Vietnam interactive map with R (part 2)

This is the update on the Vietnam choropleth map from the previous post. I thank Loan Vu for providing me with the VHLSS data.

This map presents average annual household income from two sources : "Employment" (primary and secondary jobs-related) and "Agriculture" (a sum of income from "Cultivation", "Animal Husbandry" and "Agricultural, Forestry and Aquaculture" sources). The total income is shown in this. The updates are:
  • I provide values converted to $US here, rather than in VND as before (although doing this disregard purchasing power parity altogether!). 
  • A toggle button to select income types, plus a legend for each variable, are added.
  • As before, map is zoom-able. The min and max zoom parameters are slightly changed.
I haven't figured out how to add more than 2 layers without significantly reduce the loading time (not that the current loading time is that impressive!). Additionally, to make it more interactive, the legends should be toggled together with the map layers. I did write a better UI with Shiny, but publishing it requires premium account for Rstudio. Hopefully I can do more tinkering in the future, maybe after finishing my studies.

Disclaimer:  The information on this map was derived from digital databases from OpenStreetMap. OpenStreetMap is open data, licensed under the Open Data Commons Open Database License (ODbL) by the OpenStreetMap Foundation (OSMF). The map tile used is licenced as CC BY-SA.  For more about copyright, please see https://www.openstreetmap.org/copyright 

Sunday, February 4, 2018

Vietnam interactive map with R

 

This is my first attempt at drawing an interactive map with leaflet, created by the Rstudio team (a nice introduction to the package, which I follow to create this map, can be found here). Quite a bit of data involved, so the map might load rather slowly.
The data are from the Vietnam Household Living Standard Survey (VHLSS), 2014 round. They are available for purchase from the Vietnam's General Statistics Office. Here I only use income data ("Muc 4"). I aggregated the multiple income sources at the household level (summing across all family members), and take the unweighted mean of income across all households surveyed in each province.

  • The gradient color scaling is based on the magnitude of the mean income from all sources. The darker the color scheme, the more affluence the average household there.
  • To see the underlying data for the provinces, hover your mouse over the corresponding provinces. Keep in mind that this is only a prototype. I'm planning to break the map into layers (each shows one category of income) and better representations.
  • Zoom in using mouse scroll or the buttons on the left. Click, hold and drag left mouse to refocus.
Feel free to contact me for details.

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.
 

Friday, September 15, 2017

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