#initialize submodules, set-up .git/config and .gitattributes, and pre-push hook
git submodule init && git submodule update --merge
+#filter for git-fat
+printf \
+'*.pdf filter=fat
+*.tar.xz filter=fat
+*.png filter=fat
+*.jpg filter=fat
+*.ps filter=fat\n' > .gitattributes
+
#filter for Jupyter
python .nbstripout/nbstripout.py --install --attributes .gitattributes
-#filter for git-fat [TODO: idempotent...]
-printf '*.pdf filter=fat\n*.tar.xz filter=fat\n*.png filter=fat\n*.jpg filter=fat\n*.ps filter=fat\n' >> .gitattributes
+#pre-commit and pre-push hooks: indentation, git fat push, submodules update
+cp hooks/* .git/hooks/
-#pre-push hook: git fat push, submodules update
-printf '#!/bin/sh\n./.git-fat/git-fat pull\n./.git-fat/git-fat push\ngit submodule update --merge\n' > .git/hooks/pre-push
-chmod 755 .git/hooks/pre-push
+#install formatR
+echo 'if (! "formatR" %in% rownames(installed.packages()))
+ install.packages("formatR",repos="https://cloud.r-project.org")' | R --slave
#.gitfat file with remote on gitfat@auder.net
printf '[rsync]\nremote = gitfat@auder.net:~/files/valse\n' > .gitfat
#manual git-fat init: with relative path to binary
-#1] remove filter if exists http://stackoverflow.com/questions/12179437/replace-3-lines-with-another-line-sed-syntax
+#1] remove filter if exists http://stackoverflow.com/a/12179641/4640434
sed -i '1N;$!N;s/\[filter "fat"\]\n.*\n.*//;P;D' .git/config
#2] place new filter
-printf '[filter "fat"]\n\tclean = ./.git-fat/git-fat filter-clean\n\tsmudge = ./.git-fat/git-fat filter-smudge\n' >> .git/config
+printf \
+'[filter "fat"]
+ clean = ./.git-fat/git-fat filter-clean
+ smudge = ./.git-fat/git-fat filter-smudge\n' >> .git/config
#' @return a list with several models, defined by phi, rho, pi, llh
#'
#' @export
-constructionModelesLassoRank = function(S, k, mini, maxi, X, Y, eps, rank.min,
- rank.max, ncores, fast=TRUE, verbose=FALSE)
-{
- n = dim(X)[1]
- p = dim(X)[2]
- m = dim(Y)[2]
- L = length(S)
+constructionModelesLassoRank <- function(S, k, mini, maxi, X, Y, eps, rank.min, rank.max,
+ ncores, fast = TRUE, verbose = FALSE)
+ {
+ n <- dim(X)[1]
+ p <- dim(X)[2]
+ m <- dim(Y)[2]
+ L <- length(S)
# Possible interesting ranks
- deltaRank = rank.max - rank.min + 1
- Size = deltaRank^k
- RankLambda = matrix(0, nrow=Size*L, ncol=k+1)
+ deltaRank <- rank.max - rank.min + 1
+ Size <- deltaRank^k
+ RankLambda <- matrix(0, nrow = Size * L, ncol = k + 1)
for (r in 1:k)
{
- # On veut le tableau de toutes les combinaisons de rangs possibles, et des lambdas
- # Dans la première colonne : on répète (rank.max-rank.min)^(k-1) chaque chiffre :
- # ça remplit la colonne
- # Dans la deuxieme : on répète (rank.max-rank.min)^(k-2) chaque chiffre,
- # et on fait ça (rank.max-rank.min)^2 fois
- # ...
- # Dans la dernière, on répète chaque chiffre une fois,
- # et on fait ça (rank.min-rank.max)^(k-1) fois.
- RankLambda[,r] = rep(rank.min + rep(0:(deltaRank-1), deltaRank^(r-1), each=deltaRank^(k-r)), each = L)
+ # On veut le tableau de toutes les combinaisons de rangs possibles, et des
+ # lambdas Dans la première colonne : on répète (rank.max-rank.min)^(k-1) chaque
+ # chiffre : ça remplit la colonne Dans la deuxieme : on répète
+ # (rank.max-rank.min)^(k-2) chaque chiffre, et on fait ça (rank.max-rank.min)^2
+ # fois ... Dans la dernière, on répète chaque chiffre une fois, et on fait ça
+ # (rank.min-rank.max)^(k-1) fois.
+ RankLambda[, r] <- rep(rank.min + rep(0:(deltaRank - 1), deltaRank^(r - 1),
+ each = deltaRank^(k - r)), each = L)
}
- RankLambda[,k+1] = rep(1:L, times = Size)
+ RankLambda[, k + 1] <- rep(1:L, times = Size)
if (ncores > 1)
{
- cl = parallel::makeCluster(ncores, outfile='')
- parallel::clusterExport( cl, envir=environment(),
- varlist=c("A1","Size","Pi","Rho","mini","maxi","X","Y","eps",
- "Rank","m","phi","ncores","verbose") )
+ cl <- parallel::makeCluster(ncores, outfile = "")
+ parallel::clusterExport(cl, envir = environment(), varlist = c("A1", "Size",
+ "Pi", "Rho", "mini", "maxi", "X", "Y", "eps", "Rank", "m", "phi", "ncores",
+ "verbose"))
}
computeAtLambda <- function(index)
{
- lambdaIndex = RankLambda[index,k+1]
- rankIndex = RankLambda[index,1:k]
- if (ncores > 1)
- require("valse") #workers start with an empty environment
+ lambdaIndex <- RankLambda[index, k + 1]
+ rankIndex <- RankLambda[index, 1:k]
+ if (ncores > 1)
+ require("valse") #workers start with an empty environment
# 'relevant' will be the set of relevant columns
- selected = S[[lambdaIndex]]$selected
- relevant = c()
- for (j in 1:p){
- if (length(selected[[j]])>0){
- relevant = c(relevant,j)
+ selected <- S[[lambdaIndex]]$selected
+ relevant <- c()
+ for (j in 1:p)
+ {
+ if (length(selected[[j]]) > 0)
+ {
+ relevant <- c(relevant, j)
}
}
- if (max(rankIndex)<length(relevant)){
- phi = array(0, dim=c(p,m,k))
+ if (max(rankIndex) < length(relevant))
+ {
+ phi <- array(0, dim = c(p, m, k))
if (length(relevant) > 0)
{
- res = EMGrank(S[[lambdaIndex]]$Pi, S[[lambdaIndex]]$Rho, mini, maxi,
- X[,relevant], Y, eps, rankIndex, fast)
- llh = c( res$LLF, sum(rankIndex * (length(relevant)- rankIndex + m)) )
- phi[relevant,,] = res$phi
+ res <- EMGrank(S[[lambdaIndex]]$Pi, S[[lambdaIndex]]$Rho, mini, maxi,
+ X[, relevant], Y, eps, rankIndex, fast)
+ llh <- c(res$LLF, sum(rankIndex * (length(relevant) - rankIndex +
+ m)))
+ phi[relevant, , ] <- res$phi
}
- list("llh"=llh, "phi"=phi, "pi" = S[[lambdaIndex]]$Pi, "rho" = S[[lambdaIndex]]$Rho)
+ list(llh = llh, phi = phi, pi = S[[lambdaIndex]]$Pi, rho = S[[lambdaIndex]]$Rho)
}
}
- #For each lambda in the grid we compute the estimators
- out =
- if (ncores > 1)
- parLapply(cl, seq_len(length(S)*Size), computeAtLambda)
- else
- lapply(seq_len(length(S)*Size), computeAtLambda)
+ # For each lambda in the grid we compute the estimators
+ out <- if (ncores > 1)
+ parLapply(cl, seq_len(length(S) * Size), computeAtLambda) else lapply(seq_len(length(S) * Size), computeAtLambda)
- if (ncores > 1)
+ if (ncores > 1)
parallel::stopCluster(cl)
out