In the Hmisc package there is a new data class 'mChoice' for multiple choice variables. There are format and as.numeric methods (the latter creates a matrix of dummy variables). mChoice variables are not allowed by model.frame. Is there a way to specify a conversion function that model.frame will use automatically? I would use as.factor here. model.frame does not seem to use as.data.frame.foo for individual variables. Thanks Frank -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
You could try to follow the code in the dyn package. It intercepts model.frame calls involving time series objects so that it can align lagged objects: e.g. z <- ts(seq(10)^2) library(dyn) dyn$lm(z ~ lag(z, -1)) It transforms the last line above to: dyn(lm(dyn(z ~ lag(z, -1))) and the inner dyn then produces a formula with class c("dyn", "model.frame") so that model.frame.dyn can intercept the call while the outer dyn adds "dyn" to the class of the result so that anova.dyn, predict.dyn, etc. can be used to intercept the result. Thus for any lm-like function you just preface it with dyn$ as shown and you get automatically alignment of time series or in your case you would interception of the mChoice variables. On 4/24/07, Frank E Harrell Jr <f.harrell at vanderbilt.edu> wrote:> In the Hmisc package there is a new data class 'mChoice' for multiple > choice variables. There are format and as.numeric methods (the latter > creates a matrix of dummy variables). mChoice variables are not allowed > by model.frame. Is there a way to specify a conversion function that > model.frame will use automatically? I would use as.factor here. > model.frame does not seem to use as.data.frame.foo for individual variables. > > Thanks > Frank > -- > Frank E Harrell Jr Professor and Chair School of Medicine > Department of Biostatistics Vanderbilt University > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Prof Brian Ripley
2007-Apr-25 12:49 UTC
[Rd] [R] Coercing data types for use in model.frame
Moved to R-devel .... What is the 'data class'? In particular what is its underlying type? And where in model.frame[.default] are you trying to use it (in the formula, data, in ..., etc). This is an example of where some reproducible code and the error messages would be very helpful indeed. Brian On Tue, 24 Apr 2007, Frank E Harrell Jr wrote:> In the Hmisc package there is a new data class 'mChoice' for multiple > choice variables. There are format and as.numeric methods (the latter > creates a matrix of dummy variables). mChoice variables are not allowed > by model.frame. Is there a way to specify a conversion function that > model.frame will use automatically? I would use as.factor here. > model.frame does not seem to use as.data.frame.foo for individual variables. > > Thanks > Frank >-- 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