R/semireg-engine.R

Defines functions pm_quad_diag pm_sr_satt_ddf_tmb pm_tmb_psi_vcov pm_tmb_ntheta pm_tmb_code pm_cov_given pm_sr_satt_ddf pm_sr_ddf pm_sr_gaussian pm_sr_devfun pm_sr_kr pm_sr_uncond pm_sr_uncond_cached pm_sr_result pm_sr_info pm_sr_fit_tmb pm_sr_check_wide pm_sr_check_nlev pm_sr_wide pm_sr_fit_lme4 pm_sr_call_for pm_sr_replace_Zt pm_sr_eval pm_sr_formula pm_sr_setup

## The fitting engine of semireg() and semireg_tmb() (dev/review-semireg.md).
##
## A semi-parametric model is a mixed model in which each smooth is a block
## of random effects with a given design Z (from the smooth basis) and iid coefficients.
## Both front ends:
##   1. pm_sr_setup(): the data (complete cases, numeric covariates centred
##      or scaled), the smooth designs, and the formula with one placeholder
##      term (1 | name) per smooth block;
##   2. build the model structure WITHOUT fitting (lme4::lFormula() or
##      glFormula(); glmmTMB(doFit = FALSE)), replace the placeholder rows of
##      Z by the smooth designs (pm_sr_replace_Zt()), and fit once;
##   3. pm_sr_info(): the covariance of the fixed and smooth coefficients and
##      what semipred() needs to predict.
## The data are given to the builders in an environment of their own, never
## written into the caller's frame.

## The data, the smooth designs and the formula with placeholders.
##   mc         the front end's match.call() (formula already set)
##   smoothZ    the unevaluated smoothZ argument of the workers: the smooth
##              blocks of a fit, already built (the package's refits)
##   env        where the front end was called
## Returns list(data, fomul_vars, sm_vars, smoothZm (as given, by name),
## smoothZN (blocks: a list of blocks is split into its elements),
## Zt (transposed blocks), formula (with placeholders, or NULL without
## smooths)).
pm_sr_setup <- function(mc, data, smoothZ, env) {
  formula <- mc$formula
  # smooths written in the formula (R/semireg-smooth.R)
  sm_f <- pm_sm_parse(formula, data)
  fomul_vars <- all.vars(stats::terms(if (is.null(sm_f)) formula else sm_f$formula))
  sm_vars <- character(0)
  fomul_vars <- fomul_vars[-1]
  # (the variables are used as they are: 1.x centred numeric variables by
  # default, ncenter = TRUE, which put knots given in the data's units in the
  # wrong place; removed in 2.0)
  # the observations, chosen once: 'subset', then the complete cases of the
  # variables used. The smooth bases, weights and offset are all built from
  # these rows (the builder calls get no 'subset'; review-semireg-2 C1).
  # Weights and offset given as vectors (not columns) are taken into the data
  # first, so they follow the same rows.
  data <- as.data.frame(data)
  for (a in c("weights", "offset")) {
    if (is.null(mc[[a]])) next
    v <- eval(mc[[a]], data, env)
    if (is.null(v)) next
    if (length(v) != nrow(data)) {
      stop("'", a, "' must have one value per row of 'data' (", nrow(data), "), not ", length(v), ".", call. = FALSE)
    }
    nm <- paste0(".pm_", a)
    data[[nm]] <- v
    mc[[a]] <- as.name(nm)
  }
  if (!is.null(mc$subset)) {
    keep <- eval(mc$subset, data, env)
    if (!is.logical(keep)) {
      # numbers (negative ones leave rows out) or row names, as lm()'s subset
      idx <- stats::setNames(seq_len(nrow(data)), rownames(data))[keep]
      if (anyNA(idx) || anyDuplicated(idx)) {
        stop("'subset' names rows that are not in 'data', or a row twice.", call. = FALSE)
      }
      keep <- seq_len(nrow(data)) %in% idx
    }
    keep <- keep & !is.na(keep)
    data <- data[keep, , drop = FALSE]
    mc$subset <- NULL
  }
  data <- droplevels(as.data.frame(stats::na.omit(data[, intersect(all.vars(mc), names(data)), drop = FALSE])))
  # 'smoothZ': only the package's own refits give it (the smooth blocks of a
  # fit, already built); smooths are written in the formula
  smoothZm <- eval(smoothZ, data, env)
  smooths <- NULL
  if (!is.null(sm_f) && length(smoothZm)) {
    stop("internal: smooths both in the formula and in 'smoothZ'.", call. = FALSE)
  }
  if (!is.null(sm_f)) {
    # the smooths of the formula: random blocks, and their fixed terms added
    # to the formula (those already there are not doubled)
    b <- pm_sm_build(sm_f, data)
    smoothZm <- c(smoothZm, b$blocks)
    smooths <- b$smooths
    sm_vars <- union(sm_vars, b$vars)
    lab <- attr(stats::terms(sm_f$formula), "term.labels")
    add <- setdiff(b$fixed, lab)
    formula <- if (length(add)) {
      stats::as.formula(paste(paste(deparse(sm_f$formula, width.cutoff = 500L), collapse = " "), "+",
                              paste(add, collapse = " + ")), env = environment(formula))
    } else sm_f$formula
    fomul_vars <- all.vars(stats::terms(formula))[-1]
  }
  smoothZN <- list()
  for (i in names(smoothZm)) {
    smoothZN <- c(smoothZN, if (is.list(smoothZm[[i]])) unlist(smoothZm[i], recursive = FALSE) else smoothZm[i])
  }
  Zt <- lapply(smoothZN, t)
  if (length(Zt)) {
    stopifnot(length(names(Zt)) == length(Zt), all(vapply(Zt, methods::is, logical(1), class2 = "sparseMatrix")))
  }
  # one placeholder random intercept per smooth block, its factor in the data
  f_new <- NULL
  if (length(Zt)) {
    rhs <- paste(paste(format(formula), collapse = ""), paste0("+ (1|", names(Zt), ")", collapse = " "))
    # (all q levels, also for a block wider than the data: q columns, n < q rows)
    for (i in names(Zt)) data[[i]] <- factor(rep(seq_len(nrow(Zt[[i]])), length = ncol(Zt[[i]])),
                                             levels = seq_len(nrow(Zt[[i]])))
    f_new <- stats::as.formula(rhs, env = environment(formula))
  }
  list(data = data, fomul_vars = fomul_vars, sm_vars = sm_vars, smoothZm = smoothZm, smoothZN = smoothZN, Zt = Zt,
       formula = f_new, smooths = smooths,
       rows = mc[intersect(names(mc), c("subset", "weights", "offset"))])
}

