# -*- coding: utf-8 -*-
"""
Created on Tue Jul 9 15:41:44 2019
@author: User
"""
# 《Python数据分析基础》中国统计出版社
import numpy as np
from scipy import stats
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
from statsmodels.stats.multicomp import pairwise_tukeyhsd
from statsmodels.graphics.api import interaction_plot
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname='data\msyh.ttc')
sale_points = pd.read_csv(u'data\\ch7\\sale_points.csv',encoding = "gbk")
sale_points['market'] = sale_points['market'].astype('category')
sale_points['market'].cat.categories=['market 1', 'market 2',
'market 3']
sale_points['warranty'] = sale_points['warranty'].astype('category')
sale_points['warranty'].cat.categories=['1 years', '3 years']
print(sale_points.head())
formula = 'sales ~ points + C(market) * C(warranty)'
sale_points_anova_cov_est = smf.ols(formula, data = sale_points).fit() # dc_sales_est 是一个模型对象
print(sale_points_anova_cov_est.summary())
运行:
market warranty sales points
0 market 1 1 years 26.0 1.8
1 market 1 1 years 22.0 1.1
2 market 1 1 years 21.8 0.9
3 market 1 1 years 33.1 2.2
4 market 2 1 years 22.0 2.0
OLS Regression Results
==============================================================================
Dep. Variable: sales R-squared: 0.943
Model: OLS Adj. R-squared: 0.923
Method: Least Squares F-statistic: 47.05
Date: Tue, 09 Jul 2019 Prob (F-statistic): 1.16e-09
Time: 15:43:32 Log-Likelihood: -49.711
No. Observations: 24 AIC: 113.4
Df Residuals: 17 BIC: 121.7
Df Model: 6
Covariance Type: nonrobust
================================================================================================================
coef std err t P>|t| [0.025 0.975]
----------------------------------------------------------------------------------------------------------------
Intercept 12.8441 2.386 5.382 0.000 7.809 17.879
C(market)[T.market 2] -8.0349 1.707 -4.706 0.000 -11.637 -4.433
C(market)[T.market 3] 1.3456 1.615 0.833 0.416 -2.061 4.752
C(warranty)[T.3 years] 3.0485 1.673 1.822 0.086 -0.481 6.578
C(market)[T.market 2]:C(warranty)[T.3 years] 5.4217 2.478 2.188 0.043 0.193 10.650
C(market)[T.market 3]:C(warranty)[T.3 years] 14.0594 2.338 6.014 0.000 9.127 18.991
points 8.5873 1.397 6.145 0.000 5.639 11.536
==============================================================================
Omnibus: 0.619 Durbin-Watson: 3.022
Prob(Omnibus): 0.734 Jarque-Bera (JB): 0.690
Skew: -0.305 Prob(JB): 0.708
Kurtosis: 2.435 Cond. No. 16.5
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.