donaldRwilliams / BGGM

Bayesian Gaussian Graphical Models
https://donaldrwilliams.github.io/BGGM/
GNU General Public License v2.0
54 stars 14 forks source link

ordinal variable with different Likert Scales #69

Open QMmmmLiu opened 3 years ago

QMmmmLiu commented 3 years ago

Hi Donald,

Thanks for the awesome package. I am trying to run estimate with 15 variables, first six variables are ordinal variables on a 5-point Likert Scale (from 1 to 5) and the latter 9 variables are on a 4-point Likert scale (from 1 to 4).

15 variables are stored in the matrix netdat

**************************************************|
Error in estimate(netdat2, type = "mixed") : 
  wishrnd(): given matrix is not symmetric positive definite

Any help would be appreciated to run estimate() with all variables in either mixed or ordinal type. Thanks!

donaldRwilliams commented 3 years ago

Hi. Can you share the data with me so that I can take a look at what might be causing the error ?

drwwilliams@ucdavis.edu

donaldRwilliams commented 3 years ago

I think I found the issue. Try the following

Y <- sapply(netdat, as.numeric)

fit <- estimate(Y, type = "mixed", 
                impute = TRUE, 
                iter = 1000)

which imputes the data while fitting. For some reason, this works but removing the NAs produces the error you noted above.

Also, for type = ordinal, all variables must have the same number of categories. I will note this in the documentation.

QMmmmLiu commented 3 years ago

Thanks for working on this. I applied the code but received this error:

BGGM: Posterior Sampling 
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|

error: wishrnd(): given matrix is not symmetric positive definite
**************************************************|
Error in estimate(Y, type = "mixed", impute = TRUE, iter = 1000) : 
  wishrnd(): given matrix is not symmetric positive definite
In addition: Warning message:
In estimate(Y, type = "mixed", impute = TRUE, iter = 1000) :
  imputation during model fitting is
currently only implemented for 'continuous' data.
donaldRwilliams commented 3 years ago

Try installing the developmental version

if (!requireNamespace("remotes")) { 
  install.packages("remotes")   
}   
remotes::install_github("donaldRwilliams/BGGM")
QMmmmLiu commented 3 years ago

It works! Thanks. A few additional questions regarding mixed-type:

  1. Would it be okay to mix ordinal variables of different Likert scale, binary variables, continuous variables? Does it change interpretations at all? For continuous variables, do they need to be centered/scaled beforehand?
  2. For discrete variables like categorical education, I assume that it would be okay to leave it in terms of on the scale of 1, 2, 3, 4. But for variables like race or sexuality, is it necessary to dummy code them before entering them into the model?

Thanks again.

donaldRwilliams commented 3 years ago
  1. It is fine to mix variables. It does not change the interpretation.
  2. I am honestly not sure about race or sexuality, given they do not have a real order. E.g., with education, I assume the order means something.
QMmmmLiu commented 3 years ago

Follow-up question: it seems that the same issue persist for the zero_order_cors() function when type is specified as mixed.

donaldRwilliams commented 3 years ago

Thanks for bringing that to my attention. If you want to get the correlations, you can fit the model as above, and then use pcor_2_cor() which just converts the partials to zero order correlations. So if the above works, then that will work as well.

tdt01 commented 1 year ago

Hello Donald,

I am using your package to estimate networks in the Bayesian approach for ordinal variables. I would like to have a related question. As you mentioned above, the algorithm works only when the ordinal variables have the same list of categories. I see this when I make the first run.

> exam2 = data.frame(hungry=kossa$phy_hungry, tired=kossa$phy_tired)
> apply(exam2, 2, table)
$hungry

  1   2   3   4 
984 307 166  14 

$tired

  1   2   3   4   5 
422 682 307  57   3 

> estimate(exam2, type = "ordinal")
BGGM: Posterior Sampling 
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Error: min(): object has no elements
In addition: Warning message:
In estimate(exam2, type = "ordinal") : imputation during model fitting is
currently only implemented for 'continuous'
                   and 'mixed' data.

I then change variable tired by collapsing levels 4 and 5, then the algorithm works

> exam3 = exam2
> for (i in 1 : dim(exam2)[1]){
+   if (!is.na(exam2$tired[i]) & exam2$tired[i] == 5){
+     exam3$tired[i]=4
+   }
+ }
> apply(exam3, 2, table)
  hungry tired
1    984   422
2    307   682
3    166   307
4     14    60
> estimate(exam3, type = "ordinal")
BGGM: Posterior Sampling 
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
BGGM: Finished
BGGM: Bayesian Gaussian Graphical Models 
--- 
Type: ordinal 
Analytic: FALSE 
Formula: ~ 1 
Posterior Samples: 5000 
Observations (n):
Nodes (p): 2 
Relations: 1 
--- 
Call: 
estimate(Y = exam3, type = "ordinal")
--- 
Date: Wed Jul 12 11:52:56 2023 
Warning message:
In estimate(exam3, type = "ordinal") : imputation during model fitting is
currently only implemented for 'continuous'
                   and 'mixed' data.

May I please have two questions?

  1. Until now that issue (requirement of the same number of categories) still persists? I am asking this because I just install the package today and after that, try to install the developmental version
if (!requireNamespace("remotes")) { 
  install.packages("remotes")   
}   
remotes::install_github("donaldRwilliams/BGGM")
  1. What does it mean by In estimate(exam3, type = "ordinal") : imputation during model fitting is currently only implemented for 'continuous' and 'mixed' data? Does it mean that the algorithm will drop/delete missing cases rather than making imputations for them?

Thank you so much! Kr, Trung Dung.