rrms

rms package Glm: how to add indicators in the formula


I am trying to build a glm model using Glm() from rms package in R. But I don't know the correct expression in Glm formula for the indicators.

Allow me use iris data as an example, In the base R glm function, my code works and it is like below:

model1 = glm(Sepal.Length~Sepal.Width + Petal.Length + ifelse(Sepal.Width==3,1,0),data=iris)

But if I use the same formula in Glm, it will return

 model2 = Glm(Sepal.Length~Sepal.Width + Petal.Length + ifelse(Sepal.Width==3,1,0),data=iris)

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

Or if I use

model3 = Glm(Sepal.Length~Sepal.Width + Petal.Length + asis(Sepal.Width==3),data=iris)

Error in if (asc[i] == 8) next : missing value where TRUE/FALSE needed

I just don't know the correct way to define this transformation. Also I know I can solve this by putting this indicator as a new column in the data, but then I could not use rms's Predict() function to generate the correct plot.


Solution

  • As suggested in the comments, this seems to work fine (I don't really know what I'm doing with datadist, but fumbling through ...)

     iris$sepal3 <- as.numeric(iris$Sepal.Width==3)
     library(rms)
     ddist <- with(iris,datadist(Sepal.Width, Petal.Length, sepal3))
     options(datadist="ddist")
     model1 <- Glm(Sepal.Length~Sepal.Width + Petal.Length + sepal3,
                   data=iris)
     plot(Predict(model1))
    

    (I do get a warning message about Calling 'structure(NULL, *)' is deprecated, but I think that's because I'm using a development version of R with rms version 5.1.0 ...