Least squares linear regression is probably the most well-known type of regression, but there are many other variants which can minimize the problems associated with it.
A common one is known as ridge regression. This method is very similar to least squares regression but modifies the error function slightly.
Previously, we used the sum of square errors of the regression line as a measure of error, but in ridge regression we seek to minimize the squared values of coefficients as well. This gives the error function Here, the value of lambda changes how aggressively coefficients are dampened. Notice that this error function does not penalize the size of the -intercept.
A close relative to ridge regression is simply known as “lasso.” This also penalizes the size of coefficients in the error function, but does so based on their linear size instead of their squared size. Therefore, error is given by
Below, we use a coding environment to import a LASSO class from the sklearn library and compared its results with those of normal linear regression. Press run to see the the results of each model plotted.
from sklearn import linear_model import matplotlib.pyplot as plt import numpy as np #Generate artificial data for regression lines. x = np.random.normal(0, 10, (20, 1)) y = 4*x+10+np.random.normal(0, 5, (20, 1)) regr = linear_model.LinearRegression() LASSO = linear_model.Lasso(alpha=50) #50 is a large value of alpha, and is chosen for demonstration purposes. plot_x = np.transpose(np.array([list(range(-20, 20))])) regr.fit(x, y) LASSO.fit(x, y) plt.plot(plot_x, regr.predict(plot_x)) plt.plot(plot_x, LASSO.predict(plot_x)) plt.legend(["Linear Regression", "LASSO"]) plt.plot(x, y, "ro") plt.axis([-15, 15, -70, 70]) plt.savefig("Plots.png", format="png")
It is not at all obvious why lasso would have behavior significantly differing from ridge regression, but there is an interesting geometric reason for the differences. However, to demonstrate this, we must first change the way we view both techniques.
In ridge regression, it turns out that for any values of we pick, it’s possible to find a value for such that minimizing is equivalent to minimizing the SSE when
(This can be shown using the method of Lagrange multipliers)
Similarly, for any value of there is some value of such that using lasso is equivalent to minimizing the SSE when A useful way to view the SSE when there are two predictor variables is shown in the pictures below. Here, the - and -axes represent the values of coefficients in a best-fit plane, and the ellipses show all pairs of coefficients which produce a certain value of the SSE for a data set. As the SSE increases, the ellipses get larger.
Also in the pictures are two areas. The diamond represents the coefficient values allowed by lasso. The disk represents the possible coefficient values in ridge regression. Viewing these pictures, which form of linear regression will most likely lead to coefficients of zero?
Lasso generally behaves very similarly to ridge regression but, as we saw in the previous question, there is one crucial difference. It is capable of reducing the weights of predictor variables to zero. This is useful if one wants to cull predictor variables, among other things. Usually, this is done when there are many predictor variables and using too many in the model will cause overfitting or make it overcomplicated.
A group of scientists wants to analyze bacterial growth in Petri dishes. They have done a dozen tests, and each time they have recorded every single detail of the environment. The pH levels of the dishes, sugar content of the food, and even the light levels in the room have been recorded. A total of fourteen variables have been taken into account.
In a classic example of overzealous testing, a rogue scientist has added another variable to the mix, his average mood on a scale from zero to ten. When a best-fit equation is generated with this variable included, how will the SSE most likely change? How will the average error on new data change?
The previous question is a good example of a case where ridge regression or lasso would be very useful. Each of these techniques will penalize a best-fit line for having large coefficients, so they are likely to produce equations that make minimal use of predictor variables that have little sway over the result. Because the equation has a limited “budget,” it can only afford to give large weights to variables which are important.
In the case shown previously, this means that something with as little predictive power as the scientist’s mood the day of the test will be largely ignored. In fact, if lasso is used, the variable will probably be ignored entirely.
Of course, linear regression is just one of many techniques. A non-linear method with comparable simplicity is known as K-nearest neighbors regression.
To use K-nearest neighbors regression, or KNN regression for short, we must start with a data set. As with linear regression, the dataset must take of form of pairs of predictor variables with resultant variables . The goal is to use this dataset to predict the value of a resultant variable from a vector of predictor variables .
To make a prediction for , we plot each in our dataset, ignoring the resultants, and pick out the points geometrically closest to . The estimate KNN regression provides is simply the average of the resultant values for these points.
One useful property of KNN regression is that it makes very few assumptions about the data sets it builds on. Unlike linear regression, which assumes linear relationships, KNN regression can accommodate nearly anything.
Additionally, by adjusting the value of we can change the flexibility of KNN regression. If we want to account for even the smallest trends in our data set, we can pick a very small -value. On the other hand, larger values of will eliminate smaller deviations in favor of larger trends.
Let's try applying KNN regression to a simple example. In the image below, we've plotted a dataset of ten points, where the predictor variable is given by the x-axis and the resultant variable is given by the y-axis. In this case, for all points in the dataset.
Now, suppose that we have a new point for which and we want to predict its value with KNN Regression. If we use as our value for K, what will our estimate be?
Hint: In KNN regression we pick out the K points geometrically closest to and average their resultant values.
Below, we have three data sets—A, B, C—represented by either tables or scatter plots. We can analyze one with K-nearest neighbor regression, one with lasso, and one with normal linear regression. Which pairings will give the best results?
Data Set A:
Data Set B:
Data Set C: