| Title: | Multivariate State Space Models |
|---|---|
| Description: | Provides methods to perform parameter estimation and make analysis of multivariate observed outcomes through time which depends on a latent state variable. All methods scale well in the dimension of the observed outcomes at each time point. The package contains an implementation of a Laplace approximation, particle filters like suggested by Lin, Zhang, Cheng, & Chen (2005) <doi:10.1198/016214505000000349>, and the gradient and observed information matrix approximation suggested by Poyiadjis, Doucet, & Singh (2011) <doi:10.1093/biomet/asq062>. |
| Authors: | Benjamin Christoffersen [cre, aut]
|
| Maintainer: | Benjamin Christoffersen <[email protected]> |
| License: | GPL-2 |
| Version: | 0.1.6 |
| Built: | 2026-12-03 10:56:22 UTC |
| Source: | https://github.com/cran/mssm |
This package contains particle filter methods for multivariate observed
outcomes and low dimensional state vectors. The methods are intended to
scale well in the dimension of the observed outcomes. The main function in
the package is the mssm function. The package also includes
a method to estimate the parameters using a Laplace approximation.
The README contains an example of the features in the package. See https://github.com/boennecd/mssm.
The package is still under development and the API and results of the methods may change.
Extracts the effective sample size at each time point from a mssm
object.
get_ess(object)get_ess(object)
object |
an object of class |
An object of class mssmEss with the effective sample sizes.
if(require(Ecdat)){ # load data and fit glm to get some parameters to use in an illustration data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to run particle filter library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # run particle filter pf <- ll_func$pf_filter( cfix = coef(glm_fit), disp = summary(glm_fit)$dispersion, F. = as.matrix(.0001), Q = as.matrix(.0001^2)) # summary statistics for effective sample sizes print(ess <- get_ess(pf)) }if(require(Ecdat)){ # load data and fit glm to get some parameters to use in an illustration data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to run particle filter library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # run particle filter pf <- ll_func$pf_filter( cfix = coef(glm_fit), disp = summary(glm_fit)$dispersion, F. = as.matrix(.0001), Q = as.matrix(.0001^2)) # summary statistics for effective sample sizes print(ess <- get_ess(pf)) }
Function to extract the log-likelihood from a mssm or
mssmLaplace object.
## S3 method for class 'mssm' logLik(object, ...) ## S3 method for class 'mssmLaplace' logLik(object, ...)## S3 method for class 'mssm' logLik(object, ...) ## S3 method for class 'mssmLaplace' logLik(object, ...)
object |
an object of class |
... |
un-used. |
A logLik object. The log_lik_terms attribute contains
the log-likelihood contributions from each time point.
The degrees of freedom assumes that all parameters are free. The number of observations may be invalid for some models (e.g., discrete survival analysis).
if(require(Ecdat)){ # load data and fit glm to get starting values data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to perform estimation library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # fit model with time-varying intercept with Laplace approximation disp <- summary(glm_fit)$dispersion laplace <- ll_func$Laplace( cfix = coef(glm_fit), disp = disp, F. = diag(.5, 1), Q = diag(1)) # run particle filter pf <- ll_func$pf_filter( cfix = laplace$cfix, disp = laplace$disp, F. = laplace$F., Q = laplace$Q) # compare approximate log-likelihoods print(logLik(pf)) print(logLik(laplace)) }if(require(Ecdat)){ # load data and fit glm to get starting values data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to perform estimation library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # fit model with time-varying intercept with Laplace approximation disp <- summary(glm_fit)$dispersion laplace <- ll_func$Laplace( cfix = coef(glm_fit), disp = disp, F. = diag(.5, 1), Q = diag(1)) # run particle filter pf <- ll_func$pf_filter( cfix = laplace$cfix, disp = laplace$disp, F. = laplace$F., Q = laplace$Q) # compare approximate log-likelihoods print(logLik(pf)) print(logLik(laplace)) }
Returns an object with a function that can be used to run a particle filter, a function to perform parameter estimation using a Laplace approximation, and a function to perform smoothing of particle weights.
mssm(fixed, family, data, random, weights, offsets, ti, control = mssm_control())mssm(fixed, family, data, random, weights, offsets, ti, control = mssm_control())
fixed |
|
family |
family for the observed outcome given the state variables and covariates. |
data |
|
random |
|
weights |
optional prior weights. |
offsets |
optional a priori known component in the linear predictor. |
ti |
integer vector with time indices matching with each observation of
|
control |
list with arguments passed to |
An object of class mssmFunc with the following elements
pf_filter |
function to perform particle filtering. See mssm-pf. |
Laplace |
function to perform parameter estimation with a Laplace approximation. See mssm-Laplace. |
smoother |
function to compute smoothing weights for an |
terms_fixed |
|
terms_random |
|
y |
vector with outcomes. |
X |
covariates with fixed effects. |
Z |
covariates with random effects. |
ti |
time indices for each observation. |
weights |
prior weights for each observation. |
offsets |
a priori known component in the linear predictor for each observation. |
call |
the matched call. |
family |
character describing the conditional distribution of the outcomes. |
The README of the package contains examples of how to use this function. See https://github.com/boennecd/mssm.
if(require(Ecdat)){ # load data and fit glm to get starting values . <- print data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to perform estimation library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) .(ll_func) # fit model with time-varying intercept with Laplace approximation disp <- summary(glm_fit)$dispersion laplace <- ll_func$Laplace( cfix = coef(glm_fit), disp = disp, F. = diag(.5, 1), Q = diag(1)) .(laplace) # compare w/ glm .(logLik(laplace)) .(logLik(glm_fit)) .(rbind(laplace = laplace$cfix, glm = coef(glm_fit))) # run particle filter pf <- ll_func$pf_filter( cfix = laplace$cfix, disp = laplace$disp, F. = laplace$F., Q = laplace$Q) .(pf) # compare approximate log-likelihoods .(logLik(pf)) .(logLik(laplace)) # predicted values from filtering (does not appear random...) plot(pf) # plot predicted values from smoothing distribution pf <- ll_func$smoother(pf) plot(pf, which_weights = "smooth") }if(require(Ecdat)){ # load data and fit glm to get starting values . <- print data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to perform estimation library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) .(ll_func) # fit model with time-varying intercept with Laplace approximation disp <- summary(glm_fit)$dispersion laplace <- ll_func$Laplace( cfix = coef(glm_fit), disp = disp, F. = diag(.5, 1), Q = diag(1)) .(laplace) # compare w/ glm .(logLik(laplace)) .(logLik(glm_fit)) .(rbind(laplace = laplace$cfix, glm = coef(glm_fit))) # run particle filter pf <- ll_func$pf_filter( cfix = laplace$cfix, disp = laplace$disp, F. = laplace$F., Q = laplace$Q) .(pf) # compare approximate log-likelihoods .(logLik(pf)) .(logLik(laplace)) # predicted values from filtering (does not appear random...) plot(pf) # plot predicted values from smoothing distribution pf <- ll_func$smoother(pf) plot(pf, which_weights = "smooth") }
Auxiliary function for mssm.
mssm_control(N_part = 1000L, n_threads = 1L, covar_fac = 1.2, ftol_rel = 1e-06, nu = 8, what = "log_density", which_sampler = "mode_aprx", which_ll_cp = "no_aprx", seed = 1L, KD_N_max = 10L, aprx_eps = 0.001, ftol_abs = 1e-04, ftol_abs_inner = 1e-04, la_ftol_rel = -1, la_ftol_rel_inner = -1, maxeval = 10000L, maxeval_inner = 10000L, use_antithetic = FALSE)mssm_control(N_part = 1000L, n_threads = 1L, covar_fac = 1.2, ftol_rel = 1e-06, nu = 8, what = "log_density", which_sampler = "mode_aprx", which_ll_cp = "no_aprx", seed = 1L, KD_N_max = 10L, aprx_eps = 0.001, ftol_abs = 1e-04, ftol_abs_inner = 1e-04, la_ftol_rel = -1, la_ftol_rel_inner = -1, maxeval = 10000L, maxeval_inner = 10000L, use_antithetic = FALSE)
N_part |
integer greater than zero for the number of particles to use. |
n_threads |
integer greater than zero for the number of threads to use. |
covar_fac |
positive numeric scalar used to scale the covariance matrix in the proposal distribution. |
ftol_rel |
positive numeric scalar with convergence threshold passed
to |
nu |
degrees of freedom to use for the multivariate
|
what |
character indicating what to approximate. |
which_sampler |
character indicating what type of proposal
distribution to use. |
which_ll_cp |
character indicating what type of computation should be
performed in each iteration of the particle filter. |
seed |
integer with seed to pass to |
KD_N_max |
integer greater than zero with the maximum number of particles to include in each leaf of the two k-d trees if the dual k-d trees method is used. |
aprx_eps |
positive numeric scalar with the maximum error if the dual k-d tree method is used. |
ftol_abs, ftol_abs_inner, la_ftol_rel, la_ftol_rel_inner, maxeval, maxeval_inner
|
scalars passed to |
use_antithetic |
logical which is true if antithetic variables should be used. |
mssm.
See the README of the package for details of the dual k-d tree method at https://github.com/boennecd/mssm.
library(mssm) str(mssm_control()) str(mssm_control(N_part = 2000L))library(mssm) str(mssm_control()) str(mssm_control(N_part = 2000L))
Function returned from mssm which can be used to perform
parameter estimation with a Laplace approximation.
cfix |
starting values for coefficient for the fixed effects. |
disp |
starting value for additional parameters for the family (e.g., a dispersion parameter). |
F. |
starting values for matrix in the transition density of the state vector. |
Q |
starting values for covariance matrix in the transition density of the state vector. |
Q0 |
un-used. |
mu0 |
un-used. |
trace |
integer controlling whether information should be printed during parameter estimation. Zero yields no information. |
An object of class mssmLaplace with the following elements
F. |
estimate of |
Q |
estimate of |
cfix |
estimate of |
logLik |
approximate log-likelihood at estimates. |
n_it |
number of Laplace approximations. |
code |
returned code from |
disp |
estimated dispersion parameter. |
Remaining elements are the same as returned by mssm.
mssm.
if(require(Ecdat)){ # load data and fit glm to get starting values data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to perform estimation library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # fit model with time-varying intercept with Laplace approximation disp <- summary(glm_fit)$dispersion laplace <- ll_func$Laplace( cfix = coef(glm_fit), disp = disp, F. = diag(.5, 1), Q = diag(1)) print(laplace) }if(require(Ecdat)){ # load data and fit glm to get starting values data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to perform estimation library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # fit model with time-varying intercept with Laplace approximation disp <- summary(glm_fit)$dispersion laplace <- ll_func$Laplace( cfix = coef(glm_fit), disp = disp, F. = diag(.5, 1), Q = diag(1)) print(laplace) }
Function returned from mssm which can be used to perform
particle filtering given values for the parameters in the model.
cfix |
values for for coefficient for the fixed effects. |
disp |
additional parameters for the family (e.g., a dispersion parameter). |
F. |
matrix in the transition density of the state vector. |
Q |
covariance matrix in the transition density of the state vector. |
Q0 |
optional covariance matrix at the first time point. Default is the covariance matrix in the time invariant distribution. |
mu0 |
optional mean at the first time point. Default is the zero vector. |
trace |
integer controlling whether information should be printed during particle filtering. Zero yields no information. |
seed |
integer to pass to |
what, N_part
|
same as in |
An object of class mssm with the following elements
pf_output |
A list with an element for each time period. Each element
is a list with
|
Remaining elements are the same as returned by mssm.
If gradient approximation is requested then the first elements of
stats are w.r.t. the fixed coefficients, the next elements are
w.r.t. the matrix in the map from the previous state vector to the mean
of the next, and the last element is w.r.t. the covariance matrix.
Only the lower triangular matrix is kept for the covariance
matrix. See the examples in the README at
https://github.com/boennecd/mssm. There will be an additional
element for the dispersion parameter if the family has a dispersion
parameter.
If the Hessian is requested then the s
in Poyiadjis et al. (2011) are returned after the gradient elements. These
can be used to approximate the observed information matrix. That is,
using that the approximation of the observed information matrix is
as in Poyiadjis et al. (2011). See the README for an example.
Poyiadjis, G., Doucet, A. and Singh, S. S. (2011) Particle Approximations of the Score and Observed Information Matrix in State Space Models with Application to Parameter Estimation. Biometrika, 98(1), 65–80.
mssm.
if(require(Ecdat)){ # load data and get object to perform particle filtering data("Gasoline", package = "Ecdat") library(mssm) ll_func <- mssm( fixed = lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control(N_part = 1000L, n_threads = 1L)) # run particle filter cfix <- c(0.612, -0.015, 0.214, 0.048, -0.013, -0.016, -0.022, 0.047, -0.046, 0.007, -0.001, 0.008, -0.117, 0.075, 0.048, -0.054, 0.017, 0.228, 0.077, -0.056, -0.139) pf <- ll_func$pf_filter( cfix = cfix, Q = as.matrix(2.163e-05), F. = as.matrix(0.9792), disp = 0.000291) print(pf) }if(require(Ecdat)){ # load data and get object to perform particle filtering data("Gasoline", package = "Ecdat") library(mssm) ll_func <- mssm( fixed = lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control(N_part = 1000L, n_threads = 1L)) # run particle filter cfix <- c(0.612, -0.015, 0.214, 0.048, -0.013, -0.016, -0.022, 0.047, -0.046, 0.007, -0.001, 0.008, -0.117, 0.075, 0.048, -0.054, 0.017, 0.228, 0.077, -0.056, -0.139) pf <- ll_func$pf_filter( cfix = cfix, Q = as.matrix(2.163e-05), F. = as.matrix(0.9792), disp = 0.000291) print(pf) }
Computes smoothed weights using the backward smoothing formula for a
mssm object. The k-d dual tree approximation is also used if it used
for the mssm object.
object |
an object of class |
Same as mssm-pf but where the pf_output's list elements
has an additional element called ws_normalized_smooth. This
contains the normalized log smoothing weights.
mssm.
if(require(Ecdat)){ # load data and get object to perform particle filtering data("Gasoline", package = "Ecdat") library(mssm) ll_func <- mssm( fixed = lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control(N_part = 1000L, n_threads = 1L)) # run particle filter cfix <- c(0.612, -0.015, 0.214, 0.048, -0.013, -0.016, -0.022, 0.047, -0.046, 0.007, -0.001, 0.008, -0.117, 0.075, 0.048, -0.054, 0.017, 0.228, 0.077, -0.056, -0.139) pf <- ll_func$pf_filter( cfix = cfix, Q = as.matrix(2.163e-05), F. = as.matrix(0.9792), disp = 0.000291) print(is.null(pf$pf_output[[1L]]$ws_normalized_smooth)) pf <- ll_func$smoother(pf) print(is.null(pf$pf_output[[1L]]$ws_normalized_smooth)) }if(require(Ecdat)){ # load data and get object to perform particle filtering data("Gasoline", package = "Ecdat") library(mssm) ll_func <- mssm( fixed = lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control(N_part = 1000L, n_threads = 1L)) # run particle filter cfix <- c(0.612, -0.015, 0.214, 0.048, -0.013, -0.016, -0.022, 0.047, -0.046, 0.007, -0.001, 0.008, -0.117, 0.075, 0.048, -0.054, 0.017, 0.228, 0.077, -0.056, -0.139) pf <- ll_func$pf_filter( cfix = cfix, Q = as.matrix(2.163e-05), F. = as.matrix(0.9792), disp = 0.000291) print(is.null(pf$pf_output[[1L]]$ws_normalized_smooth)) pf <- ll_func$smoother(pf) print(is.null(pf$pf_output[[1L]]$ws_normalized_smooth)) }
Plots the predicted mean and pointwise prediction interval of the state variables for the filtering distribution or smoothing distribution.
## S3 method for class 'mssm' plot(x, y, qs = c(0.05, 0.95), do_plot = TRUE, which_weights = c("filter", "smooth"), ...)## S3 method for class 'mssm' plot(x, y, qs = c(0.05, 0.95), do_plot = TRUE, which_weights = c("filter", "smooth"), ...)
x |
an object of class |
y |
un-used. |
qs |
two-dimensional numeric vector with bounds of the prediction interval. |
do_plot |
|
which_weights |
character of which weights to use. Either
|
... |
un-used. |
List with means and quantiles.
if(require(Ecdat)){ # load data and get object to perform particle filtering data("Gasoline", package = "Ecdat") library(mssm) ll_func <- mssm( fixed = lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control(N_part = 1000L, n_threads = 1L)) # run particle filter cfix <- c(0.612, -0.015, 0.214, 0.048, -0.013, -0.016, -0.022, 0.047, -0.046, 0.007, -0.001, 0.008, -0.117, 0.075, 0.048, -0.054, 0.017, 0.228, 0.077, -0.056, -0.139) pf <- ll_func$pf_filter( cfix = cfix, Q = as.matrix(2.163e-05), F. = as.matrix(0.9792), disp = 0.000291) # plot predicted values and prediction intervals plot(pf) plot(pf, qs = c(.01, .99)) pf <- ll_func$smoother(pf) plot(pf, which_weights = "smooth") }if(require(Ecdat)){ # load data and get object to perform particle filtering data("Gasoline", package = "Ecdat") library(mssm) ll_func <- mssm( fixed = lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control(N_part = 1000L, n_threads = 1L)) # run particle filter cfix <- c(0.612, -0.015, 0.214, 0.048, -0.013, -0.016, -0.022, 0.047, -0.046, 0.007, -0.001, 0.008, -0.117, 0.075, 0.048, -0.054, 0.017, 0.228, 0.077, -0.056, -0.139) pf <- ll_func$pf_filter( cfix = cfix, Q = as.matrix(2.163e-05), F. = as.matrix(0.9792), disp = 0.000291) # plot predicted values and prediction intervals plot(pf) plot(pf, qs = c(.01, .99)) pf <- ll_func$smoother(pf) plot(pf, which_weights = "smooth") }
Plots the effective sample sizes.
## S3 method for class 'mssmEss' plot(x, y, ...)## S3 method for class 'mssmEss' plot(x, y, ...)
x |
an object of class |
y |
un-used. |
... |
un-used. |
The plotted x-values, y-values, and maximum possible effective sample size.
if(require(Ecdat)){ # load data and fit glm to get some parameters to use in an illustration data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to run particle filter library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # run particle filter pf <- ll_func$pf_filter( cfix = coef(glm_fit), disp = summary(glm_fit)$dispersion, F. = as.matrix(.0001), Q = as.matrix(.0001^2)) # plot effective samples sizes plot(get_ess(pf)) }if(require(Ecdat)){ # load data and fit glm to get some parameters to use in an illustration data("Gasoline", package = "Ecdat") glm_fit <- glm(lgaspcar ~ factor(country) + lincomep + lrpmg + lcarpcap, Gamma("log"), Gasoline) # get object to run particle filter library(mssm) ll_func <- mssm( fixed = formula(glm_fit), random = ~ 1, family = Gamma("log"), data = Gasoline, ti = year, control = mssm_control( N_part = 1000L, n_threads = 1L)) # run particle filter pf <- ll_func$pf_filter( cfix = coef(glm_fit), disp = summary(glm_fit)$dispersion, F. = as.matrix(.0001), Q = as.matrix(.0001^2)) # plot effective samples sizes plot(get_ess(pf)) }