helske / KFAS

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

adding level shift without using SSMregression and dealing with an error #53

Closed suprajamalladi closed 3 years ago

suprajamalladi commented 3 years ago

Hi! I have the following questions: 1)is there a difference in the model when the level shift is introduced with or without SSMregression in an SSModel? 2)is a level shift added the same way in SSModel and SSMarima? (i have added the codes for both and want to know if they are correct?) 3) How to deal with an error like below?

Error in is.SSModel(do.call(updatefn, args = c(list(inits, model), update_args)), : System matrices (excluding Z) contain NA or infinite values, covariance matrices contain values larger than 1e+07

i got the above error using the code for structural model below:

the following is my code for reference:

structural model

cp<-matrix(c(rep(0,100),rep(1,100)),200,1) model1 <-SSModel(data.1~SSMtrend(2,Q=list(NA,NA))+ SSMseasonal(period=12,sea.type='dummy',Q=NA)+ cp,data=data.1,H=NA) ownupdatefn1 <- function(pars,model1,...){ model1$H[] <- exp(pars[1]) diag(model1$Q[,,1])[1:3]<- exp(c(pars[2],pars[3],pars[4])) model1 }

kfasfit1 <-fitSSM(inits=rep(log(var(data.1)),4), model=model1, updatefn=ownupdatefn1,method='BFGS')

outk1 <- KFS(kfasfit1$model,filtering=c('state'), smoothing=c('state','disturbance','mean'))

AR(1) model with level shift:

model3<-SSModel(y1t~-1+cp+SSMarima(ar=c(0.8),Q=1e-7,d=0),H=1e-7,y1t)

fun3<-function(pars,model,...){ model3$T[2,2,1] <-exp(pars[1]) model3$Q[,,1]<- exp(pars[2]) model3 }

KFASfit3<-fitSSM(inits=c(0,2),model3,fun3,method='BFGS',hessian=T)

out_arima <- KFS(KFASfit3$model,filtering=c('state'), smoothing=c('state','disturbance','mean'))

helske commented 3 years ago

1) SSMregression can be used for more complex covariates, e.g., with time-varying regression coefficient, but for simple level shift it does not matter whether you use y ~ shift or y ~ SSMregression(~ shift). 2) SSMarima is like SSMseasonal, SSMregressio etc i.e. an additional (independent) component to the model, so it does matter in terms of whether you add level shift or not. 3) I can't run your code as the code contains undefined variables, but the error means that there are some undefined variables or huge values in your model. This is typically because you have defined some values as NA which are not updated in your model updating function, or your initial values for fitSSM lead to infinite values.

suprajamalladi commented 3 years ago

Thank you so much for your response!