Skip to content

BUG: offset in truncated model raises valueerror #8842

Closed

Description

@josef-pkt

using offset in TruncatedLFPoisson raises
ValueError: offset is not the same length as endog

same should be in other truncated models and with exposure

        self.model_main = Poisson(self.endog, self.exog,
                                  exposure=exposure,
                                  offset=offset)

should use self.exposure and self.offset which are the reduced arrays of nontruncated observations.

The truncated countmodels do not require that we get only nontruncated observations in endog.

Activity

  1. added this to the 0.14 milestone on Apr 26, 2023
  2. josef-pkt commented on Apr 26, 2023

    @josef-pkt
    MemberAuthor

    no "offset" or "exposure" in test_truncated_model.py"

  3. josef-pkt commented on Apr 26, 2023

    @josef-pkt
    MemberAuthor

    actually the ValueError is good, The truncated models do not support offset, exposure in loglike and score.
    parameter estimates are wrong

    In standalone usage (without hurdle), users could supply data without truncated observations.
    In that case offset and exposure would have the correct size and no error would be raised.

  4. josef-pkt commented on Apr 26, 2023

    @josef-pkt
    MemberAuthor

    similar:
    HurdleCountModel has offset and exposure arguments, but they are not used at all, they are not transmitted to the submodels.
    similar for _RCensored, offset is argument but not transmitted to model_main, Poisson, NBP, GPP

    API design:
    currently exog is the same in both submodels.
    When we support offset and exposure, then internal consistency would require that those also apply to both models.
    However, then we have an ambiguity with separate exog and offsets when offset_zero = None.

    Is the None default, "use the same offset in both submodels", or does it mean "no offset in the zero model".

    We could make it conditional on if exog_zero is None. If true, then same exog, offset, exposure applies to same model.
    If false, then offset and exposure are treated independently of main model, i.e. offset_zero = None means offset_zero=0.

  5. josef-pkt commented on Apr 26, 2023

    @josef-pkt
    MemberAuthor

    related: ZeroInflation agreement is low comparing with offset=ones
    (reduces const estimate, but leaves slope parameters unchanged)

    other discrete models have much better agreement.

     ZeroInflatedPoisson
        [-0.7942453  -0.55652403  1.12264903  0.57558373] [-0.75 -0.5   1.    0.5 ] True
        [-0.79427785 -1.55657337  1.1227167   0.57563021] [-0.75 -0.5   1.    0.5 ] True
        diff offset [ 3.25538043e-05 -6.76708261e-05 -4.64791426e-05] [4.93408885e-05]
    

    Is this a bug with small effect, a loss in precision, or a too large convergence tolerance?

    ZIPoisson with nonzero offset has unit tests against Stata.
    and I don't see any problems when reading the code.

    update
    It's convergence tolerance.
    When I make gtol (for default bfgs) more strict, params agree at around 1e-10 or 1e-11

    res = model(y2, x, **kwds).fit(gtol=1e-8, disp=disp)
    res_offset = model(y2, x, offset=np.ones(nobs), **kwds).fit(gtol=1e-8, disp=disp)
    

    Also if I switch to method="newton" (which is default for basic discrete models) agreement of params is at around 1e-12

  6. josef-pkt commented on Apr 27, 2023

    @josef-pkt
    MemberAuthor

    If I mess up CountModel offset if not None, then the following unit tests fail.
    All models (*) that don't show up here, do not have unit tests for offset.
    (*) subclasses of CountModel that use self.offset from superclass in __init__ (some might still store offset directly)

    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained1a::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained1a::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained1b::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained1b::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained1c::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained1c::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonNoConstrained::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonNoConstrained::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained2a::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained2a::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained2b::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained2b::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained2c::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestPoissonConstrained2c::test_basic_method
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_constrained.py::TestGLMPoissonConstrained1b::test_compare_glm_poisson
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_count_model.py::TestZeroInflatedModel_offset::test_params
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_count_model.py::TestZeroInflatedModel_offset::test_conf_int
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_sandwich_cov.py::TestNegbinCluExposure::test_basic
    FAILED ..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\discrete\tests\test_sandwich_cov.py::TestNegbinCluExposureFit::test_basic
    
  7. added
    type-bug-wrong
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-discreteprio-hightype-bugtype-bug-wrong

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions