Wednesday, April 18, 2012

knitr Performance Report-Attempt 2

Over the years I have changed my learning process from reading thoroughly first before proceeding to reading minimally and then applying immediately.  I very quickly see the gaps in my knowledge.  This method is far more painful but seems to quicken progress up the learning curve.  You will definitely see the process in its entirety with this series on latex, knitr, and performance reporting.  For those who are still hesitating, jump right in with me and let me know your result.

As I experimented with Attempt 2, I could see from others sample .Rnw files that LyX can serve as the IDE for Sweave/knitr documents, and I thought this would be a more pleasant route.  However it was not, and I eventually reverted back to old-school manual coding.  This post from R, Ruby, and Finance talks about TeXnic Center as an IDE.  Maybe I will try it for attempt 3. I am still a long way from a useable result (any bets on how many attempts it takes?), but the report is definitely an improvement

In Attempt 2, I used the echo=FALSE option to not output the R code on the final pdf report.  However, the code is still all there for public view within the .Rnw file.

I just remembered that ttrTests offers a Latex output option. Just what I need--another distraction. Look for that in future posts.

R code from GIST:

Friday, April 13, 2012

knitr Performance Report-Attempt 1

I get very excited about new R packages, but rarely is my excitement so fulfilled as with knitr.  Even with no skill, I have already been able to adapt the example Yihui Xie provides in his knitr Graphics Manual into a crude first version of a performance report that I could actually show clients and prospects.  Although this is far from production quality, two days of experimentation already has me to a level that assures me of the wonderful potential of combining the existing amazing R packages with knitr.  Before knitr, I do not believe I could have accomplished even this rough first draft.

If you have not played with MiKTeX before, you will need to use the Package Manager to install the xcolor and tufte-handout templates for this example to work properly.  MiKTeX is installed automatically with LyX as discussed in yesterday’s post Latex Allergy Cured by knitr.  If xcolor causes problems, then just change your repository to another repository.

imageimage

knitr weaved with a little PerformanceAnalytics, lattice, and latticeExtra provides this first draft.  As always, thanks so much to the brilliant contributors that have made this possible.

R code in GIST:

Wednesday, April 11, 2012

The Making of…R Fell on Alabama

Slightly off topic but if you are as easily distracted by amazing technology as I am then you might enjoy my short (all made on my Iphone 4s) “The Making of…R Fell on Alabama.”  Clearly I am not accustomed to doing this type of video.

Latex Allergy Cured by knitr

I have always known that at some point I would have to succumb to the power of Latex, but Latex has been uncharacteristically intimidating to me.  I finally found the remedy to my Latex allergy with the amazing and fantastic knitr package from Yihui Xie.  With very minimal effort, I ran my first experiment and now am extremely excited to incorporate it in production-quality performance reports (I plan to document the steps to get there in future posts).

For those starting from scratch on Windows, I think the easiest method to get up and running is to install LyX, which will also install MikTex.  If these are successfully installed, then you should be ready to experiment with knitr in R.

I will use knitr’s stich function, which is clearly not designed for the robust production use of knitr, but makes for a very easy first test.  stitch will open a very short script, apply a template, and generate a Sweave style .Rnw (can be changed).  knit2pdf converts the .Rnw file into a pdf, and with a couple lines of code you get a remarkable result.

 

 

R code from GIST (unbelievably only 7 lines):

Monday, April 9, 2012

Piggybacking and Hopefully Publicizing R Experts

I was inspired by the Revolution Analytics blog post http://blog.revolutionanalytics.com/2009/11/charting-time-series-as-calendar-heat-maps-in-r.html on the d3.js style calendar heat map that Paul Bleicher from Humedica developed in R.  In an effort to publicize such fine work, I wanted to put a slightly different spin on it to visualize a system’s time in the market.  The system is ridiculously basic (not investment advice and will lose money), but the visualization is very helpful.

From TimelyPortfolio

To continue with the theme, I would like to continue to highlight some fine R work from http://systematicinvestor.wordpress.com.  Although his work is far more sophisticated than this, I thought I would use his plota function to plot the German Dax (used in this example) with RSI below.

From TimelyPortfolio

Thanks so much to the amazing R coders who provided the code and functions for this post.

R code from GIST:

Tuesday, March 27, 2012

R Fell on Alabama–Presentation to Birmingham Open Source

Birmingham Open Source Software (BOSS) was kind enough to invite me to do a debut presentation on R.  I had a lot of fun.  Below is the embedded Youtube.  Please let me know how I can improve.  I really appreciate helpful feedback.

Thursday, March 15, 2012

Opinions Not Backed by Money Updated Again

