mssm {mssm} | R Documentation |
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())
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") }