Skip to content

Instantly share code, notes, and snippets.

@smc77
Created October 26, 2011 01:45
Show Gist options
  • Save smc77/1315162 to your computer and use it in GitHub Desktop.
Save smc77/1315162 to your computer and use it in GitHub Desktop.
Logistic Regression
# Plot the sigmoid function
qplot(-10:10, 1/(1 + exp(-(-10:10))), geom="line", xlab="z", ylab="sigmoid function")
# Apply logistic regression to South African heart data from ESL
sa.heart <- glm(chd ~ sbp + tobacco + ldl + famhist + obesity + alcohol + age , family = binomial , data=hr)
summary(sa.heart)
# Download South African heart disease data
hr <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data", sep=",",head=T,row.names=1)
# Pretty plot
pairs(hr[1:9],pch=21,bg=c("red","green")[factor(hr$chd)])
# Scale data and create a random train / test split
n <- nrow(hr)
p <- ncol(hr)-1
test.ratio <- .2 # ratio of test/train samples
n.test <- ceiling(n*test.ratio)
testi <- sample(1:n,n.test)
traini <- setdiff(1:n,testi)
data.train <- hr[traini,]
data.test <- hr[testi,]
# Use stepwise logistic regression to reduce the dimensions
library(MASS)
sa.heart.step <- stepAIC(sa.heart)
summary(sa.heart.step)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment