Predict from a BayesR Fit

Predict from a BayesR Fit

Description

Computes genomic estimated breeding values (GEBVs) and prediction metrics. Works identically for full-fit, train/test split, and k-fold CV — the caller is responsible for data partitioning; predict() only evaluates the trained model on whatever data it receives.

Usage

## S3 method for class 'masbayes_bayesr'
predict(object, newdata = NULL, y_new = NULL, X_new = NULL, ...)

Arguments

object An object of class masbayes_bayesr.
newdata Optional numeric design matrix. If NULL, the training GEBVs (object$pred_train) are returned.
y_new Optional response vector for newdata. When supplied, prediction metrics are computed.
… Unused.

Details

GEBV formula (direct marker effects). The genomic estimated breeding value for individual \(i\) is obtained by summing the posterior mean allele effects across all loci:

\(\hat{g}_i = \hat{\mu} + \mathbf{w}_i^\top \hat{\boldsymbol{\beta}}\)

where \(\mathbf{w}_i\) is row \(i\) of the marker design matrix \(W\) and \(\hat{\boldsymbol{\beta}}\) is the vector of posterior mean allele effects. When fixed effects were supplied to the fitting function:

\(\hat{g}_i = \hat{\mu} + \mathbf{x}_i^\top \hat{\boldsymbol{\alpha}} + \mathbf{w}_i^\top \hat{\boldsymbol{\beta}}\)

For binary traits, the liability-scale GEBV is converted to a predicted probability via the probit inverse link:

\(P(y_i = 1) = \Phi(\hat{g}_i)\)

This is identical for SNP and multi-allelic (microhaplotype) markers; only the construction of \(W\) differs between the two marker types.

Three usage modes.

In-sample (full-fit)
Call predict(fit). Returns training GEBVs and the in-sample metrics already stored in the fit object.
Forecast only
Call predict(fit, W_new) without y_new. Returns GEBVs for the new design matrix; metrics are skipped.
Test evaluation
Call predict(fit, W_test, y_test). Returns test-set GEBVs plus R2, RMSE, accuracy/AUC, bias.

For test sets built with construct_wah_matrix, pass the training matrix structure as reference_structure so that newdata has identical columns to the training matrix.

Value

An object of class masbayes_prediction: a list with GEBV (liability scale for binary), prob (binary only: P(y = 1) = pnorm(GEBV), otherwise NULL), metrics (R2, RMSE, accuracy/AUC, bias or NULL; for binary, computed on the observed/probability scale so bias is the calibration slope), h2, sigma2_g, sigma2_e, variance_components, response_type, model_type, eval_scope, and has_truth.

See Also

run_bayesr, summary.masbayes_bayesr

Examples

Code
library("masbayes")

d       <- load_data("small")
W       <- construct_snp_matrix(d$snp)$W
y       <- d$pheno$y_cont_qtl_snp
W_train <- W[d$train_idx, ]
W_test  <- W[d$test_idx, ]
y_train <- y[d$train_idx]
y_test  <- y[d$test_idx]

fit  <- run_bayesr(
  w             = W_train,
  y             = y_train,
  sigma2_e_init = var(y_train) / 2,
  sigma2_ah     = var(y_train) / 2,
  mcmc_params   = list(n_iter = 1000L, n_burn = 500L,
                       n_thin = 5L, seed = 1L),
  save_rds      = FALSE
)

pred <- predict(fit, W_test, y_test)   # test-set evaluation
pred$metrics$accuracy

fc   <- predict(fit, W_test)           # forecast only, no y_new
head(fc$GEBV)

insamp <- predict(fit)                 # in-sample reference
insamp$metrics$R2