helske / KFAS

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

Estimating the dispersion parameter for the negative binomial #20

Closed andrewbcooper closed 7 years ago

andrewbcooper commented 7 years ago

Hello! I've been exploring KFAS for a few days now, and it's simply amazing! Thank you for creating it! One thing I have yet to figure out how to do is estimate the dispersion parameter for the negative binomial. u(t) appears to be set to 1 when I fit the model with fitSSM. For now, I have:

model2 <- SSModel(TERMS_ts ~ SSMseasonal(period=12,Q=NA,sea.type = c("dummy")) + SSMtrend(degree=2,Q = list(matrix(NA), matrix(NA))),distribution="negative binomial")

fit_silly2 <- fitSSM(model2,inits=c(rep(0,13)))

When I extract fit_silly2$model$u, it's just a time-series of 1s.

Is there a way to have the model estimate the dispersion parameter as well? My understanding is that I need this in order to estimate a function (e.g., sum of the observations from the most recent 4 time points) of the prediction values (e.g., apply the negative binomial observation model to the signals produced from the importanceSSM())

Thank you for your help!

helske commented 7 years ago

By default, the fitSSM function estimates only unknown parameters in Q and H (in Gaussian case), and in general you need to provide your own function which updates the model given the parameters. So something like this:

updatefn <- function(parameters, model) {
  model$Q[1,1,1] <- exp(parameters[1])
  model$Q[2,2,1] <- exp(parameters[2])
  model$Q[3,3,1] <- exp(parameters[3])
  model$u[] <- exp(parameters[4])
  model
}
fit <- fitSSM(model, updatefn=update, inits=c(0,0,0,0))
andrewbcooper commented 7 years ago

Thank you so much! I think the difference between a "state" (e.g., level, slope, 11 seasonal dummy variables) and a "parameter" was confusing me. Thanks again!

helske commented 7 years ago

Yeah, the states are estimated automatically by Kalman filter and smoother, whereas you need to use numerical optimization for the parameters.