-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsmodelsOLSBoston.py
97 lines (92 loc) · 2.5 KB
/
statsmodelsOLSBoston.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 17 10:21:17 2019
@author: user
"""
import statsmodels
import pandas as pd
import statsmodels.api as sm
duncan_prestige = sm.datasets.get_rdataset("Duncan", "carData")
Boston = sm.datasets.get_rdataset("Boston")
import sklearn
from sklearn.datasets import load_boston
import sklearn.model_selection
from sklearn.linear_model import LinearRegression
Boston = load_boston()
print(Boston.keys())
print(Boston.feature_names)
type(Boston)
Boston.data
bos = pd.DataFrame(Boston.data, columns=Boston.feature_names)
bos
bos.head()
bos['PRICE'] = Boston.target
bos.head()
print(bos.describe())
X = bos.drop('PRICE', axis=1)
Y = bos['PRICE']
X_train, X_test, Y_train, Y_test = sklearn.model_selection.train_test_split(
X, Y, test_size=0.33, random_state=5)
lm = LinearRegression()
lm.fit(X_train, Y_train)
Y_pred = lm.predict(X_test)
plt.scatter(Y_test, Y_pred)
import matplotlib.pyplot as plt
plt.scatter(Y_test, Y_pred)
mse = sklearn.metrics.mean_squared_error(Y_test, Y_pred)
print(mse)
lm.score
lm.score()
help(lm.score)
lm.score(X_test,Y_test)
import statsmodels.api as sm
df = sm.datasets.get_rdataset("Guerry", "HistData").data
df
df.keys
df = sm.datasets.get_rdataset("Guerry", "HistData").keys
df = sm.datasets.get_rdataset("Guerry", "HistData").keys()
df = sm.datasets.get_rdataset("Guerry", "HistData").columns
df = sm.datasets.get_rdataset("Guerry", "HistData").data
df = sm.datasets.get_rdataset("Guerry", "HistData").columns
df[['Lottery', 'Literacy', 'Wealth', 'Region']].dropna()
type(df)
df1 = sm.datasets.get_rdataset("Guerry", "HistData").data
type(df1)
df1.columns
mod = smf.ols(formula='Lottery ~ Literacy + Wealth + Region', data=df)
import statsmodels.formula.api as smf
mod = smf.ols(formula='Lottery ~ Literacy + Wealth + Region', data=df)
mod.fit()
res = mod.fit()
print(res.summary())
res1 = smf.ols(formula='Lottery ~ Literacy : Wealth - 1', data=df).fit()
res1.params
print(res1.params)
mod = smf.ols(formula='Lottery ~ Literacy + Wealth + Region', data=df)
res = mod.fit()
res.params()
res.params
help(res.predict)
help(res.resid)
type(res)
res.predict()
ypred = res.predict()
ypred
res.resid
fig, ax = plt.subplots(figsize=(12,8))
import statsmodels.api as sm
sm.graphics.influence_plot(res, ax=ax, criterion="cooks")
res.resid
res.rsquared
print(res.summary())
res.resid
res.scale
help(res.scale)
res.scale
res.rsquared
res.resid
norm(res.resid)/sqrt(84)
import numpy as np
np.norm(res.resid)/sqrt(84)
np.linalg.norm(res.resid)/sqrt(84)
np.linalg.norm(res.resid)/np.sqrt(84)