parallel
Suggests:
capushe,
+ methods,
roxygen2,
- testhat
+ testthat
URL: http://git.auder.net/?p=valse.git
License: MIT + file LICENSE
RoxygenNote: 5.0.1
phiLambda[col.sel[j], sel.lambda[[j]], ] <- phiLambda2[j, sel.lambda[[j]], ]
dimension <- length(unlist(sel.lambda))
- ## Computation of the loglikelihood
- # Precompute det(rhoLambda[,,r]) for r in 1...k
- detRho <- sapply(1:k, function(r) gdet(rhoLambda[, , r]))
- sumLogLLH <- 0
+ ## Affectations
+ Gam <- matrix(0, ncol = length(piLambda), nrow = n)
for (i in 1:n)
{
- # Update gam[,]; use log to avoid numerical problems
- logGam <- sapply(1:k, function(r) {
- log(piLambda[r]) + log(detRho[r]) - 0.5 *
- sum((Y[i, ] %*% rhoLambda[, , r] - X[i, ] %*% phiLambda[, , r])^2)
- })
-
- logGam <- logGam - max(logGam) #adjust without changing proportions
- gam <- exp(logGam)
- norm_fact <- sum(gam)
- sumLogLLH <- sumLogLLH + log(norm_fact) - log((2 * base::pi)^(m/2))
+ for (r in 1:length(piLambda))
+ {
+ sqNorm2 <- sum((Y[i, ] %*% rhoLambda[, , r] - X[i, ] %*% phiLambda[, , r])^2)
+ Gam[i, r] <- piLambda[r] * exp(-0.5 * sqNorm2) * det(rhoLambda[, , r])
+ }
}
- llhLambda <- c(-sumLogLLH/n, (dimension + m + 1) * k - 1)
- list(phi = phiLambda, rho = rhoLambda, pi = piLambda, llh = llhLambda)
+ Gam2 <- Gam/rowSums(Gam)
+ affec <- apply(Gam2, 1, which.max)
+ proba <- Gam2
+ LLH <- c(sum(log(apply(Gam,1,sum))), (dimension + m + 1) * k - 1)
+ # ## Computation of the loglikelihood
+ # # Precompute det(rhoLambda[,,r]) for r in 1...k
+ # detRho <- sapply(1:k, function(r) gdet(rhoLambda[, , r]))
+ # sumLogLLH <- 0
+ # for (i in 1:n)
+ # {
+ # # Update gam[,]; use log to avoid numerical problems
+ # logGam <- sapply(1:k, function(r) {
+ # log(piLambda[r]) + log(detRho[r]) - 0.5 *
+ # sum((Y[i, ] %*% rhoLambda[, , r] - X[i, ] %*% phiLambda[, , r])^2)
+ # })
+ #
+ # #logGam <- logGam - max(logGam) #adjust without changing proportions -> change the LLH
+ # gam <- exp(logGam)
+ # norm_fact <- sum(gam)
+ # sumLogLLH <- sumLogLLH + log(norm_fact) - m/2* log(2 * base::pi)
+ # }
+ #llhLambda <- c(-sumLogLLH/n, (dimension + m + 1) * k - 1)
+ list(phi = phiLambda, rho = rhoLambda, pi = piLambda, llh = LLH, affec = affec, proba = proba)
}
# For each lambda, computation of the parameters
dotProduct <- tcrossprod(Y[i, ] %*% rhoInit1[, , r, repet]
- X[i, ] %*% phiInit1[, , r, repet])
Gam[i, r] <- piInit1[repet, r] *
- gdet(rhoInit1[, , r, repet]) * exp(-0.5 * dotProduct)
+ det(rhoInit1[, , r, repet]) * exp(-0.5 * dotProduct)
}
sumGamI <- sum(Gam[i, ])
gamInit1[i, , repet] <- Gam[i, ]/sumGamI
complexity = sumPen, contrast = -LLH)
}))
tableauRecap <- tableauRecap[which(tableauRecap[, 4] != Inf), ]
+
+
if (verbose == TRUE)
print(tableauRecap)
modSel <- capushe::capushe(tableauRecap, n)
modSel@AIC_capushe$model
}
-
- mod <- as.character(tableauRecap[indModSel, 1])
- listMod <- as.integer(unlist(strsplit(mod, "[.]")))
+ listMod <- as.integer(unlist(strsplit(as.character(indModSel), "[.]")))
modelSel <- models_list[[listMod[1]]][[listMod[2]]]
-
- ## Affectations
- Gam <- matrix(0, ncol = length(modelSel$pi), nrow = n)
- for (i in 1:n)
- {
- 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) * gdet(modelSel$rho[, , r])
- }
- }
- Gam <- Gam/rowSums(Gam)
- modelSel$affec <- apply(Gam, 1, which.max)
- modelSel$proba <- Gam
modelSel$tableau <- tableauRecap
-
+
if (plot)
print(plot_valse(X, Y, modelSel, n))
}
if (ncores > 1)
parallel::stopCluster(cl)
+
+ print(out)
# Suppress models which are computed twice En fait, ca ca fait la comparaison de
# tous les parametres On veut juste supprimer ceux qui ont les memes variables
# sélectionnées
-m=11
-p=10
+m=6
+p=6
covY = array(0,dim = c(m,m,2))
covY[,,1] = diag(m)
Beta[1:4,1:4,1] = 3*diag(4)
Beta[1:4,1:4,2] = -2*diag(4)
-#Data = generateXY(100, c(0.5,0.5), rep(0,p), Beta, diag(p), covY)
-
-#Res = valse(Data$X,Data$Y, fast=FALSE, plot=FALSE, verbose = TRUE, kmax=2, size_coll_mod = 100,
-# selecMod = "BIC")
+#Data = generateXY(200, c(0.5,0.5), rep(0,p), Beta, diag(p), covY)
+#
+#Res = valse(Data$X,Data$Y, fast=FALSE, plot=FALSE, verbose = TRUE, kmax=3, size_coll_mod = 50, selecMod = "DDSE", mini = 50, maxi=100)
+#plot(Res$tableau[,3], -Res$tableau[,4])