This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-reloaded:regression_models [2024/12/02 16:31] – [Linear regression model] ktokarz | en:iot-reloaded:regression_models [2024/12/10 21:33] (current) – pczekalski | ||
---|---|---|---|
Line 11: | Line 11: | ||
Linear regression is an algorithm that computes the linear relationship between the dependent variable and one or more independent features by fitting a linear equation to observed data. In its essence, linear regression allows the building of a linear function – a model that approximates a set of numerical data in a way that minimises the squared error between the model prediction and the actual data. Data consists of at least one independent variable (usually denoted by x) and the function or dependent variable (usually denoted by y). | Linear regression is an algorithm that computes the linear relationship between the dependent variable and one or more independent features by fitting a linear equation to observed data. In its essence, linear regression allows the building of a linear function – a model that approximates a set of numerical data in a way that minimises the squared error between the model prediction and the actual data. Data consists of at least one independent variable (usually denoted by x) and the function or dependent variable (usually denoted by y). | ||
If there is just one independent variable, then it is known as Simple Linear Regression, while in the case of more than one independent variable, it is called Multiple Linear Regression. In the same way, in the case of a single dependent variable, it is called Univariate Linear Regression. In contrast, in the case of many dependent variables, it is known as Multivariate Linear Regression. | If there is just one independent variable, then it is known as Simple Linear Regression, while in the case of more than one independent variable, it is called Multiple Linear Regression. In the same way, in the case of a single dependent variable, it is called Univariate Linear Regression. In contrast, in the case of many dependent variables, it is known as Multivariate Linear Regression. | ||
- | For illustration purposes in the figure below, a simple data set is illustrated that was used by F. Galton while studying relationships between parents and their children' | + | For illustration purposes in the figure |
- | <figure Galton' | + | <figure Galton' |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 22: | Line 22: | ||
<figure Linear model 1> | <figure Linear model 1> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 32: | Line 32: | ||
* β0 and β1 y axis crossing and slope coefficients of the linear function correspondingly | * β0 and β1 y axis crossing and slope coefficients of the linear function correspondingly | ||
- | Unfortunately, | + | Unfortunately, |
It means that the following equation might describe the model: | It means that the following equation might describe the model: | ||
<figure Linear model 2> | <figure Linear model 2> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 46: | Line 46: | ||
<figure Model error> | <figure Model error> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 53: | Line 53: | ||
<figure Coefficient velues> | <figure Coefficient velues> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 62: | Line 62: | ||
* µx and µy – mean values of the vectors X and Y | * µx and µy – mean values of the vectors X and Y | ||
- | Most modern data processing packages possess dedicated functions for building linear regression models with few lines of code. The result is illustrated in the following | + | Most modern data processing packages possess dedicated functions for building linear regression models with few lines of code. The result is illustrated in the figure |
- | <figure Galton' | + | <figure Galton' |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 74: | Line 74: | ||
<figure Coefficient velues> | <figure Coefficient velues> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 83: | Line 83: | ||
* ei - error of the model' | * ei - error of the model' | ||
- | Since an error for a given yith might be positive or negative and the model itself minimises the overall error, one might expect that the error is normally | + | Since an error for a given yith might be positive or negative and the model itself minimises the overall error, one might expect that the error is typically |
- | <figure Galton' | + | <figure Galton' |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Unfortunately, | Unfortunately, | ||
- | Another important aspect is the order of magnitude of the errors compared to the measurements, | + | Another important aspect is the order of magnitude of the errors compared to the measurements, |
- | < | + | < |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
- | In this figure, both small error quantities and progression dynamics are illustrated. Another example of cyclic error distribution is provided in the following figure: | + | In figure |
- | < | + | < |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
From this discussion, a few essential notes have to be taken: | From this discussion, a few essential notes have to be taken: | ||
* Error distributions (around 0) should be treated as carefully as the models themselves; | * Error distributions (around 0) should be treated as carefully as the models themselves; | ||
- | * In most cases, error distribution is hard to notice even if the errors are illustrated; | + | * In most cases, error distribution is complex |
* It is essential to look into the distribution to ensure that there are no regularities. | * It is essential to look into the distribution to ensure that there are no regularities. | ||
If any regularities are noticed, whether a simple variance increase or cyclic nature, they point to something the model does not consider. It might point to a lack of data, i.e., other factors that influence the modelled process, but they are not part of the model, which is therefore exposed through the nature of the error distribution. It also might point to an oversimplified look at the problem, and more complex models should be considered. In any of the mentioned cases, a deeper analysis should be considered. | If any regularities are noticed, whether a simple variance increase or cyclic nature, they point to something the model does not consider. It might point to a lack of data, i.e., other factors that influence the modelled process, but they are not part of the model, which is therefore exposed through the nature of the error distribution. It also might point to an oversimplified look at the problem, and more complex models should be considered. In any of the mentioned cases, a deeper analysis should be considered. | ||
- | In a more general way the linear model might be described with the following equation: | + | In a more general way, the linear model might be described with the following equation: |
<figure Linear model> | <figure Linear model> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
- | Here, the error is considered to be normally distributed around 0, with its standard deviation sigma and variance sigma squared. Variance provides at least a numerical insight into the error distribution; | + | Here, the error is considered to be normally distributed around 0, with its standard deviation sigma and variance sigma squared. Variance provides at least a numerical insight into the error distribution; |
<figure Sigma> | <figure Sigma> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 127: | Line 127: | ||
<figure Variance> | <figure Variance> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 134: | Line 134: | ||
===== Multiple linear regression ===== | ===== Multiple linear regression ===== | ||
- | In many practical problems, the target variable Y might depend on more than one independent variable X, for instance, wine quality, which depends on its level of serenity, amount of sugars, acidity and other factors. In the case of applying a linear regression model, it seems much complicated, but it is still a linear model of the following form: | + | In many practical problems, the target variable Y might depend on more than one independent variable X, for instance, wine quality, which depends on its level of serenity, amount of sugars, acidity and other factors. In the case of applying a linear regression model that doesn' |
<figure Multiple linear model> | <figure Multiple linear model> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
Line 144: | Line 144: | ||
<figure Multiple linear model error estimate> | <figure Multiple linear model error estimate> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
- | Unfortunately, | + | Unfortunately, due to the number of factors (dimensions), the results of multiple linear regression cannot be visualised in the same way as those of a single linear regression. Therefore, numerical analysis and interpretation of the model should be done. In many situations, numerical analysis is complicated and requires a semantic interpretation of the data and model. To do it, visualisations reflecting the relation between the dependent variable and independent variables result in multiple graphs. Otherwise, the quality of the model is hardly assessable or even unassessable. |
===== Piecewise linear models ===== | ===== Piecewise linear models ===== | ||
Line 155: | Line 155: | ||
<figure Piecewise linear model> | <figure Piecewise linear model> | ||
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
As it might be noticed, the individual models are still linear and individually simple. However, the main difficulty is to set the threshold values b that splits the sample into pieces. | As it might be noticed, the individual models are still linear and individually simple. However, the main difficulty is to set the threshold values b that splits the sample into pieces. | ||
- | To illustrate the problem better, one might consider the following artificial data sample: | + | To illustrate the problem better, one might consider the following artificial data sample |
- | < | + | < |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
- | Intuition suggests splitting the sample into two pieces and, with the boundary b around 0, fitting a linear model for each of the pieces separately: | + | Intuition suggests splitting the sample into two pieces and, with the boundary b around 0, fitting a linear model for each of the pieces separately |
- | < | + | < |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
- | Since we do not know the exact best split, it might seem logical to play with different numbers of splits at different positions. For instance, a random number of splits might generate the following result: | + | Since we do not know the exact best split, it might seem logical to play with different numbers of splits at different positions. For instance, a random number of splits might generate the following result |
- | < | + | < |
- | {{ : | + | {{ : |
- | < | + | < |
</ | </ | ||
It is evident from the figure above that some of the individual linear models do not reflect the overall trends, i.e. the slope steepness and direction (positive or negative) seem to be incorrect. However, it is also apparent that those individual models might be better for the given limited sample split. This simple example brings a lot of confusion in selecting the number of splits and their boundaries. | It is evident from the figure above that some of the individual linear models do not reflect the overall trends, i.e. the slope steepness and direction (positive or negative) seem to be incorrect. However, it is also apparent that those individual models might be better for the given limited sample split. This simple example brings a lot of confusion in selecting the number of splits and their boundaries. | ||
Unfortunately, | Unfortunately, | ||
- | * Using contextual information, | + | * Using contextual information, |
* Some additional methods might be used to find the best split automatically. In this case, software packages usually have tools for this. For Python developers, a very handy package mlinsights ((https:// | * Some additional methods might be used to find the best split automatically. In this case, software packages usually have tools for this. For Python developers, a very handy package mlinsights ((https:// | ||