Pergunta

I am attempting to use custom class builder setClass() to return results from a train function (caret package).

setClass(Class="TrainResults",
         representation(
                 successrate="numeric",
                 plsFit="train"
         )
)

This is how I create TrainResults in my function:

    return(new("Trainresults",
               successrate=successrate,
               plsFit=plsFit))

"successrate" works fine as it is of numeric type, but plsFit (of type train {caret}) complains that:

Error in validObject(.Object) : 
   invalid class “Trainresults” object: undefined class for slot "plsFit" ("train")  

Any idea how to get it to pass the object of type train properly? Thanks!

Foi útil?

Solução

I suspect the return value of caret::train is not an S4 object, rather an S3 object. Use setOldClass("train"), which should then register the train class for use with S4 slots. This works:

setOldClass("train")
trn <- train(data.frame(x=1:3, y=1:3), 1:3)
isS4(trn)
# [1] FALSE
new("TrainResults", successrate=1, plsFit=trn)
# An object of class "TrainResults"
# ... omitted a bunch of output

The basic data types (e.g. numeric, etc.) are all already pre-registered as S4 classes so you don't need to do that for those to work as S4 slots.

Note you have a typo in your code as well (lower case R in Train*r*esulsts).

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