R/predict_trainPLS.R

#' predict function for trainPLS object
#' 
#' predict function for trainPLS object
#' 
#' @param x trainPLS object
#' @param ... arguments for \code{predict} function
#' 
#' @export
predict.trainPLS <- function(object, newx, ...){
  
  # preprocess newx
  x <- as.matrix(newx)
  if (object$bestmodel_pre == "Norm + Mean-centering") {
    x <- normalize(x)
  } else if (object$bestmodel_pre == "Autoscale") {
    index <- which(colSums(x) == 0)
    if (length(index) == 0) x <- x else x <- x[,-index]
  }
  
  # predict newy
  newy <- predict(object$bestmodel, ncomp = object$bestmodel_ncomp, newdata = x)[,1,1]
  
  return(newy)
  
}
chengvt/cheng documentation built on May 13, 2019, 3:52 p.m.