Photo by Stephan Henning on Unsplash

# Introduction

When I started learning about linear regression, my first thought was that this kind of technique is too simple to be commonly used. I thought the purpose of that linear regression lesson was educational more than practical.

It appears I was completely wrong! Not only is it used frequently, it is also a fundamental concept a lot of techniques are built on (as detailed on this website).

As with any tools/techniques, there are cirumstances where using linear regression is a good idea and others where it simply will not work. But how to know when to use linear regressions? Is there anything to be aware of before considering using it?

But let us talk about a few ideas to apprehend before answering those questions

# Key concepts

## Dependent Variable

It is a variable which has a value that depends on the value of other variables. That dependence is established by the hypothesis tested in the experiment. It can be called $Y$ (as in $Y = A+BX$), or “target variable”.

## Independent Variable

On the other hand, an independent variable does not depend on any other variable (in the scope of the experiment). It is the $X$ in $Y = A+BX$, or “predictor variable”.

## Covariance

It is a measurement that assesses the relation between two variables.

$$\sigma_{XY} = \dfrac{1}{n}\displaystyle\sum_{i=1}^{n}(x_i -\mu_x)(y_i — \mu_y)$$

• $\sigma_{XY}$ = Covariance between $X$ and $Y$
• $x_i$ = $i^{th}$ element of $X$
• $y_i$ = $i^{th}$ element of $Y$
• $n$ = number of data points ($X$ and $Y$ must have the same number of data points)
• $\mu_x$ = mean of the independent variable $X$
• $\mu_y$ = mean of the dependent variable $Y$

When $\sigma_{XY}$

• is positive, the variables are positively related.
• is negative, they are negatively related.
• is null, there is no linear relationship between those variables.

Unfortunately, $\sigma$ is not standardised and ranges from -$\infty$ to +$\infty$. Therefore $\sigma_{XY}$ > $\sigma_{AB}$ does not mean the relationship between XY is stronger than the relation between AB. To compare variables, we need another measurement:

## Correlation

By standardizing covariance by a measure of variability in the data, we get a metric which is consistent throughout the dataset, and which can be intuitively interpreted. For example, uisng Pearson correlation coefficient:

$$r = \frac{\sum_{i=1}^{n}(x_i -\mu_x)(y_i — \mu_y)} {\sqrt{\sum_{i=1}^{n}(x_i — \mu_x)² \sum_{i=1}^{n}(y_i-\mu_y)²}}$$

• $r$ = Pearson Correlation Coefficient
• $x_i$ = $i^{th}$ element of $X$
• $y_i$ = $i^{th}$ element of $Y$
• $n$ = number of data points ($X$ and $Y$ must have the same number of data points)
• $\mu_x$ = mean of the independent variable $X$
• $\mu_y$ = mean of the dependent variable $Y$

$r$ varies between -1 and 1.

## Coefficient of Determination

$R²$, the coefficient of determination is a measure that assesses the goodness of fit of a regression model.

$$\large R² = 1 — \dfrac{\sum_i(y_i — \hat y_i)²}{\sum_i(y_i — \overline y_i)²}$$

• $\sum_i(y_i — \hat y_i)²$ is the residual sum of squared errors. It is the squared difference between $y$ and $\hat y$. The model does not explain this part of the error.
• $\sum_i(y_i — \overline y_i)²$ is the total sum of squared error. It is the squared difference between $y$ and $\overline y$.

$R²$ can be seen as “1 — the proportion of variance that can not be explained by the model”. A perfect fit would therefore have a $R²$ of 1.

# Assumptions

A linear regression is a parametric technique; it uses parameters induced by the data. Therefore, the data must fulfil several criteria. If those assumptions are violated, the prediction might be biased, or unreliable.

• The variables have a linear relationship. A scatter plot of the data will quickly tell you if this is the case.
• Residuals are normally distributed. A histogram or a Q-Q plot of the residuals will be able to tell you more about this.
• Homoscedasticity of data. The residuals have a constant variance. A scatter plot of the residuals will reveal if this assumption holds.

# Final notes

Although it might be the perfect tool for the job, and all the assumptions are met, extrapolating the regression equation to values out of the range of data analysed might be a bad idea. Predictions will be more reliable in the scope of the linear regression.

Correlation does not imply causation.

Trust your intuition! Some correlations are spurious, or due to chance. Look at this website to see a few of these improbable correlations.

And, sometimes, there is a hidden factor. For example, this “study” links chocolate consumption with Nobel Prizes. One may argue wealth implies better education/research hence higher Nobel prices, and wealth implies luxury goods consumption like chocolate.

Finally, keep the Anscombe’s Quartet in mind when analysing data. You might be comfortable enough to crunch the numbers and understand the data from the resulting tables, but do yourself a favour and plot the data, it might go a long way!

--

--
{"ft_lang_label":"__label__en","ft_lang_prob":0.8970798,"math_prob":0.9991108,"size":4986,"snap":"2022-40-2023-06","text_gpt3_token_len":1247,"char_repetition_ratio":0.12063428,"word_repetition_ratio":0.12682927,"special_character_ratio":0.25491375,"punctuation_ratio":0.08830022,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999163,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-05T13:12:36Z\",\"WARC-Record-ID\":\"<urn:uuid:21fdf573-b3ee-4343-9266-13a81162ccaf>\",\"Content-Length\":\"150587\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f8bf8d3c-b831-46f0-bf37-85703c4a10ae>\",\"WARC-Concurrent-To\":\"<urn:uuid:abb46ca4-3eaa-46ba-901e-b167a5298931>\",\"WARC-IP-Address\":\"162.159.153.4\",\"WARC-Target-URI\":\"https://aghilissen.medium.com/linear-regressions-d61200960882\",\"WARC-Payload-Digest\":\"sha1:FFOYNBTJ5LQKMFXVEK2YTOEA5EIEGQZM\",\"WARC-Block-Digest\":\"sha1:BIA6FJPCAM534GZ43AGA6PZJMA6GK5V3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337625.5_warc_CC-MAIN-20221005105356-20221005135356-00735.warc.gz\"}"}