Piecewise Linear Regression Model. What Is It and When Can We Use It? (2024)

Piecewise Linear Regression Model. What Is It and When Can We Use It? (1)

The Problem

A few years ago I was working with a problem that at first seemed simple and not “fancy” enough compared to other ML models, but with time I learned to appreciate and get great insights out of it. As the title says, we’ll be talking about piecewise linear regression, but before diving deep into it let’s take a look at the problem I faced.

Piecewise Linear Regression Model. What Is It and When Can We Use It? (4)

The graph above shows the variation of temperature with height (dT/dz). We can see that the variation of the temperature isn't a nice straight line. This creates some implications when analyzing this data. For instance, if we try to calculate dT/dz, it’ll give us a number that doesn't represent accurately the change in the different layers (or pieces) of the atmosphere.

Piecewise Linear Regression Model. What Is It and When Can We Use It? (5)

In fact, we can visually divide it into 3 different layers (or pieces). Above we can see the representation of these layers and its approximate height (x-axis). The break points are the points where the data completely changes its behaviour. For instance, break point 1 (let’s denote it b1) is our start point; the layer between b1 and b2 has a similar dT/dz. However, by the time it gets to b2, dT/dz changes its behaviour to a more smooth change not increasing exponentially like before. What we are trying to understand is how dT/dz changes inside of each these layers and where are the break points.

Piecewise Linear Regression Model. What Is It and When Can We Use It? (6)

If we plot our data and try to fit a regression on it we fail miserably. However, it doesn't mean that a linear regression isn't the best choice for our problem, we just need to adapt it a little bit.

Piecewise Linear Regression Model. What Is It and When Can We Use It? (7)

When I was brainstorming the problem, one of the question I asked myself was: why don’t I try to fit a polynomial regression? It’s simple and we can find implementations all over the internet. Above I plotted a 3rd order polynomial regression fitting the data. The problem of polynomial regression is that you lose the interpretability of the model when adding the polynomial terms (quadratic, cubic, etc). So if you don't care so much about interpretability, you can stop reading here. However, if you need interpretability to deeply understand the problem, piecewise linear regression is your buddy.

Piecewise Linear Regression: Solution of Our Problems

The idea behind piecewise linear regression is that if the data follows different linear trends over different regions of the data, as shown before, then we should model the regression function in “pieces”. Below we have the system of equations that construct our problem:

Piecewise Linear Regression Model. What Is It and When Can We Use It? (8)

where b1 is the x location of the first break point, b2 is the x location of the second break point, and so forth until the last break point bnb for nb number of break points.

Although it's fairly simple to solve linear regressions, one question still remains: how to find the break points? For me, that's where I got stuck. You can try to solve it by brute force using a bunch of for loops, but this won't be optimal. Thankfully, there’s a relatively simple and straightforward library called PWLF created by Charles Jekel (many kudos to him!).

With a few lines of code, you can create a piecewise linear model as shown above. In my case, I knew I needed 3 regressions fit my data, so I just passed it and the break points were found by the algorithm. The way PWLF finds it is by differential evolution algorithm, which tries to find the global minimum of a given function; in our case, the sum of the square of the residuals. For more info on differential evolution, I recommend checking this article.

Piecewise Linear Regression Model. What Is It and When Can We Use It? (9)

Now with the break points known, we can fit our data. The most amazing thing about it is that you can still analyze each segment as a normal linear regression, calculate the same statistics as a linear regression, etc. We can see the PWLF result in the graph above.

The Wrap Up

Piecewise linear regression takes the best aspects of linear regression and solves complex problems that we wouldn’t be able to solve with a simple linear regression. The most awesome part of this simple algorithm is that it allows you easily understand your data by solving multiple linear regressions, so if you have data that doesn’t fit a single line, piecewise linear regression can help you. Furthermore, we saw that the PWLF library is a fairly simple way to implement piecewise linear regression and has an easy and understandable approach. Having it in your hands can help you to get many insights into your complex data.

This is it for now. I hope you enjoyed the article and if you want to talk more about it, you’re welcome to connect with me on LinkedIn.

Piecewise Linear Regression Model. What Is It and When Can We Use It? (2024)

References

Top Articles
Yolanda Busio on LinkedIn: Watch Spar Lady Trending Video | Spar Lady Video https://lnkd.in/eVe8RmHA…
Exclusive: Uncovering The Truth Behind The Mikayla Campinos Leaked Video
Nullreferenceexception 7 Days To Die
417-990-0201
Mcfarland Usa 123Movies
Cumberland Maryland Craigslist
BULLETIN OF ANIMAL HEALTH AND PRODUCTION IN AFRICA
Unraveling The Mystery: Does Breckie Hill Have A Boyfriend?
Tanger Outlets Sevierville Directory Map
Mivf Mdcalc
Mercy MyPay (Online Pay Stubs) / mercy-mypay-online-pay-stubs.pdf / PDF4PRO
Mlb Ballpark Pal
Lonadine
Current Time In Maryland
Equipamentos Hospitalares Diversos (Lote 98)
Craigslist Portland Oregon Motorcycles
Jalapeno Grill Ponca City Menu
Sni 35 Wiring Diagram
China’s UberEats - Meituan Dianping, Abandons Bike Sharing And Ride Hailing - Digital Crew
Healthier Homes | Coronavirus Protocol | Stanley Steemer - Stanley Steemer | The Steem Team
Ge-Tracker Bond
Craigslist Org Appleton Wi
Obituaries Milwaukee Journal Sentinel
Is Holly Warlick Married To Susan Patton
Medline Industries, LP hiring Warehouse Operator - Salt Lake City in Salt Lake City, UT | LinkedIn
TMO GRC Fortworth TX | T-Mobile Community
Downtown Dispensary Promo Code
Our 10 Best Selfcleaningcatlitterbox in the US - September 2024
Ringcentral Background
Sam's Club Gas Price Hilliard
Aladtec Login Denver Health
Blackstone Launchpad Ucf
The Bold And The Beautiful Recaps Soap Central
Collier Urgent Care Park Shore
Planet Fitness Santa Clarita Photos
Pay Entergy Bill
Review: T-Mobile's Unlimited 4G voor Thuis | Consumentenbond
Gt500 Forums
Citibank Branch Locations In Orlando Florida
SF bay area cars & trucks "chevrolet 50" - craigslist
Coroner Photos Timothy Treadwell
Shoecarnival Com Careers
Lyndie Irons And Pat Tenore
Panolian Batesville Ms Obituaries 2022
Petra Gorski Obituary (2024)
'The Nun II' Ending Explained: Does the Immortal Valak Die This Time?
Senior Houses For Sale Near Me
Killer Intelligence Center Download
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
American Bully Puppies for Sale | Lancaster Puppies
Congressional hopeful Aisha Mills sees district as an economical model
Saw X (2023) | Film, Trailer, Kritik
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5569

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.