## The formula keeps its own environment, where objects named in it (a basis
## function or knots in xt, a variable outside the data) are found, as for
## lm() and gam() (review-semireg-2 F2; before, it was replaced by the caller's
## frame, so a basis function local to the function that made the formula was
## not found). A formula without one (a character string) gets the caller's.
pm_sr_formula <- function(formula, env) {
  if (!inherits(formula, "formula")) return(stats::as.formula(formula, env = env))
  if (is.null(environment(formula))) environment(formula) <- env
  formula
}

## Evaluate a builder call with the semireg data, in an environment of its
## own whose parent is the caller's (for weights, offsets, subsets that refer
## to other objects).
pm_sr_eval <- function(cl, data, env) {
  e <- new.env(parent = env)
  e$.pm_sr_data <- data
  cl$data <- quote(.pm_sr_data)
  eval(cl, e)
}

## Replace the placeholder rows of Zt (one random-intercept term per smooth
## block, found through flist/Gp) by the smooth designs.
pm_sr_replace_Zt <- function(Zt, smoothZt, flist, Gp) {
  asgn <- attr(flist, "assign")
  stopifnot(all(names(smoothZt) %in% names(flist)))
  for (i in names(smoothZt)) {
    tn <- which(match(i, names(flist)) == asgn)
    if (length(tn) > 1) stop("a smoothZt factor must be associated with only one r.e. term")
    rowsi <- (Gp[tn] + 1L):Gp[tn + 1L]
    stopifnot(all(dim(Zt[rowsi, ]) == dim(smoothZt[[i]])))
    Zt[rowsi, ] <- smoothZt[[i]]
  }
  Zt
}

## Keep only the arguments of a call that the function 'fn' accepts by name.
pm_sr_call_for <- function(cl, fn, drop = character(0)) {
  keep <- c("", intersect(names(cl)[-1], setdiff(names(formals(fn)), c("...", drop))))
  cl[names(cl) %in% keep | seq_along(cl) == 1L]
}

## lme4: the structure (lFormula()/glFormula()), the smooth designs put into
## Zt, one fit. 'lmerc' is the [g]lmer call of the front end (formula with
## placeholders). Returns the merMod and, for a gaussian fit, the deviance
## function that goes with it (Satterthwaite df and varcomp() need it).
pm_sr_fit_lme4 <- function(lmerc, st, gaus, family, control, start, verbose, drop_unused_levels, mc, env,
                           ...) {
  fc <- lmerc
  fc$formula <- st$formula
  # lme4's name for the user's drop_unused_levels
  fc$drop_unused_levels <- NULL
  fc$drop.unused.levels <- drop_unused_levels
  if (gaus) {
    fc[[1]] <- quote(lme4::lFormula)
    fc <- pm_sr_call_for(fc, lme4::lFormula)
  } else {
    fc[[1]] <- quote(lme4::glFormula)
    fc$family <- family
    fc <- pm_sr_call_for(fc, lme4::glFormula, drop = "start")
  }
  # a smooth block with at least as many columns as observations (a spatial
  # basis on a grid with missing plots): lme4's checks of levels and random
  # effects against the observations are for grouping factors, not for
  # penalised bases; they are kept for the model's own random terms
  # (pm_sr_check_nlev()), and the placeholder factors keep all their levels
  wide <- pm_sr_wide(st)
  if (length(wide)) {
    ctl <- control
    ctl$checkControl$check.nobs.vs.nlev <- "ignore"
    ctl$checkControl$check.nobs.vs.nRE <- "ignore"
    fc$control <- ctl
  }
  lf <- suppressWarnings(pm_sr_eval(fc, st$data, env))
  if (length(wide)) {
    for (i in names(st$Zt)) lf$fr[[i]] <- factor(lf$fr[[i]], levels = seq_len(nrow(st$Zt[[i]])))
    mk <- if (requireNamespace("reformulas", quietly = TRUE)) reformulas::mkReTrms else lme4::mkReTrms
    lf$reTrms <- mk(pm_findbars(lf$formula), lf$fr, drop.unused.levels = FALSE)
    pm_sr_check_nlev(lf$reTrms, setdiff(names(lf$reTrms$flist), names(st$Zt)), nrow(lf$fr), control, gaus)
    if (gaus) pm_sr_check_wide(st$Zt[wide], lf$fr[["(weights)"]])
  }
  reTrms <- lf$reTrms
  reTrms$Zt <- pm_sr_replace_Zt(reTrms$Zt, st$Zt, reTrms$flist, reTrms$Gp)
  # lme4's convergence check, as lmer()/glmer() (review-semireg-2 F1): derivatives
  # when the control asks for them (lme4's default: fewer than 1e4
  # observations and 10 variance parameters), checkConv() with the control's
  # settings, the result stored in the fit (@optinfo$conv$lme4: pm_check()
  # and the advice read it). A singular fit is reported by semireg's own
  # advice (smooth_linear, singular_fit), so lme4's message for it is off.
  # The parameters are counted as lmer() and glmer() do: theta, plus beta for
  # a GLMM (its second stage optimises both; review of block 2: counting
  # theta only computed derivatives that checkConv() then ignored).
  n_fr <- nrow(lf$fr)
  cc <- control$checkConv
  npar <- length(reTrms$theta) + if (gaus) 0L else ncol(lf$X)
  calc <- if (!is.null(control$calc.derivs)) isTRUE(control$calc.derivs) else
    !is.null(cc) && n_fr < cc$check.conv.nobsmax && npar < cc$check.conv.nparmax
  conv <- function(devfun, opt) {
    if (!calc || is.null(cc)) return(NULL)
    rho <- environment(devfun)
    if (is.list(cc$check.conv.singular)) cc$check.conv.singular$action <- "ignore"
    a <- list(derivs = attr(opt, "derivs"), coefs = opt$par, ctrl = cc, lbound = rho$lower, ubound = rho$upper,
              nobs = n_fr, ndim = length(rho$lower))
    do.call(lme4::checkConv, a[intersect(names(a), names(formals(lme4::checkConv)))])
  }
  dfl <- list(fr = lf$fr, X = lf$X, reTrms = reTrms, start = reTrms$theta, drop.unused.levels = drop_unused_levels)
  # GLMMs: the optimizer of each stage (glmerControl() holds two, lmerControl()
  # one for both), and glmerControl()'s PIRLS and stage-2 settings, as glmer()
  if (!gaus && inherits(control, "glmerControl")) dfl$control <- control
  opt_k <- function(k) control$optimizer[[min(k, length(control$optimizer))]]
  glmm_fit <- function(fam) {
    dfl$family <- fam
    devfun <- do.call(lme4::mkGlmerDevfun, dfl)
    opt <- lme4::optimizeGlmer(devfun, start = start, verbose = verbose, optimizer = opt_k(1),
                               control = control$optCtrl, ...)
    devfun <- lme4::updateGlmerDevfun(devfun, dfl$reTrms)
    opt <- lme4::optimizeGlmer(devfun, stage = 2, start = start, verbose = verbose, optimizer = opt_k(2),
                               control = control$optCtrl, calc.derivs = calc,
                               restart_edge = inherits(control, "glmerControl") && isTRUE(control$restart_edge),
                               boundary.tol = if (is.null(control$boundary.tol)) 1e-5 else control$boundary.tol, ...)
    lme4::mkMerMod(environment(devfun), opt, reTrms, lf$fr, mc, lme4conv = conv(devfun, opt))
  }
  # GLMMs: a builder of the stage-1 deviance (a function of theta, with beta
  # and u by PIRLS) for the smoothing-parameter correction (pm_sr_uncond())
  if (!gaus) return(list(semer = glmm_fit(family), devfun = NULL,
                         devfun0 = function() do.call(lme4::mkGlmerDevfun, c(dfl, list(family = family)))))
  dfl$REML <- lf$REML
  devfun <- do.call(lme4::mkLmerDevfun, dfl)
  opt <- lme4::optimizeLmer(devfun, start = start, verbose = verbose, optimizer = control$optimizer,
                            control = control$optCtrl, calc.derivs = calc, ...)
  list(semer = lme4::mkMerMod(environment(devfun), opt, reTrms, lf$fr, mc, lme4conv = conv(devfun, opt)),
       devfun = devfun)
}

## Smooth blocks with at least as many columns as observations ("wide": a
## spatial basis on a grid with missing plots). semireg() fits them with
## lme4's checks for grouping factors turned off (pm_sr_fit_lme4());
## semireg_tmb() does not fit them yet.
pm_sr_wide <- function(st) names(st$Zt)[vapply(st$Zt, function(z) nrow(z) >= ncol(z), logical(1))]

## lme4's checks of the model's own random terms against the number of
## observations (check.nobs.vs.nlev and check.nobs.vs.nRE of the control, with
## lme4's rules: for lmer fits n must exceed the levels and the random effects
## of each term, for glmer fits equal is allowed), applied when
## pm_sr_fit_lme4() has turned them off for wide smooth blocks.
pm_sr_check_nlev <- function(reTrms, own, n, control, gaus) {
  if (!length(own)) return(invisible())
  act <- function(a) {
    a <- control$checkControl[[a]]
    if (is.null(a) || a == "ignore") NULL else a
  }
  over <- function(k) if (gaus) k >= n else k > n
  report <- function(a, msg) if (a == "stop") stop(msg, call. = FALSE) else warning(msg, call. = FALSE)
  if (!is.null(a <- act("check.nobs.vs.nlev"))) {
    bad <- own[vapply(own, function(g) over(nlevels(reTrms$flist[[g]])), logical(1))]
    if (length(bad)) {
      report(a, paste0("number of levels of each grouping factor must be ", if (gaus) "<" else "<=",
                       " number of observations (problems: ", paste(bad, collapse = ", "), ")"))
    }
  }
  if (!is.null(a <- act("check.nobs.vs.nRE"))) {
    terms <- which(names(reTrms$flist)[attr(reTrms$flist, "assign")] %in% own)
    for (k in terms) {
      nre <- nrow(reTrms$Ztlist[[k]])
      if (over(nre)) {
        report(a, paste0("number of observations (=", n, ") ", if (gaus) "<=" else "<", " number of random effects (=",
                         nre, ") for term (", names(reTrms$Ztlist)[k], "); the random-effects parameters and the ",
                         "residual variance (or scale parameter) are probably unidentifiable"))
      }
    }
  }
  invisible()
}

## A wide smooth block whose design Z has Z Z' proportional to the residual
## covariance (diag(1/w)) on the data rows (one column per observation, as a
## lookup of plots) has a variance that cannot be told apart from the residual
## variance: lme4 stops for the same model written as (1 | plot). The check
## uses two fixed probe vectors (Z Z' v = c v / w), O(n q) without forming
## Z Z'. Gaussian lme4 fits.
pm_sr_check_wide <- function(Zt, w) {
  for (b in names(Zt)) {
    Z <- Zt[[b]]
    n <- ncol(Z)
    wt <- if (is.null(w)) rep(1, n) else w
    d <- Matrix::colSums(Z^2)
    cw <- d * wt
    if (!all(abs(cw - mean(cw)) <= 1e-8 * mean(cw))) next
    same <- vapply(c(1.3, 2.9), function(a) {
      v <- sin(a * seq_len(n)) + cos(a * a * seq_len(n))
      u <- as.vector(Matrix::crossprod(Z, Z %*% v))
      max(abs(u - d * v)) <= 1e-8 * max(abs(d * v))
    }, logical(1))
    if (all(same)) {
      stop("The smooth block ", b, " has one independent column per observation (Z Z' proportional to the ",
           "residual covariance on the data rows): its variance cannot be told apart from the residual variance. ",
           "Use a basis that links neighbouring observations, or fewer columns.", call. = FALSE)
    }
  }
  invisible()
}

## glmmTMB: the structure (doFit = FALSE), the smooth designs put into Z,
## one fit.
pm_sr_fit_tmb <- function(tmbc, st, env) {
  tmbc$formula <- st$formula
  tmbc$doFit <- FALSE
  wide <- pm_sr_wide(st)
  if (length(wide)) {
    stop("The smooth block(s) ", paste(wide, collapse = ", "), " have at least as many columns as observations (",
         nrow(st$data), "); semireg_tmb() cannot fit them yet: use semireg() (lme4).", call. = FALSE)
  }
  tmbf <- pm_sr_eval(tmbc, st$data, env)
  rt <- tmbf$condList$reTrms
  tmbf$data.tmb$Z <- t(pm_sr_replace_Zt(rt$Zt, st$Zt, rt$flist, rt$Gp))
  list(semer = glmmTMB::fitTMB(tmbf), tmbf = tmbf)
}

## The covariance of the fixed and smooth coefficients, relative to sigma^2,
## and what semipred() needs (the coefficients of each smooth, its knots and
## type). With b = Lambda u, u ~ N(0, sigma^2 I) (as lme4), C~ = [X, Z Lambda]
## and T = diag(I, Lambda):
##   Cov(beta, b)/sigma^2 = T (C~' W C~ + diag(0, I))^-1 T'
## which equals (C' W C + D)^-1 with D = (Lambda Lambda')^-1 when Lambda is
## invertible, and needs no inverse of Lambda: a smooth whose variance is
## estimated as zero (a straight line) gets zero covariance. (Before 2.0 the
## second form was used, with nearPD() for a singular Lambda, which failed.)
##   X, Z, Lambda   of the fit; w: weights (length n)
##   uHat           random effects in the column order of Z, named
##   u_ind          number of columns of Z of each term, named by term
##   V              (optional) the covariance itself, relative to sigma^2
##                  (zero-inflated glmmTMB fits: from TMB's joint precision)
##   s2             sigma^2 when w already includes 1/sigma^2 (lme4 GLMMs with a
##                  dispersion parameter: Gamma, inverse.gaussian); the
##                  covariance is then divided by it, after the effective df
pm_sr_info <- function(X, Z, Lambda, w, uHat, u_ind, Zt_name, V = NULL, s2 = 1) {
  n_beta <- ncol(X)
  C <- cbind(X, Z)
  CTC <- Matrix::crossprod(C * w, C)
  Ct <- cbind(X, Z %*% Lambda)
  q <- ncol(Z)
  Tm <- Matrix::.bdiag(list(Matrix::Diagonal(n_beta), Lambda))
  M <- Matrix::crossprod(Ct * w, Ct) + Matrix::.bdiag(list(Matrix::Matrix(0, n_beta, n_beta), Matrix::Diagonal(q)))
  # solved with M scaled to unit diagonal: a random term whose variance is
  # much larger than the residual one (glmmTMB ar1() with the nugget
  # variance near 0) makes M badly scaled, not singular
  dM <- sqrt(Matrix::diag(M))
  dM[!is.finite(dM) | dM <= 0] <- 1
  Dm <- Matrix::Diagonal(x = 1/dM)
  Ms <- Matrix::forceSymmetric(methods::as(Dm %*% M %*% Dm, "generalMatrix"))
  fullCovMat <- if (is.null(V)) Tm %*% (Dm %*% Matrix::solve(Ms, Dm %*% Matrix::t(Tm))) else Matrix::Matrix(V)
  fullCovMat <- Matrix::forceSymmetric(methods::as(fullCovMat, "generalMatrix"))
  df <- sum(Matrix::diag(fullCovMat %*% CTC))              # effective degrees of freedom
  if (s2 != 1) fullCovMat <- fullCovMat/s2

  vcov_ind <- cumsum(c(n_beta, u_ind))
  names(vcov_ind) <- c("beta", names(u_ind))
  Zt_name <- intersect(names(u_ind), Zt_name)
  uHat_ind <- vcov_ind - n_beta
  cov_lst <- u_lst <- vector("list", length(Zt_name))
  names(cov_lst) <- names(u_lst) <- Zt_name
  block <- function(nm, ind) (ind[match(nm, names(ind)) - 1] + 1):ind[match(nm, names(ind))]
  for (i in Zt_name) {
    cov_lst[[i]] <- fullCovMat[c(seq_len(n_beta), block(i, vcov_ind)), c(seq_len(n_beta), block(i, vcov_ind))]
    u_lst[[i]] <- uHat[block(i, uHat_ind)]
  }
  # a term of several blocks (named term.level): its blocks together
  vcov_indN <- vcov_ind
  names(vcov_indN) <- gsub("\\..*", "", names(vcov_ind))
  vcov_indN <- rev(vcov_indN)[unique(names(vcov_indN))]
  for (i in setdiff(names(vcov_indN), names(vcov_ind))) {
    idx <- c(seq_len(n_beta), block(i, vcov_indN))
    cov_lst[[i]] <- fullCovMat[idx, idx]
  }
  sm_idx <- c(seq_len(n_beta), unlist(lapply(Zt_name, block, ind = vcov_ind)))
  cov_lst$sm_Cov <- fullCovMat[sm_idx, sm_idx]
  list(cov_lst = cov_lst, u_lst = u_lst, CovMat = fullCovMat, Cov_ind = vcov_ind, Cov_indN = vcov_indN, df = df)
}

## The "semireg" result of both front ends.
pm_sr_result <- function(semer, st, mc, info, extra) {
  if (is.null(info)) info <- list(cov_lst = NULL, u_lst = NULL, CovMat = NULL, Cov_ind = NULL, Cov_indN = NULL,
                                  df = NULL)
  # results computed later from the fit and kept for the next call
  # (pm_sr_uncond_cached()): an environment, so copies of the fit share it
  ans <- c(list(semer = semer, call = mc, data = st$data, fomul_vars = st$fomul_vars, sm_vars = st$sm_vars), info, extra,
           list(smooths = st$smooths, cache = new.env(parent = emptyenv())))
  class(ans) <- c("semireg", "list")
  pm_add_advice(ans, tryCatch(pm_semireg_advice(ans), error = function(e) NULL))
}

