Skip to content

Instantly share code, notes, and snippets.

View ramnathv's full-sized avatar

Ramnath Vaidyanathan ramnathv

View GitHub Profile
@ramnathv
ramnathv / diet.r
Created August 24, 2010 01:44 — forked from diegovalle/diet.r
########################################################
##### Author: Diego Valle Jones
##### Website: www.diegovalle.net
##### Date Created: Mon Mar 01 18:51:27 2010
########################################################
#1. For what foods are Americans and Mexicans outliers
#2. Partition the data around medoids to classify the
#countries of the world according to what they eat
library(ggplot2)
@ramnathv
ramnathv / mls_analysis.R
Created August 28, 2010 22:20
MLS Analysis
# ANALYSIS OF MAJOR LEAGUE SOCCER
# MOTIVATION: http://thelogcabin.wordpress.com/2010/08/16/goals-per-game-in-mls/
# http://thelogcabin.wordpress.com/2010/08/23/a-rule-change-in-major-league-soccer/
# LOAD ALL REQUIRED LIBRARIES
library(XML);
library(reshape);
plot.heat <- function(tmp,state.map,z,title=NULL,breaks=NULL,cex.legend=1,bw=.2,col.vec=NULL,main=NULL,plot.legend=TRUE, ...) {
tmp@data$zCat <- cut(tmp@data[,z],breaks,include.lowest=TRUE)
cutpoints <- levels(tmp@data$zCat)
if (is.null(col.vec)) col.vec <- heat.colors(length(levels(tmp@data$zCat)))
cutpointsColors <- col.vec
levels(tmp@data$zCat) <- cutpointsColors
cols <- as.character(tmp$zCat)
##cols <- "white"
plot(tmp,border=cols, lwd=bw,axes = FALSE, las = 1,col=as.character(tmp@data$zCat),main="A", ...)
if (!is.null(state.map)) {
@ramnathv
ramnathv / merge_sp_df.R
Created September 6, 2010 15:09
Merge a Spatial Polygon with a Data Frame
sp@data = data.frame(sp@data, df[match(sp@data[,by], df[,by]),])
# load libraries
library(ggplot2);
library(Zelig);
# function to extract coefficients of a zelig model
sd.coef2 = function(x){
est = summary(x)$coef[,1];
economist = theme_update(plot.background = theme_rect(fill = 'lightblue',
colour = 'black'),
panel.background = theme_rect(fill = 'lightblue',
colour = NA),
axis.text.x = theme_text(colour = 'black',
size = base_size * 0.8,
lineheight = 0.9,
vjust = 1),
axis.text.y = theme_text(colour = 'black',
size = base_size * 0.8,
country loccurover text
Switzerland 80 6.78
Brazil 40 5.26
Euro area 30 4.79
Canada 10 4.18
Japan 5 3.91
United States 0 3.71
Britain -5 3.63
Singapore -10 3.46
South Korea -20 3.03
@ramnathv
ramnathv / nhl_skaters.R
Created February 9, 2011 04:30
Scrape Historical NHL Skaters Data
# LOAD LIBRARIES REQUIRED
library(plyr);
library(XML)
# FIGURE OUT PATTERN OF URL FOR EACH SEASON
url.b1 = 'http://ca.sports.yahoo.com/nhl/stats/byposition?pos=C,RW,LW,D';
url.b2 = '&sort=14&conference=NHL&year=season_';
url.b3 = y;
# Load Data
poly = readShapeSpatial('1_data/maps/Precincts.shp');
df = read.csv('1_data/precincts.csv');
# Clean Data
df = df[,-grep('count', names(df))];
names(df) = sub('_pct', '', names(df));
# Plot Choropleth
choro1 = plotChoropleth(.poly = poly, .df = df,
# Panel of Choropleth Maps using Facets
# Plot Rahm Emnauel and Gery Chico
choro2 = plotChoropleth(.poly = chicago, .df = df,
id1 = list('WARD', 'PRECINCT'),
id2 = list('ward', 'precinct'),
field = c('emanuel', 'chico'),
title = 'Chicago Mayoral Election 2011',
legtitle = 'Votes %',
colpal = 'Blues', n = 5, style = 'pretty') +
opts(legend.position = c(0.15, 0.25));