statsmodels / statsmodels

Statsmodels: statistical modeling and econometrics in Python
http://www.statsmodels.org/devel/
BSD 3-Clause "New" or "Revised" License
10.19k stars 2.98k forks source link

ENH/Design: support for spline-dummy interaction in penalized/GAM #5332

Open josef-pkt opened 6 years ago

josef-pkt commented 6 years ago

R mgcv has a by option for the interaction effect of penalized splines and a categorical variables.

I don't know yet how we will support this. patsy can create the interaction term using its splines.

two cases (that I can think of)

I think the former should be easy using standard interaction (like kronecker product for unbalanced dummies). The latter will be more difficult because the spline cannot be estimated in the unobserved part. I have a simple example without penalization in the causal impact, synthetic control PR #3647, or in one of the related experimental notebooks.

josef-pkt commented 6 years ago

(random thoughts)

using interaction effect with dummy directly, i.e. we create two variables z1 = dummy * x and z0 = (1 - dummy) * x

this means part of the new variable will be zeros. We cannot include those zeros when we create the spline basis, i.e. we would need to have a ignore_zeros option.

We can do it if we use the spline basis for the x, in that case we just compute a usual interaction variable on parts of exog.

The two cases above would need 2 options for knot selection when creating the spline basis

The second two might be applicable to regression continuity design and structural change with jump points.

A corresponding option is on penalization or smoothing parameter choice

mgcv allows the common alpha case in the options for GAM, i.e. add an index that maps from alpha list to the list of smooth terms.

related: varying coefficient models which are essentially also just interaction effects. I have not looked at those yet.

two possible example:

one alternative in the structural change, regression discontinuity case: We could add only a mean shift dummy. If we use a local spline basis like B-splines with drop one basis column for removing intercept, then the shape of the curves (including the mean shift variable) in the two segments would be mostly independent of each other, i.e. restricted to common penalization and spill over across knots in transition segment.

josef-pkt commented 5 years ago

just another example: cyclical time series, e.g.

Our current implementation cannot construct spline bases ignoring some of the observations, e.g. the zero observation. Our current implementation cannot reuse and shift splines, e.g. with equal time interval the time variable is the same for all subunits and we could just shift and transpose it instead of constructing in each time separately (scipy.signal did and maybe still does that)

ideas