Welcome to BLOCK 5:
Correlation and Regression analysis.
This is the companion video for practice
in the contents of correlation and regression analysis.
We will see how to interpret bivariate analysis
and linear regression plots, basically,
and we will see how to obtain
a simple regression model using R software.
Okay.
So we have as learning objectives,
the introduction to data analysis, scatterplot and
correlation coefficient. Interpretation of the correlation,
in strength and direction, direct
or inverse, simple regression estimation.
So let's see, we have a first example.
Example 1. 22 students in a class were asked
to record the number of hours they spent studying
for a test on Monday
and also the number of hours that they spent watching TV.
Okay.
Or playing with a computer, it would be exactly the same.
So, we have for these 22 students, the study hours.
So the first coordinate is the number of hours,
and the second coordinate is the score they obtained in the test.
Now the same, but for the TV hours.
So, we have first the TV hours
and second, the score they got in the test.
Okay, let's construct scatterplots.
So, we have two scatterplots; the first takes into account
the score, always on the Y-axis, and the X-axis
contains explanatory variable study hours.
Okay.
In the second, on the right,
what we see is the relationship
between the TV hours and the test score.
So, clearly what we see is
a direct relation between the study hours
and the test score. And it is a linear relation, on the left,
it can be clearly seen. On the right,
what we see is an inverse relation,
Increasing TV hours means decreasing test score,
so the correlation coefficient would be positive
for the study hours and negative for TV hours.
Let's see now
the means and
here we have
the explanation, the positive, etc.
But we are going to see this example
and some similar examples,
in the next slides.
We have
a second example, a mathematical model
that it establishes or tries
to establish the relation between the estimated number
of alternative-fueled vehicles in the States
from 2001 to 2007.
Okay.
So, we have this kind of relation
and we have timeline t equal to 1, corresponding to 2001
and we can establish a linear
regression, in this case
some details are omitted. In this case,
we are going to use this model
in order to predict the values.
So we're going to predict the values for the current years.
So, there are the years that have been used
from 2001 to 2007, and what we see is on the table
that the right column that contains this prediction V*,
it's relatively close to
the value that is the observation, on the second column.
So, we see that the model is not bad.
Next examples.
We have three scatterplots and we have to discuss a little bit.
I will be discussing two of them.
So case A,
I have this scatterplot,
the scatterplot is basically a straight line.
I see a correlation that is positive, clearly, and very close to one,
because the points are aligned in a linear way.
So we have a straight line and the correlation
coefficient would be very large.
Okay,
I have this B case.
In this case, I have a pattern of relation between X
and Y that does not fit to a linear model.
So, in this case it makes no sense to use
a linear regression model to examine
and to determine the relationship between X and Y.
Because it is not linear, the relation.
Continuing with this,
we have some sort of variation.
We have stated the simple linear regression,
but, in fact, if we modify a little bit
the statement and we transform either
the target variable by using log
transformation, based on the natural logarithm, then
we obtain an alternative model
that it's an exponential model.
We can use some logarithm model
that it is also useful, by transforming the logarithm,
the natural logarithm, of the explanatory variable.
So, with this base, I have three examples
with this basic modification, it is useful for example,
for adapting
the estimation of a linear model to an scatterplot
of the type that I am showing you on the slide.
So, we have some sort of exponential function.
Of course, this is not a linear function, but we
we have to transform the original statement of the model
by using the logarithm transformations and then apply the estimation
of the linear coefficients and back again transforming to the original model scale.
And we fit the model.
Or in the case of the logarithm function
or this profile that is clearly logarithm.
A logarithmic profile, then it can be apply also.
And in the case C, it's a straight line.
Nothing has to be done, so directly work with X and Y.
Okay, so
Fitting a model to data, we have here.
Let's see a little bit.
Let's work on details about the exponential
fitting and the power fitting model.
Okay, so we have these scatterplots.
In this case, what we have done is, in the particular case of the data
that I showed in the previous slide, first of all, on the left I see a model
that it's exactly the model that is written on the slide.
And this works directly by
using the transformation,
the logarithm transformation to the target.
So just doing this,
we can fit a and b,
the coefficients for the intercept and the slope.
And then, we have to transform back to the original the scale of the target
that this not the logarithm scale,
it's the ordinary scale.
So, we have the exponential model.
On the right,
I have the same data and
my proposal is using a model that is a power model.
In this case, I'm using both.
I'm confirming both X and Y.
And once I have transformed
X and Y, then I estimate a and b
and then back again to the original scale.
And, I see that the model that I get is a model.
That is not an exponential, but it's a power model
and we have this two possibilities and we have to decide
which of the two models is the model
that better fits our data.
In this case, the solution is visual.
So, clearly the left chart is the best one.
So, the exponential model is the suitable model for the data.
And because it's better there, we have some lack of adjustment
on the right, but in the next slide we will see that
the possibility is to
to take a look at the coefficient of determination
and the model with the highest coefficient of determination
would be the model to be selected.
The best model.
And finally,
I have an example and this is an example
of how to produce a linear regression,
but they also include some sort of discussion
about the correlation coefficient
because this was one of the topics that we have been working
in the companion video
with more theoretic contents. So, in this case, I'm using
the dataset CPS 1985 that it's the one addressing
wage as the target variable, the response variable.
So, how much do you earn in dollars per hour, every hour?
And we have some as
I remember just 10 to 12
explanatory variables in this dataset, but
I have to focus on the numeric variables.
So, the numeric explanatory variables, because in linear regression,
a linear regression involves numeric variables
and the correlation,
Pearson's correlation coefficient
is defined for numeric variables,
not for numeric and
categorical or two category variables.
The correlation then is a measure of the linear relation between numeric variables.
This is what we have seen.
So let's address wage and we have as numeric variables
that contribute to the possibility of explaining wage.
The education,
so, years of education.
We'll have also the experience in the current job,
and also age. Okay, so
this is what I get after using the correlation,
Pearson's correlation, applied to the subset of variables that are numeric variables.
I obtain a matrix.
This is the correlation matrix, it is a symmetric matrix,
it's a symmetric matrix and contains ones in the diagonal
because the correlation between one variable
and itself is of course 1.
Okay.
And then if I pay attention to the first row of the correlation
matrix, what I see is a quick linear
relation positive with education
and extremely weak that is almost zero.
So, no relation to experience
because the
coefficient of correlation is 0.09
and also I see a positive correlation
but very weak to age.
Now, let me choose
the best explanatory variable as a candidate
variable, that clearly is education, because the correlation coefficient
is 0.38, it's weak
and this is supported by the chart.
The scatterplot that we have on the right,
clearly, this behavior is
quite normal
in the plot of sociological data.
It has large variability.
Okay.
So, we see that the points are on the X axis related to education,
years of education. And, on the Y axis is wage.
So, we see that the spread
of the values of wage around the
straight line
increases as education increases.
So, we have a variability
that does not seem to be the same
for all X values. So this is not good from the point of view
of the hypothesis of the linear regression model.
But anyway, what we see here is that the straight line
between education and wage can be obtained
by using the lm() method.
lm() method stands for linear model, and the first parameter
is a formula equation on the left hand side of the formula equation
we have wage and on the right hand side we have the explanatory
variable that is education.
If we had
some additional variables
we could use this procedure too.
This method applies for simple linear
regression, multiple linear regression, etc..
Okay.
The outcome of this method
is or it's sent to an object that is m1.
If I summarize the object, what I see it's that the intercept,
the estimate for the intercept is -0.75, roughly,
and the estimate for education is 0.75, too.
So, this would allow us to write the linear regression line
and we see also the residual standard error.
This is the estimate for the standard error
of this residual error term, in this case
4.75 and I have the multiple R-squared
at this 0.15.
So, the education explains
15% of wage variability.
We'll have a lot
of work to do to improve this model.
But this is not the case.
This is the example.
So here the references, and thank you very much for your attention,