harrelfe / rms

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

lrm and Rcmdr conflict #137

Closed MilosZarkovic closed 10 months ago

MilosZarkovic commented 10 months ago

I found a problem of incompatibility between rms and Rcmdr. When I use rms::lrm without Rcmdr everything is fine. However, when I loaded Rcmdr I got the following error: Error in X[, mmcolnames, drop = FALSE] : subscript out of bounds. I use R 4.3.2 and R Studio2023.09.1 Build 494. Reproducible example follows:

library(rms) n <- 1000 # define sample size age <- rnorm(n, 50, 10) blood.pressure <- rnorm(n, 120, 15) cholesterol <- rnorm(n, 200, 25) sex <- factor(sample(c('female','male'), n,TRUE)) diseased <- factor(sample(c('Healty','Sick'), n,TRUE))

f <- lrm(diseased ~ age + sex + cholesterol) ##this works f

library(Rcmdr, warn.conflicts=TRUE, verbose = TRUE)

f <- lrm(diseased ~ age + sex + cholesterol) ##This does not work f

Thank you

Miloš Žarković

couthcommander commented 10 months ago

Hi Miloš - I was able to take a look at this. The "Rcmdr" package changes some R options which eventually leads to the error (by renaming columns in model.matrix.default).

Under base R:

> getOption("contrasts")
        unordered           ordered 
"contr.treatment"      "contr.poly" 

With "Rcmdr" attached:

> getOption("contrasts")
[1] "contr.Treatment" "contr.poly"     

I don't use "Rcmdr" and have no idea why it's making this change (see https://github.com/cran/Rcmdr/blob/3596637878b47a86051644e54539d21d2af7c573/R/file-menu.R#L348). But if you restore the option, lrm will work as desired.

Proposed solution:

(c_opt <- getOption("contrasts"))
library(Rcmdr)
options(contrasts = c_opt)
MilosZarkovic commented 10 months ago

Thank you very much. Everything is now functioning correctly. Regards, Miloš