R/06_BF.R
BF_update.Rd
This function updates the prior and posterior weights in a fitted fash
object using
Bayes Factor (BF) control. It automatically computes the Bayes Factor (BF), estimates
the proportion of null datasets (\(\pi_0\)), and updates the model accordingly.
BF_update(fash, plot = FALSE)
The updated fash
object with the following components updated:
Updated prior mixture weights reflecting the estimated \(\pi_0\).
Updated posterior mixture weights for each dataset.
Computed Bayes Factors for each dataset.
Local False Discovery Rate (LFDR), extracted as the first column of `posterior_weights`.
This function performs the following steps:
Computes Bayes Factors (BF): The BF is calculated as the ratio of likelihood under the alternative hypothesis to the likelihood under the null hypothesis.
Estimates \(\pi_0\): The function applies BF-based control to estimate the proportion of null datasets.
Updates prior weights: The function updates the prior mixture weights to reflect the estimated null proportion.
Updates posterior weights: The posterior weights are updated based on the reweighted prior and likelihood matrix.
Stores the computed Bayes Factors and LFDR: The function now saves the computed BF
and LFDR in the fash
object for further analysis.
# Example usage:
set.seed(1)
data_list <- list(
data.frame(y = rpois(5, lambda = 5), x = 1:5, offset = 0),
data.frame(y = rpois(5, lambda = 5), x = 1:5, offset = 0)
)
grid <- seq(0, 2, length.out = 10)
fash_obj <- fash(data_list = data_list, Y = "y", smooth_var = "x",
grid = grid, likelihood = "poisson", verbose = TRUE)
#> Starting data setup...
#> Completed data setup in 0.00 seconds.
#> Starting likelihood computation...
#>
|
| | 0%
|
|=================================== | 50%
|
|======================================================================| 100%
#> Completed likelihood computation in 0.15 seconds.
#> Starting empirical Bayes estimation...
#> Completed empirical Bayes estimation in 0.00 seconds.
#> fash object created successfully.
# Update prior and posterior weights using BF control
fash_updated <- BF_update(fash_obj, plot = TRUE)
#> Warning: Bayes Factors contain only NA or NaN values. BF-based correction cannot be applied. Returning the original fash object without updates. Please consider refitting the model or checking the data if you wish to use the BF-based correction for prior.
# Access updated components
print(fash_updated$prior_weights)
#> psd prior_weight
#> 1 0 1
print(fash_updated$posterior_weights)
#> 0
#> [1,] 1
#> [2,] 1
print(fash_updated$BF)
#> NULL
print(fash_updated$lfdr)
#> [1] 1 1