R/05_lfsr.R
compute_lfsr_sampling.RdThis function computes the local false sign rate (LFSR) for a specific dataset within a fash object
using posterior sampling. It estimates the probability that the sign of the effect is positive or negative
at each grid point and returns the full probability vectors.
compute_lfsr_sampling(fash_fit, index, smooth_var = NULL, M = 3000, deriv = 0)A fash object containing posterior samples.
An integer specifying the dataset index for which to compute the LFSR.
A numeric vector specifying refined x values for prediction.
If NULL, defaults to the dataset's original x values.
An integer specifying the number of posterior samples to generate.
An integer specifying the order of the derivative to compute.
A list containing:
A numeric vector of LFSR values, where each entry corresponds to a grid point in smooth_var.
A numeric vector of probabilities that the effect is positive at each grid point.
A numeric vector of probabilities that the effect is negative at each grid point.
# Example fash object (assuming it has been fitted)
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.07 seconds.
#> Starting empirical Bayes estimation...
#> Completed empirical Bayes estimation in 0.00 seconds.
#> fash object created successfully.
# Compute LFSR for a single dataset
lfsr_result <- compute_lfsr_sampling(fash_obj, index = 1, smooth_var = seq(0, 5, by = 0.1), M = 3000)
print(lfsr_result$lfsr) # Print the LFSR values
#> [1] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
#> [8] 1.0000000 1.0000000 1.0000000 1.0000000 0.4806667 0.4760000 0.4763333
#> [15] 0.4816667 0.4640000 0.4700000 0.4686667 0.4623333 0.4493333 0.4393333
#> [22] 0.4443333 0.4333333 0.4256667 0.4170000 0.4036667 0.4000000 0.3906667
#> [29] 0.3940000 0.3816667 0.3683333 0.3650000 0.3710000 0.3633333 0.3610000
#> [36] 0.3603333 0.3623333 0.3596667 0.3490000 0.3476667 0.3486667 0.3563333
#> [43] 0.3683333 0.3760000 0.3913333 0.3983333 0.4126667 0.4250000 0.4363333
#> [50] 0.4463333 0.4606667
print(lfsr_result$pos_prob) # Print the positive probability
#> [1] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
#> [8] 1.0000000 1.0000000 1.0000000 1.0000000 0.5193333 0.5240000 0.5236667
#> [15] 0.5183333 0.5360000 0.5300000 0.5313333 0.5376667 0.5506667 0.5606667
#> [22] 0.5556667 0.5666667 0.5743333 0.5830000 0.5963333 0.6000000 0.6093333
#> [29] 0.6060000 0.6183333 0.6316667 0.6350000 0.6290000 0.6366667 0.6390000
#> [36] 0.6396667 0.6376667 0.6403333 0.6510000 0.6523333 0.6513333 0.6436667
#> [43] 0.6316667 0.6240000 0.6086667 0.6016667 0.5873333 0.5750000 0.5636667
#> [50] 0.5536667 0.5393333
print(lfsr_result$neg_prob) # Print the negative probability
#> [1] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
#> [8] 1.0000000 1.0000000 1.0000000 1.0000000 0.4806667 0.4760000 0.4763333
#> [15] 0.4816667 0.4640000 0.4700000 0.4686667 0.4623333 0.4493333 0.4393333
#> [22] 0.4443333 0.4333333 0.4256667 0.4170000 0.4036667 0.4000000 0.3906667
#> [29] 0.3940000 0.3816667 0.3683333 0.3650000 0.3710000 0.3633333 0.3610000
#> [36] 0.3603333 0.3623333 0.3596667 0.3490000 0.3476667 0.3486667 0.3563333
#> [43] 0.3683333 0.3760000 0.3913333 0.3983333 0.4126667 0.4250000 0.4363333
#> [50] 0.4463333 0.4606667