Skip to content

i() does not accept two categorical terms #921

@DanSimonet

Description

@DanSimonet

In R fixest I am able to interact two factors while setting the referent for one.

library(fixest)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)

m1 <- feols(
  mpg ~ i(cyl, gear, ref = 4),
  data = mtcars
)

etable(m1)

Result appears as follows with cycle = 4 set as referent for comparison to the combined groupings.

Coefficient Estimate (Std. Error)
Constant 26.66*** (1.026)
cyl = 6 × gear = 3 –6.914* (2.616)
cyl = 6 × gear = 4 –6.914** (1.987)
cyl = 6 × gear = 5 –6.964. (3.554)
cyl = 8 × gear = 3 –11.61*** (1.420)
cyl = 8 × gear = 5 –11.26*** (2.616)

However, when running the same syntax in pyfixest

import pyfixest as pf
import statsmodels.api as sm
mtcars = sm.datasets.get_rdataset("mtcars", "datasets").data

mtcars["cyl"]  = mtcars["cyl"].astype("category")
mtcars["gear"] = mtcars["gear"].astype("category")

m1 = pf.feols(
    fml = "mpg ~ i(cyl, gear, ref = 4)", 
    data = mtcars
)

pf.etable(m1, cat_template="{variable}-{value}")

I continue to get the following error even if cyl is category and gear is numeric.

ValueError: 
                        The second variable in the i() syntax cannot be of type "category" or "object", but
                        but it is of type category.

I have seen something akin to this issue referenced in the change log and would love a possible solution to reproduce fixest in its entirety. We make heavy use of the i() functionality for a variety of group comparisons and would like to port over to pyfixest to share pythonic versions with colleagues.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfeatureA new feature for PyFixest

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions