You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently building a wrapper around MARSS to fit a state space model with non-linear constraints. I am wanting to make use of the MARSS Kalman Smoother, so I am fitting a MARSS model with all fixed inputs and then just calling MARSSkf. All is going well, but I'm seeing some behavior that doesn't make sense to me and I was hoping someone could help me understand what is happening.
As I understand it, when fitting a model MARSS iterates between the E and M steps (assuming safe = FALSE here). So if I fit a MARSS model, and then take those fit EM fit parameters feed them in a fixed model back into MARSSkf, I should get the same (or very close) smoothed states and likelihood as the original fit model, right? Here is an example where I do just that, but I get back out a MUCH worse likelihood and the states don't seem to perform as well. How is that possible? Is it something to do with the way that the likelihood function is defined in KFAS?
#extract the fit parameters - there must be a cleaner way to get the R matrix but I wasn't sure how?
Z_est = fit.sim1$par$Z
RR = fit.sim1$par$R
R_est = matrix(0,np, np)
j = 1
for (i in 1:25){
k = j+25-i
R_est[,i] = c(rep(0,i-1), RR[j:k])
j = j+25-i+1
}
Refit a completely fixed model to get KS output and likelihood
aa <- "zero" ## 'aa' is the offset/scaling
DD <- "zero" # matrix(0,mm,1) ## 'DD' and 'd' are for covariates
dd <- "zero" # matrix(0,1,wk_last)
BB <- "identity" # diag(mm) ## 'BB' is identity: 1's along the diagonal & 0's elsewhere
uu <- "zero" # matrix(0, mm, 1) ## 'uu' is a column vector of 0's
CC <- "zero" # matrix(0, mm, 1) ## 'CC' and 'cc' are for covariates
cc <- "zero" # matrix(0, 1, wk_last)
QQ <- "identity" # diag(mm) ## 'QQ' is identity
mod_list <- list(Z = Z_est, A = aa, D = DD, d = dd, R = R_est, B = BB,
U = uu, C = CC, c = cc, Q = 'identity', x0 = 'zero', V0= 'identity')
create model object and "fit" using pre-fit params
newfit <- MARSS(Y, model = mod_list, control = con_list, fit = TRUE, silent = TRUE) # gives much worse loglike and different states
#or alternatively,
newfit <- MARSS(Y, model = mod_list, control = con_list, fit = FALSE, silent = TRUE)
newkf = MARSSkfas(newfit)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am currently building a wrapper around MARSS to fit a state space model with non-linear constraints. I am wanting to make use of the MARSS Kalman Smoother, so I am fitting a MARSS model with all fixed inputs and then just calling MARSSkf. All is going well, but I'm seeing some behavior that doesn't make sense to me and I was hoping someone could help me understand what is happening.
As I understand it, when fitting a model MARSS iterates between the E and M steps (assuming safe = FALSE here). So if I fit a MARSS model, and then take those fit EM fit parameters feed them in a fixed model back into MARSSkf, I should get the same (or very close) smoothed states and likelihood as the original fit model, right? Here is an example where I do just that, but I get back out a MUCH worse likelihood and the states don't seem to perform as well. How is that possible? Is it something to do with the way that the likelihood function is defined in KFAS?
Thank you in advance!
Fit a marss model
con_list <- list(maxit = 5000, allow.degen = FALSE, conv.test.slope.tol=1000, safe = FALSE)
ZZ = runif(1)
RR = runif(1)
inits = list(Z=ZZ, R=RR)
fit.sim1 <- MARSS(Y, model = list(m=1, R = 'unconstrained', x0 = 'zero', V0 = 'identity'), form="dfa", method = "kem", inits = inits, control = con_list)
#extract the fit parameters - there must be a cleaner way to get the R matrix but I wasn't sure how?
Z_est = fit.sim1$par$Z
RR = fit.sim1$par$R
R_est = matrix(0,np, np)
j = 1
for (i in 1:25){
k = j+25-i
R_est[,i] = c(rep(0,i-1), RR[j:k])
j = j+25-i+1
}
R_est[upper.tri(R_est)] <- t(R_est)[upper.tri(R_est)]
Refit a completely fixed model to get KS output and likelihood
aa <- "zero" ## 'aa' is the offset/scaling
DD <- "zero" # matrix(0,mm,1) ## 'DD' and 'd' are for covariates
dd <- "zero" # matrix(0,1,wk_last)
BB <- "identity" # diag(mm) ## 'BB' is identity: 1's along the diagonal & 0's elsewhere
uu <- "zero" # matrix(0, mm, 1) ## 'uu' is a column vector of 0's
CC <- "zero" # matrix(0, mm, 1) ## 'CC' and 'cc' are for covariates
cc <- "zero" # matrix(0, 1, wk_last)
QQ <- "identity" # diag(mm) ## 'QQ' is identity
mod_list <- list(Z = Z_est, A = aa, D = DD, d = dd, R = R_est, B = BB,
U = uu, C = CC, c = cc, Q = 'identity', x0 = 'zero', V0= 'identity')
create model object and "fit" using pre-fit params
newfit <- MARSS(Y, model = mod_list, control = con_list, fit = TRUE, silent = TRUE) # gives much worse loglike and different states
#or alternatively,
newfit <- MARSS(Y, model = mod_list, control = con_list, fit = FALSE, silent = TRUE)
newkf = MARSSkfas(newfit)
Beta Was this translation helpful? Give feedback.
All reactions