Nothing
#' Making mixed-level CAs
#'
#' either from a given CA D, by makes unused levels of some columns flexible (
#' arbitrary strength), or for strength 2 only,
#' from required numbers of levels, by a construction generalized from Sherwood (2008).
#' In either case, the result is treated by function postopNCK for reducing its
#' run size.
#'
#' @rdname MCA
#'
#' @aliases MCA2
#' @aliases MCAt
#' @aliases CA_to_MCA
#' @aliases N_upper_MCA
#' @aliases N_upper_MCA2
#' @aliases projBoseMCA
#'
#' @usage MCA2(nlevels, D=NULL, outerRetry=NULL, innerRetry=NULL, seed=NULL,
#' verbose=0, nocafast=FALSE, ...)
#' @usage MCAt(nlevels, t, outerRetry=NULL, innerRetry=NULL, seed=NULL,
#' verbose=0, nocafast=FALSE, ...)
#' @usage CA_to_MCA(D, cs, tolevs, t=attr(D, "t"), outerRetry=NULL,
#' innerRetry=NULL, seed=NULL, verbose=0, nocafast=FALSE, ...)
#' @usage N_upper_MCA(nlevels, t=2, internet = TRUE, ...)
#' @usage N_upper_MCA2(nlevels, internet = TRUE, returnMore=FALSE, ...)
#' @usage projBoseMCA(nlevels, t=2, ...)
#'
#' @param nlevels a vector of numbers of levels, or a data.frame that is the table
#' of that vector, with levels in decreasing order and column names \code{level}
#' and \code{frequency}; function \code{MCA2} always sorts the numbers of levels
#' into decreeasing order
#' @param D for \code{MCA2}, a CA to be used for expanding; if \code{NULL}, it is
#' automatically determined; it is meant for experts only, and it may have mixed levels\cr
#' for \code{CA_to_MCA}, a strength \code{t} CA (it may also have mixed levels)
#' @param outerRetry \code{NULL} or non-negative integer value, passed to \code{\link{postopNCK}}; if \code{0},
#' run size reduction will not be attempted. The default (\code{NULL}) depends on whether the
#' suggested package \pkg{cafast} is available (50) or not (10).
#' @param innerRetry \code{NULL} or positive integer value, passed to \code{\link{postopNCK}};
#' the default depends on whether the suggested package \pkg{CAs} is available.
#' It is sometimes but not always beneficial to increase it versus the default 1
#' also without \pkg{cafast}.
#' @param seed the seed for making \code{\link{postopNCK}} reproducible
#' @param verbose integer number, handed to \code{\link{postopNCK}}
#' @param nocafast logical; if \code{TRUE}, suppresses the use of fast functions from package \pkg{cafast}
#' @param cs integer vector of column numbers
#' @param tolevs integer vector of target numbers of levels for the columns indicated
#' by \code{cs}; must not be larger than the number of levels of the related
#' columns in \code{cs}, and must not be smaller than 2
#' @param t integer, the strength of \code{D} (smaller strength would be permitted,
#' but would not make sense) or the requested strength
#' @param internet logical: is an internet connection available (for DWYER or NIST download)
#' @param returnMore logical: should \code{k} and \code{v} be returned in addition to \code{N}?
#' @param ... currently used by \code{N_upper_MCA}, passed to \code{\link{bestN}},
#' e.g. an \code{exclude} argument, and by \code{MCA2}, passed to \code{cafast::postopNCK_c}
#'
#' @section Details:
#' Function \code{MCA2} implements Groemping's (2025) generalization of the first
#' construction of Sherwood (2008) for creating mixed covering arrays (MCAs).
#' The algorithm is described in Groemping (2025). Its key idea is to
#' start from a uniform CA, make superfluous levels flexible, and expand
#' columns with enough flexible entries into more columns of the same number of levels,
#' using a recursive substitution of flexible values with ordered designs.
#' The result is then post-processed by function \code{\link{postopNCK}},
#' unless this is suppressed by the argument \code{outerRetry=0}. Post-processing
#' can also be done separately by directly using function \code{\link{postopNCK}}
#' on the result of function \code{MCA2}, regardless whether it was created with
#' or without post-processing.
#'
#' Function \code{MCAt} implements an automated version of function \code{CA_to_MCA},
#' in which the start array \code{D} for that function is automatically determined, so
#' that the user can simply specify the required numbers of levels with the required strength.
#'
#' Function \code{CA_to_MCA} sets unused levels of the columns in \code{cs} to \code{NA}, i.e.,
#' makes them flexible, as, e.g., described in Moura et al. (2003). Subsequently, the function
#' \code{\link{postopNCK}} applies the method of Nayery et al. (2013) for
#' making entire rows flexible and removing them. Repeated runs can remove more rows.
#' The \code{outerRetry} argument gives the number of calls to \code{\link{postopNCK}}.
#' if the bottom rows of the outcome still look promising, the function can be called
#' again for removing even more rows.
#'
#' Without specifying a seed, the results will differ from call to call.
#' If you want it reproducible, make sure to set an integer seed. Run sizes and run times
#' can be very seed-dependent in some situations, and it can happen that encountering a small
#' design is a rare lucky case. Therefore, one may sometimes want to walk over a vector of
#' random seeds and pick the best resulting design (reproducible by keeping track of the seeds).
#'
#' The run size optimization can take a long time. It can be suppressed by setting
#' \code{outerRetry} to zero, and this should be done for large settings.
#' Per default, the optimization is switched on. Its progress is reported
#' by interim messages. The functions (try to) react beneficial to user interrupts:
#' They return the result from the previous successful \emph{outer} retry
#' (make sure the outer retry has finished before escaping calculations!).
#'
#' Function \code{N_upper_MCA} reports an upper bound on the run number for an MCA
#' of the setting. That bound is obtained by using function \code{\link{bestN}} for
#' the maximum number of levels with the total number of columns. Depending on the situation,
#' the bound may be sharp (e.g., for eight five-level columns with two 3-level columns)
#' or dramatically too large (e.g., for one eight-level column with ten 2-level columns).
#'
#' Function \code{projBoseMCA} implements Theorem 2.3 of Colbourn (2008) as well as
#' Corollary 2.2 of the same paper, which also references Stevens, Ling and Mendelsohn
#' (2002, their Theorem 2.6). It works for two different numbers of levels only.
#' It is most powerful for q+1 columns at q-1 levels with a single column at q+1 levels,
#' where q is a prime power (the Corollary 2.2 situation);
#' in these cases the run size is optimal ((q-1)*(q+1)=q^2-1).
#'
#' @returns Functions \code{MCA2}, \code{MCAt} and \code{CA_to_MCA} return a matrix of
#' class \code{ca} with attributes of the ingoing matrix (dimensions modified),
#' and attributes \code{Call} and \code{seed} modified,
#' and often with flexible values in most columns. For function \code{MCA2},
#' the number of levels is in decreasing order
#' (regardless of the order specified in \code{nlevels}).\cr
#' Function \code{N_upper_MCA} returns an upper bound for the run size of an MCA
#' obtainable via the constructions (named integer, obtained by \code{\link{bestN}},
#' where the size can be obtained by using the CA of the construction indicated
#' by the name as the \code{D} in function \code{CA_to_MCA}).\cr
#' Function \code{projBoseMCA} returns a matrix of class \code{ca} with attributes;
#' for numeric \code{nlevels}, the columns are in the order of \code{nlevels}, otherwise
#' the columns with more levels come before the columns with fewer levels.
#'
#' @section Warning:
#' There is not much experience yet with the post-optimization performance.
#' The defaults for the related parameters may change in the future.
#'
#' @references Colbourn (2008), Groemping (2025), Moura et al. (2003), Nayeri et al. (2013),
#' Sherwood (2008), Stevens, Ling and Mendelsohn (2002)
#'
#' @author Ulrike Groemping
#'
#' @examples
#' ##################################################
#' ### MCA2
#' ##################################################
#' ## small example with fast optimization to global optimum
#' ## which is 2*8
#' ## 16 runs
#' D <- MCA2(c(rep(2,10), 8))
#' dim(D)
#' coverage(D, 2)
#' ## N_upper_MCA is very pessimistic
#' N_upper_MCA(c(rep(2,10), 8), t=2)
#' ## N_upper_MCA2 is better but still very pessimistic
#' ## for this tiny example
#' N_upper_MCA2(c(rep(2,10), 8))
#' ## a more dramatic difference
#' N_upper_MCA(c(4,4,4,3,3,rep(2,60)), 2) ## much too large
#' N_upper_MCA2(c(4,4,4,3,3,rep(2,60)), 2) ## exact size before postprocessing
#' ## a lesser difference
#' N_upper_MCA(c(rep(5,8), 3,3))
#' N_upper_MCA2(c(rep(5,8), 3,3), returnMore=TRUE)
#' D <- MCA2(c(rep(5,8), 3,3), D=miscCA(2,9,5), outerRetry=0)
#' dim(D)
#' head(D); tail(D)
#'
#' ## create a large design without trying a run size optimization
#' D <- MCA2(c(rep(2,70), rep(3,80), rep(4,8), rep(6,2)),
#' outerRetry=0)
#' dim(D)
#' coverage(D, 2)
#' ## alternative call for the same setting
#' D <- MCA2(data.frame(levels=c(6,4,3,2), frequency=c(2,8,80,70)),
#' outerRetry=0)
#' \donttest{
#' ## make a lucky run reproducible by using a random seed
#' myseed <- sample(32000, 1)
#' print(myseed)
#' Doptimized <- postopNCK(D, 2, seed=myseed, verbose=1, restarts=1)
#' ## This runs for a while.
#' ## Lower bound 36 runs is likely not achievable.
#' ## 42 or 41 rows were repeatedly achieved.
#' }
#'
#' ## use the argument D for starting with a naive
#' ## CA of 45 runs for 30 5-level columns
#' ## (default would start with CA(29, 2, 7, 5), which is the best for
#' ## seven 5-level columns and permits expansion as required);
#' D <- MCA2(c(5, rep(4,2), rep(3,7), rep(2,20)), D=bestCA(2,30,5),
#' outerRetry=0)
#' \donttest{
#' ## make a lucky run reproducible by using a random seed
#' myseed <- sample(32000, 1)
#' ## with cafast::postopNCK_c, with increased outerMaxnochange,
#' ## the optimum size 20 can be reached (was e.g. the case for
#' ## seeds 1188 or 25063 on Windows).
#' ## Without package cafast, run time would be excessive;
#' ## can be interrupted with <ESC> if run size improvement
#' ## has been sufficient or seems to stall
#' ## (make sure to wait until the outer retry has safely finished);
#' Doptimized <- postopNCK(D, 2, seed=myseed, verbose=1,
#' outerMaxnochange=30)
#' dim(Doptimized)
#' ## optimization stopped, because the global optimum was reached
#' }
#'
#' ########################################################
#' ### MCAt
#' ########################################################
#' ## create a strength 4 CA, initially unoptimized
#' D <- MCAt(c(4,4,3,2,2,2), 4, outerRetry=0)
#' dim(D) ## much larger than the optimum run size
#'
#' ########################################################
#' ### CA_to_MCA
#' ########################################################
#' # a small example
#' ## six 3-level columns in 33 runs
#' D <- bestCA(3,6,3)
#' ## to three 3-level and three 2-level columns in 27 runs
#' Dmixed <- CA_to_MCA(D, cs=4:6, tolev=rep(2, 3), t=3, outerRetry=0)
#' dim(Dmixed)
#' ## (optimal)
#' coverage(Dmixed, 3)
#' ## works
#'
#' ## five 3-level columns in 33 runs
#' D <- bestCA(3,5,3) ## CA(33,3,5,3)
#' ## to four 3-level columns and one 2-level column in 33 runs,
#' \donttest{
#' Dmixed <- CA_to_MCA(D, cs=5, tolev=2, t=3) ## last column reduced to 2 levels
#' ## the search cannot reduce the number of runs
#' }
#' ## D can be easily reduced to the optimal 27 runs,
#' ## if two columns are reduced to 2 levels each:
#' Dmixed <- CA_to_MCA(D, cs=4:5, tolev=c(2,2), t=3)
#'
#' ######################################################
#' #### projBoseMCA #############
#' ######################################################
#'
#' ## corollary 2.2 cases (a single number of levels is different,
#' ## and two larger than the others)
#' D <- projBoseMCA(c(rep(4,6),6))
#' head(D)
#' dim(D)
#'
#' D <- projBoseMCA(c(rep(6,8),8))
#' head(D)
#' dim(D)
#'
#' @examplesIf getOption("run_heavy_examples", default = FALSE)
#' ## NOTE: example() skips the examples below (for MCA2 and MCAt),
#' ## unless the user specifies
#' ## options(run_heavy_examples = TRUE)
#' ## This is because they could be very slow depending on sample.kind.
#'
#' ## different setting,
#' ## optimize run size within the call (using a good seed)
#' D <- MCA2(c(rep(2,30), rep(3,2)), seed=333, outerRetry=3)
#' coverage(D, 2)
#' attributes(D)
#' ## reasonably quickly reduces to 11 runs on my Windows machine
#' ## lower bound is 9 runs (3*3)
#' ## CAgen yields 14 runs with IPOG-F
#'
#' D <- MCAt(c(4,4,3,2,2,2), 4, outerRetry=0)
#' ## with seed 888, the optimum is quickly reached on Windows
#' Doptimized <- postopNCK(D, 4, innerRetry = 3, seed=888)
#' dim(Doptimized)
#' ## optimum 4*4*3*2=96
#' ## many other seeds stop at 97 or 98 runs after longer iteration
#' ## could also use
#' D <- MCAt(c(4,4,3,2,2,2), 4, innerRetry=3, seed=888)
#'
#' @export
MCAt <- function(nlevels, t, outerRetry=NULL,
innerRetry=NULL, seed=NULL, verbose=0, nocafast=FALSE, ...){
if (is.null(outerRetry)) outerRetry <- ifelse(requireNamespace("cafast") && !nocafast, 50, 10)
if (is.null(innerRetry)) innerRetry <- ifelse(requireNamespace("cafast") && !nocafast, 10, 1)
# Input validation with meaningful error messages
if (missing(t))
stop("t (interaction strength) must be specified")
if (!is.numeric(t) || length(t) != 1 || t < 1 || t != as.integer(t))
stop("t must be a single positive integer")
if (t <= 2)
stop("t must be greater than 2 (for t=2, use MCA2 instead)")
if (missing(nlevels))
stop("nlevels must be specified")
if (!is.list(nlevels)){
if (!is.numeric(nlevels))
stop("if not a list or a data.frame, nlevels must be a vector of numbers of levels")
if (length(nlevels) == 0)
stop("nlevels must have at least one element")
if (any(is.na(nlevels)))
stop("nlevels must have non-NA entries only")
if (any(nlevels < 2 | nlevels != as.integer(nlevels)))
stop("all elements of nlevels must be integers >= 2")
if (is.numeric(nlevels)){
k <- length(nlevels)
levs <- nlevels
}}else{
## correct format
if (!all(c("levels", "frequency") %in% names(nlevels)))
stop("nlevels must have components 'levels' and 'frequency'")
if (!is.numeric(nlevels$levels) || !is.numeric(nlevels$frequency))
stop("components 'levels' and 'frequency' in nlevels must be numeric")
if (any(nlevels$levels < 2 | nlevels$levels != as.integer(nlevels$levels)))
stop("all 'levels' in nlevels must be integers >= 2")
if (any(nlevels$frequency < 1 | nlevels$frequency != as.integer(nlevels$frequency)))
stop("all 'frequency' values in nlevels must be positive integers")
## decreasing order
if (!all(nlevels$levels==sort(nlevels$levels, decreasing=TRUE)))
stop("levels in nlevels must be sorted in decreasing order")
if (!length(nlevels$levels)==length(nlevels$frequency))
stop("the components 'levels' and 'frequency' must have the same length")
if (!length(nlevels$levels)==length(unique(nlevels$levels)))
stop("nlevels$levels must have unique elements")
nlevels <- as.data.frame(nlevels[c("levels", "frequency")])
k <- sum(nlevels$frequency)
levs <- unlist(mapply(rep, nlevels$levels, nlevels$frequency))
}
## now levs is numeric
stopifnot(length(levs)==k)
stopifnot(all(levs%%1==0))
stopifnot(min(levs)>=2)
v <- max(levs)
D <- bestCA(t,k,v)
cs <- setdiff(1:k, which(levs==v))
CA_to_MCA(D, cs, levs[cs], t, outerRetry = outerRetry,
innerRetry = innerRetry, seed=seed, nocafast=nocafast,
verbose=verbose, ...)
}
#' @export
CA_to_MCA <- function(D, cs, tolevs, t=attr(D, "t"),
outerRetry=NULL, innerRetry=NULL, seed=NULL,
verbose=0, nocafast=FALSE, ...){
Call <- sys.call()
if (is.null(outerRetry)) outerRetry <- ifelse(requireNamespace("cafast") && !nocafast, 50, 10)
if (is.null(innerRetry)) innerRetry <- ifelse(requireNamespace("cafast") && !nocafast, 10, 1)
# Input validation with meaningful error messages
if (missing(D))
stop("D (covering array) must be specified")
if (!is.matrix(D))
stop("D must be a matrix")
if (!is.numeric(D))
stop("D must be numeric")
if (missing(cs))
stop("cs (column numbers to modify) must be specified")
if (missing(tolevs))
stop("tolevs (target number of levels) must be specified")
# Validate t parameter - distinguish between missing attribute and invalid parameter
if (missing(t)) {
# t was not provided as a parameter, check if it exists as an attribute
t <- attr(D, "t")
if (is.null(t) || !is.numeric(t))
stop("D does not have a valid attribute 't', please specify the strength t")
}
# Now validate the t value (whether from attribute or parameter)
if (!is.numeric(t) || length(t) != 1 || t < 1 || t != as.integer(t))
stop("t must be a single positive integer")
if (!is.numeric(cs))
stop("cs must be a numeric vector of column numbers")
if (any(cs < 1 | cs != as.integer(cs)))
stop("all elements in cs must be positive integers")
if (any(cs > ncol(D)))
stop("all elements in cs must be valid column numbers (must not exceed ", ncol(D), ")")
if (!is.numeric(tolevs))
stop("tolevs must be a numeric vector")
if (any(tolevs < 2 | tolevs != as.integer(tolevs)))
stop("all elements in tolevs must be integers >= 2")
if (length(cs) != length(tolevs))
stop("cs and tolevs must have the same length")
start0 <- min(D)==0
## assuming valid array starting at 0 or 1
k <- ncol(D)
N <- Norig <- nrow(D)
levs <- levels.no.NA(D)
if (any(tolevs > levs[cs])){
bad_cols <- cs[which(tolevs > levs[cs])]
stop("tolevs cannot be larger than the current number of levels; ",
"problem in column(s): ", paste(bad_cols, collapse=", "))
}
attrs_stored <- attributes(D)
if (!is.null(attr(D, "constr")))
attrs_stored$ingredients <- list(
D=ingredientCA(D, t, ncol(D), max(levels.no(D))))
levs[cs] <- tolevs
optN <- lower_bound_CA(t, k, levs)
for (j in 1:length(cs)){
D[which(D[,cs[j]] > tolevs[j] - as.numeric(start0)),cs[j]] <- NA
# sample(levs[cs[j]] - as.numeric(start0),1)
}
hilf <- rowSums(is.na(D))
D <- D[which(hilf <= k-t),]
if (nrow(D) < N){
N <- nrow(D)
attrs_stored$dim[1] <- N
}
if (optN == N){
if (Norig==N) message("D already has the optimum number of rows.")
attrs <- attrs_stored
attrs$Call <- c(attrs_stored$Call, Call)
attributes(D) <- attrs
return(D)
}
Dnow <- D
if (outerRetry >= 1) tryCatch({
aus <- postopNCK(Dnow, t, outerRetry = outerRetry,
innerRetry = innerRetry, seed=seed,
nocafast=nocafast, ...)
latest_result <- aus
Dnow <- aus
},
interrupt = function(e) {
attr(latest_result, "rowOrder") <- NULL
attrs <- attrs_stored
attrs$Call <- c(attrs_stored$Call, Call)
attrs$seed <- c(attrs$seed, ifelse(is.null(seed), NA, seed))
attributes(latest_result) <- c(dim=list(dim(latest_result)),
attrs[setdiff(names(attrs),"dim")])
return(latest_result)
})else aus <- D
if (nrow(aus) == Norig){
if (outerRetry > 0) message("The number of rows of D could not be reduced.")
attrs <- attrs_stored
attrs$Call <- c(attrs_stored$Call, Call)
attributes(D) <- attrs
return(D)
}
attr(aus, "rowOrder") <- NULL
attrs <- attrs_stored
attrs$Call <- c(attrs_stored$Call, Call)
attrs$seed <- c(attrs$seed, ifelse(is.null(seed), NA, seed))
attributes(aus) <- c(dim=list(dim(aus)), attrs[setdiff(names(attrs),"dim")])
aus
}
#' @export
MCA2 <- function(nlevels, D=NULL, outerRetry=NULL, innerRetry=NULL, seed=NULL,
verbose=0, nocafast=FALSE, ...){
Call <- sys.call()
if (is.null(outerRetry)) outerRetry <- ifelse(requireNamespace("cafast") && !nocafast, 50, 10)
if (is.null(innerRetry)) innerRetry <- ifelse(requireNamespace("cafast") && !nocafast, 10, 1)
# Input validation with meaningful error messages
if (missing(nlevels))
stop("nlevels must be specified")
if (!is.list(nlevels)){
if (!is.numeric(nlevels))
stop("if not a list or a data.frame, nlevels must be a vector of numbers of levels")
if (is.numeric(nlevels)){
if (length(nlevels) == 0)
stop("nlevels must have at least one element")
if (any(is.na(nlevels)))
stop("nlevels must have non-NA entries only")
if (any(nlevels < 2 | nlevels != as.integer(nlevels)))
stop("all elements of nlevels must be integers >= 2")
k <- length(nlevels)
tab <- table(nlevels)
nlevels <- data.frame(
levels=as.numeric(rev(names(tab))),
frequency=rev(unname(as.numeric(tab))))
}
} ## now nlevels is a list or data.frame
## correct format
if (!all(c("levels", "frequency") %in% names(nlevels)))
stop("nlevels must have components 'levels' and 'frequency'")
if (!is.numeric(nlevels$levels) || !is.numeric(nlevels$frequency))
stop("components 'levels' and 'frequency' in nlevels must be numeric")
if (any(nlevels$levels < 2 | nlevels$levels != as.integer(nlevels$levels)))
stop("all 'levels' in nlevels must be integers >= 2")
if (any(nlevels$frequency < 1 | nlevels$frequency != as.integer(nlevels$frequency)))
stop("all 'frequency' values in nlevels must be positive integers")
## decreasing order
if (!all(nlevels$levels==sort(nlevels$levels, decreasing=TRUE)))
stop("levels in nlevels must be sorted in decreasing order")
if (!length(nlevels$levels)==length(nlevels$frequency))
stop("the components 'levels' and 'frequency' must have the same length")
if (!length(nlevels$levels)==length(unique(nlevels$levels)))
stop("nlevels$levels must have unique elements")
nlevels <- as.data.frame(nlevels[c("levels", "frequency")])
## optimum N
optN <- ifelse(nlevels$frequency[1]>=2, nlevels$levels[1]^2,
prod(nlevels$levels[1:2]))
## find a suitable construction
hilf <- N_upper_MCA2(nlevels, returnMore = TRUE)
## create the ingoing array
if (is.null(D))
D <- bestCA(2, hilf$k, hilf$v)
attrs <- attributes(D)
if (!is.null(attr(D, "constr")))
attrs$ingredients <- list(D=ingredientCA(D, 2, hilf$k, hilf$v))
## apply the construction
## outerseparate re-sorts the levels of D in the order of increasing frequency
## in order to allow as many columns as possible for the expansion
aus <- outerseparate(D, nlevels, ...)
latest_result <- aus
message("found array with ", nrow(aus), " rows")
## try to improve the run size by postopNCK
## made early-stop friendly
Dnow <- aus
if (outerRetry > 0){
message("working on improving the run size")
message("on interrupt, the current best result is returned")
message("there will be at most ", outerRetry, " outer loop steps")
tryCatch({
aus <- postopNCK(Dnow, 2, innerRetry = innerRetry, outerRetry=outerRetry,
seed=seed, nocafast=nocafast, ...)
latest_result <- aus
Dnow <- aus
},
interrupt=function(e){
# nocov start
attrnow <- attributes(latest_result)
namesvorher <- names(attrs)
namesnow <- names(attrnow)
attrs$dim <- attrnow$dim
attrs$dimnames <- attrnow$dimnames
attrs[setdiff(names(attrnow), names(attrs))] <- attrnow[setdiff(names(attrnow), names(attrs))]
attributes(latest_result) <- attrs
attr(latest_result, "Call") <- c(Call, attr(latest_result, "Call"))
attr(latest_result, "eCAN") <- NULL
attr(latest_result, "PCAstatus") <- NULL
attr(latest_result, "rowOrder") <- NULL ## no longer relevant
return(latest_result)
# nocov end
})
}
attrnow <- attributes(aus)
namesvorher <- names(attrs)
namesnow <- names(attrnow)
attrs$dim <- attrnow$dim
attrs$dimnames <- attrnow$dimnames
attrs[setdiff(names(attrnow), names(attrs))] <-
attrnow[setdiff(names(attrnow), names(attrs))]
attributes(aus) <- attrs
attr(aus, "Call") <- c(Call, attr(aus, "Call"))
attr(aus, "eCAN") <- NULL ## no longer relevant
attr(aus, "rowOrder") <- NULL ## no longer relevant
attr(aus, "PCAstatus") <- NULL ## will be destroyed
return(aus)
}
#' @export
N_upper_MCA <- function(nlevels, t=2, internet=TRUE, ...){
if (!is.list(nlevels))
if (!is.numeric(nlevels))
stop("if not a list or a data.frame, \nnlevels must be a vector of numbers of levels")
if (is.numeric(nlevels)){
k <- length(nlevels)
v <- max(nlevels)
}else{
if (!all(c("levels", "frequency") %in% names(nlevels)))
stop("nlevels must have components 'levels' and 'frequency'")
if (!length(nlevels$levels)==length(nlevels$frequency))
stop("the components 'levels' and 'frequency' must have the same length")
k <- sum(nlevels$frequency)
v <- max(nlevels$levels)
}
bestN(t, k, v, internet=internet, ...)
}
#' @export
N_upper_MCA2 <- function(nlevels, internet=TRUE, returnMore=FALSE, ...){
if (!is.list(nlevels))
if (!is.numeric(nlevels))
stop("if not a list or a data.frame, \nnlevels must be a vector of numbers of levels")
if (is.numeric(nlevels)){
k <- length(nlevels)
v <- max(nlevels)
nlevels <- table(nlevels)
nlevels <- data.frame(
levels=as.numeric(rev(names(nlevels))),
frequency=rev(unname(as.numeric(nlevels)))
)} else{
if (!all(c("levels", "frequency") %in% names(nlevels)))
stop("nlevels must have components 'levels' and 'frequency'")
if (!length(nlevels$levels)==length(nlevels$frequency))
stop("the components 'levels' and 'frequency' must have the same length")
k <- sum(nlevels$frequency)
v <- max(nlevels$levels)
}
nlevels <- nlevels[order(nlevels$levels, decreasing = TRUE),]
ndifflevs <- nrow(nlevels)
mink <- nlevels$frequency[1] + ndifflevs - 1
s <- sapply(nlevels$levels, function(obj) ncol(OD2(obj)))
propflex <- (1-nlevels$levels/v)
nOD <- nlevels$levels*(nlevels$levels-1)
found <- FALSE
while(!found){
minN <- bestN(2, mink, v, internet=internet, ...)
minnflex <- ceiling(minN*propflex)
r <- floor(minnflex/nOD)
capacity <- s^r
colsneeded <- ceiling(nlevels$frequency/capacity)
if (sum(colsneeded)>mink) mink <- mink + 1 else found <- TRUE
}
if (returnMore) return(list(N=minN, k=mink, v=v)) else return(minN)
}
#' @export
projBoseMCA <- function(nlevels, t=2, ...){
if (!is.list(nlevels))
if (!is.numeric(nlevels))
stop("if not a list or a data.frame, nlevels must be a vector of numbers of levels")
if (is.numeric(nlevels)){
nlevels_ordered <- nlevels
levels <- sort(unique(nlevels), decreasing = TRUE)
if (length(levels) < 2) stop("uniform CA requested, use projBoseCA")
if (length(levels) > 2) stop("projBoseMCA not applicable for more than two different numbers of levels")
nlevels <- data.frame(levels=levels, frequency=sapply(levels, function(obj) sum(nlevels==obj)))
}else{
## correct format
stopifnot(c("levels", "frequency") %in% names(nlevels))
## decreasing order
if (!length(nlevels$levels)==length(unique(nlevels$levels)))
stop("nlevels$levels must have unique elements")
if (length(nlevels$levels)>2)
stop("projBoseMCA not applicable for more than two different numbers of levels")
if (!all(nlevels$levels==sort(nlevels$levels, decreasing=TRUE)))
stop("levels in nlevels must be sorted in decreasing order")
if (!length(nlevels$levels)==length(nlevels$frequency))
stop("the components 'levels' and 'frequency' must have the same length")
nlevels <- as.data.frame(nlevels[c("levels", "frequency")])
nlevels_ordered <- rep(nlevels$levels, nlevels$frequency)
levels <- nlevels$levels
}
## now, nlevels is a data.frame with levels sorted in decreasing order
c_temp <- min(nlevels$frequency)
## c at least c_temp, perhaps more
s <- nlevels$levels[which(nlevels$frequency==c_temp)]
levother <- setdiff(nlevels$levels, s)
if (s > levother && c_temp > 1) stop("combination of c and s not permissible")
if (s > levother){
## the special case of Colbourn 2008 Corollary 2.2
c <- 1
if (!(s - levother <= 2)) stop("this setting is not covered by projBoseMCA")
if (!(levother + 1 %in% primedat$q)) stop("this setting is not covered by projBoseMCA")
q <- levother + 1
nqminus1 <- max(nlevels$frequency)
if (nqminus1 > q + 1) stop("two many columns with ", q, "-1 levels for projBoseMCA")
aus <- projectionBose(q, c, s)[,c(1:nqminus1, q+2)]
## arrange columns in requested order for originally numeric
## nlevels
pos_s <- which.max(nlevels_ordered)
if (!pos_s==length(nlevels_ordered)){
if (pos_s==1) aus <- aus[,c(ncol(aus),1:(ncol(aus)-1))]
else aus <- aus[,c(1:(pos_s-1), ncol(aus), pos_s:(ncol(aus)-1))]
}
## assign attributes
class(aus) <- c("ca", class(aus))
attr(aus, "origin") <- "Colbourn 2008 Corollary 2.2 (and Stevens, Ling, Mendelssohn 2002)"
attr(aus, "t") <- 2
return(aus)
}
## now s < levother
## c_temp = frequency of s
c <- NA
if (levother + c_temp %in% primedat$q &&
max(nlevels$frequency) <= levother + c_temp + 1){
c <- c_temp
q <- levother + c
}else{
## now c_temp must be increased for getting
## both a prime and enough columns
repeat{
c_temp <- c_temp + 1
if (levother + c_temp %in% primedat$q &&
max(nlevels$frequency) <= levother + c_temp + 1){
c <- c_temp
q <- levother + c
break
}
}
}
## as s < levother
nqminusc <- max(nlevels$frequency)
n_s <- min(nlevels$frequency)
## create the array with maximum number of columns
hilf <- projectionBose(q, c, s)[,c(1:nqminusc, (q+2):(q+1+n_s))]
## arrange columns in requested order for originally numeric
## nlevels
pos_s <- which(nlevels_ordered==min(nlevels$levels))
pos_qminusc <- which(nlevels_ordered==max(nlevels$levels))
aus <- matrix(NA, nrow=nrow(hilf), ncol=ncol(hilf))
aus[,pos_s] <- hilf[,(nqminusc+1):(ncol(hilf))]
aus[,pos_qminusc] <- hilf[,1:nqminusc]
## assign attributes
class(aus) <- c("ca", class(aus))
attr(aus, "origin") <- "Colbourn 2008 thm 2.3"
attr(aus, "t") <- 2
aus
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.