0% found this document useful (0 votes)
26 views

Title: To Implement Logistic Regression in R

This document describes using logistic regression in R to predict the type of engine in a car based on weight and displacement. It shows how to import car data, build a logistic regression model using glm(), evaluate the model performance, and make predictions on new test data. Key steps include developing the model on a 80% training sample, predicting on the remaining 20% test sample, and reviewing diagnostic measures to evaluate the model.

Uploaded by

Swapnil More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Title: To Implement Logistic Regression in R

This document describes using logistic regression in R to predict the type of engine in a car based on weight and displacement. It shows how to import car data, build a logistic regression model using glm(), evaluate the model performance, and make predictions on new test data. Key steps include developing the model on a 80% training sample, predicting on the remaining 20% test sample, and reviewing diagnostic measures to evaluate the model.

Uploaded by

Swapnil More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Title : To Implement Logistic Regression in R

Example Problem
For this analysis, we will use the mt cars dataset that comes with R by default. mtcars is
a standard built-in dataset. Here we need to predict the type of engine if weight and
displacement is given

Import the data


library(caTools)
library("ggplot2")
write.csv(mtcars, file = "mtcars.csv")
myData <- read.csv("mtcars.csv", header = T)

use glm() function


myModel <- glm(am ~ wt+disp, data = myData, family="binomial")
summary(myModel)

ggplot(myData, aes(x=wt+disp, y=am)) + geom_point() + stat_smooth(method="glm",


method.args=list(family="binomial"), se=FALSE)
Do Logistic Regression Diagnostics
newData = data.frame(disp=120, wt=2.8)
predict(myModel, newData, type="response")

Predicting Logistic Models


a. Create the training (development) and test (validation) data samples
from original data.
split <- sample.split(myData, SplitRatio = 0.8)
split
train <- subset(myData, split=="TRUE")
test <- subset(myData, split=="FALSE")

b. Develop the model on the training data and use it to predict the type of
engine on test data.
myModel <- glm(am ~ wt+disp, data = train, family="binomial")
summary(myModel)

C. Review diagnostic measures.


res <- predict(myModel, test, type="response")
res

res <- predict(myModel, train, type="response")


res

You might also like