CecileProust-Lima / lcmm

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

Cannot use plot( which = "fit") with hlme object: 'Error in xtfrm.data.frame(x) : cannot xtfrm data frames' #216

Closed Br-Hesseling closed 9 months ago

Br-Hesseling commented 9 months ago

Dear Cécile and Vivianne,

Thank you very much for your lcmm package! I am currently conducting latent class growth analysis with up to 35 time points, for 230 individuals. I can succesfully create 1 up to 5 class models that converge. However, when I then want to use plot() to show the trajectories, I get the following error:

Error in xtfrm.data.frame(x) : cannot xtfrm data frames

My code is the following:

lcga1 <- hlme(nrsgem ~ meetmomentDag, subject = "ID", ng = 1, data = df1)
lcga2 <- hlme(nrsgem ~ meetmomentDag, subject = "ID", ng = 2, data = df1, mixture = ~ meetmomentDag, B = lcga1)
plot(lcga2, which = "fit", var.time = "meetmomentDag")

Error in xtfrm.data.frame(x) : cannot xtfrm data frames

As it should, it produces an object of class hlme. Edited to add: I can use plot() for which = "residuals" and for which = "postprob", that works fine...

When I create a similar model with the lcmm::data_hlme data, plotting the trajectories does work:

hlme1 <- hlme(Y ~ Time, subject = "ID", ng = 1, data = data_hlme)
hlme2 <- hlme(Y ~ Time, subject = "ID", ng = 2, data = data_hlme, mixture =~ Time, B = hlme1)
plot(hlme2, which = "fit", var.time = "meetmomentDag")

Rplot hlme2

When I compare lcga2 to hlme2, I do not see any differences between the two. I also made sure to use the same class variables in both models (numeric). I even tried if it made a difference if I removed cases with missing variables from my dataset, but that does not make a difference either.

Do you have any suggestions as to what else I can check or change to make plot() work for "fit"?

Best wishes, Brechtje

VivianePhilipps commented 9 months ago

Hello Brechtje,

it may be due to the data. The package expects a data frame in the data argument. We made some changes so that it works with tibbles (or other types), but there may still be issues. Could you please check if converting df1 to data.frame resolves the problem?

Viviane

Br-Hesseling commented 9 months ago

Hello Viviane,

Thank you so much for your swift reply! I thought my df1 was already a dataframe, but when I checked its class it indeed showed > class(df12) [1] "tbl_df" "tbl" "data.frame".

After converting it to data.frame it works! The R version in which this happened was R-4.3.1. I ran the same script with the same data on a virtual machine on which version R-4.1.2 is running and I did not encounter this issue. Wanted to let you know in case it's helpful. Thank you again, I am very happy to have it working now.

Best wishes, Brechtje