Closed JonBrommer closed 6 months ago
That log-likelihood value is -.Machine$double.xmax^0.75
i.e. basically -infinity. This is bevause you have NA value in Qt:
is.SSModel(model_nb, na.check=TRUE, return.logical=FALSE)
Error in is.SSModel(model_nb, na.check = TRUE, return.logical = FALSE) :
System matrices (excluding Z) contain NA or infinite values, covariance matrices contain values larger than 1e+07
Right. For the Gaussian SSM the logL is returned when fitting the model, but not for non-Gaussian. A sort of work around (continuing from the previous example) would be to estimate Qt and re-run the model with that, which produces log likelihood values as Qt != NA . Does that make sense (for this simple case) or a dumb idea?
#use fitted parameters to infer logL
#--poisson
fit_poisson <- fitSSM(model_poisson, inits = c(0, 0), method = "BFGS")
out_poisson <- KFS(fit_poisson$model)
#--nb
fit_nb <- fitSSM(model_nb, inits = c(0, 0), method = "BFGS")
out_nb <- KFS(fit_nb$model)
#re-run model with estimated parameters
model_poisson <- SSModel(y ~ -1 +
SSMcustom(Z = Zt, T = Tt, R = Rt, Q = out_poisson$model$Q, P1inf = P1inf),
distribution = "negative binomial", u = n.sites)
model_nb <- SSModel(y ~ -1 +
SSMcustom(Z = Zt, T = Tt, R = Rt, Q = out_nb$model$Q, P1inf = P1inf),
distribution = "negative binomial", u = n.sites)
logLik(model_nb)-logLik(model_poisson)
What do you mean that the log-likelihood is not returned for non-Gaussian? From the fitSSM
the final log-likelihood value is in -fit_poisson$optim.out$value
, or you can call logLik(fit_poisson$model)
as you do with logLik(fit_poisson$model)
. So there's no need to recreate the model with SSModel
as it is already in fit_poisson$model
.
Thanks for your reply. I got confused because for the gaussian case the $model is not needed to get the logL. Below example that illustrates the different calls to get the logL in gaussian and poisson. Much obliged
data("alcohol")
deaths <- window(alcohol[, 2], end = 2007)
population <- window(alcohol[, 6], end = 2007)
Zt <- matrix(c(1, 0), 1, 2)
Ht <- matrix(NA)
Tt <- matrix(c(1, 0, 1, 1), 2, 2)
Rt <- matrix(c(1, 0), 2, 1)
Qt <- matrix(NA)
a1 <- matrix(0, 2, 1)
P1 <- matrix(0, 2, 2)
P1inf <- diag(2)
model_gaussian <- SSModel(deaths / population ~ -1 +
+ SSMcustom(Z = Zt, T = Tt, R = Rt, Q = Qt, a1 = a1, P1 = P1,
+ P1inf = P1inf), H = Ht)
model_poisson <- SSModel(deaths ~ -1 +
SSMcustom(Z = Zt, T = Tt, R = Rt, Q = Qt, P1inf = P1inf),
distribution = "poisson", u = population)
fit_gaussian <- fitSSM(model_gaussian, inits = c(0, 0), method = "BFGS")
fit_poisson <- fitSSM(model_poisson, inits = c(0, 0), method = "BFGS")
logLik(fit_gaussian) #returns logL
logLik(fit_poisson) #producs error
logLik(fit_poisson$model) #slightly different syntax, returns logL
logLik(fit_gaussian$model) #works also
Hmm, that is strange, if I run your code I get an error from logLik(fit_gaussian)
as well as I would expect as fit_gaussian
is just a list for which there is no logLik
method.
You are right. I started from clean slate and I now understand the correct call to logLik()
includes the fitted model $model
. Thanks again for clarification
Hi
I find KFAS returns the same log-likelihood for different SSModel objects applied to the same data but assuming different observation processes. I think below is a reproducible example that illustrates the issue.