Skip to content

Instantly share code, notes, and snippets.

@JoesDataDiner
JoesDataDiner / Housing.R
Created October 13, 2013 16:48
UK House Prices vs. Household Incomes
rm(list=ls())
library(plyr)
library(ggplot2)
library(RColorBrewer)
price_data_dir = "C:\\***\\"
price_data_filepaths = paste(price_data_dir,list.files(price_data_dir),sep="")
price_data_list = lapply(price_data_filepaths,FUN<-function(x) read.csv(x,header=FALSE))
price_data = do.call(rbind,price_data_list) #this gives about 1.5mil transactions during 2010-2011
price_percentiles = quantile(price_data[,2],probs = seq(0.01,0.99,by=0.01)) #we know column 2 contains the prices
@JoesDataDiner
JoesDataDiner / GetOfficeMetadata.R
Created June 10, 2013 22:32
Extracting Microsoft Office metadata using R
library(XML)
#use R's inbuilt unzip function, knowing that the required metadata is in docProps/core.xml
doc = xmlInternalTreeParse(unzip('test.docx','docProps/core.xml'))
#define the namespace
ns=c('dc'= 'http://purl.org/dc/elements/1.1/')
#extract the author using xpath query
author = xmlValue(getNodeSet(doc, '/*/dc:creator', namespaces=ns)[[1]])
@JoesDataDiner
JoesDataDiner / FinCrisisPartII.R
Last active December 14, 2015 19:39
The Financial Crisis on Tape Part II
rm(list=ls())
#install the superb quantmod library
#we will use it to download the data and compute returns
library(quantmod)
#install various other libraries for the data manipulation and graphing.
library(ggplot2)
library(scales)
library(RColorBrewer)
library(reshape2)
library(grid)
@JoesDataDiner
JoesDataDiner / AllThatGlitters_Prices.R
Last active December 14, 2015 11:09
All That Glitters - Prices
#Gold and stock prices in '07-'08:
hist.prices =
do.call(cbind,
lapply(c("GLD","SPY","QQQ"), function(symbol){
symbol.data = get(symbol) #get from enviroment
symbol.data.adj = Ad(symbol.data) #extract the adjusted price
symbol.data.adj
})
)
@JoesDataDiner
JoesDataDiner / AllThatGlitters_Correl.R
Created March 3, 2013 17:58
All That Glitters - Correlation
#Extract the correlation of gold to other assets
gold_data = rolling.correlmatrix[((rolling.correlmatrix$Var1=="GLD") * (rolling.correlmatrix$Var2 !="GLD")) == 1,]
#plot the evolution of the correlation of gold
ggplot(gold_data, aes(y = Var2, x = quarter, fill = Correl))+
geom_tile() +
scale_fill_gradientn(colours = mycolvec_2
,limits = c(-1,1))+
facet_grid(.~year)+theme_bw() +
theme(panel.grid.major.x = element_blank(),
@JoesDataDiner
JoesDataDiner / FinCrisisPartI_Graph_Gold.R
Created February 23, 2013 14:16
The Financial Crisis on Tape Part I - Graph - Gold
gold_data = rolling.correlmatrix[((rolling.correlmatrix$Var1=="GLD") * (rolling.correlmatrix$Var2 !="GLD")) == 1,]
ggplot(gold_data, aes(y = Var2, x = quarter, fill = Correl))+
geom_tile() +
scale_fill_gradientn(colours = mycolvec_2
,limits = c(-1,1))+
facet_grid(.~year)+theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
axis.title.x = element_blank(),
@JoesDataDiner
JoesDataDiner / FinCrisisPartI_Graph.R
Created February 23, 2013 14:12
The Financial Crisis on Tape Part I - Graph - Final
#Personally I find the TLT anti correlation, rather obscures judging
#Put it to the right/
PrefOrder = c("SPY","QQQ","EEM","IWM","EFA","IYR","GLD","TLT")
rolling.correlmatrix$Var1 = factor(rolling.correlmatrix$Var1, PrefOrder) #careful not to just rename levels here!
rolling.correlmatrix$Var2 = factor(rolling.correlmatrix$Var2, PrefOrder) #careful not to just rename levels here!
#introduce the purple colouring to the top of the range to better highlight correlation changes.
mycolvec_2 = c(
rev(brewer.pal(8,"Blues")),
colorRampPalette(c(
@JoesDataDiner
JoesDataDiner / FinCrisisPartI_Graph_Draft.R
Last active December 14, 2015 03:19
The Financial Crisis on Tape Part I - Graph - 1st attempt
library(ggplot2)
library(scales)
library(RColorBrewer)
#this is a slightly bespoke scale - basically I wanted a blue to red scale
#that gave extra variation over the interesting 0.6-1.0 range.
mycolvec_1 = c(rev(brewer.pal(8,"Blues")),rep("white",3),brewer.pal(8,"YlOrRd"))
ggplot(rolling.correlmatrix, aes(x = Var1, y = Var2, fill = Correl)) +
geom_tile() +
scale_fill_gradientn(colours = mycolvec_1
,limits = c(-1,1))+
@JoesDataDiner
JoesDataDiner / FinCrisisPartI_CorrelComp.R
Created February 23, 2013 14:03
The Financial Crisis on Tape Part I - Correlation Computation
#now use Hadley's great plyr library and a simple function to compute the
#rolling correlation matrix
library(plyr)
DataFrameCorOutput<-function(hist.returns){
require(reshape2)
correls = melt(cor(as.matrix(na.omit(hist.returns))))
colnames(correls) = c("Var1","Var2","Correl")
correls
}
@JoesDataDiner
JoesDataDiner / FinCrisisPartI_DataRetrieval.R
Created February 23, 2013 13:55
The Financial Crisis on Tape Part I - Data Retrieval
#install the superb quantmod library
#we will use it to download the data and compute returns
library(quantmod)
# load historical prices from Yahoo Finance
# I use a set that I saw used by systematic investor CREDIT
symbols = c('SPY','QQQ','EEM','IWM','EFA','TLT','IYR','GLD')
symbols.names = c('S&P 500,Nasdaq 100,Emerging Markets,Russell 2000,EAFE,20 Year
Treasury,U.S. Real Estate,Gold')