#' @title This function returns the filtered states whenever an
#' observation is present and predicted states otherwise.
#' @param P Transition matrix P(will go to i | is in j)
#' @param E Event observation matrix P(detection event j | is in i)
#' @param e Sequence of observation events. Since we are taking
#' time increase small, it is expected to have mostly missing
#' values. The first value is expected to have an observation.
#' @return The filtered states.
#' @export
fstates <- function(P, E, e) {
# Allocate the output matrix
output <- matrix(0, ncol = length(e), nrow = nrow(P))
# Initial steady state calculation
edecomp <- eigen(P)
# The steady state might be not unique! Fix this later
i <- which(abs(edecomp$values - 1) < 1e-6)[1]
state <- edecomp$vectors[,i]
state <- state / sum(state)
# Now run the filter. It is a loop, it should be written in
# some compiled language for performance.
for (i in 1:length(e)) {
if (!is.na(e[i])) {
state <- E[,e[i]] * state
state <- state / sum(state)
}
output[,i] <- state
state <- P %*% matrix(state, ncol = 1)
}
return(output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.