Skip to content

under certain conditions, model.matrix appears to lack one column (PR#646)

4 messages · Brian Ripley, Rashid Nassar

#
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:
## 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:
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
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
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Sun, 27 Aug 2000 rnassar@duke.edu wrote:

            
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).
#
Dear Professor Ripley,

Thank you very much for your kind explanation.  If I may lamely say
something in my defence, even as I apologize for my error: I mistook the
sentence "the (quoted) name of a function" to mean "optionally quoted"
because of the parentheses surrounding "quoted", and was encouraged by the
fact that R did not complain when I gave the unquoted form and that it did
implement a change (although not the desired one), and (even more lamely)
the fact that none of the examples on the help page show the function
quoted, although I now realize that in the example the function has an
argument, and is not given simply the name of the function. 

Again, thank you for taking the time to reply and point out my mistake.

Rashid Nassar
On Mon, 28 Aug 2000, Prof Brian D Ripley wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 28 Aug 2000, Rashid Nassar wrote:

            
That was not what I was trying to do!   If this had not already been fixed
as a result of another bug fix, it would be a bug we needed to address.