rms package Glm:如何在公式中添加指标

rms package Glm: how to add indicators in the formula

我正在尝试使用 R 中 rms 包中的 Glm() 构建一个 glm 模型。 但是我不知道指标在 Glm 公式中的正确表达方式。

请允许我以虹膜数据为例, 在基础 R glm 函数中,我的代码有效,如下所示:

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

但如果我在 Glm 中使用相同的公式,它将 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

或者如果我使用

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

我只是不知道定义此转换的正确方法。我也知道我可以通过将这个指标作为数据中的一个新列来解决这个问题,但是我无法使用 rmsPredict() 函数来生成正确的图。

正如评论中所建议的那样,这似乎工作正常(我真的不知道我在用 datadist 做什么,但摸索着......)

 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))

(我确实收到了关于 Calling 'structure(NULL, *)' is deprecated 的警告消息,但我认为那是因为我使用的是 R 的开发版本和 rms 版本 5.1.0 ...