harrelfe / rms

Regression Modeling Strategies
https://hbiostat.org/R/rms
Other
172 stars 48 forks source link

Error with nomogram: Error in reformulate(attr(termobj, "term.labels")[-dropx], response = if (keep.response) termobj[[2L]], : 'termlabels' must be a character vector of length at least one #144

Closed FriedrichenGit closed 4 months ago

FriedrichenGit commented 4 months ago

Dear Prof. Harrell,

I'm trying to create a nomogram from a logistic regression model fitted with lrm, but i encounter the error message: Error in reformulate(attr(termobj, "term.labels")[-dropx], response = if (keep.response) termobj[[2L]], : 'termlabels' must be a character vector of length at least one

Here is a reproducible example:

library(rms)

set.seed(123)
n <- 1000
age <- rnorm(n, 50, 10)
blood_pressure <- rnorm(n, 120, 15)
cholesterol <- rnorm(n, 200, 25)

# Create a data frame with data
data <- data.frame(age, blood_pressure, cholesterol,
                   outcome = ifelse(cholesterol < 180, 
                                    sample(c(0,1), prob = c(0.6, 0.4), size = length(cholesterol), replace = TRUE),
                                    sample(c(0,1), prob = c(0.4, 0.6), size = length(cholesterol), replace = TRUE))) 
head(data)

# Adjust logistic regression model
dd <- datadist(data)
options(datadist = 'dd')

fit <- lrm(outcome ~ age + blood_pressure + cholesterol, data = data)
fit

# Generate the nomogram
nom <- nomogram(fit)
plot(nom)

And my session info:

  devtools::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.2 (2023-10-31 ucrt)
 os       Windows 11 x64 (build 22631)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  Spanish_Spain.utf8
 ctype    Spanish_Spain.utf8
 tz       Europe/Madrid
 date     2024-06-05
 rstudio  2023.12.1+402 Ocean Storm (desktop)
 pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package       * version date (UTC) lib source
 Hmisc         * 5.1-1   2023-09-12 [1] CRAN (R 4.3.2)
 rms           * 6.8-1   2024-05-27 [1] CRAN (R 4.3.3)

I've tried several things and even another model like the ols, but the error stays the same, so I thought it may be something inside the function. I'm quite new to this package so perhaps it's just a matter of a misunderstanding in my code, but anyways thanks in advance. And congratulations for this great package.

FriedrichenGit commented 4 months ago

It seems that working with R version 4.4.0 or higher does solve this problem.