Hey guys welcome back to the data science course. And in today's session
we'll learn and implement multiple linear regression. So let's get started
so when multiple linear regression we have more than one independent variable
and we try to determine how do all of this independent variables together
affect the dependent variable. Like over here we have a mapping between Y x1 x2
and x3 where Y is the dependent variable and x1 x2 and x3 are the independent
variables. So let's take this example to have a better understanding of multiple
linear regression. So over here we are trying to understand what factors affect
the salary of an employee. Here salary is the dependent variable and gender age
and Department are the independent variables so the linear regression model
helps us to determine the salary of an employee when specific values are given
to each gender and department. So let's go to R studio and implement multiple
linear regression right? we have R studio right in front of us and this is
our same old custom merchant data set. right? so now since we already know that
before building a linear regression model we need to divide our dataset into
training and testing sets and to do that we would require the ca tool package
so I'll type library of CA tools so we have loaded the CA package. So now
to build a linear regression model I will take the tenure column as the
dependent variable and I'll try to understand how do other columns affect
the tenure of a customer right? so I'll split this dataset with respect to the
tenure column so I will use the sample dot split function now let me select
this column customer churn dollar tenure right? and the split ratio which
I'll be giving is 0.65 and I will store this in an object called us split model
okay so basically 65% of the observations forget true values and the
rest 35 observation would get the false value. we've store this result in split model I
will divide the data set using the subset function right? Now this takes in
the first parameter as the data set, now from this data set wherever the value of
split model is equal to true, I will store all of those observations in the
training set right? similarly from the entire customer churn data set wherever
the value of split model is false, I will select all of those observations and I
will store those observations and a data set called as test and thus we have a
training and testing sets ready. Now let me just have a look at the number of
rows and training and testing. So n row of train Is 4,000 573 and And n rows of test
is 2470 right? so we have our training and testing sets ready now it's finally
time to build a linear regression model. So I'll use the LM function so the
dependent variable is tenure and the independent variables are monthly
charges
and we have gender following which we have internet service
then the final independent variable is call fact
so we are trying to understand how this tenure vary with respect to monthly
charges, gender, internet service and contract and the data would be trained
i.e. on top of the train set we are building this linear regression model
and I will store this in an object called as mod one right? so we have built
out multiple linear regression model. Now it's time to predict the values on top
of this model and to do that I will use the predict function right? predict. So
the first parameter which it would take as the model which you just built so mod
1 next is the testing set on which we want to check the accuracy of the model
okay so I have given both of the parameters
and I'll score the result and let's say result 1
now I will bind the actual values and the predicted values into a common data setr
I'll use the C bind function now let me take the original values so the
original values would be the tenure column from the test set and the
predicted values would be the value stored in result one object. So I
will name this column as actual and I will name this column as predicted
right and I will score this in a new object with the name of final data 1
right? so these are the actual values of the
tenure and these are the predicted values of the tenure which you are able
to predict with the help of the predict function right? so thus we have predicted
the values with the help of the linear regression model. Now we'll go ahead and
find out the accuracy. Again to find out the accuracy first we need to find out
all of the residuals then we need to find out the root mean square error so
let's go ahead and find out the error in this right? so now before that since this
is actually a matrix I need to convert this into a data frame I will use as.
data frame function and I will convert this into a data frame
I will store the result back to the same set final data 1 right so now this is
actually a data frame right so now I will subtract the predicted values from
the actual values to get the error in prediction so final data $1 actual minus
final data 1 dollar predicted and I will store this
in an object called as error one okay? so let me have a glance at this view of
error one so this is the error in prediction guys
now I'll also bind this error back to the same data set
again see buying of final data one I will bind it with the error column and I
will store it back to the final data one object
view of final data one now. let's see what do we get
so these are the actual values of the tenure, these are the predicted values of
the tenure and this is the error in prediction all right? so now we can find
the root mean square error, so let's do that
final deta $1 error 1 so first I'll take the square of all of the errors
right? Now after this I need to take the mean. So let me type in mean over here
following which I need to find out the square root
so I'll type sqrt so it's root mean square error okay. So the are RMSE value
which we get for the model which you build is 16 so I will store this in rmse1
right? so we built a model and we've performed an accuracy check and we
found out that RMSE value with respect to this model is 16 now what
we'll do is we'll build another multiple regression model with different set of
independent variables and we'll try to determine which of the models is more
accurate. okay? so this time the independent variables which I will be
taking would be partner phone service, total charges and payment method. okay?
so again let me use the LM function and again tenure would be the dependent
variable and the independent variables are partner then we have one service
or language we have portal charges then the final column would be payment
method
right so we are trying to understand how to stand your vary with respect to
partner phone service total charges and payment method and I will store this in
mod - okay right before that we would also have to give the data set so data
would be trained so we have built the second model now
we'll also do the prediction so credit i'll given the first parameter which is
the model which you just build then this prediction needs to be on the test set
and I will store all of the values in result
okay at this time I'll bind the actual values from the test set which is test
dollar tenure what is the tenure column compress it
and the predicted values which are stored in result two again I will name
this column as actual
the second column I'll limit as predicted
right so no I will store this in an object called as final date or to let me
have a glance at this view of final date dot who so guys these are the actual
values of the customer's tenure and these are the predicted values of the
customers tenure okay so again we will go ahead and find out the error then
we'll find our MSE value okay so before that we need to convert this
to a dataframe so a store data frame of final data - and I will store it back to
final data -
also done so it's finally time to get our errors final data $2.00 actual -
final data $2 protected and I will sue this in error - now I'll
bind this error back the same data set so see bind of final data - and I will
bind the error back to the same data set okay and I will store it back to the
same Brissette which will be final - so guys these are the actual values of the
customers tenure these are the predicted values of the customers tenure and this
column gives us the error introduction so again we will go ahead and find out
the RMS e value so final day dot do
dollar better do
then I will find the square of this
after which we'll get the mean
then after finding the mean we'd have to take the square root rescue Artie and
I've restored this in rmse too but just so that I can create the
suspense right so first I will show you guys the value of our MSC 1 which is 16
now there is something interesting over here now our MSC 2 we get any now we've
got this any because there are some na values already present in the total
charges column okay so I'm retreating guys so since there are already some any
values present in the total charge just call him that is why the our MSC value
we've got as any so to remove that we need to use na dot R M equals true so
let's do that I'll type any dot R M equals true so this time the RM AC value
which we get as twelve point eight zero okay so our MSE one a 16 and our MSC 2's
twelve point eight so this basically means that the second model which we
built is much much better than the first model again because it's a simple
explanation over here our MSE to is lower than our MSE one and that is why
the second model is much better than the first model so if we ever want to select
four columns to determine the tenure of the customer then it'll be better to
select partner phone service total charges and payment method okay so this
was a multiple linear regression guys and this brings us to the end of the
session thanks for attending and let's meet in the next class