From e3f2fe8a918614d246fe2451065b0dfcd348b366 Mon Sep 17 00:00:00 2001
From: emilie <emilie@devijver.org>
Date: Mon, 6 Mar 2017 13:26:42 +0100
Subject: [PATCH] update valse.R

---
 .gitignore      | 10 +++++-----
 DESCRIPTION     |  3 ++-
 R/gridLambda.R  |  5 ++++-
 R/initSmallEM.R | 13 +++++++++----
 R/main.R        |  2 +-
 5 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index b1cd49c..56843bc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,7 @@
-/NAMESPACE
+.Rproj.user
 .Rhistory
 .RData
-*.swp
-*~
-/man/*
-!/man/*-package.Rd
+.Ruserdata
+src/*.o
+src/*.so
+src/*.dll
diff --git a/DESCRIPTION b/DESCRIPTION
index f8f5a29..9d8a677 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -16,7 +16,8 @@ Maintainer: Benjamin Auder <Benjamin.Auder@math.u-psud.fr>
 Depends:
     R (>= 3.0.0)
 Imports:
-    MASS
+    MASS,
+    methods
 Suggests:
     parallel,
     testthat,
diff --git a/R/gridLambda.R b/R/gridLambda.R
index 855b4a6..e7946ae 100644
--- a/R/gridLambda.R
+++ b/R/gridLambda.R
@@ -1,8 +1,11 @@
 #' Construct the data-driven grid for the regularization parameters used for the Lasso estimator
 #' @param phiInit value for phi
-#' @param rhoInt	value for rho
+#' @param rhoInit	value for rho
 #' @param piInit	value for pi
 #' @param gamInit value for gamma
+#' @param X matrix of covariates (of size n*p)
+#' @param Y matrix of responses (of size n*m)
+#' @param gamma power of weights in the penalty
 #' @param mini		minimum number of iterations in EM algorithm
 #' @param maxi		maximum number of iterations in EM algorithm
 #' @param tau		threshold to stop EM algorithm
diff --git a/R/initSmallEM.R b/R/initSmallEM.R
index e2157b2..399f39f 100644
--- a/R/initSmallEM.R
+++ b/R/initSmallEM.R
@@ -3,11 +3,12 @@
 #' @param k number of components
 #' @param X matrix of covariates (of size n*p)
 #' @param Y matrix of responses (of size n*m)
-#' @param tau threshold to stop EM algorithm
 #'
 #' @return a list with phiInit, rhoInit, piInit, gamInit
 #' @export
-initSmallEM = function(k,X,Y,tau)
+#' @importFrom methods new
+#' @importFrom stats cutree dist hclust runif
+initSmallEM = function(k,X,Y)
 {
 	n = nrow(Y)
 	m = ncol(Y)
@@ -34,9 +35,13 @@ initSmallEM = function(k,X,Y,tau)
 		{
 			Z = Zinit1[,repet]
 			Z_indice = seq_len(n)[Z == r] #renvoit les indices où Z==r
-			
+			if (length(Z_indice) == 1) {
+			  betaInit1[,,r,repet] = ginv(crossprod(t(X[Z_indice,]))) %*%
+			    crossprod(t(X[Z_indice,]), Y[Z_indice,])
+			} else {
 			betaInit1[,,r,repet] = ginv(crossprod(X[Z_indice,])) %*%
 				crossprod(X[Z_indice,], Y[Z_indice,])
+			}
 			sigmaInit1[,,r,repet] = diag(m)
 			phiInit1[,,r,repet] = betaInit1[,,r,repet] #/ sigmaInit1[,,r,repet]
 			rhoInit1[,,r,repet] = solve(sigmaInit1[,,r,repet])
@@ -58,7 +63,7 @@ initSmallEM = function(k,X,Y,tau)
 		maxiInit = 11
 		
 		new_EMG = .Call("EMGLLF_core",phiInit1[,,,repet],rhoInit1[,,,repet],piInit1[repet,],
-			gamInit1[,,repet],miniInit,maxiInit,1,0,X,Y,tau)
+			gamInit1[,,repet],miniInit,maxiInit,1,0,X,Y,1e-4)
 		LLFEessai = new_EMG$LLF
 		LLFinit1[repet] = LLFEessai[length(LLFEessai)]
 	}
diff --git a/R/main.R b/R/main.R
index 42852d3..1908021 100644
--- a/R/main.R
+++ b/R/main.R
@@ -92,7 +92,7 @@ Valse = setRefClass(
 			#smallEM initializes parameters by k-means and regression model in each component,
 			#doing this 20 times, and keeping the values maximizing the likelihood after 10
 			#iterations of the EM algorithm.
-			init = initSmallEM(k,X,Y,eps)
+			init = initSmallEM(k,X,Y)
 			phiInit <<- init$phi0
 			rhoInit <<- init$rho0
 			piInit	<<- init$pi0
-- 
2.44.0