Simple Linear Regression
Simple Linear Regression
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import scipy as sp
import statsmodels.api as sm
import statsmodels.formula.api as smf
%matplotlib inline
plt.rcParams['figure.dpi'] =150
plt.rcParams['figure.figsize'] = (8,6)
plt.rcParams.update({'font.size':8})
bp = pd.read_excel('Copy of Age-BP.xlsx')
bp.head()
i Y X
0 1 144 39
1 2 220 47
2 3 138 43
3 4 145 47
4 5 162 65
bp.tail()
i Y X
25 26 158 53
26 27 154 63
27 28 130 29
28 29 125 25
29 30 175 69
bp.head(2)
i Y X
0 1 144 39
1 2 220 47
model1_fit = model1.fit()
model1_fit.summary()
<class 'statsmodels.iolib.summary.Summary'>
"""
OLS Regression Results
=============================================================================
=
Dep. Variable: Y R-squared:
0.471
Model: OLS Adj. R-squared:
0.452
Method: Least Squares F-statistic:
24.92
Date: Sat, 20 Jan 2024 Prob (F-statistic): 2.83e-
05
Time: 16:51:55 Log-Likelihood: -
126.35
No. Observations: 30 AIC:
256.7
Df Residuals: 28 BIC:
259.5
Df Model: 1
=============================================================================
=
coef std err t P>|t| [0.025
0.975]
-----------------------------------------------------------------------------
-
Intercept 97.7273 9.655 10.122 0.000 77.951
117.504
X 1.0210 0.205 4.992 0.000 0.602
1.440
=============================================================================
=
Omnibus: 44.814 Durbin-Watson:
1.727
Prob(Omnibus): 0.000 Jarque-Bera (JB):
212.423
Skew: 2.958 Prob(JB): 7.46e-
47
Kurtosis: 14.616 Cond. No.
148.
=============================================================================
=
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is
correctly specified.
"""
model:
Y = β0 + β 1 × X
Y =97.273+1.021 X
rent = pd.read_stata('rent99.dta')
rent.head()
rent.tail()
district
3077 2148.0
3078 341.0
3079 961.0
3080 381.0
3081 522.0
model2_fit = model2.fit()
model2_fit.summary()
<class 'statsmodels.iolib.summary.Summary'>
"""
OLS Regression Results
=============================================================================
=
Dep. Variable: rent R-squared:
0.253
Model: OLS Adj. R-squared:
0.252
Method: Least Squares F-statistic:
1041.
Date: Sat, 20 Jan 2024 Prob (F-statistic): 6.28e-
197
Time: 17:25:52 Log-Likelihood: -
20186.
No. Observations: 3082 AIC:
4.038e+04
Df Residuals: 3080 BIC:
4.039e+04
Df Model: 1
=============================================================================
=
coef std err t P>|t| [0.025
0.975]
-----------------------------------------------------------------------------
-
Intercept 172.4176 9.405 18.332 0.000 153.976
190.859
rentsqm 40.3613 1.251 32.257 0.000 37.908
42.815
=============================================================================
=
Omnibus: 568.919 Durbin-Watson:
1.917
Prob(Omnibus): 0.000 Jarque-Bera (JB):
1590.958
Skew: 0.972 Prob(JB):
0.00
Kurtosis: 5.934 Cond. No.
23.6
=============================================================================
=
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is
correctly specified.
"""