From 4b2f17bb108bab0f263619cfe00eabfb1e9b8860 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 30 Apr 2020 13:49:20 +0200
Subject: [PATCH] Error when K > d for computeMu and optimParams

---
 pkg/R/computeMu.R   | 2 ++
 pkg/R/optimParams.R | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/pkg/R/computeMu.R b/pkg/R/computeMu.R
index bc52bb3..f961fde 100644
--- a/pkg/R/computeMu.R
+++ b/pkg/R/computeMu.R
@@ -46,6 +46,8 @@ computeMu = function(X, Y, optargs=list())
     large_ratio <- ( abs(Σ[-d] / Σ[-1]) > 3 )
     K <- if (any(large_ratio)) max(2, which.min(large_ratio)) else d
   }
+  else if (K > d)
+    stop("K: integer >= 2, <= d")
 
   # Step 1: generate a family of d matrices to joint-diagonalize to increase robustness
   d = ncol(X)
diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R
index 8dfe3dc..175150f 100644
--- a/pkg/R/optimParams.R
+++ b/pkg/R/optimParams.R
@@ -45,8 +45,8 @@ optimParams <- function(X, Y, K, link=c("logit","probit"), M=NULL, nc=0)
   if (!is.numeric(Y) || any(is.na(Y)) || any(Y!=0 & Y!=1))
     stop("Y: binary vector with 0 and 1 only")
   link <- match.arg(link)
-  if (!is.numeric(K) || K!=floor(K) || K < 2)
-    stop("K: integer >= 2")
+  if (!is.numeric(K) || K!=floor(K) || K < 2 || K > ncol(X))
+    stop("K: integer >= 2, <= d")
 
   if (is.null(M))
   {
-- 
2.44.0