CoCalc Public Filesols-lm.ipynb
Views : 127
Compute Environment: Ubuntu 18.04 (Deprecated)

# Statsmodels OLS fit

In [1]:
import pandas as pd

In [2]:
data = pd.DataFrame({
'x': [1, 2, 4, 5, 6, 7.7, 8.2, 9, 10, 11],
'y': [3, 3.6, 4.2, 4.1, 5, 4.9, 5.5, 6, 6.1, 6.5],
})

In [3]:
import statsmodels.formula.api as smf

In [4]:
model = smf.ols('y ~ x', data=data)

In [5]:
res = model.fit()

In [6]:
res.predict(pd.DataFrame({'x': [0, 5, 10, 100]}))

0 2.741911 1 4.422732 2 6.103553 3 36.358325 dtype: float64
In [7]:
res.summary()

/usr/local/lib/python3.6/dist-packages/scipy/stats/stats.py:1535: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=10 "anyway, n=%i" % int(n))
Dep. Variable: R-squared: y 0.962 OLS 0.957 Least Squares 201.4 Sat, 20 Jun 2020 5.91e-07 10:55:57 1.2199 10 1.560 8 2.165 1 nonrobust
coef std err t P>|t| [0.025 0.975] 2.7419 0.169 16.201 0.000 2.352 3.132 0.3362 0.024 14.193 0.000 0.282 0.391
 Omnibus: Durbin-Watson: 2.06 2.907 0.357 1.18 -0.805 0.554 2.512 16.2

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [8]:
import numpy as np
import matplotlib.pyplot as plt

In [9]:
fig = data.plot.scatter('x', 'y', s=40, grid=True)
xx = np.linspace(-2, 15, 100)
yy = res.predict(pd.DataFrame({'x': xx}))
fig.plot(xx, yy, color='green')

[<matplotlib.lines.Line2D at 0x7f238a059080>]
In [10]:
data2 = pd.DataFrame({
'x1': [1, 2, 4, 5, 6, 7.7, 8.2, 9, 10, 11],
'x2': [4, 5, 4, 5, 6, 7.7, 7, 7.9, 8, 8.1],
'y': [3, 3.6, 4.2, 4.1, 5, 4.9, 5.5, 6, 6.1, 6.5],
})

In [11]:
model2 = smf.ols('y ~ x1 + x2', data=data2)
res2 = model2.fit()

In [12]:
res2.summary()

/usr/local/lib/python3.6/dist-packages/scipy/stats/stats.py:1535: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=10 "anyway, n=%i" % int(n))
Dep. Variable: R-squared: y 0.962 OLS 0.951 Least Squares 88.75 Sat, 20 Jun 2020 1.06e-05 11:01:26 1.2542 10 3.492 7 4.399 2 nonrobust
coef std err t P>|t| [0.025 0.975] 2.8433 0.496 5.730 0.001 1.670 4.017 0.3503 0.069 5.063 0.001 0.187 0.514 -0.0306 0.139 -0.219 0.833 -0.360 0.299
 Omnibus: Durbin-Watson: 1.553 2.877 0.46 0.992 -0.707 0.609 2.383 61.4

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [ ]: