Kevin Junior Mekulu
Kevin Junior Mekulu

Reputation: 17

Implement Logistic Regression

I am applying multiple ML algorithm to this dataset so I tried logistic regression and I plotted the predictions and it seems completely off since the plot only shows data points from one class. Here is the data and what I attempted

set.seed(10)

x1 <- runif(500) - 0.5

x2 <- runif(500) - 0.5

y <- ifelse(x1 ^ 2 - x2 ^ 2 > 0, 1, 0)

dat <- data.frame(x1, x2, y)

#Logistic Regression
fit.glm <- glm(y ~ x1 + x2, data = dat, family = "binomial")

y.hat.3 <- predict(fit.glm,dat)

plot(x1,x2,col = c("red","blue")[y.hat.3 + 1])

Upvotes: 0

Views: 136

Answers (1)

Marius
Marius

Reputation: 60060

predict returns log-odds for a logistic regression by default. To get predicted classes, use type = "resp" to get predicted probabilities and then use a decision rule like p > 0.5 to turn them into classes:

y.hat.3 <- predict(fit.glm,dat, type = "resp") > 0.5

plot(x1,x2,col = c("red","blue")[y.hat.3 + 1])

Upvotes: 4

Related Questions