Mixed models#
Linear mixed models with random intercepts, fitted to many features by REML or ML.
Every feature (gene) of a samples x features matrix is fitted with the same model,
y = X beta + Z_1 b_1 + … + Z_K b_K + e, b_k ~ N(0, sigma_k^2 I), e ~ N(0, sigma^2 W^-1),
where X is the fixed-effects design, Z_k assigns each sample to a level of the k-th
grouping factor (one random intercept per level, as (1 | donor) in lme4), and W
holds the feature’s precision weights (the identity without weights). The grouping
factors may be nested, such as (1 | donor/sample), or crossed, such as
(1 | donor) + (1 | batch). The variance parameters are estimated by restricted
maximum likelihood (REML; Patterson and Thompson 1971), or by maximum likelihood
(method="ml"), and the fixed effects by generalised least squares given them, as
lme4’s lmer with REML = TRUE (or FALSE) does (Bates et al. 2015). Contrasts
of the fixed effects are tested one at a time by t-tests (fixed_effect_tests()),
or several at once by F-tests (fixed_effect_f_test()), with Satterthwaite’s
degrees of freedom, as lmerTest computes them (Kuznetsova et al. 2017), or Kenward and
Roger’s, as pbkrtest computes them (Halekoh and Hojsgaard 2014). variancePartition’s
dream uses both for repeated measures in gene expression (Hoffman and Roussos
2021); cytepool.stats.dream builds dream’s precision weights and moderated
tests on these fits.
- The REML criterion:
With theta_k = sigma_k / sigma, the relative standard deviations that lme4 estimates, and V = W^-1 + sum_k theta_k^2 Z_k Z_k’, the fixed effects and sigma^2 are profiled out: beta(theta) is the generalised least squares estimate, r^2(theta) = (y - X beta)’ V^-1 (y - X beta), and sigma^2(theta) = r^2 / (n - p). The criterion minimised is lme4’s profiled REML deviance (Bates et al. 2015, section 3.4),
log det V + log det(X’ V^-1 X) + (n - p) (1 + log(2 pi r^2 / (n - p))),
whose value at the estimate is lme4’s
REMLcrit. With weights, log det V includes -sum(log w), so the criterion, the fixed effects and the variance components do not change when every weight is multiplied by the same constant; only sigma^2 scales with it. Maximum likelihood minimises the profiled deviance log det V + n (1 + log(2 pi r^2 / n)), lme4’sdevianceof a fit withREML = FALSE, and estimates sigma^2 by r^2 / n.- Computation:
The criterion, its gradient and its Hessian are computed from weighted cross- products of the samples, without forming any n x n matrix. The random effects of the grouping factor with the most levels are absorbed in closed form: the covariance matrix of the samples of one of its levels is a diagonal matrix plus a matrix of rank one. The random effects of the other factors, and the fixed effects, enter a dense Cholesky factorisation whose size is their number of levels plus the number of coefficients, as in the penalised least squares formulation of lme4 (Bates et al. 2015, section 3). With one grouping factor each evaluation costs O(levels x coefficients^2). The fixed effects are handled in an orthonormal basis of the columns of X, which keeps the cross-products as well conditioned as X itself, and the estimates are transformed back.
The derivatives of the criterion with respect to gamma_k = theta_k^2 are the classical ones of REML (Harville 1977): with P = V^-1 - V^-1 X (X’ V^-1 X)^-1 X’ V^-1, they need only Z_k’ P Z_l and Z_k’ P y; those of maximum likelihood take the traces and norms of Z_k’ V^-1 Z_l in place of those of Z_k’ P Z_l, which the same factorisation gives before its fixed-effect pivots. Newton’s method minimises the criterion over u_k = log(1 + c_k gamma_k), where c_k is the mean total weight of a level of factor k: in a balanced one-way design the criterion is convex in u, and the boundary gamma_k = 0 is u_k = 0. A component whose gradient points outward at the boundary is held there (projected Newton method, Bertsekas 1982), so variance components at zero are exactly zero. An indefinite Hessian is made positive definite by taking the absolute values of its eigenvalues, a step changes no u_k by more than 5, and a backtracking line search ensures descent. The fit converges when a step moves no u_k by more than 1e-10, as a full Newton step or where the full step promises no decrease beyond rounding; as Newton’s method converges quadratically, the estimates are then as accurate as the cross-products allow. Those of the absorbed factor are taken about the means of its levels, which keeps them accurate however much the levels differ; those of the other factors enter the Cholesky factorisation scaled by their theta, as in lme4, and lose about as many digits as theta^2 times their levels’ weights has. Every fit starts at theta_k = 1 relative to the residual standard deviation of an observation of average weight: lme4’s default start (theta = 1) when there are no weights, and one that keeps the fit independent of the scale of the weights. Like lme4’s optimiser, Newton’s method finds a local minimum: when the criterion has several, the fit can stop at one that is not the lowest. When the fixed and random effects together reproduce a feature’s values, the criterion has no minimum (it decreases without bound as sigma^2 goes to zero), so such a feature is left unfitted; see
MixedModelFit.- Fitted values and residual degrees of freedom:
The random effects are predicted at the estimates by their best linear unbiased predictors, b_k = gamma_k Z_k’ P y (Henderson 1975), and the fitted values are X beta + sum_k Z_k b_k, lme4’s
fitted. Their hat matrix H, on the scale of the weighted values, gives the approximate residual degrees of freedom tr((I - H)’ (I - H)), variancePartition’srdf.merMod, whichdreammoderates on: with I - H = W^-1/2 P W^-1/2 and P V P = P,- tr((I - H)’ (I - H)) = n - p - 2 sum_k gamma_k tr(Z_k’ P Z_k)
sum_kl gamma_k gamma_l |Z_k’ P Z_l|^2,
which is n - p with every component at zero, and in a balanced one-way design q (m - 1) + (q - 1) / (1 + m gamma)^2 for q groups of m samples.
- Satterthwaite’s degrees of freedom:
For a contrast c of the fixed effects with estimated variance v = c’ C c, where C is the covariance matrix of the estimates, the degrees of freedom are 2 v^2 / (g’ A g) (Satterthwaite 1946; Giesbrecht and Burns 1985), where g holds the derivatives of v with respect to the variance parameters and A is their asymptotic covariance matrix: twice the inverse of the Hessian of the REML deviance (the observed information, as lmerTest computes it), or of the deviance of maximum likelihood for such a fit. The degrees of freedom do not depend on how the variance parameters are parametrised; here they are the variances (sigma_1^2, …, sigma_K^2, sigma^2). lmerTest parametrises them by (theta, sigma), in which the deviance is an even function of each theta_k, so a component estimated at zero contributes nothing to g’ A g; such components are left out here in the same way, and with every component at zero the degrees of freedom are n - p, those of the fixed-effects model (found by probing lmerTest). lmerTest computes the derivatives numerically; here they are exact.
- Kenward and Roger’s method:
With few samples the variance parameters are estimated imprecisely, and the covariance matrix C of the estimates, computed as if they were known, understates the variance of the fixed effects. Kenward and Roger (1997) add to C a correction for its own bias and for the variability of the variance parameters, of order 1 / n, to second order in their estimation error, whose covariance they take as the inverse of the expected information of REML; the t-statistic of a contrast uses the adjusted matrix, and its degrees of freedom are Satterthwaite’s formula with that expected information in place of the observed one. For an F-test of l contrasts they scale the Wald statistic and match its first two moments to an F distribution on (l, m) degrees of freedom. The covariance matrix of the observations is linear in the variance parameters, so the second derivatives that their formula also allows for vanish. A variance component estimated at zero takes part, as in pbkrtest, so that the degrees of freedom are smaller than Satterthwaite’s, which leave it out. In balanced designs, such as a randomised block design, the adjustment vanishes and the method gives the tests of the analysis of variance, which are exact there.
With weights, pbkrtest builds the covariance matrix of the observations without the prior weights of an lme4 fit, with the residual variance of an observation of weight one, while it takes C from the fit, which the weights enter (found by probing pbkrtest 0.5.3); lmerTest’s Kenward-Roger tests are pbkrtest’s. dream divides each feature’s weights by their mean before it fits, so that its tests are those of every observation at the average weight, which do not depend on the scale of the weights.
kenward_roger_weights="average"computes these."precision"uses each observation’s own weight, the covariance matrix of the model fitted: Kenward and Roger’s method for it, which pbkrtest gives for the same model fitted without weights to the values, fixed effects and random effects multiplied by the square roots of the weights. With voom’s weights the two can differ by tens of percent in the degrees of freedom and by several percent in the standard errors, and the first can make standard errors smaller than the unadjusted ones, which the second never does (the Validation page gives sizes). Each feature costs O(n^3) operations for n samples, as in pbkrtest.- F-tests:
fixed_effect_f_test()tests that the contrasts L beta are all zero. With Satterthwaite’s method, as lmerTest’scontestMD, the contrasts are replaced by the eigenvectors of their covariance matrix L C L’ with positive eigenvalues (in the order of R’seigen, the largest first), whose estimates are uncorrelated; F is the mean of their squared t-statistics, and its denominator degrees of freedom are 2 E / (E - l) with E = sum nu_m / (nu_m - 2) over their Satterthwaite degrees of freedom nu_m, which matches the mean of F (Fai and Cornelius 1996): their mean when they differ by less than 1e-8, and 2 when one is at most 2, as lmerTest’sget_Fstat_ddfdocuments. These depend on how the contrasts span the hypothesis, as lmerTest’s do. Kenward and Roger’s F-test, as pbkrtest’sKRmodcomp, does not. The F-test of one contrast is its t-test, on the same degrees of freedom even below 2, ascontestMDgives it (found by running it).- Limits of this version:
Random slopes are not implemented yet.
cytepool.variance_partitioningpartitions the variance of these fits. Every feature needs a value for every sample, with a positive weight. Designs whose fixed and random effects together span every sample, and so reproduce every feature exactly, are refused, although their criterion can have a minimum (lme4 fits them).
References
Bates D, Maechler M, Bolker B, Walker S (2015). Fitting linear mixed-effects models using lme4. Journal of Statistical Software 67(1), 1-48. doi:10.18637/jss.v067.i01
Bertsekas DP (1982). Projected Newton methods for optimization problems with simple constraints. SIAM Journal on Control and Optimization 20, 221-246. doi:10.1137/0320018
Fai AH-T, Cornelius PL (1996). Approximate F-tests of multiple degree of freedom hypotheses in generalized least squares analyses of unbalanced split-plot experiments. Journal of Statistical Computation and Simulation 54, 363-378. doi:10.1080/00949659608811740
Giesbrecht FG, Burns JC (1985). Two-stage analysis based on a mixed model: large-sample asymptotic theory and small-sample simulation results. Biometrics 41, 477-486. doi:10.2307/2530872
Golub GH, Van Loan CF (2013). Matrix Computations, 4th edition. Johns Hopkins University Press, Baltimore.
Halekoh U, Hojsgaard S (2014). A Kenward-Roger approximation and parametric bootstrap methods for tests in linear mixed models: the R package pbkrtest. Journal of Statistical Software 59(9), 1-32. doi:10.18637/jss.v059.i09
Harville DA (1977). Maximum likelihood approaches to variance component estimation and to related problems. Journal of the American Statistical Association 72, 320-338. doi:10.1080/01621459.1977.10480998
Henderson CR (1975). Best linear unbiased estimation and prediction under a selection model. Biometrics 31, 423-447. doi:10.2307/2529430
Hoffman GE, Roussos P (2021). Dream: powerful differential expression analysis for repeated measures designs. Bioinformatics 37, 192-201. doi:10.1093/bioinformatics/btaa687
Kenward MG, Roger JH (1997). Small sample inference for fixed effects from restricted maximum likelihood. Biometrics 53, 983-997. doi:10.2307/2533558
Kuznetsova A, Brockhoff PB, Christensen RHB (2017). lmerTest package: tests in linear mixed effects models. Journal of Statistical Software 82(13), 1-26. doi:10.18637/jss.v082.i13
Patterson HD, Thompson R (1971). Recovery of inter-block information when block sizes are unequal. Biometrika 58, 545-554. doi:10.1093/biomet/58.3.545
Satterthwaite FE (1946). An approximate distribution of estimates of variance components. Biometrics Bulletin 2, 110-114. doi:10.2307/3002019
- class cytepool.stats.mixed_models.FixedEffectFTest(fit, contrasts, contrast_names, df_method, kenward_roger_weights, f_statistic, df1, df2, p_value, scaling)#
F-tests that several contrasts of the fixed effects are all zero.
One test per feature, of the hypothesis that every contrast is zero; the arrays are read-only.
- Variables:
fit – The mixed model fit.
contrasts –
(n_coefficients, n_contrasts)weights of each contrast on the fit’s coefficients.contrast_names – Names of the contrasts.
df_method –
"satterthwaite"or"kenward_roger".kenward_roger_weights –
"average"or"precision"for Kenward and Roger’s method, as inFixedEffectTests; None for Satterthwaite’s.f_statistic –
(n_features,)F-statistics; Kenward and Roger’s are scaled byscaling.df1 – The numerator degrees of freedom, the number of linearly independent contrasts.
df2 –
(n_features,)denominator degrees of freedom.p_value –
(n_features,)p-values off_statisticondf1anddf2degrees of freedom.scaling –
(n_features,)the factor by which Kenward and Roger scale the Wald statistic (pbkrtest’sF.scaling); 1 for Satterthwaite’s method.
Explained in: Mixed models, REML and dream
- class cytepool.stats.mixed_models.FixedEffectTests(fit, contrasts, contrast_names, df_method, kenward_roger_weights, estimate, standard_error, t, df, p_value, covariance)#
t-tests of contrasts of the fixed effects of a mixed model fit.
Rows of the arrays are features and columns are contrasts. The arrays are read-only.
- Variables:
fit – The mixed model fit.
contrasts –
(n_coefficients, n_contrasts)weights of each contrast on the fit’s coefficients.contrast_names – Names of the contrasts.
df_method – How the degrees of freedom were computed:
"satterthwaite"or"kenward_roger".kenward_roger_weights – For Kenward and Roger’s method, how its adjustment took the weights:
"average"or"precision"(seefixed_effect_tests()); None for Satterthwaite’s.estimate –
(n_features, n_contrasts)estimates c’ beta.standard_error –
(n_features, n_contrasts)standard errors (c’ C c)^(1/2), with C the covariance matrix of the coefficients: the fit’s (cov_coefficients) for Satterthwaite’s method, Kenward and Roger’s adjusted one for theirs.t –
(n_features, n_contrasts)t-statistics, the estimates over their standard errors.df –
(n_features, n_contrasts)degrees of freedom; NaN where they are not defined, as where the asymptotic covariance of the variance parameters is not.p_value –
(n_features, n_contrasts)two-sided p-values oftondfdegrees of freedom.covariance –
(n_features, n_contrasts, n_contrasts)the covariance matrix of the estimates, L’ C L for the contrasts L and the C ofstandard_error.
Explained in: Mixed models, REML and dream
- class cytepool.stats.mixed_models.MixedModelFit(coefficients, cov_coefficients, variance_components, residual_variance, reml_criterion, boundary, converged, iterations, variance_parameter_covariance, cov_coefficients_derivatives, coefficient_names, component_names, design, method, log_likelihood, fitted_values, residual_df, mean_weights, mean_expression, weights, component_levels)#
REML or maximum likelihood fits of one linear mixed model to many features.
Rows of the per-feature arrays are features, in the order of the columns of
y. Variance components are in the order of the design’s random effects. The arrays are read-only. A feature whose criterion has no minimum is not fitted: its values are NaN and it is not converged. That is a feature the fixed effects fit exactly, such as a constant feature (to within 1e-12 of its values), or one the fixed and random effects together reproduce, such as a feature constant within every level of a grouping factor (to within 1e-6 of its residuals from the fixed effects): the criterion then decreases without bound as the residual variance goes to zero, as lme4’s fit fails to converge.- Variables:
coefficients –
(n_features, n_coefficients)estimates of the fixed effects (lme4’sfixef).cov_coefficients –
(n_features, n_coefficients, n_coefficients)covariance matrix of the estimates (lme4’svcov).variance_components –
(n_features, n_components)variance of each random intercept, sigma_k^2 (lme4’sVarCorr).residual_variance –
(n_features,)residual variance sigma^2 of an observation of weight one (sigma(fit)^2); the REML estimate r^2 / (n - p), or for a maximum likelihood fit r^2 / n.reml_criterion –
(n_features,)REML criterion at the estimates, minus twice the restricted log-likelihood (lme4’sREMLcrit); NaN for a maximum likelihood fit.boundary –
(n_features, n_components)True where a variance component is estimated at zero, which lme4 reports as a singular fit.converged –
(n_features,)False where Newton’s method did not converge within 100 iterations or could not decrease the criterion further, or the feature was not fitted.iterations –
(n_features,)Newton iterations of each fit.variance_parameter_covariance –
(n_features, n_components + 1, n_components + 1)asymptotic covariance matrix of the estimates of the variance components and the residual variance, in that order: twice the inverse of the Hessian of the REML deviance, or of the deviance of a maximum likelihood fit. Rows and columns of components at the boundary are zero; NaN where the Hessian is singular.cov_coefficients_derivatives –
(n_features, n_components + 1, n_coefficients, n_coefficients)derivatives ofcov_coefficientswith respect to each variance component and the residual variance, at the estimates.coefficient_names – Names of the fixed effects.
component_names – Names of the grouping factors of the variance components, as lme4 names them, such as
"donor"or"sample:donor".design –
(n_samples, n_coefficients)fixed-effects design matrix.method –
"reml"or"ml", the likelihood the fit maximised.log_likelihood –
(n_features,)the maximised log-likelihood: the restricted one, minus half the REML criterion, for a REML fit, and the likelihood itself for a maximum likelihood fit (lme4’slogLik).fitted_values –
(n_samples, n_features)fitted values X beta + Z b, with the random effects b at their best linear unbiased predictions, gamma_k Z_k’ P y (lme4’sfitted, which includes them).residual_df –
(n_features,)approximate residual degrees of freedom, tr((I - H)’ (I - H)) for the matrix H that maps the weighted values to the weighted fitted values (variancePartition’srdf.merMod). It is n - p when every variance component is zero, and falls towards n minus the rank of the fixed and random effects together as they grow.mean_weights –
(n_features,)mean of each feature’s weights (one without weights):residual_variance / mean_weightsis the residual variance of an observation of the feature’s average weight.mean_expression –
(n_features,)unweighted mean of each feature’s values (limma’sAmean, R’srowMeansto the last bit).weights – The precision weights of the fit, as given:
(n_samples,)sample weights,(n_samples, n_features)observation weights, or None.component_levels –
(n_components, n_samples)the level of each sample in the grouping factor of each variance component, numbered from 0.
Explained in: Mixed models, REML and dream
- cytepool.stats.mixed_models.fit_mixed_model(y, design, *, weights=None, method='reml', n_threads=None)#
Fit a linear mixed model with random intercepts to every feature by REML or ML.
Each column of
yis fitted with the fixed effects and the random intercepts ofdesign, as lme4’slmer(y ~ ... + (1 \| donor), REML = TRUE, weights = w)fits one feature: see the module docstring for the model, the criterion and the algorithm. Test contrasts of the fixed effects withfixed_effect_tests().- Parameters:
y –
(n_samples, n_features)expression values on a log scale, such as the log-CPM values of voom. Every value must be finite: there are no missing values in this version.design – A
Designwhose random effects are random intercepts of one or more grouping factors, such asrandom="(1 | donor)","(1 | donor) + (1 | batch)"or"(1 | donor/sample)". Its fixed effects must have full column rank, and every random effect must passdesign.check().weights – Precision weights,
(n_samples,)for sample weights or(n_samples, n_features)for observation weights (as from voom): the residual variance of an observation is sigma^2 / weight. They must be finite and positive.method –
"reml"for restricted maximum likelihood, lme4’s default (REML = TRUE), or"ml"for maximum likelihood (REML = FALSE), which variancePartition’svoomWithDreamWeightsfits to find its precision weights.n_threads – The most threads for fitting features, as for
fit_linear_model.Noneuses numba’s setting for the calling thread (seenumba.set_num_threads). The result does not depend on it. The features are fitted in runs on Python threads, in compiled code that releases the GIL, so no numba threading layer is involved; with one thread they are fitted on the calling thread.
- Returns:
The fits. They store, for every feature, the derivatives of the covariance matrix of the coefficients with respect to each variance parameter:
n_features * (n_components + 1) * n_coefficients**2floats, and the fitted values, as many floats asy.- Raises:
TypeError – If
designis not aDesign, an input is not real-valued,methodis not a string,n_threadsis not an integer, or an input is or holds a numpy masked array.ValueError – If shapes disagree,
yis not finite, a weight is not finite and positive,methodis unknown,n_threadsis below 1, the design has no random effects or one that is not a random intercept, its fixed effects are not of full column rank, a random effect fails the design checks, or the fixed and random effects together span every sample.
Example
>>> import numpy as np >>> from cytepool.design import Design >>> samples = {"condition": ["ctrl", "stim"] * 4, ... "donor": ["D1", "D1", "D2", "D2", "D3", "D3", "D4", "D4"]} >>> design = Design(samples, "~ condition", random="(1 | donor)") >>> y = np.random.default_rng(1).normal(5.0, 1.0, (8, 3)) >>> fit = fit_mixed_model(y, design) >>> fit.component_names ('donor',)
Explained in: Mixed models, REML and dream
- cytepool.stats.mixed_models.fixed_effect_f_test(fit, contrasts, *, names=None, df_method='satterthwaite', kenward_roger_weights='average')#
F-tests that several contrasts of the fixed effects are all zero.
One test per feature of the hypothesis L beta = 0, for the contrasts in the columns of L, such as every difference among three time points or the levels of an interaction. With
df_method="satterthwaite"it is lmerTest’scontestMD(fit, L): the Wald statistic over the number of independent contrasts, whose denominator degrees of freedom combine those of independent contrasts that span the hypothesis (Fai and Cornelius 1996; Kuznetsova et al. 2017). With"kenward_roger"it is pbkrtest’sKRmodcomp(fit, L)(and lmerTest’scontestMD(fit, L, ddf = "Kenward-Roger")): the Wald statistic with the adjusted covariance matrix, scaled, on Kenward and Roger’s degrees of freedom. The module docstring gives both. Contrasts that are linear combinations of the others add nothing to the hypothesis: the numerator degrees of freedom are the number of independent contrasts, by the rank rule of R’sqr().Contrasts are matched to the coefficients as by
fixed_effect_tests().- Parameters:
fit – A fit from
fit_mixed_model(); by REML for Kenward and Roger’s method.contrasts – The contrasts, one per column of a
(n_coefficients, n_contrasts)matrix, in any formfixed_effect_tests()takes.names – Contrast names, when
contrastsis not a mapping or aContrasts.df_method –
"satterthwaite"or"kenward_roger".kenward_roger_weights –
"average"or"precision", as forfixed_effect_tests().
- Returns:
The F-statistic, its degrees of freedom and its p-value for every feature.
- Raises:
TypeError – As
fixed_effect_tests().ValueError – As
fixed_effect_tests(), or if every contrast is zero.
Explained in: Mixed models, REML and dream
- cytepool.stats.mixed_models.fixed_effect_tests(fit, contrasts=None, *, names=None, df_method='satterthwaite', kenward_roger_weights='average')#
t-tests of contrasts of the fixed effects, by Satterthwaite or Kenward-Roger.
For a contrast c, the t-statistic is c’ beta / (c’ C c)^(1/2). With
df_method="satterthwaite", C is the fit’s covariance matrix and the degrees of freedom are Satterthwaite’s, as lmerTest’scontest(fit, L, joint = FALSE)andsummarycompute them. With"kenward_roger", C is Kenward and Roger’s adjusted covariance matrix, which allows for the estimation of the variance parameters, and the degrees of freedom are theirs, as lmerTest’scontest1D(fit, L, ddf = "Kenward-Roger")computes them with pbkrtest’svcovAdjandget_Lb_ddf. The module docstring gives both methods. Without contrasts, each coefficient is tested, as in lmerTest’s coefficient table.fixed_effect_f_test()tests several contrasts at once.Contrasts that name the coefficients they weigh are matched to
fit.coefficient_namesby name, as bycontrasts_fit(): aContrasts(frommake_contrastsorDesign.contrasts) by itscolumns, and a pandas DataFrame or Series by its index. Weights without names are taken in the order of the coefficients.- Parameters:
fit – A fit from
fit_mixed_model(); by REML for Kenward and Roger’s method.contrasts – The contrasts, one per column of a
(n_coefficients, n_contrasts)matrix: aContrasts, a pandas DataFrame, a mapping of contrast name to weights, a matrix or a single vector. None tests every coefficient.names – Contrast names, when
contrastsis not a mapping or aContrasts; seecontrasts_fit.df_method –
"satterthwaite"(lmerTest’s default) or"kenward_roger".kenward_roger_weights – How Kenward and Roger’s adjustment and degrees of freedom take a fit’s weights:
"average"computes them with every observation at its feature’s average weight, as variancePartition’sdreamdoes with pbkrtest, which leaves out the weights of an lme4 fit (lmerTest’sddf = "Kenward-Roger"gives the same for weights of mean one);"precision"with each observation’s own weight, the covariance of the model fitted. The two agree without weights or with equal weights; see the module docstring. Ignored for Satterthwaite’s method.
- Returns:
The estimates, standard errors, t-statistics, degrees of freedom and p-values of every feature and contrast.
- Raises:
TypeError – If
fitis not aMixedModelFit, orcontrastsornamesis not valid or is or holds a numpy masked array.ValueError – If the contrasts do not match the coefficients or are not finite,
df_methodorkenward_roger_weightsis unknown, or Kenward and Roger’s method is asked of a maximum likelihood fit.
Explained in: Mixed models, REML and dream