Skip to content
Prev 131913 / 398506 Next

lm/model.matrix confusion (? bug)

Hi Gavin, Berwin,

Thanks for your detailed replies.  I'll make a general reply, if you don't
mind.

To reiterate, my main point is that if model.matrix() can be used in this
way, then lm() shouldn't add an intercept.
And the documentation for lm() indicates. albeit indirectly, that
model.matrix can be used in this way.  It calls for a formula, or something
that can be coerced to one.  And the following meets that criterion:
as.formula(Gas ~ model.matrix (~ Insul/Temp-1, data=whiteside)), and this
specifies no intercept.

On the question of why I want to mess about in such a labarynthine way. 
Well my email was largely expository.  With a straight call to lm(), I
wouldn't bother with model.matrix.

So, it really was about getting at lm coefficients inside a function (when
you have to get the terms &c. from somewhere else), and trying to understand
properly how things work, and why they work the way they do, and even if
they should work the way they do.

For instance:---

    if (ols) {
        obj <- x[[1]]
        mt <- terms(obj)
        mf <- model.frame(obj)
        y <- model.response(mf)
        X <- model.matrix(mt, mf, contrasts = obj$contrasts)
        if (attr(mt, "intercept") == 1)                    ## This is my
hack to overcome the double-intercept problem
            { olscf <- summary(lm(y ~ X))$coefficients }
        else {
            olscf <- summary(lm(y ~ X - 1))$coefficients
        }
        rownames(olscf) <- rownames(coef(obj))

Thanks again for your input.

Regards,
Mark.
Berwin A Turlach wrote: