From rmh at surfer.sbm.temple.edu  Tue Dec  2 07:22:12 2003
From: rmh at surfer.sbm.temple.edu (rmh@surfer.sbm.temple.edu)
Date: Tue Dec  2 07:20:48 2003
Subject: [Rd] xtabs or model.frame (PR#5461)
Message-ID: <20031202062212.E094FEFB1@slim.kubism.ku.dk>

# Your mailer is set to "none" (default on Windows),
# hence we cannot send the bug report directly from R.
# Please copy the bug report (after finishing it) to
# your favorite email program and send it to
#
#       r-bugs@r-project.org
#
######################################################

Here is the bug report I got from Professor Hsuan, and my exploration
of it.  The cause of the problem is that a character vector is not
coerced to factor by xtabs or model.frame.

Is it a bug?  I think yes.  Character vectors are so frequently
coerced to factor that it is reasonable to assume that it will happen
here.  The documentation is ambiguous as ?xtabs says
 formula: a formula object with the cross-classifying variables,
          separated by `+', on the right hand side.  Interactions are
          not allowed.
It says "variables", not "factors", implying to us that the coercion to
factor will take place.

Rich

######################
Date: Mon, 1 Dec 2003 21:53:06 -0500
From: "Francis Hsuan" <francis.hsuan@temple.edu> Add To Address Book 
To: <rmh@temple.edu> 
Cc: "Francis Hsuan" <francish@temple.edu> 

Rich, I got some strange output from R.  Does this indicate
a bug in the xtabs() function?  Thanks, Francis

> rm(list=ls(all=TRUE))
> Freq <- c(9, 2, 5, 4, 16, 26, 10, 28)
> H <- rep(c("yes", " no"), each=4)
> S <- rep(rep(c("yes", " no"), each=2), 2)
> A <- rep(c("yes", " no"), 4)
> xtabs(Freq~H+S+A)
Error in model.frame(formula, rownames, variables, varnames,
extras, extranames,  :
        invalid variable type
> Data3 <- data.frame(H, S, A, Freq)
> xtabs(Freq~H+S+A, Data3)
, , A =  no

     S
H      no yes
   no 28  26
  yes  4   2

, , A = yes

     S
H      no yes
   no 10  16
  yes  5   9
######################

The bug is generated in  model.frame.default  by the line
    data <- .Internal(model.frame(formula, rownames, variables, 
        varnames, extras, extranames, subset, na.action))

####
## Here is one repair
> H <- factor(H)
> S <- factor(S)
> A <- factor(A)
> xtabs(Freq~H+S+A)
, , A =  no

     S
H      no yes
   no 28  26 
  yes  4   2 

, , A = yes

     S
H      no yes
   no 10  16 
  yes  5   9 

> 
## Here is another way to generate the error
> class(Data3$H)
[1] "factor"
> Data3$H <- H
> class(Data3$H)
[1] "character"
> xtabs(Freq~H+S+A, Data3)
Error in model.frame(formula, rownames, variables, varnames, extras, extranames,  : 
	invalid variable type
> 
####

--please do not edit the information below--

Version:
 platform = i386-pc-mingw32
 arch = i386
 os = mingw32
 system = i386, mingw32
 status = 
 major = 1
 minor = 7.1
 year = 2003
 month = 06
 day = 16
 language = R

Windows XP Home Edition (build 2600) Service Pack 1.0

Search Path:
 .GlobalEnv, package:methods, package:ctest, package:mva, package:modreg, package:nls, package:ts, file:c:/HOME/rmh/hh/splus.library/.RData, package:grid, package:lattice, Autoloads, package:base


[Rd] xtabs or model.frame (PR#5461)
rmh@surfer.sbm.temple.edu