Last active
July 28, 2016 01:07
-
-
Save wjohnson/fb7003d080cdd5feda4a7a3011bda09b to your computer and use it in GitHub Desktop.
Model Automation in R
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
#http://archive.ics.uci.edu/ml/datasets/Bank+Marketing | |
data <- read.csv("~/in/bank/bank-full.csv",header=T, | |
sep=";") | |
####Decision Tree#### | |
library(rpart) | |
library(rpart.plot) | |
rp <- rpart(y~., data = data) | |
rpart.plot(rp) | |
####Random Forest Models#### | |
library(randomForest) | |
rf <- randomForest(y~., data = data) | |
rf$importance #Var Name + Importance | |
varImpPlot(rf) #Visualization | |
####Step Wise Regression#### | |
data("mtcars") | |
mt <- mtcars | |
#Get the Independent Variables | |
#(and exclude hp dependent variable) | |
indep_vars <-paste(names(mt)[-which(names(mt)=="hp")], | |
collapse="+") | |
#Turn those variable names into a formula | |
upper_form = formula(paste("~",indep_vars,collapse="")) | |
#~mpg + cyl + disp + drat + wt + qsec + vs + am + gear + carb | |
library(MASS) | |
mod <- lm(hp~.,data=mt) | |
#Step Backward and remove one variable at a time | |
stepAIC(mod,direction = "backward",trace = T) | |
#Create a model using only the intercept | |
mod_lower = lm(hp~1,data=mt) | |
#Step Forward and add one variable at a time | |
stepAIC(mod_lower,direction = "forward", | |
scope=list(upper=upper_form,lower=~1)) | |
#Step Forward or Backward each step starting with a intercept model | |
stepAIC(mod_lower,direction = "both", | |
scope=list(upper=upper_form,lower=~1)) | |
####Auto Arima#### | |
library(forecast) | |
library(fpp) | |
#Step Backward and remove one variable at a time | |
data("elecequip") | |
ee <- elecequip[1:180] | |
model <- auto.arima(ee,stationary = T) | |
plot(forecast(model,h=10)) | |
lines(x = 181:191, y= elecequip[181:191], | |
type = 'l', col = 'red') | |
####Hyper Parameter Search #### | |
library(caret) | |
#Step Backward and remove one variable at a time | |
tctrl <- trainControl(method = "cv",number=10, | |
repeats=10) | |
rpart_opts <- expand.grid(cp = seq(0.0,0.01, by = 0.001)) | |
rpart_model <- train(y~., data = data, method="rpart", | |
metric = "Kappa", trControl = tctrl, | |
tuneGrid = rpart_opts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment