[Rd] Unexpected behavior in factor level ordering

2012-02-25 Thread Paul Johnson
Hello, Everybody:

This may not be a "bug", but for me it is an unexpected outcome. A
factor variable's levels
do not retain their ordering after the levels function is used.  I
supply an example in which
a factor with values "BC" "AD" (in that order) is unintentionally
re-alphabetized by the levels
function.

To me, this is very bad behavior. Would you agree?


# Paul Johnson 2012-02-05

x <- c("AD","BC","AD","BC","AD","BC")
xf <- factor(x, levels=c("BC", "AD"), labels=c("Before Christ","After Christ"))
y <- rnorm(6)

m1 <- lm (y ~ xf )

plot(y ~ xf)

abline (m1)
## Just a little problem the line does not "go through" the box
## plot in the right spot because contrasts(xf) is 0,1 but
## the plot uses xf in 1,2.

xlevels <- levels(xf)
newdf <- data.frame(xf=xlevels)

ypred <- predict(m1, newdata=newdf)

##Watch now: the plot comes out "reversed", AC before BC
plot(ypred ~ newdf$xf)

## Ah. Now I see:

levels(newdf$xf)
## Why doesnt newdf$xf respect the ordering of the levels?




-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Unexpected behavior in factor level ordering

2012-02-25 Thread Uwe Ligges



On 25.02.2012 19:16, Paul Johnson wrote:

Hello, Everybody:

This may not be a "bug", but for me it is an unexpected outcome. A
factor variable's levels
do not retain their ordering after the levels function is used.  I
supply an example in which
a factor with values "BC" "AD" (in that order) is unintentionally
re-alphabetized by the levels
function.

To me, this is very bad behavior. Would you agree?


# Paul Johnson 2012-02-05

x<- c("AD","BC","AD","BC","AD","BC")
xf<- factor(x, levels=c("BC", "AD"), labels=c("Before Christ","After Christ"))
y<- rnorm(6)

m1<- lm (y ~ xf )

plot(y ~ xf)

abline (m1)
## Just a little problem the line does not "go through" the box
## plot in the right spot because contrasts(xf) is 0,1 but
## the plot uses xf in 1,2.

xlevels<- levels(xf)
newdf<- data.frame(xf=xlevels)

ypred<- predict(m1, newdata=newdf)

##Watch now: the plot comes out "reversed", AC before BC
plot(ypred ~ newdf$xf)

## Ah. Now I see:

levels(newdf$xf)
## Why doesnt newdf$xf respect the ordering of the levels?



Because xlevels was a character and you coerced it to a factor by 
calling data.frame(xf=xlevels) on it without telling anything about the 
orderiung, hence it got sorted lexicographically.


Uwe Ligges








__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] improved error message when existing implicit S4 generic is not imported?

2012-02-25 Thread Martin Morgan

pkgA's NAMESPACE has

  importFrom(graphics, plot)
  exportClasses("A")
  exportMethods("plot")

R/foo.R has

  setClass("A")
  setMethod("plot", "A", function(x, y, ...) {})

During R CMD INSTALL pkgA_1.0.tar.gz we are told

** preparing package for lazy loading
Creating a generic function for 'plot' from package 'graphics' in 
package 'pkgA'

** help
No man pages found in package  ‘pkgA’
*** installing help indices
** building package indices
** testing if installed package can be loaded

* DONE (pkgA)

pkgB has in its DESCRIPTION

  Depends; pkgA

with NAMESPACE

  importFrom(graphics, plot)
  exportClasses("B")
  exportMethods("plot")

R/bar.R has

  setClass("B")
  setMethod("plot", "B", function(x, y, ...) {})

During R CMD INSTALL pkgB_1.0.tar.gz we are told

** preparing package for lazy loading
** help

i.e., no implicit generic created (because graphics::plot already has an 
implicit generic created, when the pkgA dependency was attached?). and also


** testing if installed package can be loaded
Error : Functions for exporting methods must have been made generic, 
explicitly or implicitly; not true when loading 'pkgB' for 'plot'

Error: loading failed
Execution halted
ERROR: loading failed

which doesn't seem accurate, rather "'pkgB' does not import S4 implicit 
generic for 'plot' created by 'pkgA'"


> sessionInfo()
R Under development (unstable) (2012-02-25 r58492)
Platform: x86_64-unknown-linux-gnu (64-bit)

Martin
--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel