Lecture 22 : Regression Algorithm: Application (Part 2)

IIT Kanpur July 201
20 followers
Sep 6, 2024
[Music]
in this video we'll examine a very
important issue that econometric issue
that affx estimation which is heteros
sidity which is the non-constant
variance so as an intuition let me plot
the fitted Model S R and I'll plot the
residuals with this plot command notice
the behavior of residuals with the
fitted values notice the variance as we
go ahead with values fitted values
notice the variance is not constant
sometimes it is high sometimes it is low
and the the way residuals are scattered
around the fal line their variance seems
to be not constant which indicates that
model may be afflicted by the issue of
hetas City to check it more formly
there's ncv test if you want to know
more about it you can type
ncv
test and all the details will be
provided to you however now for the non-
constant variance error variance we'll
simply run the command so we'll type ncv
test and this is part of car package CR
package that we installed earlier we
need to supply it with the model object
that we created earlier so model and
first we'll try with the
studenti without studentized errors and
let's see
so let's see the output on the
console so I need to provide the fitted
object actually so instead of model I
need to provide the fitted
object notice the P value is very
significant which indicates and this and
simple ncv test fit is fits the values
along the fitted values
and it seems that when fitted along the
fitted values the error variance is not
constant it's varying very significantly
which is indicated by this very high
value of Ki square and a very high
significance level of P
value so if you look
at the P value it's quite High uh we can
also see if these residuals they are
varying with the Nifty Valu so we will
specify the formula this time around we
do not want to see with fitted values
but we want to see with Nifty by default
it runs with fitted values this time
we'll model it with the Nifty which is
the Nifty returns and along Nifty
returns also we can we have a similar
evidence we can
see we can see that there is a very
significant variance residual variance
that is not
constant another very interesting test
of non-constant variance
is BP test or BL span test if you want
to know more about this you can simply
typee question mark BP test and it all
the details are there so this BP test is
a very interesting test of hosas city
it's it simply checks whether error
variance is changing so let us run the
model BP
test and we'll Supply the model object
here we are supplying the model object
and we will decide whether we want this
error terms to be studentized so without
studentized again a very significant P
value very similar to one one we have
earlier which indicates a very high
level of heterostatic if we studenti
let's see if we studenti the T values
error values this time we are not able
to reject the null of homo staticity so
when we are working with homo with
studentized errors probably it seems
that the model is not Afflicted that
much with het tocity however when we are
running the this model this test DP test
Bruce ban test with
without studentized errors then it gives
us hety so probably the issue is there
the issue of hosas is slightly moderate
in the sense that without student T
values it is not reflecting our
estimation so just to summarize we tried
to visually examine the issue of
heterotic there appears to be it appears
to be the case that residual variance is
not constant however when we conduct
this using BP test with studentized
errors then our model seems to be not
much afflicted by the issue of hosas
City so there is issue of hosas but it
seems it's slightly moderate in this
video we'll discuss the issue of
autocorrelation often Financial market
returns are Seely correlated and that
leads to issue of autocorrelation in
error terms a very simple test is deran
Watson test which is you can see the
more detail about dson test with this DW
test question mark command as we have
been doing earlier you can read more
about
let's implement this DW test
command derbin Watson
test so while implementing DW test
command we need to supply it with the
fitted object which is SLR recall the
object to SLR if I run this command
notice that we get a true
autocorrelation P value as very high P
value which means it is not significant
and DW statistic is close to two so we
are not able to reject the null based on
this P value that means the as per this
test we have zero
autocorrelation now please note if DW
value statistic is close to two then
there's no correlation if it is close to
zero then there's a positive correlation
if it is much more than two then there's
a negative Auto correlation here it
seems it's very close to two which is
also indicated by P value that means we
are not able to reject the null that
there's no autool however there's
another very interesting test which is
Bruce gotf test which for which you can
see more details here with the BG test
so you can run the VG test and question
mark and see all the details here with
Bruce cfit test let's see the issue of
Auto correlation and how it afflicts our
model so we need to Simply type BG test
command and Supply the model by default
it will consider Only One lag of error
so with One lag notice there's no
autocorrelation as per P value the the
BG test is statistic which is
essentially the LM test and notice the P
value is not significant that means we
are not able to
theal however please remember many times
the correlation is not because of the
first lag it can be also because of lags
with the later periods maybe second or
third lag to account for that one can
simply put the model with higher lag lag
terms for example I can specify the
order as four and see if the error term
we have so we are able to catch some
serial correlation with four LS that
means there is some autocorrelation or
serial correlation with higher lack
terms to resolve this issue one simple
solution with the model is to add lacks
of returns in such model when there is
serial correlation a very in financial
markets a very simple and easily
available solution is to add the lacks
of returns in this case maybe lacks of
market returns or lacks of ABC returns
and see whether then issue of Serial
correlation is
resolved to summarize in this video we
conducted Durban Watson test and BG test
of autocorrelation
it seems with DW test and first lag Bru
godre BG test we are not able to find
any serial correlation but when we
examine the higher order lags with BG
test we find some evidence of Serial
Auto
correlation in Practical situations to
account for such serial Auto correlation
one can add lag of returns such as uh
ABC stock returns or maybe Market Nifty
returns lags to see if the serial Auto
correlation is issue is resolved in this
video we'll discuss that application of
robust standard
errors recall recall our previous
discussion on het dust and auto
correlation these issues afflict
standard errors and standard errors go
into the Model T values with coefficient
divided by standard errors which result
in t values under or
over so under or overvalued standard can
affect the estimation by resulting in
lower or higher T values one solution
that has been developed is to create
robust standard errors that account for
issues such as hro sastic and auto
correlation now with r with r there are
various flavors of these robust standard
errors we'll discuss the four most
prominent ones often employed in
computation of standard error the
routines that are often employed and
please note it will not affect the
coefficient estimate but it will provide
us with the robust standard erors that
will go into the computation of T values
that are arguably robust against issues
such as heteros doity and
autocorrelation so one way to compute is
to use
hccm so we'll simply use our fitted
object SLR and then we'll Supply with
the variance cence Matrix which is
hccm if you want to know what hccm does
you can type question mark hccm and it
provides you with the heteros corrected
Co matrices so if you run this you will
find the coefficients coefficient will
remain same but the standard errors will
be changed and they will correct the T
values for hetro corrected covariances
similarly we can also correct for hetro
acity and autocorrelation correction
through vcov h a this is for hosi and
autocorrelation these are part of
Sandwich Matrix so you will generate
another coefficient matric with its T
values and P values you can also correct
simply for hetro doity depending upon
the nature of whichever issue is
afflicting the model more you can choose
that so vcov
HC this will correct for hetro dustry
only and then there's new white
correction so lastly we have newest
whites correction we simply type New
West White correction and if you run
this you'll find the new West cence
metrix estimation all the discussion is
here but for implementation we'll simply
we'll use the same notation only that
instead of vcov HC we'll Supply with the
New West New West
correction and there we can obtain the
corrected standard errors and the
resulting T values so with
this in this manner we can apply the
robust standard errors there are various
flavors available we can check more
about them with this CF test command
coof
test we can check more about them
different variants different flavors how
to apply all the all the flavors are
available we can read more about this
the four most prominent we have
discussed hccm hetas
corrected Varian hetas and auto
correlation corrected new W correction
and so on so we can apply them to
observe if there are significant changes
in t values from our original values it
seems is still applying all the robust
standard our coefficient is still very
very
significant to summarize in this video
we discussed the application of robust
standard errors in correcting for issues
such as hos Dy and autocorrelation we
discussed four very prominent flavors of
Varan Co metrices that is hccm vcov HC
vcov HC and newest routines for
correcting the standard errors in the
model now that we have trained our
simple linear regression algorithm and
tested it for various econometric issues
we'll try to do some prediction using
the test
data so we'll do some prediction using
the test
data as a first step we'll create a
prediction object let's call it predict
p and we'll assign the predicted
values predicted values we'll use our
fitted object SLR and we'll Supply the
new data which is test data and assign
the values to our predict
object as a first step let us visualize
this along with the actual test data so
we'll plot
along with the date we'll plot
our actual returns that are there in the
test data so this is test
data ABC returns we are will be plotting
them
so so on XX we have
date
and on Y axis we'll have ABC
Returns the or rather let us call
returns we'll use Central object
as predicted returns
versus actual
returns now we would like to restrict
our graph for
y
limit we would like to visualize from
minus 0.2 to plus 0.2 which is a
reasonable limit to visualize daily data
you would not expect returns to vary too
much from 20%
more than
20%
Then PCH 20 will ensure that our objects
are plotted as solid dots we want them
to plot in red
colors also let us specify the type as
line L Type
L so there will be a solid line in fact
now along with a BC returns we would
also like to
have our predicted object plotted along
the date so again on the x-axis we have
date and on five AIS we would like our
predict object line width of
one let us specify the line width of one
here also
although it's not necessarily needed but
just for information you can change it
to two or any other number if you want
it more or different the WID to be
higher or
something so first let us plot this let
us plot using this command let's see if
we are getting the object right or we
are getting some error so so this object
has worked so this is the currently in
red we have the
actual returns in the test data we'll
add some lines these
lines we add some lines with this
command
so we we'll run this lines command and
these green ones are the predicted ones
this is our predict P object predict
object now to make it more aesthetically
clear I'll also add the legend here so
Legend let's put the legend on the top
right I see a lot of empty space so I'll
put the legend on the top
right and notice you can see that uh in
our window uh minus 0.2 to plus 0.2
sufficient amount of Gap is there so if
I would have put it minus one to plus
one or some higher number it would have
looked not so uh aesthetically good so
right now it is decent the windows
decent in fact I can put it as minus.
one also that would be okay as well so
if I run it for minus1 to 0.1 that also
looks better
now I'll add the legend on the top right
part of
it
so we have actual
return and we
have predicted
return and we like to fill the provide
the fill so here the actual return is in
red as we noted earlier
and pred Ed one is in green so I'll add
the
green here and then we'll run our
plot and we have the complete diagram
here let's examine the full diagram
notice uh it seems that where the green
one is reasonably capturing not in terms
of amplitude but in terms of fluctuation
where the fluctuations are hair in the
red ones fluctuations are also hair in
the green ones so there's reasonable
similarity in the movement however we
need more statistical evidence to
conclude that and for that we will use
our correlation command we'll use the
core command to test whether our
predicted returns and actual ABC returns
so this is actual ABC return and this is
our PR object let's examine the
correlation and the correlation is
reasonably high so this is 0.43 that's
almost 43.7% so we are able to capture
the Dynamics of the ABC returns to the
extent of
43.7% using our regression simple linear
regression
algorithm so to summarize this video we
created a predict object then we plotted
this predicted object along with our
actual returns contained in the test
data that we
created the Dynamics appear to be
reasonably similar that means we are
able to capture a lot of dynamics of
actual return in our predicted object we
statistically examined this with our
correlation measure we check the
correlation between actual return and
predicted returns there's almost
43.8% correlation between these two
entities so it seems that our prediction
algorithm has done a reasonably good job
in Daily data context this is a very
high level of prediction efficiency in
this video we'll discuss the computation
and examination of out of sample fit or
accuracy of our prediction algorithm
please note that you need some kind of
cost or error function cost or error
function to compare the model across to
compare across various competing models
or
algorithms now there are various cost
functions and error functions that can
be
employed starting from very simple
models such as msse or rmsc errs to more
complex
functions now let us examine how to
execute this INR and for that we need Li
Library matrics which Pro provides us
with a number of such error functions to
compute for all we'll start let's start
with this
msse MSE
computes the average square difference
between two numeric vectors so if you
want to compute MSC with Matrix Library
you can simply type
MSC
test dollar ABC so this is our actual
return object and along with the
predicted object and you can compute
compute them measure now on its own on
Standalone basis it does not have any
meaning you need some other competing
model and you would like to compare this
msse that is the average square
difference between the vectors to
compare another variant of this error
could be
rmsc RMC computes the root mean squared
error between two vectors like we have
here two returns ABC return and actual
ABC returns in the test data and its
predicted values also if you want to
read more or know more about these
measures you have to Simply type
question mark RMC and you'll get most of
the information here you can check on
the internet also but most of the
information relevant here can be
obtained from the help window now to
implement that rmsc in R you simply need
to type rmsc and then test dollar
ABC and then you have predicted object
you can compute this measure and then
you can com compare this meure measure
with other competing algorithms or
models if you have you may have various
other competing models maybe multiple
linear equation or some other variables
you want to test and then you can put it
against this value of RMC then another
important measure you have ra ra
computes the relative absolute error
between two numeric
vectors so again to compute the ra you
have to just type
ra and please remember these are cost or
error functions that help you understand
the out of sample fit why we are saying
out of sample because we are using it on
test data not on the data where we train
the algorithm so again we'll type test
dollar ABC which is our actual data
along with the predicted object and we
can compute this measure of error as
well on their own stand loone basis they
do not make much sense you need to have
a competing model or competing algorithm
to compare with then you have ma error
also ma computes the average absolute
difference between two numeric vectors
again the computation
method is same the implementation is
same we use test dollar ABC along with
the predicted object and you get the
number here in the output on the console
window similarly you have map another
very important and famous measure map of
error which is map computes the average
absolute percent difference between two
numeric
vectors if you want to compute the map
you can simply type map test dollar
ABC and you have have predict object you
can thus you can compute map
also then there are number of measures
for example you have SM
map SM map is basically it computes the
symmetric mean absolute percentage error
between two vectors so you can compute s
map for test dollar ABC
data with predict data you get that
number
in a similar manner there are other
measures we can compute them for example
you have
msle msle computes the average of
squared log error between two numeric
vectors the computation method Remains
the Same then you have
rmsle rmsle computes the root means
squared log error between two
vectors similarly you have bias measure
bias measure computes the average amount
that is actual greater than predicted
you can compute in bias in the
percentage form also you have RSC
measure RSC measure computes relative
squared error between two vectors for
example you can simply type RSC test
dollar ABC along with predict object
same the syntax Remains the Same and you
can get the RSC object you can also have
RRS which is the root relative s between
the two vectors and so on you can there
are many measures these were the most
important ones you can pick a number of
measures and then compare the M this
Matrix along with other Matrix you can
create more complex objects for example
you can give different bits to these
different errors and then create a new
object as per your requirement or some
more complex expression you can create
out of all these error objects and
create a more complex error object to
compare against competing model so there
are different flavors to it available
with you you can use them to check the
out of sample accuracy of your model to
summarize this video we discussed a
number of error or cost
measures to examine the out of
sample forecasting or prediction
accuracy of our trained
algorithm we also saw their
implementation in R and we noted that we
can create more complex forms of error
or cost functions by assigning weits or
some complex Expressions to these rade
readly avail
error or cost functions in this video
we'll start with multiple linear
regression
model most of the syntax to implement
and execute the model will remain same
so let's call it mlr multiple linear
regression model and as we did with the
simple linear regression model we'll use
LM object ABC returns and let us add
sensex returns B SX sentiment measure
since this is a multiple linear
regression model we'll be using a number
of variables on the dependent side
Nifty dividend announce
dividend and we'll use our train
data set to train the
model so now that we have put the
expression we have provided the
expression let us run it our object mlr
object saves the entire output so let us
print the output summary object
mlr let's call it
model again we we are using the same
naming convention let's see this
output let us examine the output of this
model notice the coefficient of sensex
and Nifty both are Broad market indices
while Sinex is heavily significant at
three star that is significant even at
1% level Nifty is turning out to be
negative now this is slightly surprising
for us because we already saw that Nifty
on with simple linear regression model
that Nifty also has a positive impact
now this and here Nifty is not
significant this suggest that there is
some possibility of issue of
multicolinearity as we'll examine in the
next video because sensex and Nifty both
are Market wide variable so this
provides us intuition of
multicolinearity issue that because of
that the coefficient and significance
estimation of nift is getting affected
we'll examine that later also notice
that sentiment variable is turning out
to be a very significant very very
significant even at 1% level that means
99% confidence dividend announce is also
significant it is turning out to be at
95% sign uh confidence or 5%
significance level as we can see here
significance quotes So dividend
announced is also very significant
notice the adjusted R square the power
of model is very high as compared to the
around 10% that we saw with the simple
linear regression model this is for the
fact that there are number of
significant variables that have been
added so the overall power of model to
explain the variation in return returns
is increased considerably to around
27.84
per. however we still need to observe
the issue of multicolinearity here to
resolve
that to summarize in this video we
trained our multiple linear integration
algorithm using the train data we
summarized the output and briefly
reviewed the
output in the next video we'll jump to
the m resolving multicolinearity issue
as we discussed in this video we'll skip
we'll skip residual diagnostic issue of
hrotic autocorrelation and robust
standard add because they are exactly
identical to what we have already seen
in the simple linear regression model
same commands with same notations will
be employed and the examination will be
done in an identical manner so in the
interest of time and brevity we'll skip
that part we can directly apply the same
quotes here same exactly identical codes
can be applied for the multiple linear
regression model to obtain and examine
the residual Diagnostic heteros dastic
autocorrelation and robust standard
errors related
discussion for more detail we can
directly look to the video topics on
heteros stasic autocorrelation residual
Diagnostics and robust standard errors
to video topics to get more information
on this in continuation with our last
video we noted that the issue of
multicolinearity May afflect our
estimation process and therefore in this
video we'll examine the issue of
multicolinearity
in more detail please recall we said
that there are there's a possibility
that independent some of the independent
variables may be correlated and
therefore that may give rise to Theo of
multicolinearity so as a preliminary
evidence or examination we'll try and
compute the correlation in the train
data
set for the independent variables are
independent variables include
ABC
returns sensex and we'll see which of
these variables are heavily
correlated these are all independent
variables that we employed in the
multiple lition
model these are the main variables
we are not we are leaving the dividend n
variable which is a dummy variable so
chances of this variable being
correlated is are very low this is
dividend nouns is just ones and
zeros so notice the correlation metrix
that we have produced here in console
and notice the very high correlation so
between Nifty and sensex the correlation
is almost 80% which is a clear
indication that there is a possibility
of multicolinearity because of this High
correlation between sensex and Nifty all
the other variables are though
correlated but the levels are even lower
50% much lower than 50% so they may not
have much multicolinearity related
effect so
let's also recall that we created our
model object mlr in which we trained
algorithm so we'll use this what is
called VI variance inflation Factor what
we
call variance
inflation Factor VI which measures the
extent of multicolinearity a model so
we'll use
vif and mlr if this number and it
indicates variance inflation factor for
different variables in the model if the
number is around one then multicity is
almost negligible if it is higher than
two then it is moderate if it is higher
than five then it is really really high
notice the multicolinearity with sensex
and Nifty variable and both of them are
heavily correlated is 2.9 which is on
the higher side which indicates that we
have to drop one variable
to remove the multicolinearity issue in
the
model now that we have seen our model
contains the show of multicolinearity
because of the variable Market variable
SX and Nifty we would like to remove one
of these variables to improve our
estimation and in the interest of that
we'll again run train r model on the
train data but this time we'll remove
one of the market variables and run our
regression
ABC on sensex so we are removing the
Nifty so we are only running on sensex
sentiment plus
dividend announced and we'll again use
our train
data let us run this model and let's
summarize this into a model object so
we'll create a model object in which
we'll put our summary of the model we'll
print this model object and again the
adjusted R square numbers are same but
notice the
sensex has much higher significance as
compared to
earlier the beta which is the
coefficient of sensex is around
0.55 sentiment variable is also very
significant and dividend nouns is very
significant to interpret the sensex
variable for example 1% increase in
Market or sensex leads to 0.55 %
increase in the ABC
returns also notice the dividend
announced variable to inter since it is
a dummy
variable on average there's a difference
in return When the dividend is announced
there's increase in
Return of about
0.38% on that dividend announcement
date and the model is able to explain
around 27.8 5% of the variation in the
ABC returns so we have improved our
model and accounted for the issue of
multicolinearity to summarize this video
we noted that when we computed the
correlation Matrix we found that the two
Market variables that is Nifty and
sensex were heavily correlated with a
correlation number of almost 80% and
indicated the presence of chances of
multicolinearity which we further
examined with our Varian variance
inflation factor which is
vif and vif suggested that indeed sensex
and Nifty variables are afflicted by the
show of multicolinearity so we removed
the Nifty variable and then again
trained our model and the model output
is reviewed in the
discussion in the in the next video
we'll create a predict object and we'll
compare the predicted and actual test
values and try to visualize them to see
are out of sample forecasting efficiency
in this video we'll create a prediction
object using our test data with our
trained algorithm multiple linear
regression
algorithm so
we'll create a prediction
object with test
data the procedure is very similar to
what we did with simple linear
regression model we'll create a predict
object we'll assign the values that
are predicted using mlr this is mlr
object that we have trained already
trained object and in the new
data we will assign the test data set so
we are test assigning the prediction
object creating a prediction object with
a test
data now that we have created this
object we'll try and compute the
correlation
between this predicted object and our
actual a
returns
so we'll use this predict
object and we'll compute the correlation
between predict and test and there's a
57% correlation let us also try and
visualize make a comparison using
visualization so we'll first plot
our actual returns we have already seen
these commands with the simple linear
regression model but still we'll run
them again so test dollar
date test dollar ABC so with this xlab
again the same syntax
date vlab as returns we'll be plotting
actual and predicted returns so the
central heading again here
is
actual versus
predicted returns
again we would like to see the Y Lim
here so we would like to set the Y
limits
from minus 20% Which is a reasonable
limit to plus
0.20 we would like to plot red color
line as actual ABC returns in the test
data and type as
line so this is our line
type we'll run this later with the
complete
thing next we would like to
have lines command with this with the
help of lines command we'll add
our predicted object so let's call it PR
predicted object that we have just
created and color we'll give it
green again the type will assign a type
of line so we we are using line type
other option is to plot in terms of
points for this kind of graph line type
is more preferable to make a better
comparison again we'll add Legend So
like we did last time we'll use Legend
on the top right
side on the top right side we have
Legend and we
have actual
returns first then we
have predicted
return we'll assign n colors so Phill
equal
to we have red as actual
and green as predicted as we have noted
so now let's do some plotting here so
I'll enlarge the plotting window for
better
visualization first I'll run the main
plot command so these are my actual
returns in red then I'll add the lines
through Lin command I'll add the
predicted return returns and then I'll
add the legend now let's Zoom this graph
and notice very nicely our object is
capturing as we also saw in the
correlation there was a very high
correlation and the same is reflected
here our predicted object in green is
very nicely capturing the fluctuations
in red actual returns ABC returns so
that means our multiple linear
regression algorithm has done a very
nice job of predicting the returns and
that is that was also reflected in our
very heavy correlation of 57.7% between
actual and predicted returns to
summarize in this video we created a
predict object using our test data in
this predicted object we computed the
correlation with the actual test data
the correlation is 57.7% which was very
high we also plotted and visualized the
actual return and superimpose them with
the predicted return returns which again
confirmed that our prediction has been
the algorithm has been very efficient in
predicting the
objects now here we are not doing
the out of sample for casting errors
that we did for simple linear regression
model we can go and review the video on
out of sample regression forecasting
video
Topic in that video topic on out of
sample error forecasting we have
discussed in great detail the r
implementation and the interpretation of
error functions so for in the interest
of time and bravy we'll not repeat there
the command execution and discussion
remains identical so we can refer to
that video topic on out of sample
forecasting to summarize this lesson we
model ABC stock prices using simple
regression problem with market index
variable the model is trained using
train data set and various goodness of
it measures are examined the fitted
model is examined visually as well the
model is tested using test data set and
various measures of out of sample fit
are also examined next a multiple linear
regression model is trained using train
data set on multiple variables the
fitted model is visually examined and
also various goodness of fit measures
are examined as well the model is
evaluated on various issues related to
multicolinearity heterostatic and
autocorrelation lastly the model
examined on various parameters for its
out of sample fit performance
[Music]
[Music]
[Music]
