python笔记:7.2.1.4方差分析模型的参数估计和预测(影响数码相机销量)

 

# -*- coding: utf-8 -*-
"""
Created on Mon Jul  8 14:16:26 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 matplotlib.font_manager import FontProperties
myfont=FontProperties(fname='data\msyh.ttc')

dc_sales = pd.read_csv(u'data\\ch7\\dc_sales.csv',encoding = "gbk")

dc_sales['pixel'] = dc_sales['pixel'].astype('category')
dc_sales['pixel'].cat.categories=['500万像素及以下','500-600万像素',
        '600-800万像素','800-1000万像素',
        '1000万像素及以上']
print(dc_sales.head())

formula = 'sales ~ C(pixel)'
dc_sales_est = smf.ols(formula, dc_sales).fit()  # dc_sales_est 是一个模型对象

#print("\n dc_sales_est.summary():")
#print(dc_sales_est.summary())

print("\n dc_sales_est.summary2():")
print(dc_sales_est.summary2())

运行:

   market        pixel  sales
0       1    500万像素及以下     70
1       1   500-600万像素    101
2       1   600-800万像素    114
3       1  800-1000万像素    120
4       1   1000万像素及以上    132

 dc_sales_est.summary2():
                    Results: Ordinary least squares
=======================================================================
Model:                OLS                Adj. R-squared:       0.656   
Dependent Variable:   sales              AIC:                  314.0202
Date:                 2019-07-08 16:31   BIC:                  322.4646
No. Observations:     40                 Log-Likelihood:       -152.01 
Df Model:             4                  F-statistic:          19.57   
Df Residuals:         35                 Prob (F-statistic):   1.55e-08
R-squared:            0.691              Scale:                133.77  
-----------------------------------------------------------------------
                         Coef.  Std.Err.    t    P>|t|   [0.025  0.975]
-----------------------------------------------------------------------
Intercept               81.1250   4.0892 19.8387 0.0000 72.8234 89.4266
C(pixel)[T.500-600万像素]  14.6250   5.7831  2.5289 0.0161  2.8848 26.3652
C(pixel)[T.600-800万像素]  26.0000   5.7831  4.4959 0.0001 14.2598 37.7402
C(pixel)[T.800-1000万像素] 42.8750   5.7831  7.4139 0.0000 31.1348 54.6152
C(pixel)[T.1000万像素及以上]  41.0000   5.7831  7.0897 0.0000 29.2598 52.7402
-----------------------------------------------------------------------
Omnibus:                 0.757          Durbin-Watson:            1.535
Prob(Omnibus):           0.685          Jarque-Bera (JB):         0.172
Skew:                    -0.090         Prob(JB):                 0.917
Kurtosis:                3.266          Condition No.:            6    
=======================================================================

你可能感兴趣的:(python学习)