Re: [R] Caution on the use of model.matrix.

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 03 Jun 2005 - 01:07:07 EST

On Thu, 2 Jun 2005, Dimitris Rizopoulos wrote:

> I think you could bit that using (inside your function) something like this
>
> old.o <- options(na.action = na.fail)
> # old.o <- options(na.action = na.pass)
> on.exit(old.o)

You can. But the real problem is more likely that Rolf has not passed model.matrix a model frame, so it calls model.frame() internally. The help page is a bit confused in that it says

     data: a data frame created with 'model.frame'.

which the default for the argument is not. So a better solution would then be to call model.frame and pass a model frame to model.matrix.

delete.response() might also be useful.

The suggested warning only applies if `data' is not supplied.

> ----- Original Message ----- From: "Rolf Turner" <rolf@math.unb.ca>
> To: <r-help@stat.math.ethz.ch>
> Sent: Thursday, June 02, 2005 4:14 PM
> Subject: [R] Caution on the use of model.matrix.
>
>
>> I have just been bitten by a quirk in the behaviour of model.matrix.
>> I used model.matrix inside a function, and passed to it a formula
>> that was built elsewhere.
>>
>> The formula was of the form ``y ~ x + w + z''. Now, model.matrix
>> cheerfully accepts formulae of this form, although it only
>> ***needs*** the right hand side, i.e. ``~ x + w + z'' --- the ``y''
>> can be dropped (but in general needn't be).
>>
>> The quirk by which I was bitten was that if the y column of the data
>> frame being used contains missing values, then the corresponding rows
>> are dropped (silently) and the resulting design matrix has rows
>> corresponding only to the non-missing values of y. This was not the
>> desired behaviour in my application.
>>
>> Might I respectfully suggest to R Core that a WARNING be added to the
>> help for model.matrix to the effect that
>>
>> model.matrix(y~x + w + z,XXX)
>> and
>> model.matrix(~x + w + z,XXX)
>>
>> give DIFFERENT results if the column ``y'' of the data frame XXX
>> contains missing values?
>>
>> cheers,
>>
>> Rolf Turner
>> rolf@math.unb.ca
>>
>> ______________________________________________
>> R-help@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
>>
>
> ______________________________________________
> R-help@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
>

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
R-help@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
Received on Fri Jun 03 01:12:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:21 EST