This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
formatDataTable = function(test) { | |
colnames = names(test) | |
# Fix numerics | |
pattern = '`%s` := as.numeric( `%s` )' | |
for ( field in colnames[classes == 'numeric']) | |
test[ , eval( parse( text = sprintf( pattern, field,field ) ) ) ] | |
# Fix dates | |
# Can throw a warning when trying to parse NA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#source http://stackoverflow.com/questions/13610293/how-to-get-rjava-0-9-3-to-work-on-os-x-10-7-4-with-oracle-java-1-7 | |
JAVA_LD_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre/lib/server | |
JAVA_LIBS='-L/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre/lib/server -ljvm' | |
JAVA_CPPFLAGS='-I/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/include/darwin' | |
R CMD javareconf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(ggplot2) | |
require(reshape2) | |
m = 2 | |
x = seq(from=0.1, to=5, by = .1) | |
log = log(x) | |
line = 1/m*(x - m) + log(m) | |
df = data.frame(x = x, | |
log = log, | |
line = line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot.ccf = function(x, y, lag.min=NULL, lag.max=NULL) { | |
ccf.data = ccf(x,y,plot=F) | |
indices = which(ccf.data$lag[,1,1] %in% lag.min:lag.max) | |
ccf.df = data.frame(lag = ccf.data$lag[indices,1,1], | |
correlation = ccf.data$acf[indices,1,1]) | |
ggplot(ccf.df, | |
aes(x = lag, y = correlation)) + | |
geom_bar(stat = 'identity') + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fieller = function(x,y) { | |
x.mean = mean(x) | |
y.mean = mean(y) | |
x.sd = sd(x) | |
y.sd = sd(y) | |
xy.cov = cov(x,y) | |
print(sprintf("x has mean %s, y has mean %s", x.mean, y.mean)) | |
print(sprintf("ratio of means is %s", x.mean/y.mean)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(ggplot2) | |
require(reshape2) | |
set.seed(100) | |
smallnoise = function(n) { | |
rnorm(n,5,1) | |
} | |
bignoise = function(n) { | |
if(rnorm(1) > 0) rnorm(n,50,10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
naiveTask.local = function() { | |
foreach(i = 1:10000, .inorder=T) %do% {rnorm(500)} | |
} | |
naiveTask.parallel = function() { | |
require(doMC) | |
registerDoMC() | |
foreach(i = 1:10000, .inorder=T) %dopar% {rnorm(500)} | |
} | |
batchTask.local = function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getFactorGrid = function(df) { | |
factors = getFactors(df) | |
df.factors = df[,factors,with=F] | |
unique(df.factors) | |
} | |
getFactors = function(df) { | |
classes = lapply(df, data.class) | |
colnames(df)[which(classes %in% c('character', 'factor'))] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
is_ec2 = function() {grepl("^[A-Za-z0-9].*-i-[A-Za-z0-9].*$", system('hostname', intern=T))} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nYearsAgo = function(t, n) { | |
as.Date(sprintf("%s%s", as.numeric(substr(as.character(t), 1, 4)) - n, substr(as.character(t), 5, 10)), format = "%Y-%m-%d") | |
} |