Thursday, September 11, 2014

R User Group in Birmingham, AL

If Birmingham, UK has one, then Birmingham, AL, USA should too.image

There is a big gaping hole in R user groups between Georgia and Texas that I think needs filling.  Way back in 2011, I weakly posted R User Group Birmingham Alabama, but this time I am more determined.  (note: I might even consider some Julia and Python, and sure you all know I love some Javascript)

image

 

Thanks to @matlabulous for his award winning Shiny analysis of R user groups that supplied the screenshots for this post.

Tuesday, September 9, 2014

Frazzini goes French

A kind reader commented “Don’t know if you have seen it, but Frazzini goes French now too” .  I have profusely thanked and praised Kenneth French for his incredible data library.  Now, it appears I’ll have to repeatedly do the same for Andrea Frazzini.

Here is a quick plot of Quality Minus Junk (QMJ) around the world.  The code snippet is below the chart.

image

#ssrn citation to the paper
#Asness, Clifford S. and Frazzini, Andrea and Pedersen, Lasse Heje
#Quality Minus Junk (October 9, 2013)
#Available at SSRN: http://ssrn.com/abstract=231243



require(gdata)
require(quantmod)
require(latticeExtra)

#read spreadsheet
qmjFactors <- read.xls(
"http://www.econ.yale.edu/~af227/data/QMJ%20-%20Factors%20-%20monthly.xlsx"
,pattern = "DATE"
,blank.lines.skip = T
,stringsAsFactors = F
)
#convert spreadsheet data to R xts
#remove % with gsub, make numeric, and divide by 100
qmjFactors.xts <- as.xts(
do.call(cbind,
lapply(
qmjFactors[,-1]
,function(x){
df<-data.frame(as.numeric(
gsub(
x=x
,pattern="%"
,replacement=""
)
)/100)
colnames(df) <- colnames(x)
return(df)
}
)
) #date is first column; will use in order.by
,order.by = as.Date(qmjFactors[,1]) #as.Date(paste0(qmjFactors[,1],"-01"),format="%Y%m%d")
)

#xyplot(qmjFactors.xts)

asTheEconomist(
xyplot(
cumprod(1+na.omit(qmjFactors.xts))
, scales = list( y = list( relation = "same" ) )
, main = "Quality Minus Junk (2013)\nAsness,Frazzini, Pedersen\nhttp://ssrn.com/abstract=231243"
)
)

Monday, August 11, 2014

Fama/French Factors in 1 line of code

In the past, getting Fama/French factors from the Kenneth French dataset involved a convoluted procedure to download the zip file, unzip the file, clean the data, and convert to xts.  Now with Quandl, we can do it simply in one line of code.  Note:  this data is available in multiple formats (JSON, CSV, XML) from the API and for multiple code languages.

# use Quandl Kenneth French Fama/French factors
# http://www.quandl.com/KFRENCH/FACTORS_D

library(Quandl)
library(quantmod)

f <- Quandl(
"KFRENCH/FACTORS_D"
)

f <- as.xts(f[,-1],order.by=f[,1])

plot.zoo( f, main = NA )
mtext(
text = "Fama/French Factors from Quandl"
, adj = 0
, outer = T
, line = -2
, cex = 2
)

image

Wednesday, July 30, 2014

Refresh Old rCharts+flickr post with httr and pipeR

The R world keeps moving, and I noticed this old post didn’t work anymore, so I have rewritten it to use Hadley Wickham's httr instead of Rflickr for two reasons:

  1. Rflickr is not working for me anymore
  2. httr is a very helpful package for navigating the "what was scary to me" world of http and oauth

In addition to the changes above, I will also demonstrate use of the pipeR package from Kun Ren who has been quite prolific lately. I feel pretty strongly I will be rewriting this post one more time in the near future (already done) employing his rlist package.

image

Tuesday, July 22, 2014

Chart from R + Color from Javascript

Another color experiment combining resources from R and Javascript.  I just wish I could do Mean Phylogenetic Distance in Javascript like rPlotter.  I enjoyed using d3.js zoom behavior to pan and zoom the image on canvas.  Also, filedrop.js made the drag and drop image easy.  There are lots of mini lessons in this code for someone who want to pick inside the code.

You might also notice the reference color from my last post Pick a Color Site built in R with Shiny tags %$%.

image

Thursday, July 17, 2014

Alternate Price Plots | ggplot2 + magrittr

Just some quick experiments with ggplot2 + magrittr to plot prices differently than the traditional ways. We wi€™ll get daily data on the S&P 500 from FRED using getSymbols and then push it through a magrittr pipe to various alternative plots.

A couple notes for those who might be interested in magrittr. If you want to -> or <- but do not want the pipe fun to end, then you can use assign like this.

mydata
%T>% assign( x="sp_df", value = ., envir = .GlobalEnv ) %>%

Also for those who want to use the special + inside a pipe or just want your + on a different line for tidy code and easy commenting, then you can do like this.

ggplot( ) %>%
+ geom_point()

Let me know if I have it all wrong, and now some plots as promised.

require(ggplot2)
require(dplyr)
require(magrittr)
require(quantmod)

getSymbols("SP500", src="FRED", from = "1900-01-01", auto.assign=F) %>%
na.omit %>%
data.frame(
date = index(.)
) %T>% assign( x="sp_df", value = ., envir = .GlobalEnv ) %>%
mutate( year = format(date,"%Y") ) %>%
ggplot( aes( x=SP500, group = year ) ) %>%
+ geom_density() %>%
+ facet_wrap(~year,nrow=1) %>%
+ coord_flip() %>%
+ theme_bw() %>%
+ theme(
axis.line=element_blank()
,axis.text.x=element_blank()
,axis.title.x=element_blank()
)

plot of chunk unnamed-chunk-3

sp_df %>%
ggplot( aes( x = format(date,"%Y"), y = SP500 ) ) %T>%
( function(x){ print( x + geom_line() ) } ) %T>%
( function(x){ print( x + geom_point() ) } ) %T>%
( function(x){ print( x + geom_hex( bins = 20 ) ) } ) %>%
+ geom_violin()

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3


If you are interested in seeing the source Rmd, then it is here. One more note is this is copied/pasted with only minor changes from Rstudio knitted Rmd.