r/datascience • • Feb 20 '24

Analysis Linear Regression is underrated

Hey folks,

Wanted to share a quick story from the trenches of data science. I am not a data scientist but engineer however I've been working on a dynamic pricing project where the client was all in on neural networks to predict product sales and figure out the best prices using overly complicated setup. They tried linear regression once, didn't work magic instantly, so they jumped ship to the neural network, which took them days to train.

I thought, "Hold on, let's not ditch linear regression just yet." Gave it another go, dove a bit deeper, and bam - it worked wonders. Not only did it spit out results in seconds (compared to the days of training the neural networks took), but it also gave us clear insights on how different factors were affecting sales. Something the neural network's complexity just couldn't offer as plainly.

Moral of the story? Sometimes the simplest tools are the best for the job. Linear regression, logistic regression, decision trees might seem too basic next to flashy neural networks, but it's quick, effective, and gets straight to the point. Plus, you don't need to wait days to see if you're on the right track.

So, before you go all in on the latest and greatest tech, don't forget to give the classics a shot. Sometimes, they're all you need.

Cheers!

Edit: Because I keep getting lot of comments why this post sounds like linkedin post, gonna explain upfront that I used grammarly to improve my writing (English is not my first language)

1.0k Upvotes

201 comments sorted by

View all comments

Show parent comments

152

u/caksters Feb 20 '24 edited Feb 20 '24

It didnt work first time because they did not perform feature engineering, clean the data properly.

You can model units sold by taking a log transformation of quantity sold, product price. Taking log(Q)=a + b*log(P). For this equation the parameter b has an actual meaning which is “price elasticity of demand”. taking log of those two quantities also has the benefit as it scales the values and you minimise the effects where some products sell ridiculous amounts of quantities whereas some other products sell less (e.g. expensive products).

This equation can be expanded further where you add other variables that explain the “sell-ability” of your products (seasonality, holidays, promotions, website traffic) and model it as linear equation.

You can even introduce non-linearity by multiplying terms together but this requires a careful consideration if you want to be able to explain.

Originally when they applied LR they did not scale the data, or normalise it when they were exploring Linear Regression vs some other models. Neural Networks were the only model that were somewhat capable of predicting their sales.

59

u/[deleted] Feb 20 '24

Econometrician will say B estimate is biased but it’s okay if it is not the main parameter of interest

24

u/caksters Feb 20 '24

Can you elaborate more please? It will be important parameter for other models where we want to model how pricing influences sales

69

u/[deleted] Feb 20 '24 edited Feb 20 '24

This belong to the domain of econometric called “price endogeneity” that has long been studied since 1920s.

The key is u just need to find an instrument to control for either demand or supply side factor that drives the sales otherwise u won’t know whether the change of sales is demand or supply side driven.

Without that u can’t identify the true effect of price elasticity of demand. It shouldn’t be too difficult to find the instrument to control for this if u are working with the client directly.

34

u/caksters Feb 20 '24

Thank you for this! This is new field to me so any leads like this to understand the theory better is much appreciated.

I know this is a complex subject and in my few weeks of engagement I will barely gain surface understanding of it, but just hope to get enough to make something work

40

u/[deleted] Feb 20 '24

https://perhuaman.files.wordpress.com/2014/06/econometrics-bruce-hansen-2014.pdf

P296 will show mathematically what I mean and how u can solve it :)

→ More replies (1)

4

u/kazza789 Feb 20 '24

In many pricing situations you have historical price variability that is probably obviously more then just a response to demand. For example - running a temporary promotion where price is dropped for a week or two.

Does having this in your historical dataset alleviate this problem?

7

u/[deleted] Feb 20 '24 edited Feb 20 '24

That is one way to solve it yes ! Imben or Card (I forgot whom) I remember did something similar to estimate if education causes life time wage to be higher by going into history and find out some period of the school (in France) they relaxed their intake requirement and took more students than they normally would. They use that as an IV to control for the endogeneity

4

u/[deleted] Feb 20 '24

[removed] — view removed comment

18

u/[deleted] Feb 20 '24 edited Feb 20 '24

