Machine-Learning-using-R - old 2022/C3/Logistic-Regression-using-R/English
Title of the script: Logistic Regression using R
Author: Tanmay Srinath
Keywords: R, RStudio, machine learning, supervised, unsupervised, classification, logistic regression, dataset, video tutorial.
Visual Cue | Narration |
Show Slide
Opening Slide |
Welcome to this spoken tutorial on Logistic Regression using R. |
Show Slide
Learning Objectives |
In this tutorial, we will learn about:
|
Show Slide
System Specifications |
This tutorial is recorded using,
|
Show Slide
Prerequisites |
To follow this tutorial, the learner should know:
If not, please access the relevant tutorials on this website. |
Only Narration | Let us learn what logistic regression is? |
Show Slide
What is Logistic Regression? |
|
Only Narration | Let us now learn a few practical applications of logistic regression. |
Show Side
Applications of Logistic Regression |
|
Show Slide
Logistic Regression |
Now we will implement logistic regression on the iris dataset.
Let us see how we can do it in RStudio. |
Show Slide
Download Files |
We will use a script file LogisticRegression.R
Please download this file from the Code files link of this tutorial. Make a copy and then use it for practising. |
[Computer screen]
Highlight: LogisticRegression.R Logistic Regression folder. |
I have downloaded and moved this file to the Logistic Regression folder.
This folder located in the MLProject folder on my Desktop.
|
Cursor in the Logistic Regression folder. | Let us switch to RStudio. |
Click LogisticRegression.R in RStudio
Point to LogisticRegression.R in RStudio. |
Open the script LogisticRegression.R in RStudio.
|
Highlight:
library(stats4) library(splines) library(VGAM) data(iris) |
I have already installed the required libraries, so I will directly import them.
|
Highlight
library(stats4) library(splines)
library(VGAM) |
stats4 and splines packages are needed to load the VGAM package.
|
Click in the Environment tab to load the iris dataset. | Click in the Environment tab to load the iris dataset. |
Cursor in RStudio. | Now we will scale our input variables. |
[RStudio]
iris[1:4] <- scale(iris[1:4]) Click on Save and Run buttons. |
Type this command.
This will scale our input variables.
|
Cursor in RStudio. | Now let’s split our data into training and testing parts. |
[RStudio]
set.seed(222) trn_ind=sample(1:nrow(iris), size=0.8*nrow(iris),replace=FALSE) train <- iris[trn_ind, ] test <- iris[-c(trn_ind),]
|
Type the following commands. |
Highlight
set.seed(222)
trn_ind=sample(1:nrow(iris), size=0.8*nrow(iris),replace=FALSE)
train <- iris[trn_ind, ]
test <- iris[-c(trn_ind),] |
This command sets a seed for reproducible results.
This command creates the train dataframe.
|
Highlight
set.seed(222)
trn_ind=sample(1:nrow(iris), size=0.8*nrow(iris),replace=FALSE)
train <- iris[trn_ind, ]
<- iris[-c(trn_ind),] Select the commands. Click on the Run button.
|
Select the commands and run them.
|
Click the test set and train set to load them in the Source window. | Click the test set and train set to load them in the Source window. |
Cursor in RStudio. | Now we will train our model. |
[RStudio]
model=vglm( Species ~ Petal.Length + Petal.Width, family=multinomial, train) |
Type the following command.
These warnings don’t affect the output and can be ignored. |
Highlight:
vglm()
Species ~ Petal.Length + Petal.Width
family=multinomial
|
This is the function that creates a logistic regression model.
We try to predict species based on petal length and petal width features.
|
Click on Save and Run buttons. | Save and run the command.
Here we can see the warings. |
Cursor in the Source window. | Now let us predict the output classes from test data. |
[RStudio]
prob <- predict(model, test[,1:4], type="response") pred <- apply(prob, 1, which.max) |
Type and run these commands.
|
Highlight
prob <- predict(model, test[,1:4], type="response") Highlight pred <- apply(prob, 1, which.max) |
This predicts the probability of the given data belonging to a certain class.
It is done by selecting the class with the highest probability using which.max. |
Cursor in the Source window. | Now let us measure the accuracy of our model.
|
[RStudio]
table(pred,test$Species)
|
Type and run this command.
The output is shown in the console window.
This proves that the model is accurate and robust. |
Show Slide
Drawbacks of Logistic Regression |
Let us now understand the drawbacks of logistic regression.
|
Only Narration. | With this we come to the end of this tutorial.
Let us summarise. |
Show Slide
Summary |
In this tutorial we have learnt about:
|
Show Slide
Assignment |
Now we will suggest an assignment for this Spoken Tutorial.
|
Show slide
About the Spoken Tutorial Project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Show slide
Spoken Tutorial Workshops |
We conduct workshops using Spoken Tutorials and give certificates.
|
Show Slide
Spoken Tutorial Forum to answer questions |
Please post your timed queries in this forum. |
Show Slide
Forum to answer questions |
Do you have any general/technical questions?
Please visit the forum given in the link. |
Show Slide
Textbook Companion |
The FOSSEE team coordinates the coding of solved examples of popular books and case study projects.
We give certificates to those who do this. For more details, please visit these sites. |
Show Slide
Acknowledgment |
The Spoken Tutorial and FOSSEE projects are funded by the Ministry of Education Govt of India. |
Show Slide
Thank You |
This tutorial is contributed by Tanmay Srinath and Madhuri Ganapathi from IIT Bombay.
Thank you for watching. |