Pergunta

predict is defined with arguments object and .... I derived a PCA-like model which I call pcaadd and wrote a predict.pcaadd function. So far so good. Now I'd like to add an (S4) method that works for object with signature "pcaadd" and a particular kind of object in newdata (signature "hyperSpec").

(How) can I do this if the generic is defined with object and ... only?

Here's what I do for the moment: check the class of newdata and do the appropriate processing, but it seems not quite a clean solution:

predict.pcaadd <- function (object, newdata, ...){
  ## deal with class "hyperSpec"
  if (is (newdata, "hyperSpec")){
    validObject (newdata)

    ## extract the matrix that is used for the prediction
    newdata <- newdata [[]]
  }

  ## default part of prediction
  tmp <- predict (object$pca, newdata)
  tcrossprod (tmp [, - object$refcomps], object$pca$rotation [, -object$refcomps])
}
Foi útil?

Solução

In the end, I used as.matrix (object) in the predict function which takes care of proper dispatch for different types of newdata (as it can be defined for any type of newdata).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top