helske / KFAS

KFAS: R Package for Exponential Family State Space Models
64 stars 17 forks source link

How to extract time varying coefficients and CIs corresponding to each variable in SSMregression #73

Closed ghost closed 2 years ago

ghost commented 2 years ago

Thank you very much for creating "KFAS" which is very useful. I want to ask you how to extract the coefficients for each variable used in the regression

For example, the model is as below:
regModel <- SSModel(Y ~ SSMtrend(degree = 1, Q = NA) + SSMregression( ~ x1 + x2, Q = diag(rep(NA, 2))) , and fit the model as following: fitReg <- fitSSM(regModel, inits = c(rep(1, 3))) , then how can I extract the coefficient and CI, for example, of variable x1?

I suppose that I should use the function "predict" for this purpose, but I'm not sure how to specify "states" argument to get the confidence interval of the coefficients of x1. (I tried numbers 1, 2, 3, ....for argument "states" as I show below, and the results are returned for 1~3 but I'm not sure which number for "states" corresponds to what coefficient or residual)

CI_beta1 <- predict(fitReg$model, states =1, interval = "confidence", level = 0.95)

helske commented 2 years ago

The numbering of the states is the same as the one returned by regModel$a1. You can also get the confidence intervals manually for each state by extracting the corresponding diagonal element from KFS(regModel)$V which contains the covariance matrix of the smoothed estimates and then using normal CI equations, e.g., alphahat[,1] +- 1.96.*sqrt(V[1,1,])

ghost commented 2 years ago

Thank you very much for your support, now I understand how to get time-varying CIs of coefficients.