Hello and welcome to this art tutorial.
So now that we fitted our logistic regression to the training set,
we are going to predict the test set results using this classifier
that we just built in the previous tutorial.
So usually we can do this in one step.
But for logistic regression we're going to do it in two steps.
And we are more interested in having the zero and one predictions
rather than the probabilities.
So first let's take anyway the probabilities.
So we are going to call them prob
pred which is the vector of the,
which will be the vector of the predicted probabilities of our test set.
Observations by the classifier.
So prop read equals predict.
So we're going to use this predict function to predict the probabilities
based on our GLM classifier.
And speaking of classifier this is our first argument in the predict function.
So here we input classifier
come up.
Then the next argument is type.
And for logistic regression we should choose the response type
because that will give us the probabilities listed in a single vector.
So that's what we want.
And then we have to specify the new observations that we want to predict.
So in our case these new observations
are the test set observations.
So new data equals test set.
And we're just going to remove the last column
of the test set because the last column is the dependent variable.
And that's what we want to predict.
So we will only take test set minus three.
That is the two independent variables age and salary of the test set.
All right so let's just summarize this line.
We are predicting the test set observations
using our classifier which is the logistic regression classifier.
All right.
But that returns the probabilities because as you can see if I select this line
and execute here if I type prob
pred in the console I will get.
As you can see, I will get the probabilities of each of my test
set observations.
So here, for example, that's let's go to our test set.
The first observation has index two.
The real result is zero.
That means that the user number two didn't buy the SUV.
And now if we go back to logistic regression
we can see that for this user number two
prob pred returns 0.0 16.
So what is this probability exactly.
This probability is the probability that the dependent variable is equal to one.
That is the probability that the user buys the SUV.
So here, since it's very small, it's 0.0 16.
That means that the classifier predicts a very low
probability of the dependent variable being equal to one.
That means that it predicts that the user number two
has very low chance to buy the SUV.
Therefore, in short, prop pred returns
the predicted probabilities that the user will buy the SUV.
But we don't really want that.
We would rather have the zero and one result.
And to do this, it's very simple.
We're just going to do some kind of a conversion.
So we're going to create a vector of predicted results.
That's we're going to call Y pred
equals.
And then we're just going to use ifelse to transform
those probabilities into zero and one results.
So here I'm going to write if else.
So the first argument of if else is the condition.
And the condition is going to be from
pred over 0.5,
because if prob pred is larger than 0.5,
that means that the user has more chance to buy the SUV.
So in that case, that means that we want to predict one.
And actually the second argument of the if else function is the result
you want to take when this condition is true.
So here we're going to put a one.
All right.
And the last argument is the result we want to return
if this condition is false.
And if this condition is false
that means that the predicted probability is lower than 0.5.
That means that the user has less chance to buy the SUV,
and therefore it's going to be a zero.
And that's it.
Now you're going to see if I select this line and execute.
All right.
Let's look at wipe Red. Now
wipe red as you can see
I only have zeros or one.
I no longer have the probabilities.
And remember the real result for our user two was zero.
So that's what happened in reality.
And here our classifier predicted zero.
So that's a correct prediction.
And that's because remember before the probability was very low.
So it was below 0.5.
And therefore it returned zero.
All right.
So we predicted our test results.
That's a good thing.
Now we are going to evaluate this prediction.
Thanks to the confusion matrix that we going to build in the next tutorial.
So I look forward to seeing you there.
And until then enjoy machine learning.