Quasi restricted maximum likelihood (qREML) algorithm for models with penalised splines or simple i.i.d. random effects
Quasi restricted maximum likelihood (qREML) algorithm for models with penalised splines or simple i.i.d. random effects
This algorithm can be used very flexibly to fit statistical models that involve penalised splines or simple i.i.d. random effects , i.e. that have penalties of the form [REMOVE_ME]0.5∑iλibiTSibi,[REMOVEME2]
with smoothing parameters λi, coefficient vectors bi, and fixed penalty matrices Si.
The qREML algorithm is typically much faster than REML or marginal ML using the full Laplace approximation method, but may be slightly less accurate regarding the estimation of the penalty strength parameters.
Under the hood, qreml uses the R package RTMB for automatic differentiation in the inner optimisation. The user has to specify the penalised negative log-likelihood function pnll structured as dictated by RTMB and use the penalty function to compute the quadratic-form penalty inside the likelihood.
pnll: penalised negative log-likelihood function that is structured as dictated by RTMB and uses the penalty function from LaMa to compute the penalty
Needs to be a function of the named list of initial parameters par only.
par: named list of initial parameters
The random effects/ spline coefficients can be vectors or matrices, the latter summarising several random effects of the same structure, each one being a row in the matrix.
dat: initial data list that contains the data used in the likelihood function, hyperparameters, and the initial penalty strength vector
If the initial penalty strength vector is not called lambda, the name it has in dat needs to be specified using the psname argument below. Its length needs to match the to the total number of random effects.
random: vector of names of the random effects/ penalised parameters in par
Caution: The ordering of random needs to match the order of the random effects passed to penalty inside the likelihood function.
map: optional map for fixed effects in the likelihood function
psname: optional name given to the penalty strength parameter in dat. Defaults to "lambda".
alpha: optional hyperparamater for exponential smoothing of the penalty strengths.
For larger values smoother convergence is to be expected but the algorithm may need more iterations.
smoothing: optional scaling factor for the final penalty strength parameters
Increasing this beyond one will lead to a smoother final model. Can be an integer or a vector of length equal to the length of the penalty strength parameter.
maxiter: maximum number of iterations in the outer optimisation over the penalty strength parameters.
tol: Convergence tolerance for the penalty strength parameters.
control: list of control parameters for optim to use in the inner optimisation. Here, optim uses the BFGS method which cannot be changed.
We advise against changing the default values of reltol and maxit as this can decrease the accuracy of the Laplace approximation.
silent: integer silencing level: 0 corresponds to full printing of inner and outer iterations, 1 to printing of outer iterations only, and 2 to no printing.
joint_unc: logical, if TRUE, joint RTMB object is returned allowing for joint uncertainty quantification
saveall: logical, if TRUE, then all model objects from each iteration are saved in the final model object.
epsilon: vector of two values specifying the cycling detection parameters. If the relative change of the new penalty strength to the previous one is larger than epsilon[1] but the change to the one before is smaller than epsilon[2], the algorithm will average the two last values to prevent cycling.
Returns
returns a model list influenced by the users report statements in pnll
Description
This algorithm can be used very flexibly to fit statistical models that involve penalised splines or simple i.i.d. random effects , i.e. that have penalties of the form
0.5i∑λibiTSibi,
with smoothing parameters λi, coefficient vectors bi, and fixed penalty matrices Si.
The qREML algorithm is typically much faster than REML or marginal ML using the full Laplace approximation method, but may be slightly less accurate regarding the estimation of the penalty strength parameters.
Under the hood, qreml uses the R package RTMB for automatic differentiation in the inner optimisation. The user has to specify the penalised negative log-likelihood function pnll structured as dictated by RTMB and use the penalty function to compute the quadratic-form penalty inside the likelihood.
Examples
data = trex[1:1000,]# subset# initial parameter listpar = list(logmu = log(c(0.3,1)),# step mean logsigma = log(c(0.2,0.7)),# step sd beta0 = c(-2,-2),# state process intercept betaspline = matrix(rep(0,18), nrow =2))# state process spline coefs# data object with initial penalty strength lambdadat = list(step = data$step,# step length tod = data$tod,# time of day covariate N =2,# number of states lambda = rep(10,2))# initial penalty strength# building model matricesmodmat = make_matrices(~ s(tod, bs ="cp"), data = data.frame(tod =1:24), knots = list(tod = c(0,24)))# wrapping pointsdat$Z = modmat$Z # spline design matrixdat$S = modmat$S # penalty matrix# penalised negative log-likelihood functionpnll =function(par){ getAll(par, dat)# makes everything contained available without $ Gamma = tpm_g(Z, cbind(beta0, betaspline), ad =TRUE)# transition probabilities delta = stationary_p(Gamma, t =1, ad =TRUE)# initial distribution mu = exp(logmu)# step mean sigma = exp(logsigma)# step sd# calculating all state-dependent densities allprobs = matrix(1, nrow = length(step), ncol = N) ind = which(!is.na(step))# only for non-NA obs.for(j in1:N) allprobs[ind,j]= dgamma2(step[ind],mu[j],sigma[j])-forward_g(delta, Gamma[,,tod], allprobs)+ penalty(betaspline, S, lambda)# this does all the penalization work}# model fittingmod = qreml(pnll, par, dat, random ="betaspline")
See Also
penalty to compute the penalty inside the likelihood function