Statsmodels的学习(一)——总述

statsmodels是一个Python模块,它提供对许多不同统计模型估计的类和函数,并且可以进行统计测试和统计数据的探索。
说实话,statsmodels这个词我总是记不住,但是国宝“熊猫”这个单词pandas我还是记得住的,因此每次我打开statsmodels的方式是:

  • 百度搜索pandas,然后进入pandas官网
  • 点击里面的documentation进入对应标签页面
  • 选择下方pandas Ecosystem标签
  • 此标签下看到pandas生态环境就下的第一个标签就是Statsmodels库,选择该标签

由此也可看出,Statsmodels库是非常有用的,否则怎么能放在 pandas 生态环境下的第一个标签上呢?

Statsmodels各个标签简介

查看Statsmodels的目录,发现其包含了24个主要标签,我们打开源文件,省略所有test目录后可以看到其目录树如下:

│  api.py
│  info.py
│  test.txt
│  version.py
│  __init__.py
│  
├─base
│  │  covtype.py
│  │  data.py
│  │  elastic_net.py
│  │  l1_cvxopt.py
│  │  l1_slsqp.py
│  │  l1_solvers_common.py
│  │  model.py
│  │  optimizer.py
│  │  wrapper.py
│  │  _constraints.py
│  │  _penalties.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─compat
│  │  collections.py
│  │  counter.py
│  │  numpy.py
│  │  ordereddict.py
│  │  pandas.py
│  │  python.py
│  │  scipy.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─datasets
│  │  template_data.py
│  │  utils.py
│  │  __init__.py
│  │  
│  ├─anes96
│  │  │  anes96.csv
│  │  │  data.py
│  │  └─ __init__.py
│  │    
│  │          
│  ├─cancer
│  │  │  cancer.csv
│  │  │  data.py
│  │  └─  __init__.py
│  │   
│  │          
│  ├─ccard
│  │  │  ccard.csv
│  │  │  data.py
│  │  └─  __init__.py
│  │          
│  ├─china_smoking
│  │  │  china_smoking.csv
│  │  │  data.py
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─co2
│  │  │  co2.csv
│  │  │  data.py
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─committee
│  │  │  committee.csv
│  │  │  data.py
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─copper
│  │  │  copper.csv
│  │  │  data.py
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─cpunish
│  │  │  cpunish.csv
│  │  │  data.py
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─elnino
│  │  │  data.py
│  │  │  elnino.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─engel
│  │  │  data.py
│  │  │  engel.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─fair
│  │  │  data.py
│  │  │  fair.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─fertility
│  │  │  data.py
│  │  │  fertility.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─grunfeld
│  │  │  data.py
│  │  │  grunfeld.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─heart
│  │  │  data.py
│  │  │  heart.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─longley
│  │  │  data.py
│  │  │  longley.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─macrodata
│  │  │  data.py
│  │  │  macrodata.csv
│  │  │  macrodata.dta
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─modechoice
│  │  │  data.py
│  │  │  modechoice.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─nile
│  │  │  data.py
│  │  │  nile.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─randhie
│  │  │  data.py
│  │  │  randhie.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─scotland
│  │  │  data.py
│  │  │  scotvote.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─spector
│  │  │  data.py
│  │  │  spector.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─stackloss
│  │  │  data.py
│  │  │  stackloss.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─star98
│  │  │  data.py
│  │  │  star98.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─statecrime
│  │  │  data.py
│  │  │  statecrime.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─strikes
│  │  │  data.py
│  │  │  strikes.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  ├─sunspots
│  │  │  data.py
│  │  │  sunspots.csv
│  │  │  __init__.py
│  │  │  
│  │          
│  └─tests
│     └─ ...(省略)
│          
├─discrete
│  │  discrete_margins.py
│  │  discrete_model.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─distributions
│  │  edgeworth.py
│  │  empirical_distribution.py
│  │  mixture_rvs.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─duration
│  │  api.py
│  │  hazard_regression.py
│  │  survfunc.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─emplike
│  │  aft_el.py
│  │  api.py
│  │  descriptive.py
│  │  elanova.py
│  │  elregress.py
│  │  koul_and_mc.py
│  │  originregress.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─formula
│  │  api.py
│  │  formulatools.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─genmod
│  │  api.py
│  │  cov_struct.py
│  │  generalized_estimating_equations.py
│  │  generalized_linear_model.py
│  │  _prediction.py
│  │  _tweedie_compound_poisson.py
│  │  __init__.py
│  │  
│  ├─families
│  │  │  family.py
│  │  │  links.py
│  │  │  varfuncs.py
│  │  │  __init__.py
│  │  │  
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  └─tests
│     └─ ...(省略)
│          
├─graphics
│  │  api.py
│  │  boxplots.py
│  │  correlation.py
│  │  dotplots.py
│  │  factorplots.py
│  │  functional.py
│  │  gofplots.py
│  │  mosaicplot.py
│  │  plottools.py
│  │  plot_grids.py
│  │  regressionplots.py
│  │  tsaplots.py
│  │  tukeyplot.py
│  │  utils.py
│  │  _regressionplots_doc.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─imputation
│  │  mice.py
│  │  ros.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─interface
│  │  __init__.py
│  │  
│  └─__pycache__
│          __init__.cpython-36.pyc
│          
├─iolib
│  │  api.py
│  │  foreign.py
│  │  openfile.py
│  │  smpickle.py
│  │  stata_summary_examples.py
│  │  summary.py
│  │  summary2.py
│  │  table.py
│  │  tableformatting.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─miscmodels
│  │  api.py
│  │  count.py
│  │  nonlinls.py
│  │  tmodel.py
│  │  try_mlecov.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─multivariate
│  │  pca.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│      
│          
├─nonparametric
│  │  api.py
│  │  bandwidths.py
│  │  kde.py
│  │  kdetools.py
│  │  kernels.py
│  │  kernel_density.py
│  │  kernel_regression.py
│  │  linbin.cp36-win_amd64.pyd
│  │  smoothers_lowess.py
│  │  smoothers_lowess_old.py
│  │  _kernel_base.py
│  │  _smoothers_lowess.cp36-win_amd64.pyd
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─regression
│  │  feasible_gls.py
│  │  linear_model.py
│  │  mixed_linear_model.py
│  │  quantile_regression.py
│  │  recursive_ls.py
│  │  _prediction.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─resampling
│  │  __init__.py
│  │  
│  └─__pycache__
│          __init__.cpython-36.pyc
│          
├─robust
│  │  norms.py
│  │  robust_linear_model.py
│  │  scale.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
├─sandbox
│  │  bspline.py
│  │  contrast_old.py
│  │  cox.py
│  │  descstats.py
│  │  formula.py
│  │  gam.py
│  │  infotheo.py
│  │  km_class.py
│  │  mle.py
│  │  multilinear.py
│  │  pca.py
│  │  predict_functional.py
│  │  rls.py
│  │  survival.py
│  │  survival2.py
│  │  sysreg.py
│  │  utils_old.py
│  │  __init__.py
│  │  
│  ├─archive
│  │  │  linalg_covmat.py
│  │  │  linalg_decomp_1.py
│  │  │  tsa.py
│  │  │  __init__.py
│  │  │  
│  │  └─__pycache__
│  │          linalg_covmat.cpython-36.pyc
│  │          linalg_decomp_1.cpython-36.pyc
│  │          tsa.cpython-36.pyc
│  │          __init__.cpython-36.pyc
│  │          
│  ├─datarich
│  │  │  factormodels.py
│  │  │  __init__.py
│  │  │  
│  │  └─__pycache__
│  │          factormodels.cpython-36.pyc
│  │          __init__.cpython-36.pyc
│  │          
│  ├─distributions
│  │  │  copula.py
│  │  │  estimators.py
│  │  │  extras.py
│  │  │  genpareto.py
│  │  │  gof_new.py
│  │  │  multivariate.py
│  │  │  mv_measures.py
│  │  │  mv_normal.py
│  │  │  otherdist.py
│  │  │  quantize.py
│  │  │  sppatch.py
│  │  │  transformed.py
│  │  │  transform_functions.py
│  │  │  try_max.py
│  │  │  try_pot.py
│  │  │  __init__.py
│  │  │  
│  │  ├─examples
│  │  │  │  ex_extras.py
│  │  │  │  ex_fitfr.py
│  │  │  │  ex_gof.py
│  │  │  │  ex_mvelliptical.py
│  │  │  │  ex_transf2.py
│  │  │  │  matchdist.py
│  │  │  └─ __init__.py
│  │  │          
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  ├─mcevaluate
│  │  │  arma.py
│  │  └─ __init__.py
│  │    
│  │          
│  ├─nonparametric
│  │  │  densityorthopoly.py
│  │  │  dgp_examples.py
│  │  │  kde2.py
│  │  │  kdecovclass.py
│  │  │  kernels.py
│  │  │  kernel_extras.py
│  │  │  smoothers.py
│  │  │  testdata.py
│  │  │  __init__.py
│  │  │  
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  ├─panel
│  │  │  correlation_structures.py
│  │  │  mixed.py
│  │  │  panelmod.py
│  │  │  panel_short.py
│  │  │  random_panel.py
│  │  │  sandwich_covariance.py
│  │  │  sandwich_covariance_generic.py
│  │  │  __init__.py
│  │  │  
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  ├─regression
│  │  │  anova_nistcertified.py
│  │  │  ar_panel.py
│  │  │  example_kernridge.py
│  │  │  gmm.py
│  │  │  kernridgeregress_class.py
│  │  │  ols_anova_original.py
│  │  │  onewaygls.py
│  │  │  penalized.py
│  │  │  predstd.py
│  │  │  runmnl.py
│  │  │  sympy_diff.py
│  │  │  tools.py
│  │  │  treewalkerclass.py
│  │  │  try_catdata.py
│  │  │  try_ols_anova.py
│  │  │  try_treewalker.py
│  │  │  __init__.py
│  │  │  
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  ├─stats
│  │  │  contrast_tools.py
│  │  │  diagnostic.py
│  │  │  ex_newtests.py
│  │  │  multicomp.py
│  │  │  runs.py
│  │  │  stats_dhuard.py
│  │  │  stats_mstats_short.py
│  │  │  __init__.py
│  │  │  
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  ├─tests
│  │   └─ ...(省略)
│  │          
│  ├─tools
│  │  │  cross_val.py
│  │  │  mctools.py
│  │  │  tools_pca.py
│  │  │  try_mctools.py
│  │  └─  __init__.py
│  │    
│  │          
│  └─tsa
│     │  diffusion.py
│     │  diffusion2.py
│     │  example_arma.py
│     │  fftarma.py
│     │  garch.py
│     │  movstat.py
│     │  try_arma_more.py
│     │  try_fi.py
│     │  try_var_convolve.py
│     │  varma.py
│     └─ __init__.py
│    
│          
├─src
│  │  __init__.py
│  │  
│  └─__pycache__
│          __init__.cpython-36.pyc
│          
├─stats
│  │  anova.py
│  │  api.py
│  │  base.py
│  │  contingency_tables.py
│  │  contrast.py
│  │  correlation_tools.py
│  │  descriptivestats.py
│  │  diagnostic.py
│  │  gof.py
│  │  inter_rater.py
│  │  mediation.py
│  │  moment_helpers.py
│  │  multicomp.py
│  │  multitest.py
│  │  multivariate_tools.py
│  │  outliers_influence.py
│  │  power.py
│  │  proportion.py
│  │  sandwich_covariance.py
│  │  stattools.py
│  │  tabledist.py
│  │  weightstats.py
│  │  _adnorm.py
│  │  _lilliefors.py
│  │  __init__.py
│  │  
│  ├─libqsturng
│  │  │  CH.r
│  │  │  LICENSE.txt
│  │  │  make_tbls.py
│  │  │  qsturng_.py
│  │  │  __init__.py
│  │  │  
│  │  └─tests
│  │     └─ ...(省略)
│  │          
│  └─tests
│     └─ ...(省略)
│          
├─tools
│  │  catadd.py
│  │  data.py
│  │  decorators.py
│  │  dump2module.py
│  │  eval_measures.py
│  │  grouputils.py
│  │  linalg.py
│  │  numdiff.py
│  │  parallel.py
│  │  print_version.py
│  │  rootfinding.py
│  │  sm_exceptions.py
│  │  testing.py
│  │  tools.py
│  │  transform_model.py
│  │  web.py
│  │  wrappers.py
│  │  _testing.py
│  │  __init__.py
│  │  
│  └─tests
│     └─ ...(省略)
│          
└─tsa
   │  adfvalues.py
   │  api.py
   │  arima_model.py
   │  arima_process.py
   │  arma_mle.py
   │  ar_model.py
   │  descriptivestats.py
   │  mlemodel.py
   │  seasonal.py
   │  stattools.py
   │  tsatools.py
   │  varma_process.py
   │  x13.py
   │  _bds.py
   │  __init__.py
   │  
   ├─base
   │  │  datetools.py
   │  │  tsa_model.py
   │  │  __init__.py
   │  │  
   │  └─tests
   │     │  test_base.py
   │     │  test_datetools.py
   │     └─ __init__.py
   │          
   ├─filters
   │  │  api.py
   │  │  bk_filter.py
   │  │  cf_filter.py
   │  │  filtertools.py
   │  │  hp_filter.py
   │  │  _utils.py
   │  │  __init__.py
   │  │  
   │  └─tests
   │     └─ ...(省略)
   │          
   ├─interp
   │  │  denton.py
   │  │  __init__.py
   │  │  
   │  └─tests
   │     │  test_denton.py
   │     └─  __init__.py
   │        
   │          
   ├─kalmanf
   │  │  kalmanfilter.py
   │  │  kalman_loglike.cp36-win_amd64.pyd
   │  └─ __init__.py
   │          
   ├─regime_switching
   │  │  markov_autoregression.py
   │  │  markov_regression.py
   │  │  markov_switching.py
   │  │  _hamilton_filter.cp36-win_amd64.pyd
   │  │  __init__.py
   │  │  
   │  └─tests
   │     └─ ...(省略)
   │          
   ├─statespace
   │  │  api.py
   │  │  dynamic_factor.py
   │  │  kalman_filter.py
   │  │  kalman_smoother.py
   │  │  mlemodel.py
   │  │  representation.py
   │  │  sarimax.py
   │  │  structural.py
   │  │  tools.py
   │  │  varmax.py
   │  │  _statespace.cp36-win_amd64.pyd
   │  │  __init__.py
   │  │  
   │  └─tests
   │     └─ ...(省略)
   │          
   └─tests
      └─ ...(省略)
           

        

