[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
## Here's your problem:
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))