# R/bf.R In BDgraph: Bayesian Structure Learning in Graphical Models using Birth-Death MCMC

#### Documented in bf

```## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
#                                                                              |
#     This file is part of BDgraph package.                                    |
#                                                                              |
#     BDgraph is free software: you can redistribute it and/or modify it under |
#                                                                              |
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
#     Computing the Bayes factor between two graph structures                  |
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |

bf = function( num, den, bdgraph.obj, log = TRUE )
{
G_1 = BDgraph::get_graph( num )
G_2 = BDgraph::get_graph( den )

if( ( inherits( bdgraph.obj, "bdgraph" ) ) | ( inherits( bdgraph.obj, "ssgraph" ) ) )
{

g1_min_g2 = G_1 - G_2

upper_g1_min_g2 = g1_min_g2[ upper.tri( g1_min_g2 ) ]

log_vec_bf[ upper_g1_min_g2 == 1  ] = upper_plinks[      upper_g1_min_g2 == 1  ] / upper_min_plinks[ upper_g1_min_g2 == 1  ]
log_vec_bf[ upper_g1_min_g2 == -1 ] =  upper_min_plinks[ upper_g1_min_g2 == -1 ] / upper_plinks[     upper_g1_min_g2 == -1 ]
log_vec_bf = log_vec_bf[ log_vec_bf != 0 ]

if( log == TRUE )
{
bf = sum( log( log_vec_bf ) )
}else{
bf = prod( log_vec_bf )
}
}

return( bf )
}

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
```

## Try the BDgraph package in your browser

Any scripts or data that you put into this service are public.

BDgraph documentation built on Dec. 28, 2022, 1:54 a.m.