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,
...
)
A fash
object containing the results of the FASH pipeline.
An integer specifying the dataset index to predict.
A numeric vector specifying refined x values for prediction. If NULL
, uses the x values from the model fit.
A logical value. If TRUE
, returns posterior samples. If FALSE
, summarizes the samples into mean and 95 percent confidence intervals.
An integer specifying the number of posterior samples to generate.
An integer specifying the order of the derivative to compute.
Additional arguments (not used).
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.
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.14 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.489148 1.487016 0.7924916 2.212558
#> 2 1.081633 1.492357 1.488697 0.8108650 2.196768
#> 3 1.163265 1.495567 1.491903 0.8310775 2.183535
#> 4 1.244898 1.498776 1.494167 0.8520789 2.167828
#> 5 1.326531 1.501986 1.497272 0.8716697 2.149724
#> 6 1.408163 1.505195 1.501303 0.8873631 2.130055
#> 7 1.489796 1.508404 1.504054 0.9052240 2.116422
#> 8 1.571429 1.511614 1.508078 0.9259044 2.102815
#> 9 1.653061 1.514823 1.512105 0.9451600 2.092166
#> 10 1.734694 1.518033 1.515040 0.9620380 2.080591
#> 11 1.816327 1.521242 1.517589 0.9850960 2.065811
#> 12 1.897959 1.524452 1.520211 0.9965237 2.055929
#> 13 1.979592 1.527661 1.524365 1.0109694 2.048401
#> 14 2.061224 1.530871 1.527446 1.0282717 2.036396
#> 15 2.142857 1.534080 1.532124 1.0468424 2.030073
#> 16 2.224490 1.537290 1.535939 1.0558674 2.012378
#> 17 2.306122 1.540499 1.539306 1.0725382 2.002400
#> 18 2.387755 1.543709 1.544169 1.0835865 1.988887
#> 19 2.469388 1.546918 1.546949 1.0962152 1.985589
#> 20 2.551020 1.550128 1.550789 1.1048525 1.985199
#> 21 2.632653 1.553337 1.550994 1.1079924 1.983322
#> 22 2.714286 1.556546 1.553932 1.1160428 1.986366
#> 23 2.795918 1.559756 1.558180 1.1254939 1.985668
#> 24 2.877551 1.562965 1.560115 1.1294609 1.980082
#> 25 2.959184 1.566175 1.563782 1.1436324 1.983160
#> 26 3.040816 1.569384 1.567793 1.1435650 1.981190
#> 27 3.122449 1.572594 1.571058 1.1449574 1.983185
#> 28 3.204082 1.575803 1.574672 1.1464807 1.991974
#> 29 3.285714 1.579013 1.577665 1.1487755 1.996429
#> 30 3.367347 1.582222 1.580723 1.1517959 2.003090
#> 31 3.448980 1.585432 1.584364 1.1491107 2.010830
#> 32 3.530612 1.588641 1.586117 1.1510933 2.020010
#> 33 3.612245 1.591851 1.591449 1.1512429 2.031422
#> 34 3.693878 1.595060 1.593786 1.1467798 2.045410
#> 35 3.775510 1.598269 1.595793 1.1421869 2.059394
#> 36 3.857143 1.601479 1.599768 1.1341229 2.069140
#> 37 3.938776 1.604688 1.604414 1.1265942 2.080778
#> 38 4.020408 1.607898 1.607383 1.1155145 2.098844
#> 39 4.102041 1.611107 1.608480 1.1061090 2.111370
#> 40 4.183673 1.614317 1.610638 1.0951299 2.127555
#> 41 4.265306 1.617526 1.614596 1.0872406 2.148153
#> 42 4.346939 1.620736 1.618011 1.0763904 2.165826
#> 43 4.428571 1.623945 1.620235 1.0634392 2.182944
#> 44 4.510204 1.627155 1.623043 1.0501639 2.208567
#> 45 4.591837 1.630364 1.628265 1.0325134 2.223331
#> 46 4.673469 1.633574 1.630845 1.0148095 2.244055
#> 47 4.755102 1.636783 1.632502 0.9992233 2.263697
#> 48 4.836735 1.639992 1.636524 0.9850651 2.290109
#> 49 4.918367 1.643202 1.639548 0.9742098 2.309625
#> 50 5.000000 1.646411 1.642256 0.9614250 2.332301
# 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.481165 1.483540 0.7771956 2.192821
#> 2 2 1.526142 1.523740 1.0081830 2.018000
#> 3 3 1.571119 1.571655 1.1722753 1.984236
#> 4 4 1.616096 1.622365 1.1272701 2.083401
#> 5 5 1.661073 1.672023 0.9688726 2.315041
# 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)