3 #' Estimate the normalized columns μ of the β matrix parameter in a mixture of
4 #' logistic regressions models, with a spectral method described in the package vignette.
8 #' @param X Matrix of input data (size nxd)
9 #' @param Y Vector of binary outputs (size n)
10 #' @param optargs List of optional argument:
12 #' \item 'jd_method', joint diagonalization method from the package jointDiag:
13 #' 'uwedge' (default) or 'jedi'.
14 #' \item 'jd_nvects', number of random vectors for joint-diagonalization
15 #' (or 0 for p=d, canonical basis by default)
16 #' \item 'M', moments of order 1,2,3: will be computed if not provided.
17 #' \item 'K', number of populations (estimated with rank of M2 if not given)
20 #' @return The estimated normalized parameters as columns of a matrix μ of size dxK
22 #' @seealso \code{multiRun} to estimate statistics based on μ,
23 #' and \code{generateSampleIO} for I/O random generation.
26 #' io <- generateSampleIO(10000, 1/2, matrix(c(1,0,0,1),ncol=2), c(0,0), "probit")
27 #' μ <- computeMu(io$X, io$Y, list(K=2)) #or just X and Y for estimated K
30 computeMu <- function(X, Y, optargs=list())
32 if (!is.matrix(X) || !is.numeric(X) || any(is.na(X)))
33 stop("X: real matrix, no NA")
36 if (!is.numeric(Y) || length(Y)!=n || any(Y!=0 & Y!=1))
37 stop("Y: vector of 0 and 1, size nrow(X), no NA")
38 if (!is.list(optargs))
41 # Step 0: Obtain the empirically estimated moments tensor, estimate also K
42 M <- if (is.null(optargs$M)) computeMoments(X,Y) else optargs$M
46 # TODO: improve this basic heuristic
48 large_ratio <- ( abs(Σ[-d] / Σ[-1]) > 3 )
49 K <- if (any(large_ratio)) max(2, which.min(large_ratio)) else d
52 stop("K: integer >= 2, <= d")
54 # Step 1: generate a family of d matrices to joint-diagonalize to increase robustness
57 jd_nvects <- ifelse(!is.null(optargs$jd_nvects), optargs$jd_nvects, 0)
63 M2_t <- array(dim=c(d,d,jd_nvects))
64 for (i in seq_len(jd_nvects))
66 rho <- if (fixed_design) c(rep(0,i-1),1,rep(0,d-i)) else normalize( rnorm(d) )
67 M2_t[,,i] <- .T_I_I_w(M[[3]],rho)
70 # Step 2: obtain factors u_i (and their inverse) from the joint diagonalisation of M2_t
71 jd_method <- ifelse(!is.null(optargs$jd_method), optargs$jd_method, "uwedge")
74 # NOTE: increasing itermax does not help to converge, thus we suppress warnings
75 suppressWarnings({jd = jointDiag::ajd(M2_t, method=jd_method)})
76 if (jd_method=="uwedge") jd$B else MASS::ginv(jd$A)
79 eigen(M2_t[,,1])$vectors
81 # Step 3: obtain final factors from joint diagonalisation of T(I,I,u_i)
82 M2_t <- array(dim=c(d,d,K))
84 M2_t[,,i] <- .T_I_I_w(M[[3]],V[,i])
85 suppressWarnings({jd = jointDiag::ajd(M2_t, method=jd_method)})
86 U <- if (jd_method=="uwedge") MASS::ginv(jd$B) else jd$A
87 μ <- normalize(U[,1:K])
89 # M1 also writes M1 = sum_k coeff_k * μ_k, where coeff_k >= 0
90 # ==> search decomposition of vector M1 onto the (truncated) basis μ (of size dxK)
91 # This is a linear system μ %*% C = M1 with C of size K ==> C = psinv(μ) %*% M1
92 C <- MASS::ginv(μ) %*% M[[1]]
93 μ[,C < 0] <- - μ[,C < 0]