From c68de96289d73e550e6a79a9764d6d6524df4210 Mon Sep 17 00:00:00 2001 From: schlager Date: Mon, 24 Feb 2014 10:49:05 +0100 Subject: [PATCH] some formatting in NNshapeReg --- R/NNshapeReg.r | 51 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/R/NNshapeReg.r b/R/NNshapeReg.r index 44cc5aee..6934f825 100644 --- a/R/NNshapeReg.r +++ b/R/NNshapeReg.r @@ -33,42 +33,39 @@ #' #' #' @export -NNshapeReg <- function(x,y=NULL, n=3, mahalanobis=FALSE,mc.cores = parallel::detectCores()) - { - if (is.null(y)) - y <- x - outdim <- dim(y) - if (length(dim(x)) == 3) - x <- vecx(x) - if (length(dim(y)) == 3) - y <- vecx(y) +NNshapeReg <- function(x,y=NULL, n=3, mahalanobis=FALSE,mc.cores = parallel::detectCores()) { + if (is.null(y)) + y <- x + outdim <- dim(y) + if (length(dim(x)) == 3) + x <- vecx(x) + if (length(dim(y)) == 3) + y <- vecx(y) i <- NULL win <- FALSE - if(.Platform$OS.type == "windows") - win <- TRUE + if(.Platform$OS.type == "windows") + win <- TRUE else - registerDoParallel(cores=mc.cores)### register parallel backend + registerDoParallel(cores=mc.cores)### register parallel backend out <- y - estfun <- function(i) - { + estfun <- function(i) { weighcalc <- proc.weight(x,n,i,mahalanobis=mahalanobis,report=F)$data ws <- diag(weighcalc$weight) tmpres <- apply(t(t(y[weighcalc$nr,])%*%ws),2,sum) return(tmpres) - } + } if (win) - out <- foreach(i=1:dim(x)[1],.combine=rbind) %do% estfun(i) + out <- foreach(i=1:dim(x)[1],.combine=rbind) %do% estfun(i) else - out <- foreach(i=1:dim(x)[1],.combine=rbind) %dopar% estfun(i) - - if (length(outdim) == 3) - { - out1 <- array(NA, dim=outdim) - for (i in 1:outdim[3]) - out1[,,i] <- matrix(out[i,],outdim[1],outdim[2]) - - out <- out1 - } + out <- foreach(i=1:dim(x)[1],.combine=rbind) %dopar% estfun(i) + + if (length(outdim) == 3) { + out1 <- array(NA, dim=outdim) + for (i in 1:outdim[3]) + out1[,,i] <- matrix(out[i,],outdim[1],outdim[2]) + + out <- out1 + } return(out) - } +}