Strange that I am updating this post for a third time and nothing really has changed, but the fact that nothing has changed is incredibly interesting to me.  Since it is an update, I will not duplicate the explanation, so please read the last version Opinions Not Backed by Money Are Not That Believable--Updated and with R.  The basic conclusion is that we are in a bull market and AAII surveys are bullish, but nobody is willing to bet money on a good stock market.

From TimelyPortfolio

Data in a Google Doc (https://docs.google.com/spreadsheet/ccc?key=0Amqp2r96khJPdENIRnE1SG5nanJ5OXFyYVUxOXRBVmc) sourced from AAII- The American Association of Individual Investors and ICI.

R code (not even worth putting in GIST):

require(quantmod)
require(ggplot2)

aaii_ici=read.csv("aaii-ici-noblank.csv",row.names=1)

#using ggplot example from http://learnr.wordpress.com/2009/03/16/ggplot2-split-data-range-into-multiple-chart-series/
p <- ggplot(data=aaii_ici, aes(x = AAIIBULL-AAIIBEAR, y = runSum(aaii_ici[,4],3), colour = Range, shape = Range, label = Range))
p1 <- p + geom_point() + scale_y_continuous(limits = c(-200000, 200000),formatter=comma) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + stat_smooth(method="lm", se=FALSE) + ylab("ICI Equity Rolling 3-mo Sum") + opts(title = "ICI Equity Flows by AAII Survey")
print(p1)

Tuesday, March 13, 2012

Japan Trade More Specifically with Korea

Macro analysis of Japanese trade in posts Japanese Trade and the Yen and Japan Trade by Geographic Region revealed some very interesting changes.  Since the Korean Won is so undervalued versus the Japanese Yen on a Purchasing Power Parity (PPP) basis, I was very interested in how this undervaluation might have affected Japanese and Korean trade between themselves and with the United States.  The 2008-2009 financial crisis ended the growing Japanese surplus with Korea, and currently the Japanese surplus with Korea is at its lowest level since January 2003.

From TimelyPortfolio

When we look at the Japanese and Korean deficits with the United States, we can see Korea has maintained the level of surplus achieved prior to the 2008-2009 financial crisis, while the Japanese surplus has eroded rapidly over that same time frame.

From TimelyPortfolio

If the Korean Won appreciates versus the Yen, will Korean exporters maintain their recent market share gains?  If the Korean Won appreciates versus the Yen, will there be any impact on the US $?  If there is unexpected inflation, will this exacerbate the Korean Won appreciation?

R Code from GIST:

Monday, March 12, 2012

Japan Trade by Geographic Region

To further the analysis presented in Japanese Trade and the Yen, I thought I would take the more granular data provided by the Japanese Ministry of Finance on trade by geographic region.  Of course, I will use R to read, analyze, and plot the .csv data.  Below you will see there are only two remaining regions where Japan has a trade surplus (North and South America), and those surpluses are declining very rapidly.

From TimelyPortfolio

R code from GIST:

Wednesday, March 7, 2012

Japanese Trade and the Yen

I have had the pleasure over the last couple of weeks to help plan the CFA Society of Alabama 2012 Dinner featuring Jim Rogers and Barron’s Senior Editor Jack Willoughby.  The event was fantastic, and I would like to publicly thank Jim Rogers and Jack Willoughby for investing so much time and effort into the State of Alabama.  Both are incredible men, and I was delighted to meet them.

Jack Willoughby challenged Jim Rogers’ statement “you must understand currencies” with the point that  leveraged forex trading that has grown popular is not suitable for investors.  Jim responded by saying currencies seem unsafe just like all investments seem unsafe before a bull run, and that with proper education and management, currencies represent outstanding opportunities.  I think both are right.

Over a year ago in Japan Intentional or Accidental Pursuit of Deflation, I contended that the persistently appreciating Yen was starting to pose an extreme competitive disadvantage to the Japanese exporters.  Korean electronic, auto, and appliance manufacturers (LG, Samsung, Hyundai, Kia, etc.) have exploded their market share in the US due to their huge pricing advantage over their Japanese neighbors discussed recently in Hyundai Motor Europe confident on 2012 goal. 

“…Japanese rival Toyota, which has been overshadowed by the success of South Koreans in Europe in recent years, said good products backed by a currency which is 'extremely competitive' make Hyundai and Kia outperform the market.

'It is probably the mirror of the yen,' Mr Alain Uyttenhoven, vice-president at Toyota Europe, told reporters, referring to the strong yen that hurt Japanese carmakers. “

Japan seems very slow to change, but the Yen seems to have finally found a limit as the Japanese now suffer a significant trade deficit with the world.  The Japanese no longer can pursue the harmful Yen policy of the past.  Instead of Yen to US$ and Euro, they should focus on the Yen versus their competing Asian currencies.

From TimelyPortfolio

The question then becomes does this represents an opportunity for investors, and if it does, how do you go from US stocks and bonds to a short Yen long emerging currency position?

R code from GIST: