How to analysis multiple measurements of the same process with MARSS package? I'm trying to estimate the parameters for A multivariate time series using the MARSS package in R. The default model in MARSS is
$$x_t = B x_{t-1} + w_t$$
$$y_t = Zx_t + v_t$$.
The problem is that, instead of having the continuous measurement $y_t$ for $t\in(0,1000)$ , I only have continuous observation from two measuring sessions, that is, $t\in(0,400)$ and $t\in(600,1000)$. This means that I can not concatenate the data in a single MARSS model. However, I don't want to do the analysis using two independent models on each session and then average the results. So what should I do to share the model parameters across the two sessions such that I can simultaneously estimate the parameters using both data? Should the two sessions be the same length?
 A: As mentioned above, an easy way of dealing with this is to consider the observations in the middle period to be "missing". The Kalman filter easily allows for this by skipping the state update phase at each missing observation. The implementation in MARSS will do this for you if put NA for the time periods where you don't have any observations.
Here's an example. Consider a random walk without drift, observed with noise, in state-space form:
$$x_t = x_{t-1} + w_t, \quad w_t \sim \mathcal{N}(0,Q)$$
$$y_t = x_t + v_t, \quad v_t \sim \mathcal{N}(0,R)$$
We observe $y_t$ for $t=1, ..., 399$ and $t = 601, ..., 1000$, and nothing in between. We use the function MARSS::MARSS to fit this model with a Kalman filter:
library(MARSS)

set.seed(1234)

# Simulate some data, a random walk without drift, observed with noise
# Observations 400-600 are missing
x <- ts(cumsum(rnorm(1000))+rnorm(1000)*0.5)
window(x,400,600) <- NA

# Set up the model and fit it
model <- list(B=matrix(1),U=matrix(0),Q="unconstrained",Z=matrix(1),A=matrix(0),R="unconstrained",x0=matrix(0),V0=matrix(1000))
fit <- MARSS(t(as.data.frame(x)), model = model, control = list(trace=1))

# Not sure if there's an easier way to extract the filtered state mean and filtered state variance
filtered_state <- ts(t(MARSSkfss(fit)$xtt))
filtered_state.se <- ts(apply(MARSSkfss(fit)$Vtt,3,function(y) sqrt(y[1,1])))

# Plot result
plot(x,ylim=c(-50,25))
lines(filtered_state, col="red")
lines(filtered_state+1.96*filtered_state.se,col="blue")
lines(filtered_state-1.96*filtered_state.se,col="blue")
abline(v=c(400,600))

The result is the following plot:
During the period where no new observations come along, the Kalman filter's beliefs about the state don't change: it continues along with the conditional mean (which is flat in this case because our model has no drift, but need not be in general). It does, however, become increasingly uncertain about the state, hence the widening blue lines. Once observation 601 rolls around, it quickly adapts to the new level and decreases its uncertainty.
Ultimately, the result of the Kalman filter is a likelihood value for your model, for any given value of the parameters (just $Q$ and $R$ here), which allows you to estimate those parameters by MLE. Note that here $Q$ and $R$ are the same for both the early and late observation periods.
It's quite possible that there are computational shortcuts to jump from the filtered distribution at $t=399$ to the one at $t=601$ without computing those in between if you don't need them and if you know in advance that there is a jump (i.e. you are not running the Kalman filter on-line). Here you would probably not see much benefit in terms of speed, and you would have to code it up yourself rather than use the MARSS package.
