\HeaderA{balKfold.xvspec}{generate a partition function for cross-validation,
where the partitions are approximately balanced with respect to
the distribution of a response variable}{balKfold.xvspec}
\keyword{models}{balKfold.xvspec}
\keyword{manip}{balKfold.xvspec}
\begin{Description}\relax
generate a partition function for cross-validation,
where the partitions are approximately balanced with respect to
the distribution of a response variable
\end{Description}
\begin{Usage}
\begin{verbatim}
balKfold.xvspec(K)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{K}] number of partitions to be computed 
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This function returns a closure.  The symbol \code{K} is
bound in the environment of the returned function.
\end{Details}
\begin{Value}
A closure consisting of a function that can be used
as a \code{partitionFunc} for passage in \code{\LinkA{xvalSpec}{xvalSpec}}.
\end{Value}
\begin{Author}\relax
VJ Carey <stvjc@channing.harvard.edu>
\end{Author}
\begin{Examples}
\begin{ExampleCode}
## The function is currently defined as
function (K) 
function(data, clab, iternum) {
    clabs <- data[[clab]]
    narr <- nrow(data)
    cnames <- unique(clabs)
    ilist <- list()
    for (i in 1:length(cnames)) ilist[[cnames[i]]] <- which(clabs == 
        cnames[i])
    clens <- lapply(ilist, length)
    nrep <- lapply(clens, function(x) ceiling(x/K))
    grpinds <- list()
    for (i in 1:length(nrep)) grpinds[[i]] <- rep(1:K, nrep[[i]])[1:clens[[i]]]
    (1:narr)[-which(unlist(grpinds) == iternum)]
  }
# try it out
data(crabs)
p1c = balKfold.xvspec(5)
inds = p1c( crabs, "sp", 3 )
table(crabs$sp[inds] )
inds2 = p1c( crabs, "sp", 4 )
table(crabs$sp[inds2] )
allc = 1:200
# are test sets disjoint?
intersect(setdiff(allc,inds), setdiff(allc,inds2))
\end{ExampleCode}
\end{Examples}

