1

I have tried to reproduce the results from the answers for this question “Estimating random effects and applying user defined correlation/covariance structure with R lme4 or nlme package “ https://stats.stackexchange.com/questions/18563/estimating-random-effects-and-applying-user-defined-correlation-covariance-struc

Aaron Rendahl's codes
 library(pedigreemm)
 relmatmm <- function (formula, data, family = NULL, REML = TRUE, relmat = list(), 
control = list(), start = NULL, verbose = FALSE, subset, 
weights, na.action, offset, contrasts = NULL, model = TRUE, 
x = TRUE, ...) 
{
mc <- match.call()
lmerc <- mc
lmerc[[1]] <- as.name("lmer")
lmerc$relmat <- NULL
if (!length(relmat)) 
    return(eval.parent(lmerc))
stopifnot(is.list(relmat), length(names(relmat)) == length(relmat))
lmerc$doFit <- FALSE
lmf <- eval(lmerc, parent.frame())
relfac <- relmat
relnms <- names(relmat)
stopifnot(all(relnms %in% names(lmf$FL$fl)))
asgn <- attr(lmf$FL$fl, "assign")
for (i in seq_along(relmat)) {
    tn <- which(match(relnms[i], names(lmf$FL$fl)) == asgn)
    if (length(tn) > 1) 
        stop("a relationship matrix must be associated with only one random effects term")
    Zt <- lmf$FL$trms[[tn]]$Zt
    relmat[[i]] <- Matrix(relmat[[i]][rownames(Zt), rownames(Zt)], 
        sparse = TRUE)
    relfac[[i]] <- chol(relmat[[i]])
    lmf$FL$trms[[tn]]$Zt <- lmf$FL$trms[[tn]]$A <- relfac[[i]] %*% Zt
}
ans <- do.call(if (!is.null(lmf$glmFit)) 
    lme4:::glmer_finalize
else lme4:::lmer_finalize, lmf)
ans <- new("pedigreemm", relfac = relfac, ans)
ans@call <- match.call()
ans
}

the original example

 set.seed(1234)
 mydata <- data.frame (gen = factor(rep(1:10, each = 10)),
                  repl = factor(rep(1:10, 10)),
                  yld = rnorm(10, 5, 0.5))
library(lme4)
covmat <- round(nearPD(matrix(runif(100, 0, 0.2), nrow = 10))$mat, 2)
diag(covmat) <- diag(covmat)/10+1
rownames(covmat) <- colnames(covmat) <- levels(mydata$gen)
m <- relmatmm(yld ~ (1|gen) + (1|repl), relmat=list(gen=covmat), data=mydata)

here is the error message

Error in lmf$FL : $ operator not defined for this S4 class
In addition: Warning message:
In checkArgs("lmer", doFit = FALSE) : extra argument(s) ‘doFit’ disregarded

I will appreciate any help ? Thanks

Community
  • 1
  • 1
hema
  • 725
  • 1
  • 8
  • 20
  • 1
    this is using internal structures from `lme4` that have changed in the new version of `lme4`. I don't have time to look this over right now: you may need to ask the original author (Aaron Rendahl?) to update the code to work with the new version of `lme4` (see `help("modular",package="lme4")` for a start. It might also be helpful to contact the maintainer of the `pedigreemm` package to see if they can help. – Ben Bolker Oct 11 '13 at 21:22
  • PS this should actually be easier to do with the new version of `lme4` than it was before due to the improved modularity (see previous comment), but it still means the code needs to be redone ... – Ben Bolker Oct 11 '13 at 21:23
  • Related: http://stackoverflow.com/questions/19104475/how-to-modify-slots-lme4-1-0/19106863#19106863 – Ben Bolker Oct 11 '13 at 21:29

1 Answers1

2

This is a re-implementation of the previous code -- I have done some slight modifications, and I have not tested it in any way -- test yourself and/or use at your own risk.

First create a slightly more modularized function that constructs the deviance function and fits the model:

