Skip to content

Instantly share code, notes, and snippets.

@helgejo
Last active December 26, 2015 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgejo/a44bbdce9f474698b104 to your computer and use it in GitHub Desktop.
Save helgejo/a44bbdce9f474698b104 to your computer and use it in GitHub Desktop.
R useful code snippets
#import data
training <- read.csv("data/adult.data", header = FALSE, na.strings = "?")
library(Hmisc);
#Always set the seed to get the same answer
set.seed(1337)
#Data description
summary(training)
describe(training)
head(training)
sapply(training, class)
str(training)
# Split training data into train and test to do cross-validation
inTrain <- createDataPartition(training$V15, p = 0.75, list = FALSE)
train.train <- training[inTrain,]
train.test <- training[-inTrain,]
#train the model
model <- train(V15 ~., data = train.train, method = "rpart")
vari <- varImp(model)
#Make predictions
rpartpred <- predict(model, train.test[,c(1:14)])
#Summarize results
results <- confusionMatrix(rpartpred, train.test$V15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment