0

In the code below, models 1 & 2 work just fine and produce the same result. Model 4 works fine as well.

    require(rms)

    mtcars$ind <- I(mtcars$gear==4)

    dd <- datadist(mtcars)
    options(datadist = "dd")

    ols(as.formula(mpg ~ wt + cyl + gear + cyl:gear), data=mtcars)    #1
    ols(as.formula(mpg ~ wt + cyl + gear + cyl * gear), data=mtcars)  #2

    ols(as.formula(mpg ~ wt + cyl + gear + cyl:ind), data=mtcars)     #3
    ols(as.formula(mpg ~ wt + cyl + gear + cyl * ind), data=mtcars)   #4

Model 3 gives the following error

    Error in if (!length(fname) || !any(fname == zname)) { :
      missing value where TRUE/FALSE needed

The output of traceback() is

    2: Design(eval.parent(m))
    1: ols(as.formula(mpg ~ wt + cyl + gear + cyl:ind), data = mtcars)

I tried to set debug() on ols and rms:::Design and didn't get very far!! I ran into this issue when I was using rms::lrm() and realized that it was happening in ols as well.

Why does #4 work while #3 does not? They have the same terms in the formula where one uses :, the other uses *. Thx.

ironv
  • 978
  • 10
  • 25
  • 1
    Why do you wrap your formula in `as.formula`? That shouldn't be necessary. Furthermore, you should provide a [reproducible example](http://stackoverflow.com/a/5963610/1412059) to make the question easier to answer. However, your third model contains the interaction `cyl:ind`, but doesn't contain `ind` as a main effect. Something like that is a good idea only under very specific circumstances, but not in general. It might well (this is speculation) be that `ols` assumes that the effects in an interaction will always be present as main effects. – Roland Apr 14 '14 at 07:50
  • Thanks @roland. While I do not have code for generating the instance here, mtcars is part of [datasets](https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html). Hence the code above can be run as is. – ironv Apr 14 '14 at 16:12

1 Answers1

0

Thanks @roland. When you run #4, it seems to add the other main effects. The following produce the exact same output as #4:

    ols(mpg ~ wt + cyl + gear + ind + cyl:ind,   data=mtcars) #3b 
    ols(mpg ~ wt       + gear       + cyl * ind, data=mtcars) #4b
ironv
  • 978
  • 10
  • 25