Skip to content

BUG: append broken in DynamicFactorMQ if updated months do not span updated quarters #7452

Open

Description

@Mlaouar

Describe the bug

I have a statsmodels.tsa.statespace.mlemodel.MLEResultsWrapper object obtained after fitting a DynamicFactorMQ model. I would like to apply the append method to the result object with a new step of data (adding one month to the monthly endog and one quarter to the quarterly endog ) but i don't seem to find the right index format for my monthly and quarterly endog DataFrames. I always get the following error Given "endog" does not have an index that extends the index of the model.. I should mention that the same happens when using the extend method but the apply works properly.

Code Sample, a copy-pastable example if possible

here is where i call the method

image

here are the monthly and quarterly endog
image
image

here is the original endog data

image

Expected Output

A new result object containing the filtered.

Output of import statsmodels.api as sm; sm.show_versions()

INSTALLED VERSIONS

Python: 3.9.2.final.0

statsmodels

Installed: 0.12.2

Required Dependencies

cython: Not installed
numpy: 1.19.2
scipy: 1.6.2
pandas: 1.2.3
dateutil: 2.8.1
patsy: 0.5.1

Optional Dependencies

matplotlib: 3.3.4
cvxopt: Not installed
joblib: 1.0.1

Developer Tools

IPython: 7.22.0
jinja2: 2.11.3
pygments: 2.8.1
pytest: Not installed
virtualenv: Not installed

Activity

  1. bashtage commented on May 18, 2021

    @bashtage
    Member

    Can you please post copy-and-pastable and runnable code? This is a lot more helpful than screenshots of code.

  2. Mlaouar commented on May 18, 2021

    @Mlaouar
    Author

    Can you please post copy-and-pastable and runnable code? This is a lot more helpful than screenshots of code.

    I'm sorry i've only put screen shots because the code i showed is data dependent, i will try to write something to replicate the error. In the mean time by tracing it through the package i found that the functions append and extend can't handle adding less than 3 months in the monthly endog when adding a quarter, the reason why is in the function construct_endog in the DynamicFactorMQ class. The two frequencies are joined on a monthly basis which skips a month (because the resample function takes end as convention)

  3. bashtage commented on May 18, 2021

    @bashtage
    Member

    Feel free to simulate some data that is like your original data, e.g, same shape.

  4. Mlaouar commented on May 19, 2021

    @Mlaouar
    Author

    I apologize for the delay, here's some code to replicate the error

    import pandas as pd
    import numpy as np
    import statsmodels.api as sm
    
    index_m = pd.period_range(start='2014-02',periods=74,freq='M')
    endog_m = pd.DataFrame(np.random.rand(74,10), index=index_m)
    
    index_q = pd.period_range(start='2014-Q2',periods=24,freq='Q')
    endog_q = pd.DataFrame(np.random.rand(24,5), index=index_q)
    
    model = sm.tsa.DynamicFactorMQ(endog=endog_m, endog_quarterly=endog_q)
    result = model.fit()
    
    
    
    updated_m_index = [endog_m.index.max() + 1]
    updated_endog_m = pd.DataFrame(np.random.rand(1,10), index=updated_m_index)
    
    updated_q_index = [endog_q.index.max() + 1]
    updated_endog_q = pd.DataFrame(np.random.rand(1,5), index=updated_q_index)
    
    result_updated  = result.append(endog=updated_endog_m,
                                    endog_quarterly=updated_endog_q)
    
    

    As you can see the updated monthly and quarterly data's indexes directly extend the original data.

    Much thanks in advance.

  5. ChadFulton commented on May 19, 2021

    @ChadFulton
    Member

    Thanks for posting the replication code and for looking into the cause. This should be straightforward to fix. In the meantime, I think a workaround (you may have already discovered) is to extend updated_endog_m with nan values to the last month of the last quarter given in updated_endog_q.

  6. changed the title [-]Trouble with the append method [/-] [+]BUG: append broken in DynamicFactorMQ if updated months do not span updated quarters[/+] on May 19, 2021
  7. Mlaouar commented on May 19, 2021

    @Mlaouar
    Author

    Thank you for the the response and the workaround tip, i have already tried it and it works greatly.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions