A simple linear regression model attempts
to explain the relationship between two variables using
the best fitting straight line.
Basically, this technique finds out a linear relationship
between the two variables.
The variable that you want to predict
is called the dependent variable, or the response
variable.
The variable that you are using to predict the other variable's
value is called the independent variable, or the predictor
variable.
In reality, you can have one independent variable x that
affects the dependent variable y,
or you can have cases where there
are many independent variables that affect y.
Linear regression models describe the relationship
between a response variable as a function of one or more
predictor variables.
Linear regression is mostly used for finding out
the relationship between variables
and also for forecasting.
In this video, we will be talking
about how to fit a linear regression model.
A typical workflow involves the following.
You start off by importing data, fit a regression,
test its quality, modify it to improve the quality,
and finally, share it.
Let's now switch to Matlab and open out a new live script.
We will start off by importing the data
that we are going to use.
We will use an Excel file hospital.xls.
Let's read the file as a table using the read table command.
The hospital file contains information like patient names,
age, sex, weight, blood pressure,
and dates of treatment in an experimental protocol.
We will now index into the first few rows of data
to see the variables.
When we look at this table, the sex and the smoke field
have two choices each--
male and female, and smoke 0, indicating no,
and 1, indicating yes.
Let's go ahead and change these fields to categorical.
Step two is to create a fitted model.
Our goal is to model the systolic pressure
as a function of a patient's age, weight, sex,
and smoking status.
Let's create a linear formula for systolic pressure
as a function of age, weight, sex, and smoke.
We will then use the fit lm function
to fit a linear regression model.
And then let's also go ahead and plot it.
The model display includes the model formula,
estimated coefficients, and model summary statistic.
We use the p value to determine if a feature of the regression
model is significant.
In this case, the smoke yes feature
has a P value much less than 0.05,
indicating that it is statistically significant,
while the other three factors are not
as helpful in the prediction.
Make sure you check out the documentation linked below
to get more information on these terms.
Now for step three.
We are going to plot the residuals to see
if there are outliers in the data that should
be excluded from the fit.
From this plot, histogram of residuals,
there is one possible outlier with a value greater than 12.
Here is how you can find and remove the outlier.
You will now see that observation 84
is no longer in the model.
Now that we have located and removed the outliers,
the next step is to simplify the model, one
with fewer predictors but the same predictive accuracy.
To do this, let's use the step function.
The step function improves the linear regression model
by adding or removing terms.
Specify n steps as 10 to allow at most 10 steps
of stepwise regression.
Although the maximum allowed number of steps is 10,
step terminates the process after two steps.
We can now predict responses to new data.
For example, if we have four new people--
aged 25, 30, 40, and 65--
the first and the third smoke, we
can predict their systolic pressure using MDL 1.
And finally, we can share the model.
So that others can use this model for prediction.
We can also access the terms in the linear model, view model
formula, and access coefficients of the terms.
Now, if you think this programmatic workflow seems
a bit tedious, you can interactively
train, validate, and tune different models
using the regression learner app.
We can open the tool by clicking on the Apps tab
and selecting Regression Learner,
or by typing Regression Learner and then running the section--
start a new session, select the input,
specify a response variable and variables to use as predictors,
and click on Start Session.
On the regression Learner tab in the model section,
select the algorithm to be trained.
Or you can also choose all.
Let's go ahead and choose linear regression,
and then click on Train.
The RMSE validation score is outlined in a box.
We can view the regression model results
by using the response plot and use
the predicted versus actual plot to check the model performance.
If you want to learn more about the linear regression
workflow, Regression Learner app and the examples
that we used in this video, feel free to go through the links
in the description.