importpaths.rst 4.1 KB
Newer Older
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
.. _importpaths:

Import Paths and Structure
==========================

We offer two ways of importing functions and classes from statsmodels:

1. `API import for interactive use`_

   + Allows tab completion

2. `Direct import for programs`_

   + Avoids importing unnecessary modules and commands

API Import for interactive use
------------------------------

For interactive use the recommended import is:

.. code-block:: python

    import statsmodels.api as sm

Importing `statsmodels.api` will load most of the public parts of statsmodels.
This makes most functions and classes conveniently available within one or two
levels, without making the "sm" namespace too crowded. 

To see what functions and classes available, you can type the following (or use
the namespace exploration features of IPython, Spyder, IDLE, etc.): 

.. code-block:: python

    >>> dir(sm)
    ['GLM', 'GLS', 'GLSAR', 'Logit', 'MNLogit', 'OLS', 'Poisson', 'Probit', 'RLM',
    'WLS', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
    'add_constant', 'categorical', 'datasets', 'distributions', 'families',
    'graphics', 'iolib', 'nonparametric', 'qqplot', 'regression', 'robust',
    'stats', 'test', 'tools', 'tsa', 'version']

    >>> dir(sm.graphics)
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
    'abline_plot', 'beanplot', 'fboxplot', 'interaction_plot', 'qqplot',
    'rainbow', 'rainbowplot', 'violinplot']

    >>> dir(sm.tsa)
    ['AR', 'ARMA', 'DynamicVAR', 'SVAR', 'VAR', '__builtins__', '__doc__',
    '__file__', '__name__', '__package__', 'acf', 'acovf', 'add_lag',
    'add_trend', 'adfuller', 'ccf', 'ccovf', 'datetools', 'detrend',
    'filters', 'grangercausalitytests', 'interp', 'lagmat', 'lagmat2ds',
    'pacf', 'pacf_ols', 'pacf_yw', 'periodogram', 'q_stat', 'stattools',
    'tsatools', 'var']

Notes
^^^^^

The `api` modules may not include all the public functionality of statsmodels. If
you find something that should be added to the api, please file an issue on
github or report it to the mailing list.

The subpackages of statsmodels include `api.py` modules that are mainly
intended to collect the imports needed for those subpackages. The `subpackage/api.py`
files are imported into statsmodels api, for example ::

     from .nonparametric import api as nonparametric

Users do not need to load the `subpackage/api.py` modules directly.

Direct import for programs
--------------------------

``statsmodels`` submodules are arranged by topic (e.g. `discrete` for discrete
choice models, or `tsa` for time series analysis). Our directory tree (stripped
down) looks something like this::

    statsmodels/
        __init__.py
        api.py
        discrete/
            __init__.py
            discrete_model.py
            tests/
                results/
        tsa/
            __init__.py
            api.py
            tsatools.py
            stattools.py
            arima_model.py
            arima_process.py
            vector_ar/
                __init__.py
                var_model.py
                tests/
                    results/
            tests/
                results/
        stats/
            __init__.py
            api.py
            stattools.py
            tests/
        tools/
            __init__.py
            tools.py
            decorators.py
            tests/

The submodules that can be import heavy contain an empty `__init__.py`, except
for some testing code for running tests for the submodules. The intention is to
change all directories to have an `api.py` and empty `__init__.py` in the next
release.

Import examples
^^^^^^^^^^^^^^^

Functions and classes::

    from statsmodels.regression.linear_model import OLS, WLS
    from statsmodels.tools.tools import rank, add_constant

Modules ::

    from statsmodels.datasets import macrodata
    import statsmodels.stats import diagnostic

Modules with aliases ::

    import statsmodels.regression.linear_model as lm
    import statsmodels.stats.diagnostic as smsdia
    import statsmodels.stats.outliers_influence as oi

We do not have currently a convention for aliases of submodules.