From 366204dc5ed4883a81ef7efbde528b5b4c5fd31e Mon Sep 17 00:00:00 2001 From: Gleb Tikhonov Date: Wed, 10 Apr 2024 19:10:08 +0300 Subject: [PATCH] Fixes for LFix and LRan summations. --- R/computeInitialParameters.R | 5 +---- R/computeWAIC.R | 5 +---- R/predict.R | 4 ++-- R/updateBetaSel.R | 2 +- R/updateEta.R | 2 +- R/updateInvSigma.R | 2 +- R/updateZ.R | 2 +- R/updatewRRR.R | 2 +- 8 files changed, 9 insertions(+), 15 deletions(-) diff --git a/R/computeInitialParameters.R b/R/computeInitialParameters.R index 8adfec0..a7a6f34 100644 --- a/R/computeInitialParameters.R +++ b/R/computeInitialParameters.R @@ -249,10 +249,7 @@ computeInitialParameters = function(hM, initPar, computeZ=TRUE){ LRan[[r]] = LRan[[r]] + (Eta[[r]][hM$Pi[,r],,drop=FALSE]*hM$rL[[r]]$x[as.character(hM$dfPi[,r]),r]) %*% Lambda[[r]][,,k] } } - if(hM$nr > 0){ - Z = LFix + Reduce("+", LRan) - } else - Z = LFix + Z = Reduce("+", c(list(LFix), LRan)) if(computeZ){ Z = updateZ(Y=hM$Y,Z=Z,Beta=Beta,iSigma=sigma^-1,Eta=Eta,Lambda=Lambda, Loff=hM$Loff,X=XScaled,Pi=hM$Pi,dfPi=hM$dfPi,distr=hM$distr,rL=hM$rL) diff --git a/R/computeWAIC.R b/R/computeWAIC.R index e28fae9..9b4b271 100644 --- a/R/computeWAIC.R +++ b/R/computeWAIC.R @@ -72,10 +72,7 @@ computeWAIC = function(hM, ghN=11, byColumn=FALSE){ LRan[[r]] = LRan[[r]] + (Eta[[r]][Pi[,r],]*rL[[r]]$x[as.character(dfPi[,r]),k]) %*% Lambda[[r]][,,k] } } - if(nr > 0){ - E = LFix + Reduce("+", LRan) - } else - E = LFix + E = Reduce("+", c(list(LFix), LRan)) indNA = is.na(Y) std = matrix(sigma^-0.5,ny,ns,byrow=TRUE) diff --git a/R/predict.R b/R/predict.R index 3cba9a7..0761830 100644 --- a/R/predict.R +++ b/R/predict.R @@ -262,7 +262,7 @@ get1prediction <- LRan[[r]] = LRan[[r]] + (Eta[[r]][as.character(dfPiNew[,r]),]*rL[[r]]$x[as.character(dfPiNew[,r]),k]) %*% sam$Lambda[[r]][,,k] } } - L = Reduce("+", c(LFix, LRan)) + L = Reduce("+", c(list(LFix), LRan)) if(!is.null(Loff)) L = L + Loff ## predict can be slow with Yc and especially with high mcmcStep @@ -294,7 +294,7 @@ get1prediction <- sam$Lambda[[r]][,,k] } } - if(object$nr > 0){L = LFix + Reduce("+", LRan)} else L = LFix + L = Reduce("+", c(list(LFix), LRan)) } if(!expected){ Z = L + matrix(sqrt(sam$sigma),nrow(L),object$ns,byrow=TRUE) * matrix(rnorm(nrow(L)*object$ns),nrow(L),object$ns) diff --git a/R/updateBetaSel.R b/R/updateBetaSel.R index 3bdf086..8b134db 100644 --- a/R/updateBetaSel.R +++ b/R/updateBetaSel.R @@ -42,7 +42,7 @@ updateBetaSel = function(Z,XSelect, BetaSel, Beta, iSigma, LFix = matrix(NA,ny,ns) for(j in 1:ns) LFix[,j] = X[[j]]%*%Beta[,j] - E = Reduce("+", c(LFix,LRan)) + E = Reduce("+", c(list(LFix), LRan)) if(!is.null(Loff)) E = E + Loff ll = matrix(NA,ny,ns) diff --git a/R/updateEta.R b/R/updateEta.R index 63de714..fb3d769 100644 --- a/R/updateEta.R +++ b/R/updateEta.R @@ -30,7 +30,7 @@ updateEta = function(Y,Z,Beta,iSigma,Eta,Lambda,Alpha, rLPar, Loff,X,Pi,dfPi,rL) } for(r in seq_len(nr)){ rnames=rownames(Eta[[r]]) - S = Z - Reduce("+", c(LFix,LRan[setdiff(1:nr, r)])) + S = Z - Reduce("+", c(list(LFix), LRan[setdiff(1:nr,r)])) if(!is.null(Loff)) S = S - Loff lambda = Lambda[[r]] diff --git a/R/updateInvSigma.R b/R/updateInvSigma.R index 5e2894d..2060b9c 100644 --- a/R/updateInvSigma.R +++ b/R/updateInvSigma.R @@ -27,7 +27,7 @@ updateInvSigma = function(Y,Z,Beta,iSigma,Eta,Lambda, distr,Loff,X,Pi,dfPi,rL, a LRan[[r]] = LRan[[r]] + (Eta[[r]][Pi[,r],]*rL[[r]]$x[as.character(dfPi[,r]),k]) %*% Lambda[[r]][,,k] } } - Eps = Z - Reduce("+", c(LFix,LRan)) + Eps = Z - Reduce("+", c(list(LFix), LRan)) if(!is.null(Loff)) Eps = Eps - Loff Yx = !is.na(Y) diff --git a/R/updateZ.R b/R/updateZ.R index 61351f6..7be3afa 100644 --- a/R/updateZ.R +++ b/R/updateZ.R @@ -28,7 +28,7 @@ updateZ = function(Y,Z,Beta,iSigma,Eta,Lambda, Loff,X,Pi,dfPi,distr,rL, ind){ LRan[[r]] = LRan[[r]] + (Eta[[r]][Pi[,r],]*rL[[r]]$x[as.character(dfPi[,r]),k]) %*% Lambda[[r]][,,k] } } - E = Reduce("+", c(LFix,LRan)) + E = Reduce("+", c(list(LFix), LRan)) if(!is.null(Loff)) E = E + Loff Z = matrix(NA,ny,ns) diff --git a/R/updatewRRR.R b/R/updatewRRR.R index 808465a..dd1febd 100644 --- a/R/updatewRRR.R +++ b/R/updatewRRR.R @@ -40,7 +40,7 @@ updatewRRR = function(Z,Beta,iSigma,Eta,Lambda, Loff,X1A,XRRR,Pi,dfPi,rL,PsiRRR, LRan[[r]] = LRan[[r]] + (Eta[[r]][Pi[,r],]*rL[[r]]$x[as.character(dfPi[,r]),r]) %*% Lambda[[r]][,,k] } } - S = Z - Reduce("+", c(LFix,LRan)) + S = Z - Reduce("+", c(list(LFix), LRan)) if(!is.null(Loff)) S = S - Loff A1 = BetaRRR%*%diag(iSigma,nrow = length(iSigma))%*%t(BetaRRR)