# Error message from grm() in ltm: subscript out of bounds

I am analyzing a survey data set. One of the items in the survey is:

"I plan to work hard in my statistics course"
1=Strongly Disagree
2=Disagree
3=Neither Disagree or Agree
4=Agree
5=Strongly Agree


No one selected option 1 of Strongly Disagree. This is to be expected but the function grm() in package ltm generates an error: "Subscript out of bounds".

How do I get around this problem and still keeping the responses as they are or without removing that item?

• Could you provide a reproducible example? – Sven Hohenstein Feb 4 '13 at 12:26

As the help page of ?grm points out

grm() can also handle binary items, which should be coded as ‘1, 2’ instead of ‘0, 1’.

This means that items have to include 1 as a value. So, therefore you have to recode the variable in such a way that it starts at 1. So the easiest thing would be to subtract 1 from each value for the variable in your example!

• "subtract -1" would be the same as "add 1". Did you really mean that? It would seem to make more sense to subtract 1 – Glen_b Oct 13 '13 at 22:17
• You were right I changed it – Huub Hoofs Oct 30 '13 at 15:13

[This question might be better off on SO since it's mainly about how to do something in R.]

Here's an example that illustrates the problem and two possible solutions:

## Generate data and run grm()
dat <- data.frame(w=as.factor(sample(1:5, 100, replace=TRUE)),
x=as.factor(sample(1:5, 100, replace=TRUE)),
y=as.factor(sample(1:5, 100, replace=TRUE)),
z=as.factor(sample(1:5, 100, replace=TRUE)))
grm(dat)
#
# Call:
# grm(data = dat)
#
# Coefficients:
#    Extrmt1  Extrmt2  Extrmt3   Extrmt4  Dscrmn
# w   -0.802   -0.162    0.388     0.802   4.136
# x  611.563  279.736  -84.675  -441.193  -0.002
# y   -5.898   -3.247    0.150     3.287   0.328
# z   15.681    3.388   -6.762   -18.028  -0.106
#
# Log.Lik: -631.277


So that works. Now, remove the "1" value, rerun the model and see what happens:

with(dat, table(x))
# x
#  1  2  3  4  5
# 19 15 21 19 26
# dat$x[dat$x == "1"] <- "2"
grm(dat)
# Error in log.pr[xj, ] : subscript out of bounds
with(dat, table(x))
# x
#  1  2  3  4  5
#  0 34 21 19 26


So one solution as Huub Hoofs suggested is to subtract 1 from the variable(s) that have counts of 0:

dat <- within(dat, x <- as.numeric(x))
dat <- within(dat, x <- (x - 1))
with(dat, table(x))
# x
#  1  2  3  4
# 34 21 19 26
grm(dat)
# grm(dat)
#
# Call:
#   grm(data = dat)
#
# Coefficients:
# w
# Extrmt1  Extrmt2  Extrmt3  Extrmt4   Dscrmn
# -0.805   -0.163    0.390    0.806    4.039
#
# x
# Extrmt1  Extrmt2  Extrmt3   Dscrmn
# -99.579   30.061  156.913    0.007
#
# y
# Extrmt1  Extrmt2  Extrmt3  Extrmt4   Dscrmn
# -5.876   -3.236    0.149    3.273    0.330
#
# z
# Extrmt1  Extrmt2  Extrmt3  Extrmt4   Dscrmn
# 15.575    3.366   -6.715  -17.903   -0.106
#
#
# Log.Lik: -607.946


You could also use droplevels():

dat <- within(dat, x <- droplevels(x))

• That will work unless you actually need those levels. What if you are doing subgroup or DIF analysis and one group has 0s in a level and the other doesn't? – robin.datadrivers Mar 27 '15 at 18:05