bashtage / linearmodels

Additional linear models including instrumental variable and panel data models that are missing from statsmodels.

Home Page:https://bashtage.github.io/linearmodels/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KeyError for PanelOLS from_formula example

eng543 opened this issue · comments

Attempting to run the example in documentation after seeing unexpected KeyError for my own application.

I am running:

import linearmodels as lm
from linearmodels.panel import generate_panel_data
panel_data = generate_panel_data().data
mod = lm.PanelOLS.from_formula('y ~ 1 + x1 + EntityEffects', panel_data)

and receiving the following error:

KeyError                                  Traceback (most recent call last)
/var/folders/ty/5h56jw1538522fcq6kz4zwj80000gn/T/ipykernel_145/1280086150.py in <module>
      1 model = lm.PanelOLS.from_formula(
----> 2     'session_end_count ~ treated', dat_snap_sub_agg
      3 )

~/Documents/data-science/growth/growth_exploration_2022q2/.pyenv/lib/python3.7/site-packages/linearmodels/panel/model.py in from_formula(cls, formula, data, weights, other_effects, singletons, drop_absorbed, check_rank)
   1373         >>> res = mod.fit(cov_type='clustered', cluster_entity=True)
   1374         """
-> 1375         parser = PanelFormulaParser(formula, data)
   1376         entity_effect = parser.entity_effect
   1377         time_effect = parser.time_effect

~/Documents/data-science/growth/growth_exploration_2022q2/.pyenv/lib/python3.7/site-packages/linearmodels/panel/model.py in __init__(self, formula, data, context)
    148         self._context = context
    149         self._dependent = self._exog = None
--> 150         self._parse()
    151 
    152     def _parse(self) -> None:

~/Documents/data-science/growth/growth_exploration_2022q2/.pyenv/lib/python3.7/site-packages/linearmodels/panel/model.py in _parse(self)
    157         rm_list = []
    158         effects = {"EntityEffects": False, "FixedEffects": False, "TimeEffects": False}
--> 159         for term in formula.terms[1]:
    160             if str(term) in effects:
    161                 effects[str(term)] = True

~/Documents/data-science/growth/growth_exploration_2022q2/.pyenv/lib/python3.7/site-packages/formulaic/parser/types/structured.py in __getitem__(self, key)
    153         if isinstance(key, int) and self._has_root and isinstance(self.root, tuple):
    154             return self.root[key]
--> 155         raise KeyError(key)
    156 
    157     def __iter__(self) -> Generator[Union[ItemType, Structured[ItemType]]]:

KeyError: 1

Is there a bug in this method or have I made some subtle error?

Not sure what is going on here. You should probably upgrade formulaic and make sure you have the latest linearmodels. I can't reproduce using master.

Please continue to post if you have issues. Closing since I can't reproduce.