[6460] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: Bug in predict?

daemon@ATHENA.MIT.EDU (Bill Venables)
Sun Feb 19 18:29:28 1995

To: s-news@utstat.toronto.edu
From: wvenable@attunga.stats.adelaide.edu.au (Bill Venables)
Date: 17 Feb 1995 23:14:13 GMT


>>>>> Luciano Molinari <molinari@stat.math.ethz.ch> writes:

: I believe I have come across a minor bug in predict, as
: documented by the example given below. I could trace the bug back
: to model.matrix and I guess it is in model.frame.default.
: 
: I am working with S-Plus for Windows 3.2.  Does anybody have or
: suggest a fix?

The problem is a known shortcoming rather than a bug in predict,
(and in my view yet another one that should be rectified).

: --------- Example -----------
: names(wink)
: [1] "PlCh" "PlAT" "Gr"
: 
: zzf <- lm(PlAT ~ PlCh * Gr, data = wink)

...

: wink[22,]
:    PlCh PlAT  Gr
: 22   41    8 Pat
: wink[26,]
:    PlCh PlAT   Gr
: 26   41   26 Cntr
: 
: fitted(zzf)[22]
:        22
:  13.18492
: fitted(zzf)[26]
:        26
:  22.69162
: 
: predict(zzf, data.frame(PlCh = 41, Gr = "Cntr"))
:         1
:  22.69162
: predict(zzf,data.frame(PlCh=41,Gr="Pat"))
:         1
:  22.69162

Gr is a factor with two levels, but in the new data frame you
have given it you have supplied only one level, so it has taken
it as a factor with one level only and used information from the
first level of the factor Gr in the data frame wink on both
occasions.

What you need to do is specify how many levels there are in Gr
and in which order they occur:

predict(zzf, data.frame(PlCh=41, Gr=factor("Pat",levels=c("Cntr", "Pat"))


: predict(zzf,data.frame(PlCh=c(41,41),Gr=c("Pat","Cntr")))
:         1        2
:  13.18492 22.69162

Supplying enough information to construct the levels, in the
correct order, leads to the correct result.

This is clearly a big trap, and one that could be averted to some
extent if the newdata data frame had the convention of taking the
levels attributes from any factors it uses from the original data
frame used for fitting, if there is one.  Indeed, where else
should it come from?

Bill
--
_________________________________________________________________
Bill Venables, Department of Statistics,     Tel.: +61 8 303 3026
The University of Adelaide,                  Fax.: +61 8 232 5670
South AUSTRALIA.     5005.  Email: venables@stats.adelaide.edu.au

home help back first fref pref prev next nref lref last post