How to predict the values of a variable of an existing dataframe using line of best fit using python?

Viewed 24

I don't know how to go about using line of best fit to predict the values of 2 variables in an existing dataframe. For example, let's say column 'Year' in a dataframe stopped at 2013 butI want my line of best fit to extend beyond 2050 to predict the values of 'Income' column (like the trend) plotting it using matplotlib. I know I would probably use linregress() function from scipy.stats but I don't know how to predict it. Thanks in advance for your help,

1 Answers

A linear regression will give you a slop and an intercept. For a given (unseen) x value (Year in your case), and a model, res built with res = stats.linregress(x, y)

res.intercept + res.slope*x

will tell you the y value (Income in your case)

Related