7676# ' @name fitGLMM
7777# '
7878# ' @importMethodsFrom Matrix %*%
79- # ' @importFrom Matrix Matrix solve crossprod kronecker
79+ # ' @importFrom Matrix Matrix solve crossprod kronecker chol
8080# ' @importFrom stats runif var
8181# ' @importFrom BiocParallel bpstopOnError
8282# ' @export
@@ -131,7 +131,10 @@ fitGLMM <- function(X, Z, y, offsets, init.theta=NULL, Kin=NULL,
131131 }
132132
133133 # create full Z with expanded random effect levels
134+ # add a residual variance parameter
134135 full.Z <- initializeFullZ(Z = Z , cluster_levels = random.levels )
136+ # random.levels <- c(random.levels, list("Residual"=rownames(Z)))
137+
135138 if (is.null(glmm.control [[" init.u" ]])){
136139 curr_u <- matrix (runif(ncol(full.Z ), 0 , 1 ), ncol = 1 )
137140 } else {
@@ -172,13 +175,18 @@ fitGLMM <- function(X, Z, y, offsets, init.theta=NULL, Kin=NULL,
172175 } else {
173176 curr_sigma <- Matrix(glmm.control [[" init.sigma" ]], ncol = 1 , sparse = TRUE )
174177 }
175- rownames(curr_sigma ) <- colnames(Z )
178+
179+ rownames(curr_sigma ) <- names(random.levels )
176180
177181 # # add the genetic components
178182 # # augment Z with I
179- geno.I <- diag(nrow(full.Z ))
183+ # # Use K=LD^1/2(LD^1/2)^T instead of ZZ^T for GRM
184+ # # chol return L^T
185+ geno.I <- t(chol(Kin ))
186+ # geno.I <- diag(nrow(full.Z))
180187 colnames(geno.I ) <- paste0(" CovarMat" , seq_len(ncol(geno.I )))
181188 full.Z <- do.call(cbind , list (full.Z , geno.I ))
189+
182190 # add a genetic variance component
183191 sigma_g <- Matrix(runif(1 , 0 , 1 ), ncol = 1 , nrow = 1 , sparse = TRUE )
184192 rownames(sigma_g ) <- " CovarMat"
@@ -201,7 +209,10 @@ fitGLMM <- function(X, Z, y, offsets, init.theta=NULL, Kin=NULL,
201209
202210 # if we only have a GRM then Z _is_ full.Z?
203211 # full.Z <- initializeFullZ(Z, cluster_levels=random.levels)
204- full.Z <- Z
212+ # # Use K=LD^1/2(LD^1/2)^T instead of ZZ^T for GRM
213+ # # chol return L^T
214+ full.Z <- t(chol(Kin ))
215+ # full.Z <- Z
205216 # should this be the matrix R?
206217 colnames(full.Z ) <- paste0(names(random.levels ), seq_len(ncol(full.Z )))
207218
@@ -255,6 +266,7 @@ fitGLMM <- function(X, Z, y, offsets, init.theta=NULL, Kin=NULL,
255266 } else if (is.null(Kin )){
256267 # create full Z with expanded random effect levels
257268 full.Z <- initializeFullZ(Z = Z , cluster_levels = random.levels )
269+ # random.levels <- c(random.levels, list("Residual"=rownames(Z)))
258270
259271 # random value initiation from runif
260272 if (is.null(glmm.control [[" init.u" ]])){
@@ -456,7 +468,7 @@ initialiseG <- function(cluster_levels, sigmas, Kin=NULL){
456468 diag(G [c(i : (i + x.q - 1 )), c(i : (i + x.q - 1 )), drop = FALSE ]) <- sigmas [x , ] # is this sufficient to transform the sigma to the model scale?
457469 } else {
458470 if (rownames(sigmas [x , , drop = FALSE ]) %in% c(" Genetic" )){
459- diag(G [c(i : (i + x.q - 1 )), c(i : (i + x.q - 1 )), drop = FALSE ]) <- sigmas [x , ] * Kin
471+ diag(G [c(i : (i + x.q - 1 )), c(i : (i + x.q - 1 )), drop = FALSE ]) <- sigmas [x , ] # * Kin
460472 }else {
461473 diag(G [c(i : (i + x.q - 1 )), c(i : (i + x.q - 1 )), drop = FALSE ]) <- sigmas [x , ] # is this sufficient to transform the sigma to the model scale?
462474 }
@@ -565,7 +577,12 @@ initializeFullZ <- function(Z, cluster_levels, stand.cols=FALSE){
565577
566578 i.z.list [[colnames(Z )[i ]]] <- i.z
567579 }
580+ # # Add the residual variance
581+ # resid.eye <- diag(nrow(Z))
582+ # colnames(resid.eye) <- rownames(Z)
583+ # i.z.list[["Residual"]] <- resid.eye
568584 full.Z <- do.call(cbind , i.z.list )
585+
569586 return (full.Z )
570587}
571588
0 commit comments