[Rd] S4 objects in formulas (really, model frames)

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 24 07:15:38 CEST 2012


The help for model.frame says

      Only variables whose type is raw, logical, integer, real, complex
      or character can be included in a model frame: this includes
      classed variables such as factors (whose underlying type is
      integer), but excludes lists.

Some S4 objects are of one of those types, but some are not.  Some 
matrices are, some are not.  Objects of class "Surv" are.

On 23/07/2012 21:33, David L Lorenz wrote:
> Hi,
>    I have very carefully developed several S4 classes that describe
> censored water-quality data. I have routines for them that will support
> their use in data.frames and so forth. I have run into a problem when I
> try to use the S4 class as the response variable in a formula and try to
> extract the model frame. I get an error like:
>
> Error in model.frame.default(as.lcens(Y) ~ X) : object is not a matrix
>
>    In this case, as.lcens works much like the Surv function in the survival
> package except that the object is an S4 class and not a matrix of class
> Surv. I would have expected that the model.frame function would have been
> able to manipulate any kind of object that can be subsetted and put into a
> data.frame. But that appears not to be the case. I'm using R 2.14.1 if
> that matters.
>    I can supply the routines for the lcens data if needed.
>    Am I looking at needing to write a wrapper to convert all of my S4
> classes into matrices and then extract the necessary data in the matrices
> according to rules for the particular kind of S4 class? Or, am I missing a
> key piece on how model.frame works?
>    Thanks.
> Dave
>
> 	[[alternative HTML version deleted]]

The posting guide asked you not to do that.  And to do your own homework.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list