## pm_sr_uncond(), computed once per fit: the correction needs a numerical
## Hessian and Jacobian (lme4, many deviance evaluations) or TMB's joint
## precision and its inverse, and each semipred() or predictmeans() call on
## the fit used to repeat it. Kept in fit$cache (fits made before 2.0.0, or
## without a cache, compute it each time). NULL (not available) is kept too.
pm_sr_uncond_cached <- function(semireg) {
  cache <- semireg$cache
  if (!is.environment(cache)) return(pm_sr_uncond(semireg))
  if (!exists("uncond", envir = cache, inherits = FALSE)) assign("uncond", pm_sr_uncond(semireg), envir = cache)
  get("uncond", envir = cache, inherits = FALSE)
}

## print(), summary() and the extractors of a fit: R/semireg-methods.R.

## ---- Uncertainty of the smoothing parameters ----
##
## The covariance of the coefficients from pm_sr_info() is conditional on the
## variance parameters theta (the smoothing parameters): bands made from it
## covered the true curves too rarely in GLMMs (dev/sim-curve-difference.R).
## The first-order correction (Kass and Steffey 1989; Wood, Pya and Saefken
## 2016, mgcv's unconditional = TRUE) adds J V_theta J', with J the
## derivative of the estimated coefficients (beta, b) in theta and V_theta
## the covariance of theta-hat:
##   lmer     beta and b from the fit's deviance function (profiled, as
##            REML); V_theta from its Hessian (pm_lmer_varpar())
##   glmer    beta and b from the stage-1 deviance (theta only; beta and u
##            by PIRLS), V_theta from its Hessian
##   glmmTMB  the joint covariance of (beta, b) from TMB's joint precision of
##            all parameters, which includes theta's uncertainty
## Returns the matrix to add to CovMat (relative to sigma^2, as CovMat), or
## NULL with a message when it cannot be computed.
pm_sr_uncond <- function(semireg) {
  semer <- semireg$semer
  s2 <- stats::sigma(semer)^2
  if (inherits(semer, "glmmTMB")) {
    pm_need("TMB", "unconditional = TRUE for glmmTMB fits")
    sdr <- TMB::sdreport(semer$obj, getJointPrecision = TRUE)
    P <- sdr$jointPrecision
    nm <- rownames(P)
    ib <- which(nm == "beta")[seq_len(length(glmmTMB::fixef(semer)$cond))]
    iu <- which(nm == "b")[seq_len(ncol(lme4::getME(semer, "Z")))]
    Vj <- solve(P)[c(ib, iu), c(ib, iu)]
    return((Vj - as.matrix(semireg$CovMat) * s2)/s2)
  }
  th0 <- lme4::getME(semer, "theta")
  if (methods::is(semer, "semerMod")) {
    devfun <- pm_sr_devfun(semer)
    vp <- pm_lmer_varpar(semer)
    Vth <- vp$vcov_varpar[seq_along(th0), seq_along(th0), drop = FALSE]
  } else if (!is.null(semireg$devfun0)) {
    devfun <- semireg$devfun0()
    devfun(th0)
    h <- numDeriv::hessian(devfun, th0)
    Vth <- 2 * pm_vc_inv(h)
  } else {
    # (semipred() says so in its settings and advice, inference_fallback)
    return(NULL)
  }
  coefs <- function(th) {
    devfun(th)
    e <- environment(devfun)
    c(e$pp$beta(1), as.vector(e$pp$b(1)))
  }
  J <- numDeriv::jacobian(coefs, th0)
  devfun(th0)                                   # leave the deviance function at the optimum
  (J %*% Vth %*% t(J))/s2
}

## df for linear combinations L of the fixed and random coefficients (beta,
## u) of a semireg() fit (means and curves with the smooths' random effects;
## 'idx': the columns of (beta, u) that the columns of L are; 'terms': the
## model terms of the means). The one rule for predictmeans() and semipred()
## on semireg fits, as pm_inference() for other models:
##   gaussian     Satterthwaite (pm_sr_satt_ddf()), smoothing parameters held
##   generalized  inner-outer df (R/df-innerouter.R; the GLMM default) from
##                the design's grouping factors, the smooth blocks left out;
##                the smooth coefficients vary within every group, so a row
##                takes the df of the fixed coefficients it uses; Inf (z
##                tests, as glm with a known dispersion) when there is no
##                grouping factor besides the smooths
## Returns the df with attribute "method" (the label for settings).
## Kenward-Roger for contrasts of semireg means (gaussian lme4 engine, REML;
## the maintainer's decision of 2026-11-26): the rows of L (columns: the
## coefficients 'idx' of the fit, fixed effects first) that use fixed effects
## only (the smooth parts cancel, as for pairs of levels of a factor added to
## the smooth) get the KR-adjusted covariance and df of the mixed model
## (pm_vcov_kr(), R/kr.R; equal to predictmeans() of fit$semer); rows with
## smooth random coefficients, where KR is not defined, keep pm_sr_ddf().
## Returns list(rows (logical), V (their adjusted covariance), df) or NULL (no
## such row, a glmmTMB or ML fit, or a failed adjustment, with a message).
## Coverage of 95% pair limits: KR 94.9%, unadjusted 94.2-94.5%
## (dev/sim-semireg-kr/). Used by predictmeans() of semireg fits; semipred()
## differences of curves use the smooths and keep pm_sr_ddf().
pm_sr_kr <- function(semireg, L, idx) {
  semer <- semireg$semer
  if (!inherits(semer, "lmerMod") || !isTRUE(lme4::getME(semer, "is_REML"))) return(NULL)
  p <- length(lme4::fixef(semer))
  L <- as.matrix(L)
  fx <- idx <= p
  # (a row with NA: a pair that is not estimable, no KR)
  rows <- !apply(is.na(L), 1, any) & rowSums(abs(L[, !fx, drop = FALSE]) > 1e-10, na.rm = TRUE) == 0
  if (!any(rows)) return(NULL)
  Lf <- matrix(0, sum(rows), p)
  Lf[, idx[fx]] <- L[rows, fx, drop = FALSE]
  Vadj <- pm_try_vcov_kr(semer)
  if (is.null(Vadj)) return(NULL)
  df <- tryCatch(as.numeric(pm_kr_ddf(Lf, as.matrix(stats::vcov(semer)), Vadj)), error = function(e) {
    message("Kenward-Roger df failed (", conditionMessage(e), "); using Satterthwaite df and unadjusted SEs.")
    NULL
  })
  if (is.null(df)) return(NULL)
  list(rows = rows, V = Lf %*% as.matrix(Vadj) %*% t(Lf), df = df)
}

