Search code examples
rmodel

Why am I getting the "replacement has length zero" error for rol model?


I want to run a Rank-ordered Logit model (rol) for which I have created the following dataframe.

Wild<-c(1,1,2,2,3,3)
Input<-c(2,3,1,3,1,2)
Sale<-c(3,2,3,2,1,1)
HH<-c(6,5,4,3,2,1)
data<-data.frame(Wild,Input,Sale)

However, when I run this model

summary(rol(data,HH))

I get the following error:

Error in pr[i] <- pr[i] * exp(util_combine[i, oset[i, j]])/deno : replacement has length zero

When I run the same model with a slightly different dataset, I get the results.

X1<-c(1,1,2,2,3,3)
X2<-c(2,3,1,3,1,2)
X3<-c(3,2,3,1,2,1)
X4<-c(6,5,4,3,2,1)
test<-data.frame(X1,X2,X3)
summary(rol(test,X4))

Coefficients:
             Estimate Std. Error
Beta0item1 -6.9393820  4.0165882
Beta1item0 -1.6710604  2.0818818
Beta1item1  2.0350267  1.1053167
Beta1item2  0.4255481  0.5267721

Why am I getting the error in the first case?


Solution

  • The rol function from the pmr package:

    The Rank-ordered Logit (ROL) Models for ranking data. ROL models are extensions of the Luce models by incorporating covariates.

    The error suggests suggests that the previous value is the same as current value, which is the case in your data in row 5 from Input to Sale 1 -> 1. So if you change that from to 2 -> 1 for example it works. Here reproducible example:

    Wild<-c(1,1,2,2,3,3)
    Input<-c(2,3,1,3,1,2)
    Sale<-c(3,2,3,2,1,1)
    HH<-c(6,5,4,3,2,1)
    data<-data.frame(Wild,Input,Sale)
    data
    #>   Wild Input Sale
    #> 1    1     2    3
    #> 2    1     3    2
    #> 3    2     1    3
    #> 4    2     3    2
    #> 5    3     1    1
    #> 6    3     2    1
    Wild<-c(1,1,2,2,3,3)
    Input<-c(2,3,1,3,1,2)
    Sale<-c(3,2,3,1,2,1)
    HH<-c(6,5,4,3,2,1)
    data2<-data.frame(Wild,Input,Sale)
    data2
    #>   Wild Input Sale
    #> 1    1     2    3
    #> 2    1     3    2
    #> 3    2     1    3
    #> 4    2     3    1
    #> 5    3     1    2
    #> 6    3     2    1
    library(pmr)
    #> Warning: package 'pmr' was built under R version 4.1.2
    #> Loading required package: stats4
    summary(rol(data, HH))
    #> Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': replacement has length zero
    summary(rol(data2, HH))
    #> Maximum Likelihood Estimation of the Rank-ordered Logit Model
    #> Maximum likelihood estimation
    #> 
    #> Call:
    #> `<UNDEFINED>`
    #> 
    #> Coefficients:
    #>              Estimate Std. Error
    #> Beta0item1 -6.9393820  4.0165911
    #> Beta1item0 -1.6710604  2.0818838
    #> Beta1item1  2.0350267  1.1053174
    #> Beta1item2  0.4255481  0.5267725
    #> 
    #> -2 log L: 13.12662
    

    Created on 2022-07-12 by the reprex package (v2.0.1)