1
predict
は、引数object
と...
で定義されています。私はpcaaddと呼ばれるPCAのようなモデルを導き、predict.pcaadd
関数を書いた。ここまでは順調ですね。今度は"pcaadd"
のobject
と("hyperSpec"
の)newdata
という特定の種類のオブジェクトで動作する(S4)メソッドを追加したいと思います。オブジェクトと新データクラスの `predict`ディスパッチングのS4メソッド
(generic)genericが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])
}