logistic regression model r code example

Example 1: how to do logistic regression in r

glm(response ~ ., data = your_train_data, family = "binomial")

Example 2: logistic regression curve fit R

fit = glm(vs ~ hp, data=mtcars, family=binomial)
newdat <- data.frame(hp=seq(min(mtcars$hp), max(mtcars$hp),len=100))
newdat$vs = predict(fit, newdata=newdat, type="response")
plot(vs~hp, data=mtcars, col="red4")
lines(vs ~ hp, newdat, col="green4", lwd=2)

Example 3: how to code binary response in r

data$response <- ifelse(response == "yes", 1, 0)

Tags:

R Example