Skip to content

Commit

Permalink
DOC: Improve regression doc strings
Browse files Browse the repository at this point in the history
Improve regression doc strings
Fix small spelling errors
  • Loading branch information
bashtage committed Aug 5, 2019
1 parent 299cf07 commit 1c3ed0f
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 141 deletions.
1 change: 1 addition & 0 deletions docs/source/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2259,3 +2259,4 @@ statawriter
Nonparameteric
exceedance
separatevar
OaxacaResults
22 changes: 20 additions & 2 deletions statsmodels/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ def initialize(self):
def loglike(self, params):
"""
Log-likelihood of model.
Parameters
----------
params : ndarray
The model parameters used to compute the log-likelihood.
Notes
-----
Must be overridden by subclasses.
"""
raise NotImplementedError

Expand Down Expand Up @@ -983,9 +992,18 @@ def __init__(self, model, params, **kwd):
self.initialize(model, params, **kwd)
self._data_attr = []

def initialize(self, model, params, **kwd):
def initialize(self, model, params, **kwargs):
"""
Initialize (possibly re-initialize) a Results instance.
Parameters
----------
model : Model
The model instance.
params : ndarray
The model parameters.
**kwargs
Any additional keyword arguments required to initialize the model.
"""
self.params = params
self.model = model
Expand Down Expand Up @@ -1016,7 +1034,7 @@ def predict(self, exog=None, transform=True, *args, **kwargs):
Returns
-------
prediction : {ndarray, Series, DataFrame}
array_like
See self.model.predict.
Notes
Expand Down
14 changes: 8 additions & 6 deletions statsmodels/base/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import inspect
import functools

from statsmodels.compat.python import iteritems

import functools
import inspect
from textwrap import dedent


class ResultsWrapper(object):
"""
Expand Down Expand Up @@ -43,11 +44,11 @@ def __getattribute__(self, attr):
return obj

def __getstate__(self):
#print 'pickling wrapper', self.__dict__
# print 'pickling wrapper', self.__dict__
return self.__dict__

def __setstate__(self, dict_):
#print 'unpickling wrapper', dict_
# print 'unpickling wrapper', dict_
self.__dict__.update(dict_)

def save(self, fname, remove_data=False):
Expand Down Expand Up @@ -111,7 +112,8 @@ def wrapper(self, *args, **kwargs):
sig = inspect.signature(func)
formatted = str(sig)

wrapper.__doc__ = "%s%s\n%s" % (func.__name__, formatted, wrapper.__doc__)
doc = dedent(wrapper.__doc__) if wrapper.__doc__ else ''
wrapper.__doc__ = "\n%s%s\n%s" % (func.__name__, formatted, doc)

return wrapper

Expand Down
3 changes: 1 addition & 2 deletions statsmodels/graphics/regressionplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def abline_plot(intercept=None, slope=None, horiz=None, vert=None,
Returns
-------
fig : Figure
Figure
The figure given by `ax.figure` or a new instance.
Examples
Expand All @@ -779,7 +779,6 @@ def abline_plot(intercept=None, slope=None, horiz=None, vert=None,
>>> plt.show()
.. plot:: plots/graphics_regression_abline.py
"""
if ax is not None: # get axis limits first thing, do not change these
x = ax.get_xlim()
Expand Down
31 changes: 22 additions & 9 deletions statsmodels/regression/_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@
# this is similar to ContrastResults after t_test, copied and adjusted
class PredictionResults(object):
"""
Results class for predictions
Results class for predictions.
Parameters
----------
predicted_mean : ndarray
The array containing the prediction means.
var_pred_mean : ndarray
The array of the variance of the prediction means.
var_resid : ndarray
The array of residual variances.
df : int
The degree of freedom used if dist is 't'.
dist : {'norm', 't', object}
Either a string for the normal or t distribution or another object
that exposes a `ppf` method.
row_labels : list[str]
Row labels used in summary frame.
"""

def __init__(self, predicted_mean, var_pred_mean, var_resid,
Expand Down Expand Up @@ -61,7 +77,6 @@ def conf_int(self, obs=False, alpha=0.05):
ci : ndarray, (k_constraints, 2)
The array has the lower and the upper limit of the confidence
interval in the columns.
"""

se = self.se_obs if obs else self.se_mean
Expand All @@ -75,7 +90,7 @@ def summary_frame(self, what='all', alpha=0.05):
# TODO: finish and cleanup
import pandas as pd
from collections import OrderedDict
ci_obs = self.conf_int(alpha=alpha, obs=True) # need to split
ci_obs = self.conf_int(alpha=alpha, obs=True) # need to split
ci_mean = self.conf_int(alpha=alpha, obs=False)
to_include = OrderedDict()
to_include['mean'] = self.predicted_mean
Expand All @@ -86,10 +101,10 @@ def summary_frame(self, what='all', alpha=0.05):
to_include['obs_ci_upper'] = ci_obs[:, 1]

self.table = to_include
#OrderedDict does not work to preserve sequence
# OrderedDict does not work to preserve sequence
# pandas dict does not handle 2d_array
#data = np.column_stack(list(to_include.values()))
#names = ....
# data = np.column_stack(list(to_include.values()))
# names = ....
res = pd.DataFrame(to_include, index=self.row_labels,
columns=to_include.keys())
return res
Expand Down Expand Up @@ -157,11 +172,9 @@ def get_prediction(self, exog=None, transform=True, weights=None,
if weights is not None:
weights = np.asarray(weights)
if (weights.size > 1 and
(weights.ndim != 1 or weights.shape[0] == exog.shape[1])):
(weights.ndim != 1 or weights.shape[0] == exog.shape[1])):
raise ValueError('weights has wrong shape')

### end

if pred_kwds is None:
pred_kwds = {}
predicted_mean = self.model.predict(self.params, exog, **pred_kwds)
Expand Down
Loading

0 comments on commit 1c3ed0f

Please sign in to comment.