Skip to content

ENH Betareg rebased2 - #7282

Closed
josef-pkt wants to merge 25 commits into
statsmodels:mainfrom
josef-pkt:Betareg_rebased2
Closed

josef-pkt wants to merge 25 commits into
statsmodels:mainfrom
josef-pkt:Betareg_rebased2

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Jan 25, 2021 •

Copy link
Copy Markdown
Member

rebased version of #4238
no changes except skipped 2nd commit additing csv file to package data in setup.py

no test failures locally, but many warnings. including pandas deprecation warnings

@pep8speaks

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 27:1: F401 'statsmodels.genmod.families.Binomial' imported but unused
Line 196:48: E701 multiple statements on one line (colon)
Line 197:47: E701 multiple statements on one line (colon)
Line 453:5: F811 redefinition of unused 'patsy' from line 21

Line 4:1: F401 'sys' imported but unused
Line 7:1: E402 module level import not at top of file
Line 8:1: E402 module level import not at top of file
Line 9:1: E402 module level import not at top of file
Line 10:1: E402 module level import not at top of file
Line 11:1: E402 module level import not at top of file
Line 40:80: W605 invalid escape sequence '\s'
Line 41:90: W605 invalid escape sequence '\s'
Line 43:90: W605 invalid escape sequence '\s'
Line 44:100: W605 invalid escape sequence '\s'

@josef-pkt

josef-pkt commented Jan 25, 2021 •

Copy link
Copy Markdown
Member Author

I get these 3 types of warnings (reformatted)

...\statsmodels_py37\statsmodels\statsmodels\miscmodels\tests\test_beta.py:40
  ...\statsmodels_py37\statsmodels\statsmodels\miscmodels\tests\test_beta.py:40: 
DeprecationWarning: invalid escape sequence \s
    expected_income_mean = pd.read_table(io.StringIO(_income_estimates_mean), sep="\s+")
...\statsmodels_py37\statsmodels\statsmodels\miscmodels\tests\test_beta.py:40
  ...\statsmodels_py37\statsmodels\statsmodels\miscmodels\tests\test_beta.py:40: 
FutureWarning: read_table is deprecated, use read_csv instead.
    expected_income_mean = pd.read_table(io.StringIO(_income_estimates_mean), sep="\s+")
statsmodels/miscmodels/tests/test_beta.py::TestBeta::test_scores
  ...\statsmodels_py37\statsmodels\statsmodels\base\model.py:2503: 
UserWarning: df_resid differs from nobs - nparams
    warnings.warn("df_resid differs from nobs - nparams")

@lgtm-com

lgtm-com Bot commented Jan 25, 2021

Copy link
Copy Markdown

This pull request introduces 3 alerts when merging 42ecff4 into b08825a - view on LGTM.com

new alerts:

  • 1 for Module is imported more than once
  • 1 for Unused local variable
  • 1 for Unused import

@josef-pkt

Copy link
Copy Markdown
Member Author

unit tests for this are still green, pep-8 failures

one statespace test failure on WindowsTest python39_win_latest
one exception still with scipy, that will be fixed on scipy side

@josef-pkt josef-pkt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hessian is not used, and therefore not unit tested
Is it working correctly?

varname Estimate StdError zvalue Pr(>|z|)
(Intercept) -0.62254806 0.223853539 -2.781051 5.418326e-03
income -0.01229884 0.003035585 -4.051556 5.087819e-05
persons 0.11846210 0.035340667 3.352005 8.022853e-04"""

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to strip "***" to get column converted to numeric
I don't know if pandas handles scientific format like 5.087819e-05 in csv

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit tests seem to work with stars ?

age -0.03471 0.03276 -1.059 0.289"""


expected_income_mean = pd.read_table(io.StringIO(_income_estimates_mean), sep="\s+")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_table -> read_csv

>>> from sm.families.links import identity
>>> Z = patsy.dmatrix('~ temp', dat, return_type='dataframe')
>>> mod = Beta.from_formula('iyield ~ C(batch, Treatment(10)) + temp',
... dat, exog_precision=Z, link_precision=identity())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add example when exog_precision is defined by formula

usage for it is not clear, from_formula uses exog_precision_formula keyword


@classmethod
def from_formula(cls, formula, data, exog_precision_formula=None,
*args, **kwargs):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check usage, API with and without formula for precision part

beta = (1 - mu) * phi

if np.any(alpha <= np.finfo(float).eps): return np.array(-np.inf)
if np.any(beta <= np.finfo(float).eps): return np.array(-np.inf)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this in place of a clip?

both mu and phi are positive with default links
I guess this only handles the case >=0 and <= eps, essentially =0.


d1 = np.dot(sf[:, 0], self.exog)
d2 = np.dot(sf[:, 1], self.exog_precision)
return np.concatenate((d1, d2))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

mut = digamma((1 - mu) * phi) - digamma(phi)

t = 1. / self.link.deriv(mu)
h = 1. / self.link_precision.deriv(phi)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this less "backwards", (i.e. I prefer inverse links)
use link derive_inverse(linpred)

@josef-pkt

Copy link
Copy Markdown
Member Author

API issues/questions

  • from_formula, specify exog_precision
  • predict should have options "what"

aside: it looks like ZeroInflated models don't allow for formula for exog_infl part, does not override inherited from_formula

get_distribution looks good, needs checking for consistent naming and interface with other models

refactor: this should inherit from something like the top discrete model instead of GenericLikelihoodModel
final location othermod
see #7187

@josef-pkt

josef-pkt commented Jan 27, 2021 •

Copy link
Copy Markdown
Member Author

I was trying out the 2 test case examples in a notebook.

params and bse agree with the R base on numbers in unit test reasonably well.

df_resid looks wrong, it looks like it doesn't take precision parameters into account. We also get a UserWarning for that.
But if I try to adjust df for bse then bse doesn't match R results anymore.
df_resid might be a generic problem (in GenericLikelihoodModel code). Beta model and results class are not defining anything directly for df_xxx

llf and other results statistic are not verified.
This still needs full unit tests for extras against R and Stata, and possibly figure out differences in methods or definitions and small sample corrections.

I guess around 2 weeks of work.

@josef-pkt

Copy link
Copy Markdown
Member Author

offset option is missing
also no code for llnull

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants