CecileProust-Lima / lcmm

R package lcmm
https://CecileProust-Lima.github.io/lcmm/
48 stars 13 forks source link

Error while using predictClass #250

Open Arjuntk opened 2 months ago

Arjuntk commented 2 months ago

I am trying to use predictClass on external data. I am using multlcmm model, below is my code:

m1 <- multlcmm(var1 + var2 ~ Year,
               random=~Year,
              subject='ID', ng=1, verbose=TRUE,
               data=mydata, link="thresholds", nproc=32, maxiter = 100)

m2_grid <-gridsearch(multlcmm(var1 + var2 ~ Year,
                         random=~Year,  subject='ID', ng=2, 
                         data=mydata, link="thresholds", verbose=TRUE,
                        mixture=~Year, nproc=32),
                         rep=10, maxiter = 100, minit=m1)

I am getting below error when i run predictClass:

predictClass(model=m2_grid, newdata=newdata, subject='ID')
Error in multlcmm(fixed = var1 + var2 ~  : 
  Vector B should be of length 18

newdata is of the same format as mydata. i also tried running predictClass on mydata and it ran. But when I tried to run it on sample mydata, I get an error.

predictClass(model=m2_grid, newdata=mydata, subject='ID')
No error (Expected result).

But if i run a sample of mydata, I get an error.

predictClass(model=m2_grid, newdata=mydata[1:200,], subject='ID')
Error in multlcmm(fixed = var1 + var2 ~  : 
  Vector B should be of length 18

Please help me figure out this error. Thanks.

VivianePhilipps commented 1 month ago

Hi,

I think you get the error because you have not the same modalities in the var1 and var2 variables between your "mydata" sample and "newdata". In the thresholds model, the number of parameters depends on the number of different values in the outcomes. For example, if var1 and var2 ranges both from 1 to 5, you have 8 threshold parameters. If in newdata the modality "3" is not observed for var2, the model expects 7 threshold parameters. A workaround in to add a virtual subject that has the missing modalities, in order to have the same number of parameters.

Viviane