Skip to content

Commit f33f3bc

Browse files
authored
Merge pull request #372 from MarioniLab/devel
Resolve matrix commute error in model using random effect and GRM at the same time.
2 parents 874ed1b + c1a01b9 commit f33f3bc

5 files changed

Lines changed: 24 additions & 8 deletions

File tree

R/glmm.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ fitGLMM <- function(X, Z, y, offsets, init.theta=NULL, Kin=NULL,
179179
geno.I <- diag(nrow(full.Z))
180180
colnames(geno.I) <- paste0("CovarMat", seq_len(ncol(geno.I)))
181181
full.Z <- do.call(cbind, list(full.Z, geno.I))
182-
183182
# add a genetic variance component
184183
sigma_g <- Matrix(runif(1, 0, 1), ncol=1, nrow=1, sparse=TRUE)
185184
rownames(sigma_g) <- "CovarMat"

R/testNhoods.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,25 @@ testNhoods <- function(x, design, design.df, kinship=NULL,
572572
"SE"= unlist(lapply(lapply(fit, `[[`, "SE"), function(x) x[ret.beta])),
573573
"tvalue" = unlist(lapply(lapply(fit, `[[`, "t"), function(x) x[ret.beta])),
574574
"PValue" = unlist(lapply(lapply(fit, `[[`, "PVALS"), function(x) x[ret.beta])),
575-
matrix(unlist(lapply(fit, `[[`, "Sigma")), ncol=length(rand.levels), byrow=TRUE),
576575
"Converged"=unlist(lapply(fit, `[[`, "converged")), "Dispersion" = unlist(lapply(fit, `[[`, "Dispersion")),
577576
"Logliklihood"=unlist(lapply(fit, `[[`, "LOGLIHOOD")))
578577

579-
rownames(res) <- 1:length(fit)
580-
colnames(res)[6:(6+length(rand.levels)-1)] <- paste(names(rand.levels), "variance", sep="_")
578+
# need to know how many variance components there are to get proper data frame dimensions
579+
n.sigmas <- unique(unlist(lapply(lapply(fit, `[[`, "Sigma"), length)))
580+
varcomps <- as.data.frame(matrix(unlist(lapply(fit, `[[`, "Sigma")), ncol=n.sigmas, byrow=TRUE))
581+
582+
if(n.sigmas == length(rand.levels)){
583+
colnames(varcomps) <- paste(names(rand.levels), "variance", sep="_")
584+
} else{
585+
colnames(varcomps) <- paste(c(names(rand.levels), "CovarMat"), "variance", sep="_")
586+
}
587+
588+
res <- do.call(cbind.data.frame, list(res[, c("logFC", "logCPM", "SE", "tvalue", "PValue")],
589+
varcomps,
590+
res[, c("Converged", "Logliklihood")]))
591+
592+
rownames(res) <- c(1:n.nhoods)
593+
# colnames(res)[6:(6+length(rand.levels)-1)] <- paste(names(rand.levels), "variance", sep="_")
581594
} else {
582595
# need to use legacy=TRUE to maintain original edgeR behaviour
583596
fit <- glmQLFit(dge, x.model, robust=robust, legacy=TRUE)

src/fitGeneticPLGlmm.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ List fitGeneticPLGlmm(const arma::mat& Z, const arma::mat& X, const arma::mat& K
220220
Vmu = computeVmu(muvec, curr_disp, vardist);
221221
W = computeW(curr_disp, Dinv, vardist);
222222
Winv = W.i();
223+
223224
// pre-compute matrics: X^T * W^-1, Z^T * W^-1
224225
arma::mat xTwinv = X.t() * Winv;
225226
arma::mat zTwin = Z.t() * Winv;
@@ -272,6 +273,7 @@ List fitGeneticPLGlmm(const arma::mat& Z, const arma::mat& X, const arma::mat& K
272273
if(REML){
273274
arma::mat VstarZ = V_star_inv * Z;
274275
VP_partial = precomp_list["PZZt"];
276+
275277
VS_partial = pseudovarPartial_VG(u_indices, Z, VstarZ, K);
276278

277279
score_sigma = sigmaScoreREML_arma(VP_partial, y_star, P,

src/paramEst.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ arma::vec sigmaScoreREML_arma (const Rcpp::List& pvstar_i, const arma::vec& ysta
3030
for(int i=0; i < c; i++){
3131
const arma::mat& P_pvi = pvstar_i(i); // this is Vstar_inv * partial derivative
3232
const arma::mat& Pdifi = remldiffV(i);
33+
3334
double lhs = -0.5 * arma::trace(Pdifi);
3435
arma::mat mid1(1, 1);
3536
mid1 = arma::trans(ystarminx) * P_pvi * Vstarinv * ystarminx;

src/pseudovarPartial.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ List pseudovarPartial_VG(const List& u_indices, const arma::mat& Z, const arma::
156156
arma::uvec u_idx = u_indices[i];
157157
arma::mat omat(n , n);
158158

159+
arma::mat VsZcols = VstarZ.cols(u_idx-1);
160+
arma::mat Zcols = Z.cols(u_idx-1).t();
161+
159162
if(i == c - 1){
160-
omat = VstarZ.cols(u_idx-1) * K * Z.cols(u_idx-1);
163+
omat = VstarZ.cols(u_idx-1) * K * Zcols;
161164
} else{
162-
omat = VstarZ.cols(u_idx-1) * Z.cols(u_idx-1);
165+
omat = VstarZ.cols(u_idx-1) * Zcols;
163166
}
164167

165-
166-
167168
outlist[i] = omat;
168169
}
169170

0 commit comments

Comments
 (0)