mssm: Get Multivariate State Space Model Functions

Description Usage Arguments Value See Also Examples

View source: R/mssm.R

Description

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.

Usage

1
2
mssm(fixed, family, data, random, weights, offsets, ti,
  control = mssm_control())

Arguments

fixed

formula with outcome variable on the left hand side and covariates with fixed effects on the right hand side.

family

family for the observed outcome given the state variables and covariates.

data

data.frame or environment containing the variables in fixed and random.

random

formula for covariates with a random effect. Left hand side is ignored.

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 fixed and random.

control

list with arguments passed to mssm_control.

Value

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 mssm object returned by the pf_filter function. See mssm-smoother.

terms_fixed

terms.object for the covariates with fixed effects.

terms_random

terms.object for the covariates with random effects.

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.

See Also

The README of the package contains examples of how to use this function. See https://github.com/boennecd/mssm.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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")
}

mssm documentation built on Jan. 31, 2022, 9:08 a.m.