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