## ----setup, message=FALSE-----------------------------------------------------
library(sommer)

## ----random-intercept-example, eval=FALSE-------------------------------------
# fit <- mmes(
#   count ~ treatment + offset(log(exposure)),
#   random = ~ site,
#   data = dat,
#   family = poisson(),
#   method = mmes_method(objective = "ml", integration = "laplace")
# )

## ----mixed-family-example, eval=FALSE-----------------------------------------
# fit <- mmes(
#   value ~ trait - 1 + trait:environment,
#   random = ~ vsm(usm(trait), ism(genotype)),
#   data = long_data,
#   family = familym(
#     "trait",
#     disease = binomial("probit"),
#     count = nbinom2(theta = 2),
#     biomass = Gamma("log"),
#     yield = gaussian()
#   ),
#   method = mmes_method(objective = "ml", integration = "laplace"),
#   familyParameters = list(
#     count = list(theta = 2, estimate = TRUE),
#     biomass = list(dispersion = 0.4, estimate = TRUE),
#     yield = list(dispersion = 1, estimate = FALSE)
#   )
# )

## ----pql-example, eval=FALSE--------------------------------------------------
# fit_pql <- mmes(
#   response ~ treatment,
#   random = ~ subject,
#   rcov = ~ units,
#   data = dat,
#   family = binomial(),
#   control = mmes_control(
#     working = list(maxit = 30L, tol = 1e-5)
#   )
# )

## ----laplace-fit, eval=FALSE--------------------------------------------------
# fit_laplace <- mmes(
#   outcome ~ treatment,
#   random = ~ subject,
#   data = dat,
#   family = binomial(),
#   method = mmes_method(objective = "ml", integration = "laplace"),
#   control = mmes_control(
#     optimizer = list(maxit = 1000L),
#     mode = list(maxit = 100L, tol = 1e-8))
# )
# logLik(fit_laplace)
# AIC(fit_laplace)
# fit_laplace$laplaceDiagnostics

## ----post-fit-pev, eval=FALSE-------------------------------------------------
# fit_with_pev <- postPEV(fit_laplace, mode = 1L)
# head(fit_with_pev$uPevList)
# 
# # Request the dense coefficient covariance only when its size is appropriate.
# fit_with_full_covariance <- postPEV(fit_laplace, mode = 2L)
# dim(fit_with_full_covariance$Ci)

## ----diagnostics, eval=FALSE--------------------------------------------------
# rq <- residuals(fit_laplace, type = "quantile")
# replicates <- simulate(fit_laplace, nsim = 100)

