'EMGLLF.R'
'generateXY.R'
'A_NAMESPACE.R'
+ 'util.R'
.EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
X, Y, eps)
{
- # Matrix dimensions: NOTE: phiInit *must* be an array (even if p==1)
- n <- dim(Y)[1]
- p <- dim(phiInit)[1]
- m <- dim(phiInit)[2]
- k <- dim(phiInit)[3]
+ # Matrix dimensions
+ n <- nrow(X)
+ p <- ncol(X)
+ m <- ncol(Y)
+ k <- length(piInit)
+
+ # Adjustments required when p==1 or m==1 (var.sel. or output dim 1)
+ if (p==1 || m==1)
+ phiInit <- array(phiInit, dim=c(p,m,k))
+ if (m==1)
+ rhoInit <- array(rhoInit, dim=c(m,m,k))
# Outputs
- phi <- array(NA, dim = c(p, m, k))
- phi[1:p, , ] <- phiInit
+ phi <- phiInit
rho <- rhoInit
pi <- piInit
llh <- -Inf
## E step
# Precompute det(rho[,,r]) for r in 1...k
- detRho <- sapply(1:k, function(r) det(rho[, , r]))
+ detRho <- sapply(1:k, function(r) gdet(rho[, , r]))
sumLogLLH <- 0
for (i in 1:n)
{
.EMGrank_R <- function(Pi, Rho, mini, maxi, X, Y, tau, rank)
{
# matrix dimensions
- n <- dim(X)[1]
- p <- dim(X)[2]
- m <- dim(Rho)[2]
- k <- dim(Rho)[3]
+ n <- nrow(X)
+ p <- ncol(X)
+ m <- ncol(Y)
+ k <- length(Pi)
# init outputs
phi <- array(0, dim = c(p, m, k))
for (r in seq_len(k))
{
dotProduct <- tcrossprod(Y[i, ] %*% Rho[, , r] - X[i, ] %*% phi[, , r])
- logGamIR <- log(Pi[r]) + log(det(Rho[, , r])) - 0.5 * dotProduct
+ logGamIR <- log(Pi[r]) + log(gdet(Rho[, , r])) - 0.5 * dotProduct
# Z[i] = index of max (gam[i,])
if (logGamIR > maxLogGamIR)
{
maxi, tau, fast)
{
n <- nrow(X)
- p <- dim(phiInit)[1]
- m <- dim(phiInit)[2]
- k <- dim(phiInit)[3]
+ p <- ncol(X)
+ m <- ncol(Y)
+ k <- length(piInit)
list_EMG <- EMGLLF(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda = 0,
X, Y, tau, fast)
if (verbose)
print(paste("Computations for lambda=", lambda))
- n <- dim(X)[1]
- p <- dim(phiInit)[1]
- m <- dim(phiInit)[2]
- k <- dim(phiInit)[3]
+ n <- nrow(X)
+ p <- ncol(X)
+ m <- ncol(Y)
+ k <- length(piInit)
sel.lambda <- S[[lambda]]$selected
# col.sel = which(colSums(sel.lambda)!=0) #if boolean matrix
col.sel <- which(sapply(sel.lambda, length) > 0) #if list of selected vars
return(NULL)
# lambda == 0 because we compute the EMV: no penalization here
- res <- EMGLLF(array(phiInit[col.sel, , ],dim=c(length(col.sel),m,k)), rhoInit,
- piInit, gamInit, mini, maxi, gamma, 0, as.matrix(X[, col.sel]), Y, eps, fast)
+ res <- EMGLLF(array(phiInit,dim=c(p,m,k))[col.sel, , ], rhoInit, piInit, gamInit,
+ mini, maxi, gamma, 0, as.matrix(X[, col.sel]), Y, eps, fast)
# Eval dimension from the result + selected
phiLambda2 <- res$phi
{
delta <- (Y %*% rhoLambda[, , r] - (X[, col.sel] %*% t(phiLambda[col.sel, , r])))
} else delta <- (Y %*% rhoLambda[, , r] - (X[, col.sel] %*% phiLambda[col.sel, , r]))
- densite <- densite + piLambda[r] * det(rhoLambda[, , r])/(sqrt(2 * base::pi))^m *
+ densite <- densite + piLambda[r] * gdet(rhoLambda[, , r])/(sqrt(2 * base::pi))^m *
exp(-diag(tcrossprod(delta))/2)
}
llhLambda <- c(sum(log(densite)), (dimension + m + 1) * k - 1)
constructionModelesLassoRank <- function(S, k, mini, maxi, X, Y, eps, rank.min, rank.max,
ncores, fast, verbose)
{
- n <- dim(X)[1]
- p <- dim(X)[2]
- m <- dim(Y)[2]
+ n <- nrow(X)
+ p <- ncol(X)
+ m <- ncol(Y)
L <- length(S)
# Possible interesting ranks
#' @importFrom stats cutree dist hclust runif
initSmallEM <- function(k, X, Y, fast)
{
- n <- nrow(Y)
- m <- ncol(Y)
+ n <- nrow(X)
p <- ncol(X)
+ m <- ncol(Y)
nIte <- 20
Zinit1 <- array(0, dim = c(n, nIte))
betaInit1 <- array(0, dim = c(p, m, k, nIte))
dotProduct <- tcrossprod(Y[i, ] %*% rhoInit1[, , r, repet]
- X[i, ] %*% phiInit1[, , r, repet])
Gam[i, r] <- piInit1[repet, r] *
- det(rhoInit1[, , r, repet]) * exp(-0.5 * dotProduct)
+ gdet(rhoInit1[, , r, repet]) * exp(-0.5 * dotProduct)
}
sumGamI <- sum(Gam[i, ])
gamInit1[i, , repet] <- Gam[i, ]/sumGamI
ncores_inner = 1, thresh = 1e-08, size_coll_mod = 10, fast = TRUE, verbose = FALSE,
plot = TRUE)
{
- p <- dim(X)[2]
- m <- dim(Y)[2]
- n <- dim(X)[1]
+ n <- nrow(X)
+ p <- ncol(X)
+ m <- ncol(Y)
if (verbose)
print("main loop: over all k and all lambda")
for (r in 1:length(modelSel$pi))
{
sqNorm2 <- sum((Y[i, ] %*% modelSel$rho[, , r] - X[i, ] %*% modelSel$phi[, , r])^2)
- Gam[i, r] <- modelSel$pi[r] * exp(-0.5 * sqNorm2) * det(modelSel$rho[, , r])
+ Gam[i, r] <- modelSel$pi[r] * exp(-0.5 * sqNorm2) * gdet(modelSel$rho[, , r])
}
}
Gam <- Gam/rowSums(Gam)
params <- EMGLLF(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
X, Y, eps, fast)
- p <- dim(phiInit)[1]
- m <- dim(phiInit)[2]
+ p <- ncol(X)
+ m <- ncol(Y)
# selectedVariables: list where element j contains vector of selected variables
# in [1,m]
selectedVariables <- lapply(1:p, function(j) {
# from boolean matrix mxk of selected variables obtain the corresponding boolean
# m-vector, and finally return the corresponding indices
- seq_len(m)[apply(abs(params$phi[j, , ]) > thresh, 1, any)]
+ if (m>1) {
+ seq_len(m)[apply(abs(params$phi[j, , ]) > thresh, 1, any)]
+ } else {
+ if (any(params$phi[j, , ] > thresh))
+ 1
+ else
+ numeric(0)
+ }
})
list(selected = selectedVariables, Rho = params$rho, Pi = params$pi)
--- /dev/null
+# ...
+gdet <- function(M)
+{
+ if (is.matrix(M))
+ return (det(M))
+ return (M[1]) #numeric, double
+}