I have some data that varies with time and some that stays constant (e.g., location, race stay constant). Is it possible to implement a mixed time-varying coefficient model in python? What I mean is:
$$
y = beta_0(t) cdot x_0 + beta_1(t) cdot x_1 + beta_2 cdot x_3 + beta_3
$$
where $beta_0, beta_1$ are time-varying (dependent on $t$), and the rest of the betas are not.
This is similar to the SAS package TVEM (page 7, eq. 4 of this doc).
It seems this might be possible in R using the gam
models, but I'm not very familiar with R. Any clues to approach this in python (or get me started) would be helpful. I'm familiar with the statsmodels
package and the scipy
stack.
Best Answer
Welcome to Cross Validated!
One way to fit such a model is to use Kalman filter. It is not an out-of-the-box experience, but it works. I used pykalman
package, which is very nice, but is no longer maintained (or it seems so from it's github repository). I saw that statsmodels
also has a Kalman filter implementation, it may do the job as well.
To fit the time varying coefficients (using pykalman
notation from https://pykalman.github.io/#mathematical-formulation), put your $beta_i$'s into $A_t$ and adjust their change rate by choosing appropriate value of $Q$. You may want to use diagonal $Q$ with zeroes for fixed coefficients, and positive values for larger ones. You can use MLE (or grid search, or random search), to find the best ones.
Values of $x_i$ go into $C_t$ and values of $y$ into $y_t$.
Similar Posts:
- Solved – Interpretation of Cox regression with time varying covariates
- Solved – Schoenfeld residual test for model with time varying coefficients
- Solved – Why does a AR(1) model that’s mean reverting revert back to B0 as opposed to B0 + B1*B0
- Solved – Can Cox models be used with time-varying covariates
- Solved – How to check if the effect from independent variable is changing over time