Generates posterior predictions for a specific dataset from a fash object using Bayesian Model Averaging.

# S3 method for class 'fash'
predict(
  object,
  index = 1,
  smooth_var = NULL,
  only.samples = FALSE,
  M = 3000,
  deriv = 0,
  ...
)

Arguments

object

A fash object containing the results of the FASH pipeline.

index

An integer specifying the dataset index to predict.

smooth_var

A numeric vector specifying refined x values for prediction. If NULL, uses the x values from the model fit.

only.samples

A logical value. If TRUE, returns posterior samples. If FALSE, summarizes the samples into mean and 95 percent confidence intervals.

M

An integer specifying the number of posterior samples to generate.

deriv

An integer specifying the order of the derivative to compute.

...

Additional arguments (not used).

Value

If only.samples = TRUE, a matrix of posterior samples where rows correspond to smooth_var and columns correspond to posterior draws. If only.samples = FALSE, a data frame summarizing posterior predictions with columns:

- x: The refined x values.

- mean: The posterior mean.

- lower: The lower bound of the 95 percent interval.

- upper: The upper bound of the 95 percent interval.

- median: The posterior median.

Examples


set.seed(1)

# Example 1: Predict for a specific dataset with summarized results
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)
)
S <- list(rep(0.5, 5), rep(0.8, 5))
Omega <- list(diag(5), diag(5))
grid <- seq(0, 2, length.out = 10)
fash_obj <- fash(data_list = data_list, Y = "y", smooth_var = "x", offset = "offset", S = S, Omega = Omega, 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.
predict(fash_obj, index = 1, smooth_var = seq(1, 5, length.out = 50), only.samples = FALSE)
#>           x     mean   median     lower    upper
#> 1  1.000000 1.511823 1.508512 0.9385132 2.105484
#> 2  1.081633 1.514174 1.515938 0.9534898 2.091176
#> 3  1.163265 1.516211 1.514223 0.9570467 2.103179
#> 4  1.244898 1.517553 1.513125 0.9637114 2.090135
#> 5  1.326531 1.519220 1.516390 0.9689170 2.095153
#> 6  1.408163 1.521087 1.519267 0.9744707 2.091852
#> 7  1.489796 1.522293 1.517619 0.9915556 2.091435
#> 8  1.571429 1.523457 1.516547 0.9984360 2.094902
#> 9  1.653061 1.524640 1.519909 1.0104642 2.067074
#> 10 1.734694 1.526857 1.519688 1.0179101 2.058536
#> 11 1.816327 1.529916 1.522799 1.0203448 2.057015
#> 12 1.897959 1.533068 1.531879 1.0380680 2.053585
#> 13 1.979592 1.536569 1.535928 1.0339586 2.050855
#> 14 2.061224 1.541676 1.539655 1.0449462 2.038544
#> 15 2.142857 1.546737 1.543339 1.0593976 2.046744
#> 16 2.224490 1.551749 1.547263 1.0606680 2.058580
#> 17 2.306122 1.556091 1.550158 1.0602502 2.055718
#> 18 2.387755 1.560377 1.559092 1.0701813 2.055676
#> 19 2.469388 1.565698 1.561499 1.0682681 2.051702
#> 20 2.551020 1.570522 1.571643 1.0772941 2.054883
#> 21 2.632653 1.574645 1.576636 1.0752173 2.064658
#> 22 2.714286 1.578140 1.582502 1.0903589 2.052199
#> 23 2.795918 1.581473 1.580438 1.0954687 2.068033
#> 24 2.877551 1.586945 1.587386 1.1170004 2.052606
#> 25 2.959184 1.591735 1.591033 1.1205819 2.053698
#> 26 3.040816 1.595232 1.596495 1.1277280 2.063927
#> 27 3.122449 1.599372 1.600171 1.1216656 2.072251
#> 28 3.204082 1.603851 1.603815 1.1312861 2.091426
#> 29 3.285714 1.607790 1.605605 1.1375171 2.098252
#> 30 3.367347 1.611463 1.609165 1.1336041 2.107286
#> 31 3.448980 1.614490 1.614103 1.1407456 2.106733
#> 32 3.530612 1.618403 1.614090 1.1462168 2.116452
#> 33 3.612245 1.622942 1.615790 1.1514101 2.128197
#> 34 3.693878 1.627147 1.619923 1.1623517 2.119792
#> 35 3.775510 1.631404 1.623869 1.1631335 2.117404
#> 36 3.857143 1.636007 1.631204 1.1715228 2.117474
#> 37 3.938776 1.638376 1.632424 1.1759606 2.124672
#> 38 4.020408 1.638661 1.631205 1.1621779 2.138275
#> 39 4.102041 1.632673 1.619467 1.1573629 2.134970
#> 40 4.183673 1.625508 1.612199 1.1362522 2.144971
#> 41 4.265306 1.619155 1.606100 1.1295994 2.138171
#> 42 4.346939 1.612771 1.600542 1.1122584 2.135308
#> 43 4.428571 1.606346 1.599788 1.0931983 2.142901
#> 44 4.510204 1.599577 1.593056 1.0750417 2.125746
#> 45 4.591837 1.592675 1.588394 1.0661062 2.130050
#> 46 4.673469 1.585484 1.584413 1.0581362 2.117598
#> 47 4.755102 1.578391 1.577782 1.0547074 2.110304
#> 48 4.836735 1.571459 1.572563 1.0307216 2.099487
#> 49 4.918367 1.563724 1.569579 1.0158428 2.094453
#> 50 5.000000 1.555628 1.557445 0.9997975 2.094231

# Example 2: Generate posterior samples
samples <- predict(fash_obj, index = 1, smooth_var = seq(1, 5, length.out = 50), only.samples = TRUE)
dim(samples)  # Rows: refined_x, Columns: posterior samples
#> [1]   50 3000

# Example 3: Use original x values for prediction
summary <- predict(fash_obj, index = 1, smooth_var = NULL, only.samples = FALSE)
head(summary)
#>   x     mean   median     lower    upper
#> 1 1 1.522001 1.521678 0.9619668 2.110596
#> 2 2 1.543040 1.537600 1.0562331 2.050040
#> 3 3 1.598225 1.599876 1.1303469 2.067805
#> 4 4 1.640844 1.641464 1.1628212 2.106994
#> 5 5 1.562080 1.557129 1.0272469 2.107570

# Example 4: Increase number of posterior samples
samples <- predict(fash_obj, index = 1, smooth_var = seq(1, 5, length.out = 50), only.samples = TRUE, M = 5000)
summary <- predict(fash_obj, index = 1, smooth_var = seq(1, 5, length.out = 50), only.samples = FALSE, M = 5000)