donaldRwilliams / BGGM

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

Prior specification #79

Closed martinobelvederi closed 3 years ago

martinobelvederi commented 3 years ago

First of all, many thanks for you great work. I have been using BGGM and hope to learn also blsmeta someday. I'm primarily a clinician, but I'm trying to approach bayesian methods bit by bit; please forgive if I'm imprecise. Is there a way to estimate networks (possibly with BGGM) but providing specifications on prior edges? I have noticed BGGM lets you specify prior sd, but not a sort of "prior network" with specific edges. I mean something similar to the "whitelist" or "blacklist" I encountered playing with DAGs in bnlearn, if not outright partial correlation values. I have also tried looking at BDgraph but it seems to me that it only returns the presence (and probability) of each edge, but not its weight (partial cor value). Guess this would make a extremely helpful additional feature.

BTW My intention would be to estimate networks "building up" from prior knowledge /studies that looked only at a subset of the total variables (hope this makes sense).

Another, more far reaching question would be the following (maybe for another time?) Suppose I estimated a hierarchical bayesian cognitive model (e.g. from brms), can you "integrate" parameter values (with their posterior distribution) as nodes into a BGGM network model? or maybe one should build the whole model in Stan, I suppose. do you think it would make sense in the first place? not that I have done anything similar yet, but I hope one day to go in this direction. Thanks for the inspiration and for providing the tools to play!

donaldRwilliams commented 3 years ago

There is. The most straightforward way is to make a "prior network" in which you assign each edge a prior probability (well, actually, the prior is for the hypotheses). By default, this is set to 1, so equal prior probs for null vs alternative model. Then, with this in hand, the BF can be recomputed in light of the prior network.

To make sense of this, if you think there is likely an edge, then you can assign a higher prior prob to the alternative, say, 0.90, and then 0.10 to the null. Then the prior odds is 0.9/0.1 in favor of an edge. This is used to compute the Bayes factor. If you are right, then there will be more power.

I will provide an example later today that you can use for now, and then I will add a feature in the future

donaldRwilliams commented 3 years ago

let me think more about the second question. more details would help

martinobelvederi commented 3 years ago

amazing news!! thanks so much as for the second poin, it's just an embrional idea from the computational psychiatry course I attended. I think it would be quite interesting to combine parameters from, say, cognitive computational models, with sypmtom ratings to understand how they interact. Parameters (e.g. lambda of delay discounting) are usually fit with hierarchical bayesian models and we get a posterior estimate for each subject. So I was wondering if there was a way to integrate posterior samples or estimates from an existing brms model into bggm, retaining the rich info that they carry. However, as I write, I realize that 1) sypmtom severity ratings are only point estimates, although we can (should?) hypothesize their generating distribution, and 2) bggm estimates the posterior for the partial correlation...maybe it makes more sense to build an overarching model in stan? (although I wouldn't dare with my current skills) anyway thanks for bearing with me

donaldRwilliams commented 3 years ago

I added a function called prior_belief_ggm.

I am having some trouble with the checks to sure the package builds, etc.

I hope to have it figured out tomorrow, and then you can safely install the new version of BGGM.

martinobelvederi commented 3 years ago

thank you so much!

donaldRwilliams commented 3 years ago

I am getting ready to merge to the master, so you can install from github in an hour or so.

Here is an example, assuming we have perfect prior information and set the prior odds to 3/1.

library(BGGM)

# Assume perfect prior information
# synthetic ggm
p <- 20
main <- gen_net()

# prior odds 3:1, assuming graph is known
prior_ggm <- ifelse(main$adj == 1, 3, 1)

# generate data
y <- MASS::mvrnorm(n = 200, 
                   mu = rep(0, 20), 
                   Sigma = main$cors)

# prior est
prior_est <- prior_belief_ggm(Y = y, progress = FALSE,
                              prior_ggm = prior_ggm)

# check scores
BGGM:::performance(Estimate = prior_est$adj, 
                  True = main$adj)

#> $results
#>       measure     score
#> 1 Specificity 0.9774436
#> 2 Sensitivity 0.6666667
#> 3   Precision 0.9268293
#> 4      Recall 0.6666667
#> 5    F1_score 0.7755102
#> 6         MCC 0.7175273

# default in BGGM
default_est <- select(explore(y, progress = FALSE))

# check scores
BGGM:::performance(Estimate = default_est$Adj_10, 
                  True = main$adj)

#> $results
#>       measure     score
#> 1 Specificity 0.9774436
#> 2 Sensitivity 0.4736842
#> 3   Precision 0.9000000
#> 4      Recall 0.4736842
#> 5    F1_score 0.6206897
#> 6         MCC 0.5669467

Notice the sensitivity is higher in the first example.

Now to get the edges for that graph, you can use constrained_posterior or if you want a point estimate you can just use the following from my other package GGMnonreg

pcor_known_g <- GGMnonreg::constrained(cor(y), prior_est$adj)

pcor_known_g$wadj

Alternatively, you could just use the adjacency matrix from prior_belief_ggm.

donaldRwilliams commented 3 years ago

@martinobelvederi

you can now install from github.

martinobelvederi commented 3 years ago

fantastic!! in a few days I'll be able to test it thanks so much for the incredible support