Skip to content

Instantly share code, notes, and snippets.

View zachmayer's full-sized avatar

Zach Deane-Mayer zachmayer

View GitHub Profile
@zachmayer
zachmayer / Kaggle Code.R
Created April 29, 2011 16:35
Cross validate RFE
#Directory
setwd('~/wherever')
#Load Required Packages
library('caret')
library('glmnet')
library('ipred')
library('e1071')
library('caTools')
@zachmayer
zachmayer / 1. Install Packages.R
Created May 3, 2011 14:34
Kaggle introduction
install.packages(c("caret","reshape2","plyr","caTools"),dependencies=c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances"))
@zachmayer
zachmayer / 1. Parameters.R
Created May 12, 2011 18:21
Kaggle Competition Walkthrough: Fitting a model
####################################
# Training parameters
####################################
MyTrainControl=trainControl(
method = "repeatedCV",
number=10,
repeats=5,
returnResamp = "all",
classProbs = TRUE,
summaryFunction=twoClassSummary
@zachmayer
zachmayer / 1. Setup.R
Created June 1, 2011 17:56
Kaggle Competition Walkthrough: Wrapup
#Setup
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
#Directory
setwd("~/Overfitting")
#Load Required Packages
library('caTools')
library('caret')
library('glmnet')
@zachmayer
zachmayer / Example.R
Created July 1, 2011 15:33
Example code for casualfactors
library(quantmod)
getSymbols(c('AHETPI','CES0500000003','CPIAUCSL','GASREGM'),src = "FRED")
getSymbols('XAU/USD',src = "oanda")
#Definitions:
#AHETPI = Average Hourly Earnings of Production and Nonsupervisory Employees: Total Private
#CES0500000003 = Average Hourly Earnings of All Employees: Total Private
#CPIAUCSL = Consumer Price Index for All Urban Consumers: All Items
#GASREGM = US Regular All Formulations Gas Price
#XAUUSD USD per 1 oz of gold. Last 500 days only
@zachmayer
zachmayer / GetNews.R
Last active May 31, 2017 21:01
Get News
getNews <- function(symbol, number){
# Warn about length
if (number>300) {
warning("May only get 300 stories from google")
}
# load libraries
library(XML); library(plyr); library(stringr); library(lubridate);
library(xts); library(RDSTK)
@zachmayer
zachmayer / multiRF.R
Created July 22, 2011 13:55
multiRF.R
multiRF <- function(x,...) {
foreach(i=x,.combine=combine,.packages='randomForest',
.export=c('X','Y'),.inorder=FALSE) %dopar% {
randomForest(X,Y,mtry=i,...)
}
}
multiRF(c(rep(3,10),rep(4,10),rep(5,10)),ntree=500)
@zachmayer
zachmayer / Recessions.R
Created August 9, 2011 16:33
Forecasting Recessions
#Code to re-create John Hussman's Recession warning index
#http://www.hussmanfunds.com/wmc/wmc110801.htm
#R code by Zach Mayer
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
library(quantmod)
#################################################
# 1. Credit spreads
#################################################
@zachmayer
zachmayer / WebScrape.R
Created August 10, 2011 13:28
Scrape Web Data
rm(list = ls(all = TRUE)) #CLEAR WORKSPACE
library(quantmod)
#Scrape data from the website
library(XML)
rawPMI <- readHTMLTable('http://www.ism.ws/ISMReport/content.cfm?ItemNumber=10752')
PMI <- data.frame(rawPMI[[1]])
names(PMI)[1] <- 'Year'
#Reshape
@zachmayer
zachmayer / Run.R
Created August 10, 2011 17:59
R googlepredictionapi
## Load googlepredictionapi and dependent libraries
library(rjson)
library(RCurl)
library(googlepredictionapi)
#Save dataframe to a file, upload to google storage, and train a model
write.csv(iris,'iris.csv')
model <- PredictionApiTrain(data='iris.csv', remote.file="gs://rdata/iris")
#Summarize model and predict for new data