Logistic regression with R codes

Train the model using the training sets and check score

logistic <- glm(formula = Survived ~ Pclass + Sex + Embarked + Age, data = train,  family ="binomial")

summary(logistic)

# Let us make prediction using logistic model

my_log_prediction <- predict(logistic, test, type = "response")

# Suppose you wish to calculate probability

submit <- data.frame(PassengerId = test$PassengerId, Survived = my_log_prediction)

write.csv(submit, file = "logistic_prob.csv", row.names = FALSE)

# Suppose you wish to calculate classifcation

predicted.classes <- ifelse(predicted > 0.5, "1", "0")
table(predicted.classes)

# If you want import the data in csv format
submit <- data.frame(PassengerId = test$PassengerId, Survived = my_log_prediction, Classification = predicted.classes)
write.csv(submit, file = "logistic_class_prob.csv", row.names = FALSE)

Leave a Reply

Your email address will not be published. Required fields are marked *