I would call it the quest for an unbiased, consistent, and efficient estimator rather than simply minimising RSMF/maximising R2 :)

I don’t know what is it for DS people everything econometric they box it down into “casual inference”, which is really just one of the many topics

3

u/relevantmeemayhere Feb 21 '24

Cuz econometrics and agronomists is where causal really got started :)

→ More replies (1)

0

u/Drakkur Feb 23 '24

This only matters when modeling markets, not for businesses which control the supply of their product.

If you were a business that sold a commodity into a market then endogeneity is a big problem. Most companies do not sell a commoditized product, so endogeneity can be assumed to be of little to no impact on regression estimates.

→ More replies (1)

9

u/[deleted] Feb 20 '24

I did a bit of research and find this talking u through Fulton fish market demand vs supply dataset which is iconic. Just follow and it should solve your issue in no time: https://youtu.be/fpZC_tEfnLM?si=MHNCHFcJvg9Uxk2S

12

u/Brain_Damage53 Feb 20 '24

Pricing is not the only factor that can explain quantity sold. If you omit other potential variables that could have an impact on quantity, you suffer from omitted variable bias and draw spurious inferences from your b estimate.

→ More replies (1)

4

u/helpmeplox_xd Feb 20 '24

Can you explain to a newbie why do you need to normalize the data?

11

u/[deleted] Feb 20 '24

At a high level, its the principle of "apples to apples" when drawing inference or making comparisons. If you don't normalize or scale your data, the inherently different "raw" scales your predictors are measured in can lead to artificial undue influence. Example: predicting happiness from age and annual salary. Imagine that in your dataset age ranges from 20 to 100 and salary ranges from 0 to 250,000,  with a much wider spread. You need to scale them so they are on "equal footing". Hopefully that makes sense 

6

u/helpmeplox_xd Feb 21 '24

Thank you! I understand we need to do that sometimes. However, I thought that in linear regression, the parameters' coefficients would take care of that. For instance, in your example, the coefficient for the age variable would be higher between 0.1 and 10, and for income, the coefficient would be between 0.001 and 0.010.. or something like that. Is it not the case?

4

u/save_the_panda_bears Feb 21 '24 edited Feb 21 '24

You’re correct, OLS is scale invariant. However if you introduce any sort of regularization a la ridge or lasso regression, you’re gonna want to normalize the data. I believe sklearn uses gradient descent for their linear regression, which also isn’t scale invariant.

2

u/ilyanekhay Feb 21 '24

Gradient descent is orthogonal to regularization - it's still minimizing the loss function which includes the L1/L2/... loss terms, so you're correct about that.

In general, I believe the particular optimization method used (e.g. gradient descent, Newton, BFGS, ...) would always be orthogonal to regularization.

4

u/TheTackleZone Feb 20 '24

I would suggest splitting your conversion and elasticity models, and using GBMs for conversion and GLMs for elasticity. In fact use a simplified GLM for conversion and then feed that into your GBM for conversion.

In my experience the "stability" of GLMs is more important for elasticity where you are better off being approximately right all the time than precisely right more often but quite wrong the rest of the time.

6

u/RepresentativeFill26 Feb 20 '24

Thanks for your response, very insightful! I know the polynomial expansion by combining features, but the log part is new. Do you have a source for this I can take a look at?

4

u/[deleted] Feb 20 '24

1-1 transformations on features for linear regression are actually pretty common. You can just look up things about transforming features for LR.

6

u/Aranka_Szeretlek Feb 20 '24

In my bachelor's studies, my professors used to say that every bullsh*t is linear on a Log-Log scale. This was not a good thing, however, but a cautionary statement that even if something seems linear in a Log-Log scale, it can still be meaningless to perform a linear regression.

3

u/DJ_laundry_list Feb 21 '24

Any more insight into why they said that?

6

u/Aranka_Szeretlek Feb 21 '24

It should probably be considered that every error metric (standard deviation, for example) will be exponentiated in production. This transformation also makes the error function nonlinear, so for example a +/- 0.1 error bar at Log(a)=2 is ten times larger in reality than at Log(a)=1, with linear regression conveniently ignoring this fact.

→ More replies (4)