After the recent foray into stock analysis using quantmod, I thought it worthwhile to mention that the library can be used to analyze a wide variety of investments, including precious metals. It is also worthwhile to mention that there are other possibilities for using R related to investment research that do not fit as neatly into the paradigm for which quantmod was designed. You might be interested in knowing - as indicated by the chart above - that the demand for gold for dentistry has remained steady throughout the past year.
This is straightforward when dealing with ETFs and indexes that essentially function like stocks. For example the XAU PHLX Gold/Silver Sector tracks the performance of a set of companies engaged in gold or silver mining.
loadSymbols(c('^XAU', 'GLD', 'IAU'))
This index consists of stocks and so the chart can be handled like any other quantmod stock chart.
Exchange Traded Funds like GLD and IAU invest in gold and seek to track the price of gold. One question I had was whether the IAU and GLD ETFs performed the same. However, this does illustrate how easy this sort of comparison is to make using R.
chartSeries(GLD - IAU)
Other than occasional outliers - they do seem to track the same. You can also directly query the price of gold using the getMetals function. There is a limit on how much data can be obtained using this method - so you will need to specify a date range or an error will occur.
However, there is a wealth of information that does not fit simply into the stock analysis model. For instance, supply and demand - although reflected in the price of equities and volume of trading - is not directly being tracked and reported on in the example above. It is also not apparent who might be interested in gold and why they might be interested in it.
The chart indicates a general increase in demand (at least as far as can be seen due to the legend obscuring the top of the bars). Instead, lets look at what is possible with ggplot2.
The records above constitute the detail information without summarization or aggregation. One unusual number I noticed was the negative number related to bar hoarding. The best I can tell, this was a bit of an anomaly that occurred when individuals in certain countries sold off gold due to fluctuations in currency value. In any case, this negative number is a point of interest as the various charts are rendered. However, keep in mind, my purpose is not to provide financial interpretation of the data (and certainly nothing like investment advice of any sort).
Other identified retail invest
The original data was - of all things - an image. Therefore, the data is entered in as a matrix rather than scraping the web. Row names and column names are assigned, and a chart can be created using base R functionality. Rather than using a default R color palette, specific colors are assigned.
99.2,48.2,36.2,50.5,25.6), nrow=7,ncol=5, byrow=TRUE)
rownames(gold.demand)=c('Electronics','Other Industrial','Dentistry','Bar Hoarding','Official Coin','Medals/Imitation Coin','Other identified retail invest')
Bar charts in ggplot 2 require data in a different format - first transpose and reshape the data. The t function is like magic when compared with this type of operation in other languages (like SQL). All of three characters to pivot the data.
The "melt" function in the reshape library breaks out the cross tab organization into individual records.
Again, an exceedingly simple operation that illustrates the strength of R for manipulating data.
Name Quarter Value
1 Electronics 2009-Q1 49.9
2 Other Industrial 2009-Q1 16.0
3 Dentistry 2009-Q1 13.1
4 Bar Hoarding 2009-Q1 -28.1
5 Official Coin 2009-Q1 69.4
6 Medals/Imitation Coin 2009-Q1 3.9
Now that the data is properly structured, the chart can be rendered.
qplot(Quarter, Value, data=melted_gold, fill=Name, geom="bar" ) +
opts(title='2009-2010 Gold Demand')
ggplot(melted_gold, aes(Quarter, Value))
+ facet_wrap(~Name, ncol = 2)
ggplot(melted_gold, aes(Quarter, Value, fill=Name)) +
facet_wrap(~Name, nrow=4) +
R's ability as a functional language to manipulate data objects is really amazing. It is a bit of a paradigm shift for me. I tend to think in terms of how the data structure might be created (make some loops, iterate through data reassemble a multidimensional array etc). R's functional capabilities (and well designed solutions by others) allow focus on what is being done rather than how. The ggplot2 library really does have some marvelous capabilities as well. There is a pretty clear separation of functionality between various function calls when assembling a graph. The challenge remains making adjustments to "think in R" and quickly articulate the constraints of a problem and map this to a concise solutions that uses the language in the way it was intended.