并行R与引用类

时间:2014-01-14 19:20:42

标签: r parallel-processing

我创建了各种引用类以适应一些arima,garch进程,并希望使用parSapply

在并行计算中使用它们

我先做了一些出口

cl <- makeCluster(mc <- getOption("cl.cores", 20))
        clusterExport(cl, c("merge.xts", "index", "coredata", "xts", "lag.xts", "zoo", "LearnerPredict", "arima", "generic_learner", "arma_simple", "logwarn"))
        clusterEvalQ(cl, "arma_simple")
        clusterEvalQ(cl, "generic_learner")


generic_learner <- setRefClass(

        Class = "generic_learner",

        fields = list(

            params = "list"

        ),

        methods = list(

            fitModel = function() {cat("overload function with fitting function \n")},

            fcastModel = function() {cat("overload function with forecast function \n")},

            fmt_params = function() {cat("overload function with formatted parameters \n")},

            fmt_class = function() {cat("overload class\n")},

            fmt_ref = function() {paste(.self$fmt_class(), .self$fmt_params(),  sep = "_")},

            fcastload = function() {cat("overload function with load function \n")},

            fcastsave = function() {cat("overload function with save function \n")},

            exp_pred = function(){
                x = .self$pred.nofilter()

                if (is.null(x) | class(x) == "try-error"){
                    return(NA)
                } else {
                    return(x)           
                }
            }   
        )
)

我创建了一个名为call arma_simple的上述类,该类继承自generic_learner

    setOldClass("Arima")
arma_simple <- setRefClass(

        Class = "arma_simple",

        contains = "generic_learner",

        fields = list(
                fitted.ins = "Arima",
                pred.object = "list"
        ),


        methods = list(

                initialize = function(order.ins, fcast_length, rnd = rnorm(1)) {
                    .self$params$order.ins = order.ins
                    .self$params$fcast_length = fcast_length
                    .self$params$rnd = rnd * 100
                },

                fitModel = function(obj, order = .self$params$order.ins, ...) {

                    f <- try(arima(obj, order = .self$params$order.ins, ...))

                    if(!(class(f)=="try-error")){
                        fitted.ins <<- f
                    }

                },

                fcastModel = function(...) {
                    p <- try(predict(object = .self$fitted.ins, n.ahead = .self$params$fcast_length, ...))

                    if(!(class(p)=="try-error")){
                        pred.object <<- p
                    }
                },

                pred.nofilter = function() return(.self$pred.object$pred),

                fmt_class = function() .self$getClass()@className[[1]],
                fmt_params = function(){paste("p=", .self$params$order.ins[1],
                            "_d=", .self$params$order.ins[2],
                            "_q=", .self$params$order.ins[3],
                            "fcast=",
                            .self$params$fcast_length, sep = "")}
        )
)

我想使用这些引用类运行一个简单的函数。它适用于sapply,但我使用parSapply

收到错误
LearnerPredict = function(
        objectStudy,
        bacters,
        lrnr,
        period.study,
        ...){   

        objectStudy = replace(objectStudy, is.na(objectStudy), 0)

        logwarn("into learner")

        lrnr$fitModel(objectStudy)

        if (is.null(lrnr$fitted.ins) | class(lrnr$fitted.ins) == "try-error"){

            forecastreturn <- NA

        } else {

            lrnr$fcastModel()

            if  (!is.null(lrnr$pred.object) | class(lrnr$pred.object) == "try-error"){

                forecastreturn = lrnr$exp_pred()

            } else {

                forecastreturn <- NA

            }
        }

        fcastSave(objectStudy, bacters, lrnr, period.study)

        return(forecastreturn)
    #}
}





parSapply(cl, colnames(timeserie),

            function(x, y, learner.ins, funcmap, period.study, ...) {
                get(funcmap)(y[,x], bacters = x, lrnr = learner.ins, period.study = period.study, ...)
            },

        y = timeserie,
        learner.ins = arma_simple$new(order.ins = c(1,0,0), fcast_length = 1),
        funcmap = "LearnerPredict",
        period.study = 50
    )

Error in checkForRemoteErrors(val) : 
20 nodes produced errors; first error: attempt to apply non-function

首先,有没有办法并行追溯? 其次,您能否告诉我为什么sapply有效,而不是parSapply

0 个答案:

没有答案