## The deviance function of a gaussian semireg() fit (semerMod): the stored
## one while it works; otherwise (a fit read back with readRDS(): its C++
## objects do not survive saving, "NULL value passed as symbol address";
## review-semireg-2 F3) one rebuilt by lme4::mkLmerDevfun() from the fit's R
## data (the frame with its weights and offset, X, Zt, Lambdat, theta), kept
## in the fit's cache. Left at the fitted theta. GLMM fits rebuild theirs
## from R data already (devfun0).
pm_sr_devfun <- function(semer) {
  info <- semer@semireg_info
  th <- lme4::getME(semer, "theta")
  ok <- function(f) is.function(f) && isTRUE(tryCatch(is.finite(f(th)), error = function(e) FALSE))
  if (ok(info$devfun)) return(info$devfun)
  cache <- info$cache
  if (is.environment(cache) && ok(cache$devfun)) return(cache$devfun)
  g <- function(nm) lme4::getME(semer, nm)
  rt <- list(Zt = g("Zt"), theta = th + 0, Lind = g("Lind"), Lambdat = g("Lambdat"), lower = g("lower"),
             flist = g("flist"), cnms = semer@cnms, Gp = g("Gp"))
  f <- lme4::mkLmerDevfun(semer@frame, g("X"), rt, REML = lme4::isREML(semer), start = th)
  f(th)
  if (is.environment(cache)) cache$devfun <- f
  f
}

## A gaussian fit (lmer, or glmmTMB with the gaussian family): the rule for
## Satterthwaite df and for the default of 'unconditional' (the smoothing-
## parameter correction is the default for generalized fits only).
pm_sr_gaussian <- function(semer) {
  if (inherits(semer, "glmmTMB")) stats::family(semer)$family == "gaussian" else lme4::isLMM(semer)
}

pm_sr_ddf <- function(semireg, L, idx, terms = NULL) {
  semer <- semireg$semer
  L <- as.matrix(L)
  if (pm_sr_gaussian(semer)) {
    df <- pm_sr_satt_ddf(semireg, L, idx)
    # NA: Satterthwaite failed at that row (normal there, said in the label and
    # by the advice); Inf: normal by design (a fit without Satterthwaite df)
    nfail <- sum(is.na(df))
    lab <- if (nfail == length(df) && nfail) "normal (semireg; Satterthwaite not available)" else
      if (any(is.finite(df))) "Satterthwaite (fixed + smooth coefficients)" else "normal (semireg)"
    if (nfail && nfail < length(df)) lab <- paste0(lab, "; normal at ", nfail, " of ", length(df), " rows")
    return(structure(df, method = lab, df_fallback = nfail))
  }
  io <- tryCatch(pm_inner_outer_df(semer, drop = names(semireg$refit$smoothZ)), error = function(e) NULL)
  if (is.null(io)) return(structure(rep(Inf, nrow(L)), method = "normal (semireg)"))
  p <- length(io)
  fx <- idx <= p
  Lf <- L[, fx, drop = FALSE]
  df <- pm_io_row_df(io, names(io)[idx[fx]], Lf, terms)
  # a row without fixed coefficients (smooth coefficients only): within groups
  df[is.na(df)] <- max(io)
  structure(df, method = paste0("inner-outer (design strata; smooths within",
                                 if (!identical(attr(io, "count"), "rows")) paste0("; ", attr(io, "count"), " counted"), ")"),
            df_cases = pm_df_cases(list(df_cases = attr(io, "count"), df_trials = attr(io, "trials_df"))))
}

## Satterthwaite df for linear combinations L of the fixed and random
## coefficients (beta, u) of a gaussian semireg() fit (lme4 engine), e.g.
## means at a covariate value, which include the smooths' random effects.
## As for fixed effects (R/satterthwaite.R), with the prediction error
## variance v(psi) = L C(psi) L' of (beta, u - u_hat) as a function of the
## variance parameters psi = (theta, sigma): df = 2 v^2 / (g' A g), g the
## gradient of v, A the covariance of psi (SAS: DDFM=SATTERTHWAITE for
## estimates that involve random effects). For L without random effects the
## df are those of pm_satt_ddf(). 'idx': the columns of (beta, u) that the
## columns of L are. Inf for other fits (normal-based tests).
## 'hold': random terms whose variance parameters are taken as known; by
## default the smooth blocks, so that the df count the design's variance
## components and the residual (the smoothing variances' uncertainty is what
## 'unconditional' adds). With them free, a point where a smoothing variance
## dominates got 2 df (WarsawApts); the coverage of bands was the same
## either way in dev/sim-semipred-df.R.
pm_sr_satt_ddf <- function(semireg, L, idx, hold = names(semireg$refit$smoothZ)) {
  semer <- semireg$semer
  L <- as.matrix(L)
  if (inherits(semer, "glmmTMB")) return(pm_sr_satt_ddf_tmb(semer, L, idx, hold))
  if (!methods::is(semer, "semerMod") || !lme4::isLMM(semer)) return(rep(Inf, nrow(L)))
  vp <- pm_lmer_varpar(semer)
  X <- lme4::getME(semer, "X")
  Z <- lme4::getME(semer, "Z")
  Lt <- lme4::getME(semer, "Lambdat")
  Lind <- lme4::getME(semer, "Lind")
  w <- semer@resp$weights
  p <- ncol(X)
  q <- ncol(Z)
  Lf <- matrix(0, nrow(L), p + q)
  Lf[, idx] <- L
  k <- length(vp$varpar)
  v <- function(psi) {
    Lt@x <- psi[-k][Lind]
    Lam <- Matrix::t(Lt)
    Ct <- cbind(X, Z %*% Lam)
    M <- Matrix::forceSymmetric(Matrix::crossprod(Ct * w, Ct) +
                                  Matrix::.bdiag(list(Matrix::Matrix(0, p, p), Matrix::Diagonal(q))))
    B <- Lf %*% Matrix::.bdiag(list(Matrix::Diagonal(p), Lam))
    psi[k]^2 * pm_quad_diag(M, B)
  }
  v0 <- v(vp$varpar)
  # variance components estimated at zero are left out (as Kenward-Roger,
  # R/kr.R): their derivative and variance at the boundary are not defined
  th <- lme4::getME(semer, "theta")
  bnd <- which(lme4::getME(semer, "lower") == 0 & th < 1e-6*max(1, max(abs(th))))
  # 'hold': random terms whose variance parameters are taken as known
  cnms <- lme4::getME(semer, "cnms")
  nth <- lengths(cnms)*(lengths(cnms) + 1)/2
  held <- which(rep(names(cnms), nth) %in% hold)
  free <- setdiff(seq_len(k), c(bnd, held))
  vfree <- function(x) { y <- vp$varpar; y[free] <- x; v(y) }
  G <- pm_fd_jac(vfree, vp$varpar[free], scale = 1e-2)         # R/deriv.R
  denom <- rowSums((G %*% pm_cov_given(vp$vcov_varpar, free)) * G)
  df <- 2*v0^2/denom
  df[!is.finite(df) | df <= 0] <- NA_real_
  df
}

## The covariance of the parameters 'keep' given the others (fixed at their
## estimates): V_kk - V_kb V_bb^-1 V_bk.
pm_cov_given <- function(V, keep) {
  b <- setdiff(seq_len(nrow(V)), keep)
  if (!length(b)) return(V[keep, keep, drop = FALSE])
  V[keep, keep, drop = FALSE] - V[keep, b, drop = FALSE] %*% pm_vc_inv(V[b, b, drop = FALSE]) %*% V[b, keep, drop = FALSE]
}

## glmmTMB's code of a covariance structure (glmmTMB:::.valid_covstruct; NA
## when the installed version has no such structure).
pm_tmb_code <- function(name) {
  codes <- tryCatch(utils::getFromNamespace(".valid_covstruct", "glmmTMB"), error = function(e) NULL)
  if (is.null(codes) || !name %in% names(codes)) NA_integer_ else as.integer(codes[[name]])
}

## The number of theta parameters a glmmTMB random term estimates: a propto()
## term one (its scale) and an equalto() term none, though blockNumTheta
## counts the supplied matrix's entries; other terms blockNumTheta.
pm_tmb_ntheta <- function(r) {
  code <- as.integer(r$blockCode)
  if (identical(code, pm_tmb_code("propto"))) 1 else if (identical(code, pm_tmb_code("equalto"))) 0 else
    as.numeric(r$blockNumTheta)
}

## The variance parameters of a glmmTMB fit (theta, thetazi and those named in
## 'extra', e.g. the residual's betad/betadisp) and their covariance: pn the
## names of parfull without b/bzi, use those that are variance parameters, pos
## their positions in parfull, V their covariance (NULL when it cannot be
## matched). vcov(full = TRUE) has one row per entry of pn, except with a
## propto()/equalto() term, where it lists every entry of the supplied matrix
## (mapped, not estimated); then TMB's cov.fixed (the free parameters, in the
## order of fit$par) gives the same block. Used by pm_sr_satt_ddf_tmb() and
## pm_tmb_varcomp().
pm_tmb_psi_vcov <- function(m, extra = c("betad", "betadisp")) {
  pf <- m$fit$parfull
  keep <- !names(pf) %in% c("b", "bzi")
  pn <- names(pf)[keep]
  vp <- c("theta", "thetazi", extra)
  use <- pn %in% vp
  V <- tryCatch(stats::vcov(m, full = TRUE), error = function(e) NULL)
  Vu <- if (!is.null(V) && nrow(V) == length(pn)) V[use, use, drop = FALSE] else {
    cf <- m$sdr$cov.fixed
    fp <- unname(names(m$fit$par))
    k <- fp %in% vp
    if (!is.null(cf) && identical(fp[k], pn[use])) cf[k, k, drop = FALSE]
  }
  list(pn = pn, use = use, pos = which(keep)[use], V = Vu)
}

## The same for a gaussian semireg_tmb() fit (glmmTMB engine; dispformula ~1,
## or ~0 with a residual covariance structure such as ar1()): psi is
## glmmTMB's own vector (theta, and the log residual SD unless dispformula =
## ~0), its covariance from vcov(full = TRUE) (or TMB's cov.fixed, below); the covariances of the random
## terms at a psi from VarCorr(), as pm_tmb_varcomp() does. (The df do not
## depend on the parameterisation.) Inf for other families, zero inflation
## or a dispersion model with covariates.
pm_sr_satt_ddf_tmb <- function(m, L, idx, hold = character(0)) {
  L <- as.matrix(L)
  inf <- rep(Inf, nrow(L))
  dp <- function(e) paste(deparse(e, width.cutoff = 500L), collapse = " ")
  if (stats::family(m)$family != "gaussian") return(inf)
  if (!dp(m$modelInfo$allForm$dispformula) %in% c("~1", "~0")) return(inf)
  if (!identical(dp(m$modelInfo$allForm$ziformula), "~0")) return(inf)
  pf <- m$fit$parfull
  ps <- pm_tmb_psi_vcov(m)
  pn <- ps$pn
  use <- ps$use
  pos <- ps$pos
  Vu <- ps$V
  if (is.null(Vu)) return(inf)
  on.exit(m$obj$env$report(m$fit$parfull))   # leave the TMB object as it was
  X <- glmmTMB::getME(m, "X")
  Z <- glmmTMB::getME(m, "Z")
  w <- tryCatch(as.numeric(stats::weights(m)), error = function(e) NULL)
  if (length(w) != nrow(X)) w <- rep(1, nrow(X))
  re <- pm_re_terms(m)
  p <- ncol(X)
  q <- ncol(Z)
  Lf <- matrix(0, nrow(L), p + q)
  Lf[, idx] <- L
  at <- function(x) {
    mm <- m
    mm$fit$parfull[pos] <- x
    for (nm in unique(pn[use])) mm$fit$par[names(mm$fit$par) == nm] <- x[pn[use] == nm]
    mm
  }
  v <- function(x) {
    mm <- at(x)
    vc <- glmmTMB::VarCorr(mm)$cond
    s <- stats::sigma(mm)
    Lam <- Matrix::bdiag(lapply(seq_along(re), function(j) {
      G <- matrix(as.numeric(vc[[j]]), nrow(vc[[j]]))
      Lg <- tryCatch(t(chol(G)), error = function(e) pm_chol_lower(G))   # (pm_chol_lower(): semidefinite G; slow for large G)
      kronecker(Matrix::Diagonal(re[[j]]$nl), Matrix::Matrix(Lg, sparse = TRUE))
    }))
    Ct <- cbind(X, Z %*% Lam)
    M <- Matrix::crossprod(Ct * (w/s^2), Ct) + Matrix::.bdiag(list(Matrix::Matrix(0, p, p), Matrix::Diagonal(q)))
    dM <- sqrt(Matrix::diag(M))
    dM[!is.finite(dM) | dM <= 0] <- 1
    Dm <- Matrix::Diagonal(x = 1/dM)
    Ms <- Matrix::forceSymmetric(methods::as(Dm %*% M %*% Dm, "generalMatrix"))
    B <- Lf %*% Matrix::.bdiag(list(Matrix::Diagonal(p), Lam))
    pm_quad_diag(Ms, B %*% Dm)
  }
  x0 <- pf[pos]
  v0 <- v(x0)
  # random terms with their variances estimated at zero are left out (their
  # log-SD parameters sit far out on a flat likelihood)
  pu <- pn[use]
  th_pos <- which(pu == "theta")
  nth <- vapply(m$modelInfo$reStruc$condReStruc, pm_tmb_ntheta, numeric(1))   # (a mismatch: no df)
  if (sum(nth) != length(th_pos)) return(inf)
  vc0 <- glmmTMB::VarCorr(m)$cond
  sds <- lapply(vc0, function(x) attr(x, "stddev"))
  # (the reference scale: terms with estimated variances; an equalto() term's supplied matrix has none)
  ref <- max(unlist(sds[nth > 0]), if (dp(m$modelInfo$allForm$dispformula) == "~1") stats::sigma(m))
  at0 <- vapply(sds, function(x) max(x) < 1e-3*ref, logical(1))
  bnd <- th_pos[rep(at0, nth)]
  held <- setdiff(th_pos[rep(names(m$modelInfo$reTrms$cond$cnms) %in% hold, nth)], bnd)
  free <- setdiff(seq_along(x0), c(bnd, held))
  # Held terms keep their ratio to the residual variance (the smoothing
  # parameter, as lme4's relative theta does): with the residual log-variance
  # parameter d (betad = log sigma^2, or log sigma in other glmmTMB versions),
  # x = A phi with theta_h = phi_h + c phi_d, and phi_h is held
  A <- diag(length(x0))
  d <- which(pu %in% c("betad", "betadisp"))
  if (length(held) && length(d) == 1) {
    ls <- log(stats::sigma(m))
    c_d <- if (abs(2*ls - x0[d]) < 1e-6*max(1, abs(ls))) 0.5 else if (abs(ls - x0[d]) < 1e-6*max(1, abs(ls))) 1 else 0
    A[held, d] <- c_d
  }
  move <- c(free, held)
  vmove <- function(x) { y <- x0; y[move] <- x; v(y) }
  G <- matrix(0, length(v0), length(x0))
  G[, move] <- pm_fd_jac(vmove, x0[move], scale = 1e-2)
  Ai <- solve(A)
  Vphi <- Ai %*% Vu %*% t(Ai)
  # (glmmTMB's covariance has NaN when its Hessian is not positive definite: no df, as below)
  if (!all(is.finite(Vphi))) return(rep(NA_real_, nrow(L)))
  G <- (G %*% A)[, free, drop = FALSE]
  Vf <- tryCatch(pm_cov_given(Vphi, free), error = function(e) NULL)
  if (is.null(Vf)) return(rep(NA_real_, nrow(L)))
  # The covariance of the free variance parameters (those at zero left out) is
  # the inverse of the Hessian's free block: when it is not positive definite
  # (glmmTMB's non-positive-definite Hessian, a variance near but not at zero)
  # there are no df: NA (label and advice say so), never a number from it
  if (length(free) && min(eigen(Vf, symmetric = TRUE, only.values = TRUE)$values) <= 0)
    return(rep(NA_real_, nrow(L)))
  denom <- rowSums((G %*% Vf) * G)
  df <- 2*v0^2/denom
  df[!is.finite(df) | df <= 0] <- NA_real_
  df
}

## diag(B M^-1 B') for a symmetric positive definite (sparse) M and the rows
## of B. Only the columns J that B uses matter: with S = (M^-1)[J, J] (from
## |J| solves with M's sparse Cholesky factor), diag = rowSums((B_J S) * B_J).
## The population curves of semipred() use the fixed and smooth coefficients
## only, a few dozen columns of a large M (the subjects' random effects).
pm_quad_diag <- function(M, B) {
  J <- which(Matrix::colSums(abs(B)) > 0)
  if (!length(J)) return(rep(0, nrow(B)))
  E <- Matrix::sparseMatrix(i = J, j = seq_along(J), x = 1, dims = c(nrow(M), length(J)))
  ch <- tryCatch(Matrix::Cholesky(M, perm = TRUE, LDL = FALSE), error = function(e) NULL)
  S <- as.matrix(if (is.null(ch)) Matrix::solve(M, E) else Matrix::solve(ch, E))[J, , drop = FALSE]
  BJ <- as.matrix(B[, J, drop = FALSE])
  rowSums((BJ %*% S) * BJ)
}

Try the predictmeans package in your browser

Any scripts or data that you put into this service are public.

predictmeans documentation built on Dec. 3, 2026, 5:07 p.m.