DS 325 - Linear regression
In this notebook, we will perform some basic data analysis. Your homework assignment will be to perform the same steps with different datasets adding your work into this Jupyter notebook in the designated areas and answering questions about your work in a corresponding Moodle quiz.
First, we import necessary libraries into our Python environment. You will not need to re-import these for your exercises below (its already loaded into the kernel).
numpy is a scientific computing library which includes fast, efficient, multidimensional arrays (i.e. tables) for our data
pandas builds on numpy to give us efficient data structures (e.g. dataframes) and data analysis tools which are fundamental to data science
matplotlib is a 2D plotting library for scientific visualization
statsmodel provides a variety of regression, classification, and machine learning algorithms for building models of our data, many alternative sklearn, scipy, ...
These imports need only occur once before their use, and should not be copied and repeated below. To start the notebook fresh and rerun the cells of your notebook, "Restart and run all..." (using the button or Kernel menu item).
Data: Advertising
The advertising dataset contains the following data
200 samples of the budget for advertising for TV, Radio, and Newspaper
Dependent variable is Sales
Sales are in thousands of units and the budget is in thousands of dollars.
Our first step is to read data from a comma-separated values (CSV) file named "data.csv". We will load the CSV data into a pandas dataframe "df" like this:
For each exercise, I will provide two methods to import the data. One uses a url (download the data and store in RAM), or you may download the dataset and import from the working directory. I'll usually do the later as I can work on a project offline. Please uncomment the method you want to use and comment the other.
.describe() generates descriptive statistics that summarize the central tendency, dispersion and shape of a dataset’s distribution, excluding NaN values.
.info() prints a concise summary of a DataFrame
plot correlations and distribution for our dataset
Lets quantify the correlations between variables.
We see that TV and Sales have the highest correlation. However we also see that Radio and Newspaper have a correlations of 0.35 -- we'll come back to that on your Exercise.
Onto some simple analysis
Linear regression intro
Lets focus on the Sales (in thousands) as a function of the TV advertising budget (in thousands). From the above plots, this is a reasonably linear relationship that doesn't seem to depend much on the species.
Least square regression
So background, with a little matrix algebra
y is the predicted value
is the feature.
is the model parameter Here is the vector of unknown parameters
ParseError: KaTeX parse error: {align} can be used only in display mode.
Similarly, we group both the coefficients into a single vector (i.e., a 2 × 1 matrix)
ParseError: KaTeX parse error: {align} can be used only in display mode.
We’d also like to group the observations of the predictor variable together, but we need something which looks a little unusual at first:
ParseError: KaTeX parse error: {align} can be used only in display mode.
This is an n×2 matrix, where the first column is always 1, and the second column contains the actual observations of x. We have this apparently redundant first column because of what it does for us when we multiply x by :
ParseError: KaTeX parse error: {align} can be used only in display mode.
Finding the model parameters
The model is simple, but how do we find those parameters? First we must decide on the regression model. The most common is the Root Mean Square Error (RMSE). Our model will fit (or train) the model to find the which minimizes the RMSE. Below is the 'cost function' which we are trying to minimize.
Mean Squared Error
At each data point, using the coefficients β results in some error of prediction, so we have n prediction errors. These form a vector:
Here is the error, also known as the residuals.
The mean squared error is then
or
is the transpose of
in terms of and we have,
The normal equation
To find the which minimizes our cost function, MSE, there is an analytical solution.
is the value which minimizes the MSE
is the inverse of the matrix
is the transpose
That is, we’ve got one matrix equation which gives us both coefficient estimates.
Now, from the above, we can find and ,
the denominator of is the variance of x (where variance is the square of the standard deviation)
the numerator is the covariance of the product of x and y
Let's try this on our dataset.
From the normal equation above, we can also use our matrix algebra to do this.
a few alternative to simple regression
numpy.polyfit(x,y,deg) return coefficients that minimizes the MSE, scipy.polyfit() is the same algo
numpy.linalg.lstsq(a,b), simple or multi-variate regression; need to append a column of 1’s to the x data for intercept (A = [1 x])
Stats.linregress() - cannot do multivariate data, return R2, standard error
sklearn.linear_model.LinearRegression() use .fit(X,y)
Statsmodels.OLS(y,x) - extensive list of result statistics, need to append a column of 1’s to the x data for intercept
scipy.ptimize.curve_fit(f,x,y) - can fit any user-defined function to a data set by doing least-square minimization.
Linear regression with StatsModels
Interpreting the Regression Results
Adjusted. R-squared reflects the fit of the model. R-squared values range from 0 to 1, where a higher value generally indicates a better fit, assuming certain conditions are met.
Intercept is your Y-intercept. It means that if the X value is zero, then the expected output (i.e., the Y) would be equal to the const coefficient.
TV coef represents the change in the output Y due to a change of one unit in the X (everything else held constant)
std err reflects the level of accuracy of the coefficients. The lower it is, the higher is the level of accuracy
P >|t| is your p-value. A p-value of less than 0.05 is considered to be statistically significant
Confidence Interval [] represents the range in which our coefficients are likely to fall (with a likelihood of 95%)
Want individual attributes? You can access and print them independently like this:
Reporting your results, or more than you wanted to know about p-values
A p-value ≤ 0.05 is an arbitrary but commonly used criterion for determining whether an observed difference is "statistically significant" or not. While it does not take into account the possible effects of bias or confounding, a p-value of ≤ 0.05 suggests that there is a 5% probability or less that the observed differences were the result of sampling error (chance). While it does not indicate certainty, it suggests that the null hypothesis is probably not true, so we reject the null hypothesis and accept the alternative hypothesis if the p-value is less than or equal to 0.05.
The 0.05 criterion indicates the probability of incorrectly rejecting the null hypothesis.
A p-value > 0.05 would be interpreted by many as "not statistically significant," meaning that there was not sufficiently strong evidence to reject the null hypothesis. Here we fail to reject the null rather than "accept the null" as there is insufficient evidence that they are different.
There is an unfortunate tendency for p-values to devolve into a conclusion of "significant" or "not significant" based on the p-value.
P-values do not imply causation.
P-values do not indicate whether the null or alternative hypothesis is really true.
P-values do not indicate the strength or direction of an effect, i.e., the "magnitude of effect."
Statistical significance does not take into account the evaluation of bias and confounding. Also consider the sample size.
what to report?
Many researchers and practitioners now prefer confidence intervals, because they focus on the estimated effect size and how precise the estimate is rather than "Is there an effect?"
Also note that the meaning of "significant" depends on the audience. To scientists it mean "statistically significant," i.e., that p ≤ 0.05, but to a lay audience significant means "important."
Measure of effect: the magnitude of the difference between the groups, e.g., difference in means, risk ratio, risk difference, odds ratio, etc.
P-value: The probability of observing differences this great or greater if the null hypothesis is true.
Confidence interval: a measure of the precision of the measure of effect. The confidence interval estimates the range of values compatible with the evidence.
Now let's try making some predictions using this model.
TV = 100
Digging a little deeper, examining the residulas
We can also plot the residuals against the fitted values. One thing we look for here is that the residuals are uniformly scattered with no (or little) x-dependence.
We can also look for points with high leverage, that is, which data points may be outliers.
A data point is influential if it unduly influences any part of a regression analysis, such as the predicted responses, the estimated slope coefficients, or the hypothesis test results. Outliers and high leverage data points have the potential to be influential, but we generally have to investigate further to determine whether or not they are actually influential.
Interpreting our results
We see that Newspaper advertising has a small coefficient (0.0383) with a relatively large std err (0.017). The resulting p_value for the slope is 0.025, which is less than 0.05. However, the confidence interval for the slope is quite large.