在介绍各个类别函数的时候,首先介绍一下Statsmodels强大的预处理函数

预处理函数

自版本0.5以来,statsmodels允许用户使用R语言的公式来匹配统计模型。在内部,statsmodels使用patsy包将公式和数据转换为模型拟合中使用的矩阵。
公式框架非常强大;下面的例子只触及表面。对公式语言的完整描述可以在patsy文档中找到

dmatrices是一个很好用的预处理函数,功能如下

Notice that dmatrices has
注意到dmatrices具有以下几个功能
    split the categorical Region variable into a set of indicator variables.
    将分类区域变量分解为一组指示器变量。
    added a constant to the exogenous regressors matrix.
    增加了一个常数给外源的回归矩阵。
    returned pandas DataFrames instead of simple numpy arrays. This is useful because DataFrames allow statsmodels to carry-over meta-data (e.g. variable names) when reporting results.
    返回数据是Pandas的DataFrames,而不是简单的numpy数组。这是有用的,因为DataFrames允许statsmo模型在报告结果时对元数据(例如变量名)进行处理。

得到处理好的数据后,我们即可进行一些统计分析,最常用的就是

Fitting a model in statsmodels typically involves 3 easy steps:
在statsmomodel中安装一个模型通常需要3个简单的步骤:
    Use the model class to describe the model
    使用模型类来描述模型
    Fit the model using a class method
    使用类方法来匹配模型
    Inspect the results using a summary method
    使用摘要方法检查结果

比如下面一个例子:

import statsmodels.api as sm
import pandas as pd
from patsy import dmatrices
import statsmodels

df = sm.datasets.get_rdataset("Guerry", "HistData").data
vars = ['Department', 'Lottery', 'Literacy', 'Wealth', 'Region']
df = df[vars]
df = df.dropna()
y, X = dmatrices('Lottery ~ Literacy + Wealth + Region', data=df, return_type='dataframe')
mod = sm.OLS(y, X)
res = mod.fit()

预处理函数已经讲完,后面将一个一个简单学习各个模型的使用。

猜你喜欢

转载自blog.csdn.net/qq_19528953/article/details/80170402