R/opt1D.R

Defines functions opt1D

Documented in opt1D

opt1D <-
    function(nsim=50,nprocessors=1,setpen="L1",cl=NULL,...){
        if (!(identical(setpen,"L1")|identical(setpen,"L2"))) stop("setpen must be L1 or L2")
        clusterIsSet <- "cluster" %in% class(cl)
        if(nprocessors>1 | clusterIsSet){
            if(!clusterIsSet){
                nprocessors <- as.integer(round(nprocessors))
                cl <- makeCluster(nprocessors, type="PSOCK")
            }
            clusterSetRNGStream(cl, iseed=NULL)
            if(identical(setpen,"L1")){
                thisopt <- parLapply(cl,1:nsim,function(n,...){
                    optL1(...)
                },...)
            }else{   ##if(identical(setpen,"L1")){
                thisopt <- parLapply(cl,1:nsim,function(n,...){
                    optL2(...)
                },...)
            }
            if(!clusterIsSet){
                stopCluster(cl)
            }
        }else{  ##if(nprocessors>1){
            if(identical(setpen,"L1")){
                thisopt <- lapply(1:nsim,function(n,...) optL1(...),...)
            }else{
                thisopt <- lapply(1:nsim,function(n,...) optL2(...),...)
            }
        }
        output <- sapply(thisopt,function(x){
            coefs <- coefficients(x$fullfit,"all")
            tmp <- c(x$lambda,x$cvl,coefs)
            names(tmp) <- c(setpen,"cvl",names(coefs))
            return(tmp)
        })
        return(t(output))
    }

Try the pensim package in your browser

Any scripts or data that you put into this service are public.

pensim documentation built on Dec. 9, 2022, 1:10 a.m.