I am trying to generate a decision boundary of logistic regression
. My Training set is 2/3 and the test set is 1/3, I have however tried producing the decision boundary but not sure whether is it the desired behavior or not. I am using the caret
library for the logistic model and the lattice
library for the plot.
here is my approach:
#Data creation
library(mvtnorm)
a1 <- c(1, 0)
a2 <- c(0, 1)
M <- cbind(a1, a2)
C0 <- rmvnorm(100, c(0, 0), M)
C1 <- rmvnorm(100, c(5, 0), M)
dat <- rbind(C0, C1)
C <- data.frame(dat)
y <- sign(-1 - 2 * dat[,1] + 4 * dat[,2] )
y[y == -1] <- 0
df1 <- cbind.data.frame(y, C)
df1
library(caret)
#Create training and test sets
set.seed(123)
trainIndex <- sample(c(FALSE,TRUE), size = nrow(df1), prob =
c(.33,.67), replace = TRUE)
train_set <- df1[trainIndex, ]
test_set <- df1[!trainIndex, ]
# Learn Logistic Regression Model
fit <- glm(y ~ ., data = train_set, family = "binomial")
pred <- predict(fit, newdata = test_set, type = "response")
tab <- table(actual = test_set$y, predicted = round(pred))
cm1 <- confusionMatrix(tab)
cm1
slope <- coef(fit)[2]/(-coef(fit)[3])
intercept <- coef(fit)[1]/(-coef(fit)[3])
library(lattice)
xyplot( x2 ~ x1 , data = df1, groups = y,
panel=function(...){
panel.xyplot(...)
panel.abline(intercept , slope)
panel.grid(...)
})
My decision boundary
As you can observe it has not got any clear separation of two classes which I'm highly uncertain of.
Also, I have learned that the hypothesis space is represented as a set of the conjunction of constraints but I cannot visualize this in R. Is it the decision boundary per se or there are different plots/visualizations to represent it?