Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
150 commits
Select commit Hold shift + click to select a range
6442798
Added build_check script and check output
GregForkutza Mar 25, 2025
c479ce2
Delete build_check.sh
GregForkutza May 12, 2025
f6ee5e2
Delete submission directory
GregForkutza May 12, 2025
23df847
Merge branch 'master' into flexSigma
bbolker May 12, 2025
5164904
Merge remote-tracking branch 'origin/master' into flexSigma
GregForkutza Jun 10, 2025
5b9b7f2
Merge branch 'flexSigma' of github.com:lme4/lme4 into flexSigma
GregForkutza Jun 10, 2025
3b75090
Start implementing structured covariance class definitions
GregForkutza Jun 10, 2025
d0bce0e
feat: Implemented foundational S4 covariance structure classes and Di…
GregForkutza Jun 11, 2025
2c700a4
test: Add initial test for DiagonalCov creation
GregForkutza Jun 11, 2025
9856f91
test: Add method and edge cases for DiagonalCov.
GregForkutza Jun 12, 2025
a15e5f8
Merge branch 'master' into flexSigma
GregForkutza Jun 13, 2025
1828d3b
feat: Implemented Unstructured covariance class definition and methods.
GregForkutza Jun 13, 2025
80324f5
refactor(cov): Simplify S4 class structure based on mentor feedback.
GregForkutza Jun 14, 2025
69984a0
fix: Correct vech_diag_indices and get_chol_from_params and get_vech_…
GregForkutza Jun 16, 2025
8a4bb84
fix: Calculate log-det correctly for d=1 UnstructuredCov.
GregForkutza Jun 16, 2025
7c8d222
feat: Add tests for UnstructuredCov class and refactor: DiagonalCov t…
GregForkutza Jun 16, 2025
62a1961
wip: feat(cov): Implement CompoundSymmetryCov class and methods.
GregForkutza Jun 19, 2025
df30fd6
feat(cov): Add helper function for compound symmetry params
GregForkutza Jun 19, 2025
db7c7e4
feat(cov): Implement CompoundSymmetryCov class and methods
GregForkutza Jun 21, 2025
2a6dcbe
test: Add tests for CompoundSymmetryCov
GregForkutza Jun 21, 2025
9685030
feat(covariance): Implement HeterogeneousCSCov and refactor all show …
GregForkutza Jun 21, 2025
ca984be
feat(helpers): Add HCS helper and fix Matrix deprecation warning
GregForkutza Jun 21, 2025
0e649ea
test(covariance): Add full test suite for HeterogeneousCSCov and alig…
GregForkutza Jun 21, 2025
f6c2672
feat(covariance): Introduce composer class for covariance models
GregForkutza Jun 23, 2025
1d08d06
refactor(helpers)
GregForkutza Jun 23, 2025
cbe1ff2
test(covariance): Update tests to validate new composer design
GregForkutza Jun 23, 2025
ef03ef4
eat(cov): refactor to decouple var/cor params without inspecting S4 o…
GregForkutza Jun 25, 2025
9829a7e
refactor(helpers): update helper functions for refactored covariance …
GregForkutza Jun 25, 2025
f4bbc51
test(cov): update test suite for refactored covariance structures
GregForkutza Jun 25, 2025
4f00f17
feat(formula): parse covariance structures and instantiate S4 objects
GregForkutza Jun 25, 2025
b2682f0
feat: Internal helper functions that produce S4 objects from model fo…
GregForkutza Jun 26, 2025
4298a20
attempting to get get_lambdat method to work for CS. fixed get_lind a…
GregForkutza Jun 26, 2025
29533c8
fix: lambdat,lind and lambda construction methods are now correct
GregForkutza Jun 27, 2025
aaeaaa1
fix: replace `setIs()` with `contains = ""`
GregForkutza Jun 30, 2025
1218214
refactor: Use S4 dispatch for interpretable covariance parameters
GregForkutza Jun 30, 2025
9f2c0f7
n_paramters now returns an integer value.
GregForkutza Jul 1, 2025
663796f
fix: use cnms for dimension
GregForkutza Jul 1, 2025
fbdc406
fix: n_paramters correctly gives the number of unique, estiamted valu…
GregForkutza Jul 1, 2025
7434692
fix(s4): Correct parameter splitting in set_parameters method
GregForkutza Jul 1, 2025
1564261
refactor(s4): Use S4 dispatch for get_start_values method
GregForkutza Jul 1, 2025
25ff95e
feat: Add specific get_lower_bounds methods for each covariance struc…
GregForkutza Jul 1, 2025
2f369d6
Revert "feat: Add specific get_lower_bounds methods for each covarian…
GregForkutza Jul 1, 2025
7e85190
feat: Add specific get_lower_bounds methods for each covariance struc…
GregForkutza Jul 1, 2025
fb38204
fix: Handle unstructured case in initialize method
GregForkutza Jul 1, 2025
786a6a2
fix: Replace Diagonal with sparseMatrix in get_lambda for DiagonalCov…
GregForkutza Jul 1, 2025
6d22b30
fix: Correct theta index mapping in get_lind methods for unstructure…
GregForkutza Jul 1, 2025
b268de1
fix: Correct syntax error from missing bracket
GregForkutza Jul 1, 2025
100b571
feat: Implement mkReLambdat function
GregForkutza Jul 1, 2025
6c1f089
fix: Arguments passed to parse_model_formula is now correct
GregForkutza Jul 4, 2025
9383463
WIP: lFormula structured covariance - two issues need feedback
GregForkutza Jul 4, 2025
00ee585
test for calling lmer()
GregForkutza Jul 4, 2025
78c472d
Fix: lFormula model frame creation
GregForkutza Jul 4, 2025
764b7e1
dummy functions that allow covariance syntax to be found
GregForkutza Jul 4, 2025
b54efed
remove useless covariance structure parsing code
GregForkutza Jul 6, 2025
a3cadcc
Rename build_correlation_matrix to compute_correlation_matrix
GregForkutza Jul 6, 2025
fe95d62
feat: helper function to handle matrix coercion. Avoids depreciation …
GregForkutza Jul 6, 2025
72ba041
fix: compute_cor & compute_cov compatible with updated n_parm method.
GregForkutza Jul 7, 2025
3861ee2
helper function for compute_log_det
GregForkutza Jul 7, 2025
b69fbdc
feat: helper function for compute_inverse_covariance_matrix
GregForkutza Jul 7, 2025
8923462
fix compute_x methods
GregForkutza Jul 7, 2025
d018320
Fix: Remove homo/het distinction for unstructured.unstructured is no …
GregForkutza Jul 7, 2025
d271176
fix(formula): handle UnstructuredCovariance class in formula parser
GregForkutza Jul 7, 2025
7635afa
Add validity constraint: require dimension >= 1 for covariance struct…
GregForkutza Jul 7, 2025
b6f7d10
Fix CS/AR1 parameter counting and simplify parameter methods
GregForkutza Jul 7, 2025
28415f9
Add helper functions for CS/AR1 to eliminate code duplication
GregForkutza Jul 7, 2025
9d9e48a
- Remove dimension 0 special case handling in computation methods
GregForkutza Jul 8, 2025
d38e7a6
Handle diag conflict between lme4,base and Matrix
GregForkutza Jul 8, 2025
2542ec1
diag() to dcov() syntax to avoid NAMESPACE conflicts
GregForkutza Jul 8, 2025
eac334a
change syntax from diag() to dcov() to avoid conflicts
GregForkutza Jul 8, 2025
edfb2f2
add hook method for ar1 theta vector
GregForkutza Jul 10, 2025
f2d1cbf
Complete VarrCorr implementation expect for VarCorr.merMod and lFomru…
GregForkutza Jul 10, 2025
3d2d55b
VarCorr Implementation complete and working
GregForkutza Jul 10, 2025
d9e36f5
Fix VarCorr for structured covariance matrices
GregForkutza Jul 10, 2025
81fe4ec
Fix scaling issue
GregForkutza Jul 10, 2025
30dfc70
move denv creation before it is called
GregForkutza Jul 10, 2025
875f6c1
feat(varcorr):add logic to handle ar1 extended theta
GregForkutza Jul 10, 2025
a028773
fix function name
GregForkutza Jul 11, 2025
ef88c8b
covariance scaling fix for VarrCorr
GregForkutza Jul 11, 2025
c393d16
Fix getME naming bug
GregForkutza Jul 11, 2025
5a99773
fix tnames to be backwards compatible with us
GregForkutza Jul 11, 2025
ff144e1
add checks for flexSigma [run ci]
bbolker Jul 11, 2025
61c6cc6
trigger GH actions (?) [run ci]
bbolker Jul 11, 2025
468e7ac
Merge branch 'flexSigma' of github.com:lme4/lme4 into flexSigma
bbolker Jul 11, 2025
b73ac19
trigger GH actions (?) try again [run ci]
bbolker Jul 11, 2025
3a9006f
comment out non-Linux LaTeX GH actions stuff [run ci]
bbolker Jul 11, 2025
1eb369b
refactored formulas parseing and cov structure detection
GregForkutza Jul 13, 2025
2858079
Merge branch 'flexSigma' of github.com:lme4/lme4 into flexSigma
GregForkutza Jul 13, 2025
52383c5
Trigger CI build [run ci]
GregForkutza Jul 13, 2025
a1b700a
decision tree method did not work so revert back to string parsing
GregForkutza Jul 13, 2025
608a1bc
Fix grepl vector issue in formula parsing
GregForkutza Jul 13, 2025
219f00c
Trigger CI build [run ci]
GregForkutza Jul 13, 2025
294bc03
Replace dcov with diag throughout codebase and removed R/formula-wrap…
GregForkutza Jul 13, 2025
a0a7571
Remove global functions wrappers and refactor model formula string pa…
GregForkutza Jul 15, 2025
2e1d20f
change ar1 and cs param from tanh/atanh to match glmmTMB
GregForkutza Jul 16, 2025
d397614
reparameterize ar1 using glmmTMB parameterization
GregForkutza Jul 16, 2025
73f2fda
ar1 only needs extended paramter vector for n> 2
GregForkutza Jul 16, 2025
bf204bd
playing with AR1
bbolker Jul 18, 2025
60a0fdf
fix correlation parameterization direction
GregForkutza Jul 19, 2025
4bc2e70
fix cs compute_covariance and ar1 param issue
GregForkutza Jul 20, 2025
6da06da
fix starting values to be consistent
GregForkutza Jul 20, 2025
e7721ef
explicit Cholesky factor of CS
bbolker Jul 22, 2025
ff2b4a1
Merge branch 'master' into flexSigma
jaganmn Jul 23, 2025
2d74833
eliminate noise in R CMD check output
jaganmn Jul 23, 2025
ed5697d
working on tests (skip covstruc tests for now)
bbolker Jul 24, 2025
cbb0e7d
Merge branch 'master' into flexSigma
jaganmn Jul 24, 2025
9ba0ac3
remove debug text
GregForkutza Jul 25, 2025
b98f5fd
updated test for diag and us for refactored code
GregForkutza Jul 25, 2025
5a1292a
Merge remote-tracking branch 'origin/master' into flexSigma
GregForkutza Jul 25, 2025
60716fb
store raw sd values
GregForkutza Jul 29, 2025
eee875e
use raw sd values
GregForkutza Jul 29, 2025
95aa2a4
store raw sd values
GregForkutza Jul 29, 2025
882219c
start values should have 1 on diagonal and 0 on off-diagonal
GregForkutza Jul 29, 2025
b13e8f2
start values should be 1 on diagonal and 0 on the off-diagonal
GregForkutza Jul 29, 2025
3f8e356
use raw sd values
GregForkutza Jul 29, 2025
0465b34
Fix diag() homogeneity default to align with double-bar expansion
GregForkutza Jul 29, 2025
87a5d1f
Update tests, comments, and documentation for raw std dev parameteriz…
GregForkutza Aug 1, 2025
000dac8
preserve cov_structures attribute in refitML
bbolker Aug 2, 2025
fff8e5b
Fix UnstructuredCovariance parameterization: remove log-scale transfo…
bbolker Aug 2, 2025
21b74ce
Add UnstructuredCovariance unit tests for direct-scale parameterization
GregForkutza Aug 4, 2025
1864018
Add CS covariance structure unit tests
GregForkutza Aug 7, 2025
fabd555
adapt profile test to avoid cov structures [run ci]
bbolker Aug 9, 2025
fa6251d
Merge branch 'master' into flexSigma
bbolker Aug 9, 2025
fa9c878
add AR1 covariance structure unit tests
GregForkutza Aug 10, 2025
ee96261
tests for cs structure to determine if error in varcorr workflow
GregForkutza Aug 22, 2025
8480f43
fix up cs_tests
bbolker Aug 22, 2025
7439180
testing
bbolker Aug 22, 2025
3b617c6
add us to specials list and remove seperate parsing pathway
GregForkutza Aug 25, 2025
d37aadb
reorder list of S4 covariance objects by optimizer order (decreasing …
GregForkutza Aug 25, 2025
9a4b538
move args extraction to after 'us'
GregForkutza Aug 25, 2025
8d0c14f
Update reformulas dependency to >= 0.4.2
GregForkutza Aug 25, 2025
f544fba
Added Remotes: bbolker/reformulas for GitHub fallback
GregForkutza Aug 25, 2025
897f906
mkLmerDevfun stores structured covariance information
GregForkutza Aug 25, 2025
8855281
wip: enhancing mkdevfun to reparamterize theta as cholesky factors
GregForkutza Aug 26, 2025
cc25609
remove wip of glmResp pathway from devfun
GregForkutza Aug 27, 2025
2f66704
Fix UnstructuredCovariance diagonal start values to match lme4 initia…
GregForkutza Aug 27, 2025
42664b0
Implement uniform S4 processing for mixed covariance structure support
GregForkutza Aug 29, 2025
ce9f6c2
fire ecology examples
bbolker Aug 29, 2025
3041dcc
clean up ecoreg
bbolker Aug 29, 2025
5226763
attempts at fixes
bbolker Sep 5, 2025
b0b6f3e
compute_lambdat_x for cs returns correct vector of the correct size
GregForkutza Sep 7, 2025
bdc2515
Merge branch 'master' into flexSigma
jaganmn Oct 20, 2025
820a1d2
testing info
bbolker Oct 20, 2025
d30fd49
transpose Lambda to get Lambdat\!
bbolker Oct 20, 2025
d91557e
remove some debugging statements
bbolker Oct 20, 2025
b25d1ea
minimize diff against master
jaganmn Oct 21, 2025
d2c508a
clean up tests; no more dsyMatrix
bbolker Oct 21, 2025
f8a479c
Merge branch 'flexSigma' of github.com:lme4/lme4 into flexSigma
bbolker Oct 21, 2025
1aed93c
fix 'getME' example ('tnames' did not respect diag.only=TRUE)
jaganmn Oct 21, 2025
b4ba3d3
'VarCorr.merMod' once again returning a list of traditional matrices …
jaganmn Oct 21, 2025
e10ef29
skip reordering of RE terms
bbolker Oct 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches:
- main
- master
- flexSigma
pull_request:
branches:
- main
Expand Down
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Description: Fit linear and generalized linear mixed-effects models.
Depends: R (>= 3.6.0), Matrix, methods, stats
LinkingTo: Rcpp (>= 0.10.5), RcppEigen (>= 0.3.3.9.4), Matrix (>= 1.2-3)
Imports: graphics, grid, splines, utils, parallel, MASS, lattice, boot,
nlme (>= 3.1-123), minqa (>= 1.1.15), nloptr (>= 1.0.4), reformulas (>= 0.3.0), rlang,
nlme (>= 3.1-123), minqa (>= 1.1.15), nloptr (>= 1.0.4), reformulas (>= 0.4.2), rlang,
Rdpack
RdMacros: Rdpack
Suggests:
Expand Down Expand Up @@ -69,3 +69,5 @@ License: GPL (>=2)
URL: https://github.com/lme4/lme4/
BugReports: https://github.com/lme4/lme4/issues
Encoding: UTF-8
Remotes:
bbolker/reformulas
15 changes: 11 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ importFrom("graphics", par, plot)
importFrom("grid", gpar, viewport)
importFrom("splines", backSpline, interpSpline, periodicSpline)
importFrom("methods", as, getDataPart, extends, is, new, cbind2, rbind2,
show, slot, slotNames, setRefClass, getClass)
show, slot, slotNames, setRefClass, getClass,
callNextMethod, validObject)
importFrom("stats",
AIC, BIC, anova, approx, approxfun,
as.formula, asOneSidedFormula, ave,
Expand All @@ -25,7 +26,8 @@ importFrom("stats",
resid, residuals, rgamma, rnbinom, rnorm, rpois,
rstudent,
runif, sd, setNames, simulate, symnum, terms, terms.formula,
update, update.formula, var, vcov, weights)
update, update.formula, var, vcov, weights,
plogis, qlogis)
importFrom("utils", flush.console, packageVersion, sessionInfo, stack, str, capture.output)
if(getRversion() >= "3.5.0") {
importFrom("utils", warnErrList)
Expand Down Expand Up @@ -154,8 +156,13 @@ export(.prt.VC, .prt.aictab, .prt.call, .prt.family,


##------ Our S4 Classes ------------------------
exportClasses(glmerMod, lmerMod, nlmerMod, merMod)
exportClasses(lmList4)
exportClasses(glmerMod, lmerMod, nlmerMod, merMod, lmList4,
VirtualCovariance, HomogeneousVariance, HeterogeneousVariance,
UnstructuredCovariance,
DiagonalCovariance, CSCovariance, AR1Covariance,
HomogeneousDiagonalCovariance, HeterogeneousDiagonalCovariance,
HomogeneousCSCovariance, HeterogeneousCSCovariance,
HomogeneousAR1Covariance, HeterogeneousAR1Covariance)

##------ Our S4 Generics / Methods --------------
exportMethods(getL)
Expand Down
234 changes: 234 additions & 0 deletions R/covariance-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
#
# Helper Functions for Covariance Structures


##' Forward CS transformation: theta -> rho
cs_theta_to_rho <- function(theta,n) {
a <- 1/(n-1)
rho <- plogis(theta) * (1 + a) - a
return(rho)
}

##' Inverse CS transformation: rho -> theta
cs_rho_to_theta <- function(rho, n) {
a <- 1 / (n - 1)
theta <- qlogis((rho + a) / (1 + a))

return(theta)
}

##' Forward AR1 transformation: theta -> rho
ar1_theta_to_rho <- function(theta) {
rho <- theta / sqrt(1 + theta^2)

return(rho)
}

##' Inverse AR1 transformation: rho -> theta
ar1_rho_to_theta <- function(rho) {
theta <- rho / sqrt(1 - rho^2)

return(theta)
}

##' @title Get Vech Diagonal Indices
##'
##' @description Internal helper to find the 1-based indices of the diagonal elements
##' within a 'vech' vector (the vectorized lower triangle of a matrix).
##'
##' @param d An integer, the dimension of the square matrix.
##' @return A numeric vector of integer indices for the diagonal elements.
##' @keywords internal
vech_diag_indices <- function(d) {
if (d == 0) return(integer(0))
if (d == 1) return(1L)
1L + c(0, cumsum(d:2))
}

##' @title Reconstruct Cholesky Factor from Parameters
##'
##' @description Internal helper function that reconstructs the dense
##' lower-triangular Cholesky factor `L` from an unconstrained parameter vector.
##'
##' @param param_vec The vector of unconstrained parameters.
##' @param d The dimension of the matrix.
##' @return A traditional matrix representing the Cholesky factor `L`.
##' @keywords internal
get_chol_from_params <- function(param_vec, d) {
## MJ: really, in what sense is 'param_vec' "unconstrained"??
L <- matrix(0, nrow = d, ncol = d)
L[lower.tri(L, diag = TRUE)] <- param_vec
L
}

##' @title Get Lower-Triangular Indices
##' @description Internal helper function computes the 1-based row and column
##' indices for elements in the lower triangle (including the diagonal) of a
##' square matrix. The indices are returned in column-major order.
##' @param d An integer, the dimension of the square matrix.
##' @return A list with two integer vector components: `i` (row indices)
##' and `j` (column indices).
##' @keywords internal
get_lower_tri_indices <- function(d) {
list(
i = unlist(lapply(1:d, function(j) j:d)),
j = unlist(lapply(1:d, function(j) rep(j, d - j + 1)))
)
}

##' Force Matrix to Symmetric Dense Format
##'
##' Internal helper to convert matrices to dsyMatrix class using modern Matrix package syntax.
##'
##' @param x A matrix-like object to convert
##' @return A dsyMatrix object
##' @keywords internal
force_dsyMatrix <- function(x) {
as(as(as(x, "dMatrix"), "symmetricMatrix"), "unpackedMatrix")
}

##' Helper function for log determinant computation of structured covariance matrices
##'
##' Computes log determinant using decomposition: log(det(D*R*D)) = log(det(D²)) + log(det(R))
##' where D is variance scaling and R is correlation matrix.
##'
##' @param object A structured covariance object
##' @return Numeric log determinant value
##' @keywords internal
compute_log_det_structured <- function(object) {
d <- object@dimension

R <- compute_correlation_matrix(object)
log_det_R <- as.numeric(Matrix::determinant(R, logarithm = TRUE)$modulus)

log_det_V <- if (is(object, "HomogeneousVariance")) {
# All variances equal: log(det(sigma^2 * I)) = d * log(sigma^2)
d * object@vparameters[1]
} else {
# Individual variances: log(det(D^2)) = sum(log(variances))
sum(object@vparameters)
}

return(log_det_V + log_det_R)
}

##' Helper function for inverse computation of structured covariance matrices
##'
##' Computes inverse using decomposition: inv(D*R*D) = inv(D)*inv(R)*inv(D)
##' where D is variance scaling and R is correlation matrix.
##'
##' @param object A structured covariance object
##' @return Inverse covariance matrix as dsyMatrix
##' @keywords internal
compute_inverse_structured <- function(object) {
d <- object@dimension

R <- compute_correlation_matrix(object)
inv_R <- solve(R)

if (is(object, "HomogeneousVariance")) {
# inv(sigma^2 * R) = (1/sigma^2) * inv(R)
inv_sigma_sq <- 1 / object@vparameters[1]^2
inv_Sigma <- inv_sigma_sq * inv_R
} else {
# inv(D * R * D) = inv(D) * inv(R) * inv(D)
inv_st_devs <- 1 / object@vparameters
inv_D <- Diagonal(d, x = inv_st_devs)
inv_Sigma <- inv_D %*% inv_R %*% inv_D
}

force_dsyMatrix(inv_Sigma)
}

# Helper Functions for CS and AR1 Covariance Structures

##' Get Start Values for Structured Covariance Models
##'
##' @description Helper function to generate start values for CS and AR1 covariance structures.
##' @param object A covariance structure object (CS or AR1).
##' @return Numeric vector of start values: std dev parameters (1.0) followed by
##' correlation parameter (0.0) if dimension > 1.
##' @keywords internal
get_structured_start_values <- function(object) {
d <- object@dimension
n_v_params <- if (is(object, "HomogeneousVariance")) 1L else d
v_starts <- rep(1.0, n_v_params)

if (d > 1) {
c_starts <- 0.0
return(c(v_starts, c_starts))
} else {
return(v_starts)
}
}

##' Get Lower Bounds for Structured Covariance Models
##'
##' @description Helper function to generate parameter bounds for CS and AR1 covariance structures.
##' @param object A covariance structure object (CS or AR1).
##' @return Numeric vector of lower bounds: st devs bounds (0) followed by
##' correlation bound (-Inf) if dimension > 1.
##' @keywords internal
get_structured_lower_bounds <- function(object) {
d <- object@dimension
n_v_params <- if (is(object, "HomogeneousVariance")) 1L else d
v_low <- rep(0, n_v_params)

if (d > 1) {
c_low <- -Inf
return(c(v_low, c_low))
} else {
return(v_low)
}
}

##' Create vech position to matrix distance mapping for structured covariance
##'
##' Maps vectorized half positions to matrix coordinates and distances for
##' distance-based covariance structures like AR1.
##'
##' @param d Matrix dimension
##' @return List mapping vech positions to matrix coordinates, distances, and parameter indices
##' @keywords internal
get_vech_distance_mapping <- function(d) {
# For dimension d, create mapping from vech position to matrix distance
mapping <- list()
vech_pos <- 1

for (j in 1:d) { # column
for (i in j:d) { # row (lower triangle)
distance <- abs(i - j)
mapping[[vech_pos]] <- list(
matrix_pos = c(i, j),
distance = distance,
param_index = if (distance == 0) 1 else (1 + distance) # 1=st_dev, 2+=correlations
)
vech_pos <- vech_pos + 1
}
}

return(mapping)
}

##' Check if merMod Object Has Structured Covariance
##'
##' Utility function to determine if a fitted merMod object contains
##' structured covariance information.
##'
##' @param x A merMod object
##' @return Logical indicating presence of structure information
##' @export
has_structured_covariance <- function(x) {
!is.null(attr(x, "cov_structures"))
}

##' Get Structure Types from merMod Object
##'
##' Utility function to extract the covariance structure types used in a model.
##'
##' @param x A merMod object
##' @return Character vector of structure types, or NULL if unstructured
##' @export
get_structure_types <- function(x) {
attr(x, "structure_types")
}
Loading