balKfold.xvspec         package:MLInterfaces         R Documentation

_g_e_n_e_r_a_t_e _a _p_a_r_t_i_t_i_o_n _f_u_n_c_t_i_o_n _f_o_r _c_r_o_s_s-_v_a_l_i_d_a_t_i_o_n,
_w_h_e_r_e _t_h_e _p_a_r_t_i_t_i_o_n_s _a_r_e _a_p_p_r_o_x_i_m_a_t_e_l_y _b_a_l_a_n_c_e_d _w_i_t_h _r_e_s_p_e_c_t _t_o
_t_h_e _d_i_s_t_r_i_b_u_t_i_o_n _o_f _a _r_e_s_p_o_n_s_e _v_a_r_i_a_b_l_e

_D_e_s_c_r_i_p_t_i_o_n:

     generate a partition function for cross-validation, where the
     partitions are approximately balanced with respect to the
     distribution of a response variable

_U_s_a_g_e:

     balKfold.xvspec(K)

_A_r_g_u_m_e_n_t_s:

       K: number of partitions to be computed 

_D_e_t_a_i_l_s:

     This function returns a closure.  The symbol 'K' is bound in the
     environment of the returned function.

_V_a_l_u_e:

     A closure consisting of a function that can be used as a
     'partitionFunc' for passage in 'xvalSpec'.

_A_u_t_h_o_r(_s):

     VJ Carey <stvjc@channing.harvard.edu>

_E_x_a_m_p_l_e_s:

     ## 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))

