Skip to content

Instantly share code, notes, and snippets.

View zachmayer's full-sized avatar

Zach Deane-Mayer zachmayer

View GitHub Profile
#Download S&P 500 data, adjust, and conver to monthly
set.seed(42)
library(quantmod)
getSymbols('^GSPC', from='1990-01-01')
GSPC <- adjustOHLC(GSPC, symbol.name='^GSPC')
GSPC <- to.monthly(GSPC, indexAt='lastof')
Target <- ClCl(GSPC)
#Calculate some co-variates
periods <- c(3, 6, 9, 12)
@zachmayer
zachmayer / multiclass.R
Created July 6, 2012 16:46
Multi Class Error Metrics
#Multi-Class Summary Function
#Based on caret:::twoClassSummary
require(compiler)
multiClassSummary <- cmpfun(function (data, lev = NULL, model = NULL){
#Load Libraries
require(Metrics)
require(caret)
#Check data
@zachmayer
zachmayer / Backtesting Part 5.R
Created June 25, 2012 22:19
Backtesting Part 5
rm(list = ls(all = TRUE))
memory.limit(size = 4095)
#Get Data
library(quantmod)
getSymbols('^GSPC',from='1900-01-01')
myStock <- Cl(GSPC)
bmkReturns <- dailyReturn(myStock, type = "arithmetic")
@zachmayer
zachmayer / InstallPackages.R
Created June 19, 2012 14:33
Install Packages
install.packages(c('caret', 'doParallel', 'ggplot2', 'data.table', 'caTools', 'XML', 'quantmod'), dependencies=TRUE)
@zachmayer
zachmayer / cv.ts 4.R
Created June 11, 2012 17:52
Time series cross-validation 4: forecasting the S&P 500
#Setup
rm(list = ls(all = TRUE))
setwd('path.to/cv.ts')
#Load Packages
require(forecast)
require(doParallel)
source('R/cv.ts.R')
source('R/forecast functions.R')
@zachmayer
zachmayer / 1.parDEoptim.R
Created January 23, 2012 14:28
parDEoptim
setClass("parDEoptim", representation(optim="list", member="list"))
parDEoptim <- function (fn, lower, upper, n=1, control = DEoptim.control(), .packages=NULL, ...) {
library("DEoptim")
library("foreach")
segSizes <- (upper-lower)/n
segments <- lapply(1:n, function(x){
lower+segSizes*(x-1)
})
@zachmayer
zachmayer / 1. Functions.R
Created December 29, 2011 18:05
Forecasting S&P 500
#Function to cross-validate a time series.
cv.ts <- function(x, FUN, tsControl, xreg=NULL, ...) {
#Load required packages
stopifnot(is.ts(x))
stopifnot(is.data.frame(xreg) | is.matrix(xreg) | is.null(xreg))
stopifnot(require(forecast))
stopifnot(require(foreach))
stopifnot(require(plyr))
@zachmayer
zachmayer / cv.ts.R
Created December 12, 2011 16:26
Time series cross-validation 3
#Function to cross-validate a time series.
cv.ts <- function(x, FUN, tsControl, xreg=NULL, ...) {
#Load required packages
stopifnot(is.ts(x))
stopifnot(is.data.frame(xreg) | is.matrix(xreg) | is.null(xreg))
stopifnot(require(forecast))
stopifnot(require(foreach))
stopifnot(require(plyr))
#!/usr/bin/env python
# encoding: utf-8
#Constants.R
#Created by Kenneth J. Shackleton on 14 June 2011.
#Copyright (c) 2011 Ringo Limited.
#All rights reserved.
#R Port by Zach Mayer on 4 December 2011
@zachmayer
zachmayer / additional demos.R
Created November 21, 2011 15:52
Functional and Parallel time series cross-validation
#Setup
set.seed(1)
library(fpp) # To load the data set a10
HZ <- 12
myControl <- list( minObs=60,
stepSize=1,
maxHorizon=HZ,