pr_tree_cv: Cross-Validation for Probabilistic Regression Trees

View source: R/prtree_cv.R

pr_tree_cvR Documentation

Cross-Validation for Probabilistic Regression Trees

Description

Performs cross-validation for Probabilistic Regression Trees. Supports both k-fold and Monte Carlo (repeated random subsampling) methods.

Usage

pr_tree_cv(y, X, control_cv = list(), verbose = TRUE, ...)

Arguments

y

A numeric vector of response values.

X

A numeric matrix or data frame of predictor variables.

control_cv

A list of cross-validation control parameters, typically created by pr_tree_control_cv. Default values are taken from pr_tree_control_cv for any parameters not specified. Alternatively, control parameters can be passed directly via the ... argument.

verbose

Logical. If TRUE (default), prints progress and informational messages. If FALSE, suppresses all non-error console output.

...

Control parameters to be passed to pr_tree_control.

Details

The pr_tree_cv function provides flexible cross-validation for Probabilistic Regression Trees, supporting three common scenarios:

1. Sigma + test error estimation (default)

When both prop_valid > 0 and a grid of \boldsymbol\sigma values is provided (either via control$sigma_grid or automatically generated), the function performs a two-step procedure in each iteration/fold:

  • The training data is further split into training and validation sets according to prop_valid.

  • The validation set is used to select the optimal \boldsymbol\sigma from the grid.

  • The model is then refitted using the full training set (training + validation) with the selected \boldsymbol\sigma.

  • Finally, the refitted model is evaluated on the test set.

This yields both the selected \boldsymbol\sigma values for each iteration/fold and estimates of the test error.

2. Test error estimation only (sigma vector fixed)

When prop_valid = 0 or NULL, no internal validation is performed. This is appropriate when \boldsymbol\sigma is already known (e.g., from a previous analysis) and you only need to estimate the test error. In this case:

  • The model is trained directly on the full training set using the provided \boldsymbol\sigma values (a vector or 1 by n_feat matrix).

  • The test error is computed on the test set.

This mode is computationally lighter and focuses solely on error estimation.

3. Sigma selection only (no test error)

When prop_test = 0 (for Monte Carlo) or when you're interested only in the selected \boldsymbol\sigma values and not in test error estimation, the function can be used without a test set. In this case:

  • All data is used for training and validation.

  • The optimal \boldsymbol\sigma is selected via internal validation (if prop_valid > 0) or using the full training set (if prop_valid = 0).

  • No test error is computed (rmse_by_rep will contain NULL).

This mode is useful when you want to estimate \boldsymbol\sigma from the entire dataset before refitting a final model, or when you're only interested in the stability of \boldsymbol\sigma estimates across different splits.

Cross-validation methods

Two resampling methods are available:

  • Monte Carlo CV (method = "montecarlo"): In each of n_rep iterations, the data is randomly split into training (1 - prop_test) and testing (prop_test) sets. Observations may appear in multiple test sets across iterations.

  • k-fold CV (method = "kfold"): The data is partitioned into n_rep disjoint subsets. Each fold serves as the test set once, while the remaining n_rep - 1 folds are used for training and validation. This ensures every observation is tested exactly once.

Value

A list of class prtree.cv with components:

sigma_matrix

Matrix of selected \boldsymbol\sigma values, with one row per iteration/fold and one column per feature.

rmse_by_rep

A list with components:

  • validation: Numeric vector of validation RMSE values (NULL if no validation set used).

  • test: Numeric vector of test RMSE values (NULL if only_sigma = TRUE or no test set).

track_sigma

Logical indicating whether sigma varies across iterations/folds (TRUE) or is fixed (FALSE). When TRUE, different sigma values may be produced for each iteration/fold, either because they are estimated from the data of that iteration (sigma_grid = NULL) or selected from a grid of candidates (grid_size > 1). When FALSE, the same sigma value is used for all iterations/folds.

Examples


# Generate example data
set.seed(123)
X <- matrix(runif(1000, 0, 10), ncol = 2)
y <- 2 * sin(X[, 1]) + 0.5 * X[, 2] + rnorm(500, 0, 0.2)

# Default Monte Carlo CV
cv1 <- pr_tree_cv(y, X)
plot(cv1)

# Monte Carlo CV with custom parameters
control_cv <- pr_tree_control_cv(
  method = "montecarlo",
  n_rep = 5,
  prop_test = 0.3,
  prop_valid = 0.2
)
cv2 <- pr_tree_cv(y, X, control_cv = control_cv)
plot(cv2)



PRTree documentation built on Nov. 23, 2026, 9:08 a.m.