jmbh / mgm

Estimation of Time-Varying k-order Mixed Graphical Models
27 stars 8 forks source link

Error in mgmsampler function #28

Open LiuJL1100 opened 1 year ago

LiuJL1100 commented 1 year ago

Hi Jonas,

I run the exampling code in the documantation of mgmsampler function, but got the warning message:

Error in if (!(class(interactions[[i]][[row]]) %in% c("array", "matrix"))) stop("The parameters of each interaction have to be provided as k-dimensional array.") : 
  the condition has length > 1

How to slove this problem?

Jiahao-ZHU commented 1 year ago

I also came across this problem.

My mgm version is "1.2-14", R version is "4.3.2 (2023-10-31)", and RStudio version is "2023.09.1+494"

When I ran example 1 in the mgmsampler() function, I got the same warning message. However, sexamples 2 and 3 (focus on 3-way interaction) are fine. I wonder if there is any problem with 2-way interaction or else.

Hoping for solutions. Thanks.

Jiahao-ZHU commented 1 year ago

I went through the code of the function, and I think this problem might occur because the code: f (!(class(interactions[[i]][[row]]) %in% c("array", "matrix"))) in

for (i in 1:n_order) { n_ints <- length(interactions[[i]]) if (n_ints > 0) for (row in 1:n_ints) if (!(class(interactions[[i]][[row]]) %in% c("array", "matrix"))) stop("The parameters of each interaction have to be provided as k-dimensional array.") }

returns more than one logic element (as for example 1, the result is TRUE TRUE). Therefore, the condition has length > 1.

To solve this problem, I defined a new function mgm_sampler on my own, and replaced the original code with for(i in 1:n_order) { n_ints <- length(interactions[[i]]) if(n_ints > 0) { for(row in 1:n_ints) { if(!inherits(interactions[[i]][[row]], "array") && !inherits(interactions[[i]][[row]], "matrix")) { stop("The parameters of each interaction have to be provided as k-dimensional array.") } } } }

Hope this solution can help. :)

TomRow90 commented 3 weeks ago

Hello,

I was hoping to use this for some simulations I need to run. I just installed the latest developmental version and it looks like this error is still present (it was still present in the CRAN version as well). I tried the workaround posted by Jiahao-ZHU above but I can't quite get that to work. So I was just wondering whether anyone has found another workaround or whether an update to the developmental version could be made? Many thanks.

jmbh commented 3 weeks ago

I'll try to fix this asap. Meanwhile, an alternative and possibly more convenient way to simulate data with multivariate dependencies between variables with different domains is to simulate from a DAG. This avoids the issues arising from needing a proper joint distribution over all variables (see Appendix F in https://osf.io/preprints/psyarxiv/926pv and the accompanying code in corresponding reproducibility repo).

TomRow90 commented 3 weeks ago

Great, thanks Jonas, and for the tip and link as well.