A taxi ride down regression street
By: Santiago Rodrigues Manica
Tags: Stata, Data Science, Linear Regression, Epidemiology, Statistics

A taxi ride down regression street

From Pexels by rodolfoclix

A classic taximeter is a linear regression machine. Right after you sit on the backseat and close the door the taximeter starts doing its math.

Here, image a linear regression function, where the xx axis represents the distance in Km, or independent variable, and the yy axis represents price in EUR (€), our dependent variable.

In this formula,

y= β0 + β1(x)

y represents the price (€) and x distance (km). What about β0 and β1?

β0 is also called the intercept because it represents the value of y when it crosses the vertical axis. In other words, β0 is the value of y, when x=0. In this case the price at the beginning of the ride (distance=0 km).

β1 is the slope, it represents how much y changes when x increases 1 unit.

If β1>0 (positive value), there is a direct linear relationship bet ween variables. As x increases, y increases.

there is a ween variables. As x increases, y increases. If β1<0 (negative value), there is an inverse linear relationship between variables. As x increases, y decreases.

there is an between variables. As x increases, y decreases. If β1=0, there is no linear association between variables, just a flat line where y remains the same regardless of the value of x.

Right after you sit on the backseat and close the door, the taximeter won’t start at €0, but at €3.90. This means when x=0 (when km=0), y=3.90. In other words, β0=3.90. Then, for each extra 1 Km, you ride the travel will cost you more €0.54. This means the slope, or the β1, of our line, is equal to 0.54.

Now, given

y= β0 + β1(x)

if we are traveling 15km, then…

y= 3.90 + 0.56(x) where, if x=15 y= 3.90 + 0.56(15) = 12.30

The price (y) of a 15km (x) ride will be €12.30. Plus a tip if you wish…

adapted from our post https://www.instagram.com/p/CDw3CzVlBti/

Now replace x and y for whatever you need.

For example, assuming there is a linear regression between systolic blood pressure (dependent variable/yy axis) and age (independent variable/xx axis), we could do the same math.

What about Clinical Epidemiology?

However, in natural sciences, such as Medicine, we do not fix the starting value and the slope, it is not like running a ride business.

In this case, we use statistical software to “discover” the value of β1 in our data set.

How?

If you have a dataset with a single observation of 4,240 different patients and given that we know the age and systolic blood pressure we can try and answer the following question using linear regression;

Is there an association between age and systolic blood pressure?

We are extracting a publically available version of the Framingham heart study from https://www.kaggle.com/amanajmera1/framingham-heart-study-dataset/version/1

After downloading the file, we go to Stata and open the file via the following options (this is the easiest way)

File -> Import -> Text data (delimited, *.csv…)

browse age sysbp

Now, we can draw a scatter plot to eyeball the relationship between age and systolic blood pressure.

graph twoway scatter sysbp age

and now we are testing the association between these two continuous variables…

regress sysbp age

sysbp represents the dependent variable (systolic blood pressure) and age represents the independent variable

We see there are 4,240 observations (top right corner).

Our β0 (_cons) is 82. Even though it does not add to the clinical question we made.

Most importantly, the β1/slope/betta coefficient for systolic blood pressure (sysbp) is 1.01, meaning for each increase in 1 unit of age (1 year), systolic blood pressure increases 1.02 units (in the same direction). Notice the 95% confidence interval goes from 0.94 to 1.08. This means there is a 95% chance that the true value of β1 lies between 0.94 and 1.08. Since this value does NOT include 0 (β1=0 represents a flat horizontal line), we can say there is a statistically significant association (thus, our p-value <0.05).

A little trick for the beta coefficient is that, every time our 95% confidence interval does NOT include 0, p will be <0.05.

graph twoway (lfitci sysbp age) (scatter sysbp age) **(lfitci sysbp age) calls the linear fitted line

** (scatter sysbp age) calls the scatter plot

The green dots represent individual values of blood pressure and age, drawing a scatter plot. The blue line represents the linear line (from the linear regression shown above). The gray shadow is the confidence interval for the regression line.

Is there an association between age and systolic blood pressure?

Ans: Yes, there is a direct linear association between age and systolic blood pressure, which is statistically significant.

But why a confidence interval?

Contrary to the taxi ride, where the driver (or the regulator) sets the price for each km (our β1). Here we are trying to “discover” the slope of a naturally occurring association using our observations. Since science is not deterministic, we need to establish a margin of error, or type 1 error (probability of a false positive).

Per convention, a chance of 5% of type 1 error is acceptable. When we state there is a 95% probability (expressed as 95% confidence interval) of the real value for β1 being somewhere between two numbers, we are saying we are accepting a 5% chance the real value lays somewhere below the minimum or above the maximum value.

Some limitations — what a linear regression assumes

Any statistical software will give you what you ask for. In other words “Garbage in, garbage out”.

This meaning you must be careful when choosing a linear regression because it assumes:

Linearity ; The regression line is straight;

; The regression line is straight; Homoscedasticity ; There are equal variances across the xx axis.

; There are equal variances across the xx axis. Gaussian ; The error is normally distributed.

; The error is normally distributed. Independence: Observations are independent of each other

All the technical jargon will be explained step by step in future articles. But most importantly,

You should use linear regression, only for independent observations, when assessing a continuous outcome (eg; age, height, weight) which has a linear distribution.

Now it’s your turn to run the models…

For more info regarding linear regression building stay tuned (more posts to come).