Skip to content

Instantly share code, notes, and snippets.

@arthurwuhoo
Created June 14, 2016 15:21
Show Gist options
  • Save arthurwuhoo/6d9c4908b1a139447cbc3e1689555bf0 to your computer and use it in GitHub Desktop.
Save arthurwuhoo/6d9c4908b1a139447cbc3e1689555bf0 to your computer and use it in GitHub Desktop.
# ------------------------------------------------------------------
# EXERCISE 3
# Use the birthwt data in the MASS package to construct a model for low birth
# weight. Are there any features which should be excluded from the model?
# ------------------------------------------------------------------
library(MASS)
library(caret)
birthwt2 <- birthwt #saving dataset in case I screw the original one up
head(birthwt)
str(birthwt)
## in order for confusionMatrix() to work,we actually need the "low"
## variable to be a factor. so let's do that before we split the data
## and start training.
birthwt$low <- factor(birthwt$low, labels = c("High","Low"))
# now split + train
index = createDataPartition(birthwt$low, list = FALSE, p = 0.8)[,1]
birthwt.train = birthwt[index,]
birthwt.test = birthwt[-index,]
birthwt.glm <- train(low ~ ., data = birthwt.train, method = "glm", family = binomial())
#no variable is significant?
confusionMatrix(predict(birthwt.glm, birthwt.test), birthwt.test$low, positive = "Low") #100% accuracy, wow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment