Skip to contents

Fits a linear mixed model for continuous responses under a KK matching-on-the-fly design. The matched-pair strata enter as a subject-level random intercept (1 | group_id), accounting for within-pair correlation.

When use_rcpp = TRUE (default) the likelihood is maximised by an internal Rcpp/L-BFGS routine that requires no external packages. Set use_rcpp = FALSE to fall back to glmmTMB.

The treatment coefficient \(\beta_T\) is on the response's natural (untransformed) scale — a mean difference, not a ratio or log-scale effect. likelihood_tier = "full": likelihood-ratio, score, and Wald tests are all available when the model converges (see $get_likelihood_test_spec() inherited from the shared count/GLMM likelihood plumbing). Validity requires the random-intercept-per-pair structure to correctly capture the design's matching dependence and the usual linear mixed model assumptions (conditional normality of responses and pair effects, correctly specified fixed-effects formula).

See also

Comparable Python API: statsmodels MixedLM. See also: Mixed model (Wikipedia).

Super class

Inference -> InferenceContinKKGLMM

Methods

+ inherited public methods from Inference


InferenceContinKKGLMM$new()

Initialize inference for the linear mixed model \(Y_i = \beta_0 + \beta_T W_i + X_i^\top \gamma + b_{g(i)} + \epsilon_i\), \(b_g \sim N(0, \sigma_b^2)\), \(\epsilon_i \sim N(0, \sigma_e^2)\), where \(g(i)\) is subject \(i\)'s matched-pair group id, \(W_i\) is the treatment indicator, \(X_i\) are covariates, and \(\beta_T\) is the treatment effect (mean difference on the response's natural scale). The random intercept \(b_g\) absorbs the within-pair correlation induced by matching, so \(\beta_T\)'s standard error correctly reflects the design.

Usage

InferenceContinKKGLMM$new(
  des_obj,
  model_formula = NULL,
  use_rcpp = TRUE,
  use_gls_fast_path = TRUE,
  use_gls_fast_path_bootstrap = FALSE,
  verbose = FALSE,
  smart_cold_start_default = NULL,
  optimization_alg = NULL
)

Arguments

des_obj

A completed Design object with a continuous response.

model_formula

Optional formula for covariate adjustment. If NULL (default), the formula from the design object is used and its pre-computed design matrix is reused. If a formula is provided, a new design matrix is constructed from the design's imputed covariates.

use_rcpp

Logical. If TRUE (default), use the optimised Rcpp Gaussian LMM implementation (no external package required). If FALSE, use glmmTMB.

use_gls_fast_path

Logical. If TRUE (default), use a fast GLS estimator (no optimisation) for estimate_only calls during randomisation inference once variance components are cached from a prior full fit. Statistically exact: fixing VC at the null-fit MLE and permuting only the treatment assignment gives a valid permutation test by exchangeability. Set FALSE to always run full L-BFGS.

use_gls_fast_path_bootstrap

Logical. If TRUE, also use the fast GLS estimator for non-studentised bootstrap draws (estimate_only = TRUE weighted calls). Asymptotically valid by the plug-in principle (VC orthogonal to beta_T in the Fisher information), but not exact in finite samples. Default FALSE.

verbose

Whether to print progress messages.

smart_cold_start_default

Whether to use smart cold start values.

optimization_alg

The optimization algorithm to use. Default is dispatched via policy.


InferenceContinKKGLMM$compute_estimate()

Fits the linear mixed model by maximum likelihood and returns \(\hat\beta_T\). When use_rcpp = TRUE (the default), the log-likelihood \(\ell(\beta, \sigma_b^2, \sigma_e^2)\) is maximized directly by an internal Rcpp/L-BFGS routine over \(\beta\) and the log-variance components; otherwise glmmTMB performs the fit. If use_gls_fast_path = TRUE and variance components are already cached from a prior full fit (used during randomization inference, where only the treatment column changes across permutations), estimate_only = TRUE calls instead solve the generalized least-squares problem at the cached variance components — exact under exchangeability of the permuted treatment assignment, since fixing the variance components at their null-fit MLE and permuting only \(W\) preserves the permutation test's validity.

Usage

InferenceContinKKGLMM$compute_estimate(estimate_only = FALSE)

Arguments

estimate_only

If TRUE, skip variance component calculations (standard error, degrees of freedom) needed for confidence intervals or p-values; only \(\hat\beta_T\) is returned.


InferenceContinKKGLMM$compute_estimate_with_bootstrap_weights()

Refits the linear mixed model with subject/block-level weights applied to each row's contribution to the likelihood (Bayesian-bootstrap or nonparametric-bootstrap draw weights, expanded from subject/block level to individual rows via private$expand_subject_or_block_weights_to_row_weights()), and returns the reweighted estimate \(\hat\beta_T^{(w)}\). When weights are effectively constant, this collapses to the unweighted compute_estimate() call (returns df = Inf to signal a degenerate/skipped bootstrap replicate rather than refitting). When use_rcpp = TRUE, a weighted Rcpp fast path is tried first via private$weighted_rcpp_estimate(); otherwise private$compute_weighted_glmm_bootstrap_estimate() refits via glmmTMB-based machinery.

Usage

InferenceContinKKGLMM$compute_estimate_with_bootstrap_weights(
  subject_or_block_weights,
  estimate_only = FALSE
)

Arguments

subject_or_block_weights

Numeric vector of nonnegative weights, one per matched-pair group or reservoir subject (bootstrap draw weights), expanded to per-row weights before fitting.

estimate_only

Logical. If TRUE, skip standard-error computation.

Returns

The reweighted treatment estimate \(\hat\beta_T^{(w)}\).


InferenceContinKKGLMM$compute_asymp_confidence_interval()

Computes a \(1-\alpha\) Wald confidence interval for \(\beta_T\) from the fitted-model standard error, using a normal or \(t\) critical value depending on the resolved degrees of freedom (see private$compute_z_or_t_ci_from_s_and_df).

Usage

InferenceContinKKGLMM$compute_asymp_confidence_interval(alpha = 0.05)

Arguments

alpha

The confidence level of the interval is \(1 - \code{alpha}\). Default 0.05.


InferenceContinKKGLMM$compute_asymp_two_sided_pval()

Computes a two-sided Wald p-value for \(H_0: \beta_T = \code{delta}\) using the fitted-model estimate and standard error (same statistic that $compute_asymp_confidence_interval() inverts).

Usage

InferenceContinKKGLMM$compute_asymp_two_sided_pval(delta = 0)

Arguments

delta

The null treatment-effect value. Default 0.


InferenceContinKKGLMM$clone()

The objects of this class are cloneable with this method.

Usage

InferenceContinKKGLMM$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# \donttest{
seq_des = DesignSeqOneByOneKK14$new(n = 10, response_type = 'continuous')
for (i in 1:10) {
  seq_des$add_one_subject_to_experiment_and_assign(data.frame(x1 = rnorm(1), x2 = rnorm(1)))
}
seq_des$add_all_subject_responses(rnorm(10))
inf = InferenceContinKKGLMM$new(seq_des)
inf$compute_estimate()
#> [1] -1.028912
# }