Explain with a case study of logistic regression in R.

693    Asked by NatashaKadam in Data Science , Asked on Nov 30, 2019
Answered by Natasha Kadam

Same as Python, we will be following similar steps in R also while creating a model.

We will import the dataset initially

# Importing the dataset

dataset = read.csv('Social_Network_Ads.csv')

dataset = dataset[3:5]

For encoding target features as a factor with levels 0,1 we need to do this

# Encoding the target feature as factor

dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))

Now we will split the dataset

# Splitting the dataset into the Training set and Test set

# install.packages('caTools')



split = sample.split(dataset$Purchased, SplitRatio = 0.75)

training_set = subset(dataset, split == TRUE)

test_set = subset(dataset, split == FALSE)

Now we will fit the model

# Fitting Logistic Regression to the Training set

classifier = glm(formula = Purchased ~ .,family = binomial,data = training_set)

We will predict the model and evaluate

# Predicting the Test set results

prob_pred = predict(classifier, type = 'response', newdata = test_set[-3])

y_pred = ifelse(prob_pred > 0.5, 1, 0)

# Making the Confusion Matrix

cm = table(test_set[, 3], y_pred > 0.5)

This is how implementing in R is done.

Your Answer


Parent Categories