doFit <- function(lmod,lmm=TRUE) {
    ## see ?modular
    if (lmm) {
        devfun <- do.call(mkLmerDevfun, lmod)
        opt <- optimizeLmer(devfun)
        mkMerMod(environment(devfun), opt, lmod$reTrms, fr = lmod$fr)
    } else {
        devfun <- do.call(mkGlmerDevfun, lmod)
        opt <- optimizeGlmer(devfun)
        devfun <- updateGlmerDevfun(devfun, lmod$reTrms)
        opt <- optimizeGlmer(devfun, stage=2)
        mkMerMod(environment(devfun), opt, lmod$reTrms, fr = lmod$fr)
    }
}

Now create a function to construct the object that doFit needs and modify it:

relmatmm <- function (formula, ..., lmm=TRUE, relmat = list()) {
    ff <- if (lmm) lFormula(formula, ...) else glFormula(formula, ...)
    stopifnot(is.list(relmat), length(names(relmat)) == length(relmat))
    relnms <- names(relmat)
    relfac <- relmat
    flist <- ff$reTrms[["flist"]]   ## list of factors
     ## random-effects design matrix components
    Ztlist <- ff$reTrms[["Ztlist"]]
    stopifnot(all(relnms %in% names(flist)))
    asgn <- attr(flist, "assign")
    for (i in seq_along(relmat)) {
        tn <- which(match(relnms[i], names(flist)) == asgn)
        if (length(tn) > 1) 
            stop("a relationship matrix must be",
                 " associated with only one random effects term")
        zn <- rownames(Ztlist[[i]])
        relmat[[i]] <- Matrix(relmat[[i]][zn,zn],sparse = TRUE)
        relfac[[i]] <- chol(relmat[[i]])
        Ztlist[[i]] <-  relfac[[i]] %*% Ztlist[[i]]
    }
    ff$reTrms[["Ztlist"]] <- Ztlist
    ff$reTrms[["Zt"]] <- do.call(rBind,Ztlist)
    fit <- doFit(ff,lmm)
}

Example

set.seed(1234)
mydata <- data.frame (gen = factor(rep(1:10, each = 10)),
                  repl = factor(rep(1:10, 10)),
                  yld = rnorm(10, 5, 0.5))

library(lme4)
covmat <- round(nearPD(matrix(runif(100, 0, 0.2), nrow = 10))$mat, 2)
diag(covmat) <- diag(covmat)/10+1
rownames(covmat) <- colnames(covmat) <- levels(mydata$gen)

m <- relmatmm(yld ~ (1|gen) + (1|repl), relmat=list(gen=covmat), 
     data=mydata)

This runs -- I don't know if the output is correct. It also doesn't make the resulting object into a pedigreemm object ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Dear Ben: thank you so much for the reimplementing Aaron codes --- i have tried to post this comment long ago but i was not allowed to do so--- when applied your new codes i got this error: Error in relmat[[i]][zn, zn] : invalid or not-yet-implemented 'Matrix' subsetting – hema Nov 17 '13 at 00:55
  • works for me. Can you post results of `sessionInfo()` (specifically, versions of `Matrix` and `lme4`) ? – Ben Bolker Nov 17 '13 at 16:03
  • here is my sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-w64-mingw32/x64 (64-bit) [1] pedigreemm_0.3-1 ------ [22] lme4_1.0-4 ---- Matrix_1.0-12 – hema Nov 17 '13 at 18:05
  • Are you willing to try updating `Matrix`, `RcppEigen`, and `lme4` (in that order) to versions 1.1-0, 0.3.2.0, and 1.0-5 respectively ... ? (Updating Matrix is the important part, but if you don't then update (or at least re-install) `RcppEigen` and `lme4`, you'll run into trouble – Ben Bolker Nov 17 '13 at 18:18
  • i will try updating all of them as you recommended – hema Nov 17 '13 at 18:25
  • Ben: i reinstalled R it self + the packages that you recommended and here is what i got: > m <- relmatmm(yld ~ (1|gen) + (1|repl), relmat=list(gen=covmat), + data=mydata) Error in relmat[[i]][zn, zn] : invalid or not-yet-implemented 'Matrix' subsetting > sessionInfo() R version 3.0.2 (2013-09-25) Platform: i386-w64-mingw32/i386 (32-bit) [1] pedigreemm_0.3-1 lme4_1.0-5 Matrix_1.1-0 – hema Nov 17 '13 at 18:49