atsa-es / marsstmb Goto Github PK
View Code? Open in Web Editor NEWCompanion package for MARSS that allows you to use TMB to fit MARSS models
Home Page: https://atsa-es.github.io/marssTMB/
Companion package for MARSS that allows you to use TMB to fit MARSS models
Home Page: https://atsa-es.github.io/marssTMB/
Q.diagonal() = Q.diagonal().exp();
https://stackoverflow.com/questions/33834873/modify-eigen-matrix-diagonal. vector diag_exp = Q.diagonal().array().exp();` Q.diagonal().array() = diag.exp;@ericward-noaa When I change .hpp files, the .so and .o files don't get rebuild when I hit 'Install' and rebuild the package the .so and .o files don't rebuild. What am I doing wrong? Thanks!!
I'm fitting a DLM with TMB to pass to tmbstan, but am getting negative values for Q and R matrices.
Here's the data:
weekly_ne_anom.txt
weekly_sst_ne.txt
And here's the code:
B <- diag(2)
U <- matrix(0, 2, 1)
Q <- matrix("0", 2, 2)
diag(Q) <- c("q.alpha", "q.beta")
Z <- array(NA, c(1, 2, 1356))
Z[1, 1, ] <- rep(1, 1356)
Z[1, 2, ] <- na.approx(weekly_sst_ne$anom)
x0 <- matrix(c(0,0), nrow = 2)
model1 <- list(B = B, U = U, Q = Q,
Z = Z, A = matrix(0), R = matrix("r"))
inits_list <- list(x0 = x0)
tmb_ne <- estimate_marss(MARSS(ts(weekly_ne_anom$anom), model = model1, inits = inits_list, fit = F))
This gives me initial values of -1.5 for Q and R diagonals and 0 for everything else. When optimized, the entire Q matrix is negative. If I use estimate_marss2, I get very different answers and a false convergence (8) warning. I'm also not sure why it's estimating the Q off diagonal parameter, as I've set it to zero.
par should be in marss form
> names(m1.em$par)
[1] "Z" "A" "R" "B" "U" "Q" "x0" "V0" "G" "H" "L"
> names(m1.tmb2$par)
[1] "Z" "A" "R" "B" "U" "Q" "x0" "V0" "D" "C" "d" "c" "G" "H" "L"
>
I think one the first tasks is to write a generic TMB function that does:
A %*% x
So rather than writing code for Bx, and Cc, and Dd. We have a function for mult(A, x). Maybe you have something like that already?
Idea is to have maps for all the MARSS parameters and then code to do
x(t) - Bx(t-1) + u + Cc(t) ~ MVN(0, Q)
and
y(t) - Zx(t) + a + Dd(t) = MVN(0, R)
Seems pretty straight forward from Tim's code, but that mult(A, x) function would make the code cleaner.
This is lower with BFGS. Even if. Perhaps has something to do with the NAs in the data. Seems to go away wo the NAs.
library(marssTMB)
U1 <- matrix(0,2,1)
Q1 <- matrix(c("q11","q12","q12","q22"),2,2)
Z1 <- matrix(c(1,0,1,1,1,0),3,2)
A1 <- matrix(list("a1",0,0),3,1)
R1 <- matrix(list("r11",0,0,0,"r",0,0,0,"r"),3,3)
pi1 <- matrix(0,2,1); V1=diag(1,2)
model.list <- list(U=U1, Q=Q1, Z=Z1, A=A1, R=R1, x0=pi1, V0=V1, tinitx=0)
dat <- t(harborSealWA)[2:4, ] # remove the year row
fit1 <- MARSS(dat, model=model.list, method="BFGS")
fit2 <- MARSS(dat, model=model.list, method="TMB", inits=fit1)
dat <- t(harborSealWA); dat <- dat[2:4, ]
modlist = list(U="zero", A="zero", x0="zero", Q="unconstrained")
MARSS(dat[,14:22], method="TMB", silent=TRUE, model=modlist)$logLik
MARSS(dat[,14:22], silent=TRUE, model=modlist)$logLik
maxit not getting set for nlminb
a = MARSS(dat[,14:22], method="TMB", model=modlist, control=list(maxit=20000))
false convergence for nlminb but seems ok
modlist = list(U="zero", A="zero", Q="unconstrained", tinitx=1)
a = MARSS(dat[,14:22], method="TMB", model=modlist)
a = MARSS(dat[,14:22], method="BFGS_TMB", model=modlist)
a = MARSS(dat[,14:22], model=modlist)
false convergence for nlminb and no convergence at 5000+ iter for optim,
modlist = list(U="zero", A="zero", Q="unconstrained", x0="zero", tinitx=1)
a = MARSS(dat[,14:22], method="TMB", model=modlist) # reports false convergence but better LL than EM
a = MARSS(dat[,14:22], method="BFGS_TMB", model=modlist) # not remotely converged
a = MARSS(dat[,14:22], method="BFGS", model=modlist) # bad
a = MARSS(dat[,14:22], model=modlist)
a = MARSS(dat[,14:22], method="BFGS", model=modlist, inits=a) # good same as TMB
false/no convergence but LL looks ok
modlist = list(U="zero", A="zero", Q="unconstrained")
a = MARSS(dat[,14:22], method="TMB", model=modlist)
a = MARSS(dat[,14:22], method="BFGS_TMB", model=modlist)
a = MARSS(dat[,14:22], model=modlist)
Not handling convergence messages from nlminb
Running into numerical problems but not really reporting that.
Should put in the option to compute the chol of the var-cov matrix the way I did for MARSSoptim(). Something is off with the separation method.
library(MARSS)
data(lakeWAplankton, package = "MARSS")
phytoplankton <- c("Cryptomonas", "Diatoms", "Greens", "Unicells", "Other.algae")
dat <- as.data.frame(lakeWAplanktonTrans) |>
subset(Year >= 1980 & Year <= 1989) |>
subset(select=phytoplankton) |>
t() |>
MARSS::zscore()
modlist = list(U="zero", A="zero", Q="unconstrained")
a = MARSS(dat[,14:22], method="BFGS_TMB", model=modlist, control=list(trace=1))
LL much lower than
a = MARSS(dat[,14:22], method="BFGS_TMB")
also LL doesn't match. The LL output is from the KF but within the opt output is the LL coming from TMB. Should match.
a$iter.record$opt.output$value
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.