Patrick Lindsey
2002-Mar-14 11:16 UTC
[Rd] Row-binding factor variables looses the ordered attribute.
Hi everyone, I've just notice a problem with ordered factor variables. It appears that row-binding two ordered factors together looses the ordered attribute. The following example happens both in R1.3.1 and R1.4.1 (on RedHat 7.2):> y <- ordered(gl(3,6),labels=1:3) > y[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 Levels: 1 < 2 < 3> data <- data.frame(y=ordered(gl(3,6),labels=1:3)) > data[,1][1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 Levels: 1 < 2 < 3> rbind(data,data)[,1][1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 Levels: 1 2 3 I've also noticed a change of behavior, between R1.3.1 and R1.4.1, in the way some covariate names are constructed. This is really a minor comment but this change in behavior seems unnecessary and complicates the retrieval of coefficients using the covariate's name. For example, fitting the following glm model in R1.3.1 results in> data <- data.frame(y=rnorm(20),cov=gl(4,5)) > glm(y~I(cov!=2),data=data)Call: glm(formula = y ~ I(cov != 2), data = data) Coefficients: (Intercept) I(cov != 2) 0.8009 -0.7547 Degrees of Freedom: 19 Total (i.e. Null); 18 Residual Null Deviance: 16.34 Residual Deviance: 14.21 AIC: 55.92 whereas with R1.4.1 we now get> data <- data.frame(y=rnorm(20),cov=gl(4,5)) > glm(y~I(cov!=2),data=data)Call: glm(formula = y ~ I(cov != 2), data = data) Coefficients: (Intercept) I(cov != 2)TRUE 0.4570 -0.7629 Degrees of Freedom: 19 Total (i.e. Null); 18 Residual Null Deviance: 24.39 Residual Deviance: 22.2 AIC: 64.85 The covariate name "I(cov != 2)" is now (in R1.4.1) appended by the value TRUE. I think that this must come from "model.matrix" which must be transforming a boolean vector into a factor variable (which I would guess is not really necessary as this is just a binary vector). This change of behavior is not really a problem in itself (if it only concerns the behavior of "model.matrix") but quite unexpected. Thanks too all, Patrick. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
ripley@stats.ox.ac.uk
2002-Mar-14 11:38 UTC
[Rd] Row-binding factor variables looses the ordered attribute.
On Thu, 14 Mar 2002, Patrick Lindsey wrote: [...]> The covariate name "I(cov != 2)" is now (in R1.4.1) appended by the value > TRUE. I think that this must come from "model.matrix" which must be > transforming a boolean vector into a factor variable (which I would guess > is not really necessary as this is just a binary vector). > This change of behavior is not really a problem in itself (if it only > concerns the behavior of "model.matrix") but quite unexpected.It is in the NEWS file .... -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._