1
library(rms)
x<-c("yes","no","yes","no","yes")
y<-c(-340,-310,-289,-189,-300)
z<-c(1,0,1,0,1)
data<-data.frame(x,y,z)
A.B<-data$x
C.D<-data$y
ef<-data$z
sign<-data.frame(A.B,C.D,ef)

names(sign)<-c("A B","C D","ef")
model<-lrm(ef~.,data=sign)

Error in X[, mmcolnames, drop = FALSE] : subscript out of bounds

I replaced the "." with space in the column name. If I did not rename the column, it would run without Error.

What's wrong? Thanks.

zier
  • 11
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. List any non-base R packages you are using. I'm guessing it's because of the spaces in the column names which are usually frowned upon. Are they absolutely necessary when fitting the model? It might be better to clean things up afterward. – MrFlick Jan 05 '21 at 02:23
  • I can't reproduce this with `library(rms); set.seed(101); dd <- data.frame(ef=sample(0:1,size=20,replace=TRUE),A.B=rnorm(20),C.D=rnorm(20)); m1 <- lrm(ef~., data=dd); names(dd) <- c("A B","C D","ef"); m2 <- lrm(ef~., data=dd)`. We really do need a [mre] ... – Ben Bolker Jan 05 '21 at 02:36
  • I have add a minimal reproducible example. Thank you. – zier Jan 05 '21 at 03:03

1 Answers1

1

This error

Error in X[, mmcolnames, drop = FALSE] : subscript out of bounds

means that you are selecting something that is out of a range. This range can be the number of rows, columns, or both. Unfortunately, you also get this error when you are not naming your rows/columns properly. For example, in R you should never include spaces in your column or row names - just as variable names ! - because some functions interprete them literally. This is exactly what happens in your case. This line

model <- lrm(ef ~ ., data=sign)

attemps to select the columns A B and C D, observe the missing " ! Of course, this causes problems because spaces are not allowed as a separater for names. So change your script to

library(rms)

x    <- c("yes", "no", "yes", "no", "yes")
y    <- c(-340, -310, -289, -189, -300)
z    <- c(1, 0, 1, 0, 1)
data <- data.frame(x, y, z)

A.B  <- data$x
C.D  <- data$y
ef   < -data$z
sign <- data.frame(A.B, C.D, ef)

# This does the trick!
names(sign) <- c("AB","CD","ef")

model <- lrm(ef ~ .,
             data = sign)

and you will be fine.

The rational above also explains your observation here

I replaced the "." with space in the column name. If I did not rename the column, it would run without Error.

because if you are not (!) renaming your column names with names(sign) <- c("A B","C D","ef") you column names would be

> sign <- data.frame(A.B, C.D, ef)
> names(sign)
[1] "A.B" "C.D" "ef"

which works (see explanation above). So either use this

names(sign) <- c("A.B","C.D","ef")

or this

names(sign) <- c("A_B","C_D","ef")

Long story shot, always use as a separater either . or _, but never space for names.

HTH

MacOS
  • 1,149
  • 1
  • 7
  • 14
  • If you have messy column names, you may find the package [`janitor`](https://github.com/sfirke/janitor) helpful. For example, `janitor::clean_names(sign)`. – vikjam Jan 05 '21 at 18:50