Petr Pikal
2003-Jul-16 15:13 UTC
[R] bwplot does something weird with Hmisc library attached
Dear all
I would like to ask you about possible bug in using bwplot (from lattice)
together
with Hmisc library attached. I found it in my actual data, but here is a toy
example. It appears only when some levels are missing.
library(lattice)
library(Hmisc)
# preparing data
x1<-rnorm(10,5,1)
x2<-rnorm(10,5,5)
x3<-rnorm(10,1,1)
x4<-rnorm(10,1,5)
x<-c(x1,x2,x3,x4)
x<-c(x,x+5)
f2<-as.factor(rep(c("a","b","a","b"),c(20,20,20,20)))
f3<-as.factor(rep(rep(c("c","d"),each=10),4))
f1<-as.factor(rep(c(1,2),c(40,40)))
data<-data.frame(x,f1,f2,f3)
rm(x,f1,f2,f3)
attach(data)
# 1st plot
bwplot(f1~x|f2)
# 2 is bigger than 1 **OK**
# 2nd plot
bwplot(f1~x|f2*f3)
# again 2 is bigger than 1 **OK**
# removing some levels
detach("data")
data<-data[-c(1:10,21:30),]
attach(data)
# 3rd plot
bwplot(f1~x|f2)
# 2 is bigger than 1 **OK**
# 4th plot
bwplot(f1~x|f2*f3)
### OOOPS 1 is bigger than 2 ***FALSE***
after detaching Hmisc
detach("package:Hmisc")
#5th plot
bwplot(f1~x|f2*f3)
# again 2 is bigger than 1 **OK**
The levels for factor f1 are in the OOOPS case reversed, maybe it has something
to do with factor redefinition as stated when attaching Hmisc library.
Is it a bug or am I doing something wrong?
R 1.7.1 WNT
Package: Hmisc
Version: 1.6-1
Date: 2003-06-21
Package: lattice
Version: 0.7-14
Date: 2003/06/07
Thank you for any response.
Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz
Martin Maechler
2003-Jul-17 08:24 UTC
[R] bwplot does something weird with Hmisc library attached
>>>>> "Petr" == Petr Pikal <petr.pikal at precheza.cz> >>>>> on Wed, 16 Jul 2003 17:13:57 +0200 writes:Petr> Dear all I would like to ask you about possible bug in Petr> using bwplot (from lattice) together with Hmisc Petr> library attached. I found it in my actual data, but Petr> here is a toy example. It appears only when some Petr> levels are missing. Petr> library(lattice) Petr> library(Hmisc) .............. Petr> The levels for factor f1 are in the OOOPS case Petr> reversed, maybe it has something to do with factor Petr> redefinition as stated when attaching Hmisc library. exactly. Because of this: >> > library(Hmisc) >> Hmisc library by Frank E Harrell Jr >> >> Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview') >> to see overall documentation. >> >> Hmisc redefines [.factor to drop unused levels of factor variables >> when subscripting. To prevent this behaviour, issue the command >> options(drop.unused.levels=F). >> >> Attaching package 'Hmisc': >> >> >> The following object(s) are masked from package:survival : >> >> as.data.frame.Surv untangle.specials >> >> >> The following object(s) are masked from package:stepfun : >> >> ecdf >> >> >> The following object(s) are masked from package:base : >> >> [.factor %in% interaction [.terms where only the last line is the real crucial problem, (and "%in%" is I think identical to R base's definition) using Hmisc unfortunately can break anything that internally uses "[.factor"(), interaction(), or "[.terms"() where the first is your problem. If you look closer at the text above, you see >>> To prevent this behaviour, issue the command >> options(drop.unused.levels=F). and that's your solution: Issue options(drop.unused.levels = FALSE) ## "FALSE", not "F", since "F" can be anything: it's valid variable name which initially is set to FALSE! I would set this option everytime when using Hmisc, because otherwise `anything' else can be broken by the changed factor subsetting behavior. --- Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <><