rnassar@duke.edu
2000-Aug-27 20:24 UTC
[Rd] under certain conditions, model.matrix appears to lack one column (PR#646)
Dear R Team, # Summary of the problem: setting contrasts as > contrasts(g) <- contr.treatment or > contrasts(g) <- matrix(c(1,-1,0),ncol=1) (i.e. without quotes around `contr.treatment' or `contr.sum', etc.) and fitting an lm model without an intercept results in a model matrix that lacks one column. (I do ask for forgiveness if this is not a bug but is due to my ignorance.) Many thanks for the extraordinary R. Rashid Nassar # Details: # the data: x <- gl(3,3) y <- c(2,4,3,4,5,7,6,5,8) ## 1. Using the default (treatment) contrasts unchanged gives correct results. ## 2. Setting contrasts using either of the following two forms also gives correct results:> contrasts(x) <- "contr.treatment" > contrasts(C(x,contr.treatment))## 3. However I believe that the following (contr.treatment without quotation marks) results in a problem (also contrasts(x) <- matrix(c(1,-1,0),ncol=1)) when an intercept is not included in the model:> contrasts(x) <- contr.treatment > f4 <- lm(y ~ x - 1) > summary(f4)Coefficients: Estimate Std. Error t value Pr(>|t|) x2 5.333 1.351 3.947 0.00555 x3 6.333 1.351 4.688 0.00224> anova(f4)Analysis of Variance Table Response: y Df Sum Sq Mean Sq F value Pr(>F) x 2 205.667 102.833 18.778 0.001537 Residuals 7 38.333 5.476> model.matrix(f4)x2 x3 1 0 0 2 0 0 3 0 0 4 1 0 5 1 0 6 1 0 7 0 1 8 0 1 9 0 1 --please do not edit the information below-- Version: platform = i586-pc-linux-gnu arch = i586 os = linux-gnu system = i586, linux-gnu status = major = 1 minor = 1.1 year = 2000 month = August day = 15 language = R Search Path: .GlobalEnv, package:MASS, Autoloads, package:base -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
2000-Aug-28 06:28 UTC
[Rd] under certain conditions, model.matrix appears to lack one column (PR#646)
On Sun, 27 Aug 2000 rnassar@duke.edu wrote:> Dear R Team, > > # Summary of the problem: setting contrasts as > > contrasts(g) <- contr.treatment > or > contrasts(g) <- matrix(c(1,-1,0),ncol=1) > (i.e. without quotes around `contr.treatment' or `contr.sum', etc.) > and fitting an lm model without an intercept results in a model matrix > that lacks one column.Right, but first is not allowed by (?contrasts) ctr: either a matrix whose columns give coefficients for contrasts in the levels of `x', or the (quoted) name of a function which computes such matrices. and the second is arguably correct: you chose to use only two columns. However, the behaviour is different in the development version, which does not code x in models such as this, and so does not have the problem. That is not precisely what S does, but seems a sane way of interpreting the contradictory information given (which is that you don't want an intercept and that you only want two columns for the coding matrix for x! That applies to the function case too: there the coding matrix is evaluated before knowing if contrast are required, that is using argument contrasts FALSE). -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._