helske / KFAS

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

Problems with standardized residuals #52

Closed Vera1996 closed 3 years ago

Vera1996 commented 3 years ago

Dear Jouni,

the output of the function rstandard() produces NA values for the first 19 standardized residuals. My script is attached. Is there a solution to this? Thanks a lot!

Standardized residuals .R.zip

helske commented 3 years ago

Sorry, I cannot run your code as I don't have access to your variables.

But I'm pretty sure this is due to diffuse initialization which leads to NA values in the residuals until the diffuse phase has ended. (as the variance F is in some sense not really defined). You can switch to nondiffuse initialization (by defining P1 and setting P1inf to zero when building your model) if you need residuals for all time points and it otherwise makes sense to your model.

Vera1996 commented 3 years ago

Oke thanks for your answer! Where do I need to define the P1 and P1inf initialization in the code below? When I put the initialization into the SSM regression, my Kalman output coefficient is the same for every variable When I put the initialization into the SSMtrend I get an error. I hope you can help me. Thanks a lot!

model <- SSModel(covid_new[, 1:4] ~ SSMtrend(2, Q = list(matrix(NA, 4, 4), matrix(NA, 4, 4))) + SSMregression(list(~lag_9$Germany, ~lag_9$Netherlands, ~lag_9$Spain, ~lag_9$Sweden)) , H = matrix(NA, 4, 4))

helske commented 3 years ago

Sorry, missed this earlier. If you want to initialize the states corresponding to the SSMtrend component, you but P1 and/or P1inf inside SSMtrend, and similarly for SSMregression. You can check that the final P1 and P1inf are like you wanted them to be by model$P1 and model$P1inf (you can also modify them later by assigning to those components, but you need to be careful not to change the dimensions of the matrices)