Compute posterior predictions for new data from a fitted ratio model.
Usage
# S3 method for class 'ratiod_fit'
predict(
object,
newdata,
type = c("response", "link"),
component = c("ratio", "numerator", "denominator", "all"),
summary = TRUE,
probs = c(0.025, 0.5, 0.975),
re_formula = NULL,
allow_new_levels = FALSE,
coords.0 = NULL,
include_spatial = TRUE,
return_spatial = FALSE,
n_samples = NULL,
...
)
# S3 method for class 'ratiod_prediction'
print(x, n = 10, ...)Arguments
- object
A
ratiod_fitobject- newdata
Data frame with new observations. Must contain all predictor variables used in the original model.
- type
Type of prediction: "response" for fitted values on the response scale, "link" for linear predictor scale.
- component
Which component to predict: "ratio" (default), "numerator", "denominator", or "all".
- summary
Logical; if TRUE, return summary statistics instead of full posterior draws (default TRUE).
- probs
Quantiles to compute if
summary = TRUE.- re_formula
Formula for random effects in prediction. Use
NULLto include all random effects (if new levels match),NAor~ 0to exclude random effects (population-level prediction).- allow_new_levels
Logical; if TRUE, allows new group levels not in the original data (predictions use population mean).
- coords.0
Matrix of spatial coordinates for new prediction locations (required for GP/HSGP spatial models). Should be N x 2 matrix with same coordinate system as training data.
- include_spatial
Logical; if TRUE (default), include spatial random effects in predictions. Set to FALSE for fixed-effects-only predictions.
- return_spatial
Logical; if TRUE, return spatial effect samples at new locations as
w.0.samplesin the output (similar to spOccupancy).- n_samples
Number of posterior samples to draw for backends that don't store samples directly (e.g., Laplace, VI). Default uses all available samples for HMC/PG.
- ...
Ignored
- x
A ratiod_prediction object
- n
Number of rows to print
Value
If summary = TRUE, a data frame with posterior summaries.
If summary = FALSE, a matrix of posterior draws (rows = draws, cols = obs).
If return_spatial = TRUE, returns a list with predictions and w.0.samples.
Examples
# \donttest{
# A smooth spatial field w shared by count and effort
set.seed(321)
n <- 60
x <- runif(n)
y <- runif(n)
w <- 0.6 * sin(2 * pi * x) + 0.6 * cos(2 * pi * y)
depth <- rnorm(n)
season <- factor(sample(c("summer", "winter"), n, replace = TRUE))
df <- data.frame(
count = rpois(n, exp(2.5 + 0.3 * depth + 0.4 * (season == "winter") + w)),
effort = rgamma(n, shape = 9, rate = exp(-w)),
depth = depth,
season = season,
x = x,
y = y
)
fit <- tratio(
count | effort ~ depth + season,
data = df,
family = ratiod_poisson_gamma(),
control = list(iter = 400, chains = 1, seed = 1)
)
# Predict for new data; levels of `season` follow the training data
new_df <- data.frame(depth = c(-1, 0, 1), season = "summer")
predict(fit, newdata = new_df)
#> tulpaRatio predictions
#> =================
#>
#> Posterior draws: 200
#> New observations: 3
#>
#> obs component mean sd q2.5 q50 q97.5
#> 1 ratio 0.9735659 0.1775233 0.6779457 0.9679804 1.453112
#> 2 ratio 1.3333026 0.2068171 0.9884281 1.3121018 1.825459
#> 3 ratio 1.8428555 0.3319191 1.2453527 1.8268176 2.525829
# Full posterior draws of the predicted ratio (draws x rows)
dim(predict(fit, newdata = new_df, summary = FALSE)$ratio)
#> [1] 200 3
# Spatial prediction at new locations (kriging) from a GP fit
fit_gp <- tratio(
count | effort ~ depth,
data = df,
spatial = spatial_gp(coords = c("x", "y")),
family = ratiod_poisson_gamma(),
control = list(iter = 400, chains = 1, seed = 1)
)
new_coords <- matrix(runif(10), ncol = 2)
predict(fit_gp, newdata = data.frame(depth = rnorm(5)),
coords.0 = new_coords)
#> tulpaRatio predictions
#> =================
#>
#> Posterior draws: 200
#> New observations: 5
#>
#> obs component mean sd q2.5 q50 q97.5
#> 1 ratio 1.2564437 0.09501087 1.0784341 1.2648160 1.464611
#> 2 ratio 0.9163408 0.10964141 0.7229692 0.9235379 1.141286
#> 3 ratio 0.9952051 0.10665063 0.7998387 1.0031824 1.212690
#> 4 ratio 3.1862257 0.39104624 2.4947446 3.1493899 4.004853
#> 5 ratio 1.4459924 0.08932396 1.2788499 1.4560539 1.655003
# }