xvalLoop            package:MLInterfaces            R Documentation

_C_r_o_s_s-_v_a_l_i_d_a_t_i_o_n _i_n _c_l_u_s_t_e_r_e_d _c_o_m_p_u_t_i_n_g _e_n_v_i_r_o_n_m_e_n_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     Use cross-validation in a clustered computing environment

_U_s_a_g_e:

     xvalLoop( cluster, ... )

_A_r_g_u_m_e_n_t_s:

 cluster: Any S4-class object, used to indicate how to perform
          clustered computations.

     ...: Additional arguments used to inform the clustered
          computation.

_D_e_t_a_i_l_s:

     Cross-validiation usually involves repeated calls to the same
     function, but with different arguments. This provides an obvious
     place for using clustered computers to enhance execution. The
     method xval is structured to exploit this; 'xvalLoop' provides an
     easy mechanism to change how 'xval' performs cross-validation.

     The idea is to write an 'xvalLoop' method that returns a function.
     The function is then used to execute the cross-validation. For
     instance, the default method returns the function 'lapply', so the
     cross-validation is performed by using 'lapply'. A different
     method might return a function that executed lapply-like
     functions, but sent different parts of the function to different
     computer nodes.

     An accompanying vignette illustrates the technique in greater
     detail. An effective division of labor is for experienced cluster
     programmers to write lapply-like methods for their favored
     clustering environment. The user then only has to add the cluster
     object to the list of arguments to 'xval' to get clustered
     calculations.

_V_a_l_u_e:

     A function taking arguments like those for 'lapply'

_E_x_a_m_p_l_e_s:

     ## Not run: 
     library(golubEsets)
     data(Golub_Merge)
     smallG <- Golub_Merge[200:250,]

     # Evaluation on one node

     lk1 <- xval(smallG, "ALL.AML", knnB, xvalMethod="LOO", group=as.integer(0))
     table(lk1,smallG$ALL.AML)

     # Evaluation on several nodes -- a cluster programmer might write the following...

     library(snow)
     setOldClass("spawnedMPIcluster")

     setMethod("xvalLoop", signature( cluster = "spawnedMPIcluster"),
     ## use the function returned below to evalutae
     ## the central cross-validation loop in xval
     function( cluster, ... ) {
         clusterExportEnv <- function (cl, env = .GlobalEnv)
         {
             unpackEnv <- function(env) {
                 for ( name in ls(env) ) assign(name, get(name, env), .GlobalEnv )
                 NULL
             }
             clusterCall(cl, unpackEnv, env)
         }
         function(X, FUN, ...) { # this gets returned to xval
             ## send all visible variables from the parent (i.e., xval) frame
             clusterExportEnv( cluster, parent.frame(1) )
             parLapply( cluster, X, FUN, ... )
         }
     })

     # ... and use the cluster like this...

     cl <- makeCluster(2, "MPI")
     clusterEvalQ(cl, library(MLInterfaces))

     lk1 <- xval(smallG, "ALL.AML", knnB, xvalMethod="LOO", group=as.integer(0), cluster = cl)
     table(lk1,smallG$ALL.AML)
     ## End(Not run)

