1

predict用参数object和定义...。我导出了一个类似 PCA 的模型,我称之为 pcaadd 并编写了一个predict.pcaadd函数。到现在为止还挺好。现在我想添加一个 (S4) 方法,该方法适用于object签名和(signature )"pcaadd"中的特定类型的对象 。newdata"hyperSpec"

(如何)如果泛型是用 object...仅定义的,我可以这样做吗?

这是我目前所做的:检查类newdata并进行适当的处​​理,但这似乎不是一个干净的解决方案:

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])
}
4

1 回答 1

1

最后,我在函数中使用as.matrix (object)了该predict函数,该函数负责为不同类型的newdata(因为它可以为任何类型定义newdata)进行适当的调度。

于 2012-12-05T08:46:47.130 回答