R/levels.R

Defines functions levels.lmm

Documented in levels.lmm

### levels.R --- 
##----------------------------------------------------------------------
## Author: Brice Ozenne
## Created: okt 18 2021 (10:32) 
## Version: 
## Last-Updated: okt 20 2021 (19:08) 
##           By: Brice Ozenne
##     Update #: 12
##----------------------------------------------------------------------
## 
### Commentary: 
## 
### Change Log:
##----------------------------------------------------------------------
## 
### Code:

##' @title Contrasts and Reference Level 
##' @description Contrasts and reference level used when modeling the mean in a linear mixed modek.
##' 
##' @param x an \code{lmm} object
##' 
##' @return a list with two elements \itemize{
##' \item all: contrast matrix for each categorical or factor variable
##' \item reference: reference level: one value for each categorical variable
##' }
##' @export
levels.lmm <- function(x){
    data.X <- x$data[all.vars(stats::delete.response(stats::terms(x$formula$mean)))]
    C <- lapply(data.X, function(iCol){
        if(inherits(iCol,"factor")){stats::contrasts(iCol)}else if(inherits(iCol,"character")){stats::contrasts(as.factor(iCol))}
    })
    if(length(C)>0){
        C <- C[!unlist(lapply(C, is.null))]
        if(length(C)>0){
            ref.level <- unlist(lapply(names(C), function(iC){
                stats::setNames(rownames(C[[iC]])[1],iC)
            }))  
        }else{
            ref.level <- NULL
        }
    }else{
        ref.level <- NULL
    }
    return(list(all = C, reference = ref.level))
}

##----------------------------------------------------------------------
### levels.R ends here

Try the LMMstar package in your browser

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

LMMstar documentation built on Jan. 7, 2023, 1:20 a.m.