# Linear Regression Quickstart#

Already know what's what with linear regression, just need to know how to tackle it in Python? We're here for you! If not, continue on to the next section.

We're going to ignore the nuance of what we're doing in this notebook, it's really just for people who need to see the process.

## Pandas for our data#

As is typical, we'll be using pandas dataframes for the data.

```import pandas as pd

df = pd.DataFrame([
{ 'sold': 0, 'revenue': 0 },
{ 'sold': 4, 'revenue': 8 },
{ 'sold': 16, 'revenue': 32 },
])
df
```
sold revenue
0 0 0
1 4 8
2 16 32

## Performing a regression#

The statsmodels package is your best friend when it comes to regression. In theory you can do it using other techniques or libraries, but statsmodels is just so simple.

For the regression below, I'm using the formula method of describing the regression. If that makes you grumpy, check the regression reference page for more details.

```import statsmodels.formula.api as smf

model = smf.ols("revenue ~ sold", data=df)
results = model.fit()
results.summary()
```
Dep. Variable: R-squared: revenue 1.000 OLS 1.000 Least Squares 9.502e+30 Sun, 08 Dec 2019 2.07e-16 10:14:18 94.907 3 -185.8 1 -187.6 1 nonrobust
coef std err t P>|t| [0.025 0.975] -2.665e-15 6.18e-15 -0.431 0.741 -8.12e-14 7.58e-14 2.0000 6.49e-16 3.08e+15 0.000 2.000 2.000
 Omnibus: Durbin-Watson: nan 1.149 nan 0.471 -0.616 0.79 1.5 13.4

Warnings:
 Standard Errors assume that the covariance matrix of the errors is correctly specified.

For each unit sold, we get 2 revenue. That's about it.

## Multivariable regression#

Multivariable regression is easy-peasy. Let's add a couple more columns to our dataset, adding tips to the equation.

```import pandas as pd

df = pd.DataFrame([
{ 'sold': 0, 'revenue': 0, 'tips': 0, 'charge_amount': 0 },
{ 'sold': 4, 'revenue': 8, 'tips': 1, 'charge_amount': 9 },
{ 'sold': 16, 'revenue': 32, 'tips': 2, 'charge_amount': 34 },
])
df
```
sold revenue tips charge_amount
0 0 0 0 0
1 4 8 1 9
2 16 32 2 34
```import statsmodels.formula.api as smf

model = smf.ols("charge_amount ~ sold + tips", data=df)
results = model.fit()
results.summary()
```
Dep. Variable: R-squared: charge_amount 1.000 OLS nan Least Squares 0.000 Sun, 08 Dec 2019 nan 10:14:20 89.745 3 -173.5 0 -176.2 2 nonrobust
coef std err t P>|t| [0.025 0.975] -1.685e-15 inf -0 nan nan nan 2.0000 inf 0 nan nan nan 1.0000 inf 0 nan nan nan
 Omnibus: Durbin-Watson: nan 0.922 nan 0.52 -0.691 0.771 1.5 44

Warnings:
 Standard Errors assume that the covariance matrix of the errors is correctly specified.

There you go!

If you'd like more details, you can continue on in this section. If you'd just like the how-to-do-an-exact-thing explanations, check out the regression reference page.

```
```