source("format.R")

r section("Typical Usage")

Here are some functions we might need:

Then for mixed effect models, we'll need the following C++ things to be called from user's TMB code:

How about from the R side?

# 1.  TMB object for marginal distribution
lp_obj <- TMB::MakeADFun(data = c(list(method = "lpmarg"), lp_data),
                         parameters = lp_pars,
                         DLL = "MyModel", silent = TRUE)

# 2.  Sample from marginal distribution

# 2a. Fit normal approximation
psi_mean <- optim(par = lp_obj$par, fn = lp_obj$fn, gr = lp_obj$gr,
                  method = "BFGS")
psi_var <- losmix_mvar(obj, psi_mean, type = "numDeriv")

# 2b. Sample from it
nsamples <- 1e4
Psi <- rmvn(nsamples, psi_mean, psi_var)

# 3.  Sample from posterior random-effects distribution for a
#     particular observation.
sim_obj <- TMB::MakeADFun(data = c(list(method = "simcond"), sim_data),
                          parameters = list(Psi = Psi),
                          DLL = "MyModel", silent = TRUE)
Theta <- sim_obj$simulate()

r subsection("Tests")

Might also want to define some utility functions on the R side, if only for testing the TMB code more easily.



mlysy/losmix documentation built on Jan. 18, 2021, 5:56 a.m.