LINEAR_REGRESSION
Description
The Linear Regression function models the relationship between input variables by fitting a linear equation to the input data.
The method consists of two steps. First, a model is trained. Second, values are predicted by the model. Linear Regression is integrated into the existing table framework. Currently, model training and prediction can not be separated.
Syntax
LINEAR_REGRESSION ( TRAIN_[FILTERED_]LM ( INPUT ( table.input_column, ... ), OUTPUT ( table.output_column ) ), PREDICT ( table.predict_column, ... ) )
TRAIN_LM: Trains a Linear Regression model.
TRAIN_FILTERED_LM: The data on which the linear model is trained is filtered.
INPUT: One or more columns, which is used to train the model so that it describes the Output.
OUTPUT: One column, through which the model lays a predictor function.
PREDICT: One or more columns, on which for each row a predicted value is returned by applying the trained model.
All columns in TRAIN_LM have to be joinable. The columns in PREDICT do not have to be joinable with the columns in TRAIN_LM.
The input of the model training is regarded as an independent sub query. This means if an aggregation is used, it is independent of the dimensions defined in the rest of the query. This also means that the columns within TRAIN_LM have to be joinable, but not with the columns used in the rest of the query.
To ensure the correctness of the result, it is important that the matrix that is constructed from the input columns and an additional column of ones (representing the bias) contains at least as many linearly independent rows as there are variables in the linear model. The number of variables is equal to the number of columns (including the bias column). If this condition is not met, the function aborts with an error message.
NULL handling
If a row contains a NULL value, the value is ignored and does not affect the model.
If a
PREDICT
row contains a NULL value, the result for that row will be NULL.
Filter behavior
Standard Linear Regression
If rows of a column are filtered, it does not affect the linear model, as long as the linear model is not trained on aggregation results. This means independent of filters and selections, the underlying model stays the same. If you want to restrict the input data of a model you can use a CASE WHEN statement and map the values you want to be ignored to null. If a model is trained on results of an aggregation it still changes with the filtering because the result of the aggregation is affected by the filtering.
Filtered Linear Regression
If a filter or selection changes, the model is retrained and the resulting function adopts to the new of view of data. This has a serious performance impact.
Examples
[1] Linear regression on time series data. | ||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
|
[2] Linear regression on the same data as above, but using a constant for prediction. | ||||||||||||||||||
| ||||||||||||||||||
|
[3] Example for a higher order linear regression. | ||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||
|
[4] Example for filter behavior on standard linear regression. Despite the filter the full data is used to train the model. | ||||||||||||||||||||||||
| ||||||||||||||||||||||||
|
[5] Same example as above but this time the model is only trained on the filtered data. | ||||||||||||||||||||||||
| ||||||||||||||||||||||||
|
[6] An example with too many linearly dependent rows. | ||||||||||||||
| ||||||||||||||
|