The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Introduction to PRTree

Overview

PRTree implements Probabilistic Regression Trees (PRTrees) in R with a high-performance Fortran backend. Unlike classical regression trees (such as CART), which partition the predictor space using hard binary splits and yield piecewise-constant predictions, PRTrees assign observations to terminal regions smoothly via probability distributions, producing continuous regression surfaces.

A key feature of PRTree is its built-in support for missing predictor values (NA) during both model fitting and prediction, without requiring prior imputation.

For the theoretical foundations of PRTrees with missing data, see Prass, Neimaier, & Pumi (2026), Applied Mathematical Modelling, 117450 (https://doi.org/10.1016/j.apm.2026.117450). For comprehensive implementation details and usage examples, see Prass, Neimaier, & Pumi (2025), arXiv:2510.03634 (https://doi.org/10.48550/arXiv.2510.03634).

library(PRTree)

1. Fitting a Probabilistic Regression Tree

We start with a simple one-dimensional regression problem where the true signal is \(f(x) = \cos(x)\) observed with Gaussian noise.

set.seed(1234)
X <- matrix(runif(200, 0, 10), ncol = 1)
y <- cos(X[, 1]) + rnorm(200, 0, 0.08)

# Fit a PRTree using all observations for training (prop_hold = 0)
fit <- pr_tree(y, X, max_terminal_nodes = 9, prop_hold = 0)
summary(fit)
#> 
#> ========================================================
#> Summary of Probabilistic Regression Tree (PRTree)
#> ========================================================
#> Number of observations: 200
#> Number of features: 1
#> Number of terminal nodes: 8
#> Distribution: norm
#> Fill type: 2
#> 
#> Mean Squared Error:
#>   Train: 0.006051
#>   Validation: 0.000000
#>   Global: 0.006051
#> 
#> Selected sigma:
#>   5.7760
#> 
#> Terminal node statistics:
#>  node     gamma  P_mean     P_sd   P_min   P_q25 P_median   P_q75   P_max
#>     1     -1182 0.29456 0.162302 0.07697 0.15144  0.27374 0.43259 0.61291
#>     2      1301 0.30971 0.160608 0.07313 0.15958  0.28623 0.44602 0.60248
#>     3  55984681 0.05133 0.005906 0.03673 0.04723  0.05344 0.05635 0.05781
#>     4 -17862146 0.04073 0.010029 0.01917 0.03255  0.04435 0.04935 0.05028
#>     5  51722899 0.05586 0.010839 0.03038 0.04827  0.06077 0.06441 0.06577
#>     6 -95218723 0.04750 0.006947 0.02983 0.04427  0.04987 0.05294 0.05420
#>     7  -4128892 0.15726 0.021828 0.10408 0.14555  0.16086 0.17699 0.18090
#>     8   3170931 0.04304 0.010515 0.02167 0.03425  0.04717 0.05247 0.05437
#> ========================================================

Unlike standard regression trees, the fitted curve is smooth across split thresholds:

ord <- order(X[, 1])
plot(
  X[ord, 1], y[ord],
  pch = 16, cex = 0.6, col = "gray50",
  xlab = "x", ylab = "y",
  main = "PRTree Smooth Fit to cos(x)"
)
lines(X[ord, 1], fit$yhat[ord], col = "steelblue", lwd = 2)
lines(X[ord, 1], cos(X[ord, 1]), col = "firebrick", lwd = 1.5, lty = 2)
legend(
  "topright",
  legend = c("Observed", "PRTree fit", "True cos(x)"),
  col = c("gray50", "steelblue", "firebrick"),
  pch = c(16, NA, NA), lty = c(NA, 1, 2), lwd = c(NA, 2, 1.5),
  bty = "n"
)

Tree structure and diagnostic plots

plot_tree() displays the learned split hierarchy along with probability heatmaps at each terminal node (using base R graphics):

plot_tree(fit, max_bins = 10)

When ggplot2 is installed, plot(fit, y) produces model diagnostic panels (Observed vs. Fitted, Residuals, Q-Q plot, and Terminal Node Probabilities):

plot(fit, y, which = c(1, 2, 4, 5), ncol = 2)

Prediction on new data

Use predict() to obtain point predictions (complete = FALSE, the default) or both predictions and the terminal-node membership probability matrix P (complete = TRUE):

X_new <- matrix(c(1.0, 3.14, 6.28, 9.0), ncol = 1)
pred <- predict(fit, newdata = X_new, complete = TRUE)

round(pred$yhat, 4)
#> [1]  0.5717 -0.9948  1.0170 -0.9399
round(rowSums(pred$P), 6)
#> [1] 1 1 1 1

2. Controlling Smoothing Parameters and Distributions

Model parameters can be configured via pr_tree_control() or passed directly through ... in pr_tree().

By default, pr_tree() automatically constructs a grid of candidate smoothing parameters (sigma) scaled to the standard deviation of each predictor and selects the best vector on a holdout validation split (prop_hold = 0.2). You can also inspect or customize this grid using expand_sigma_grid(), or fix sigma_grid to a scalar, vector, or custom matrix:

set.seed(42)
X2 <- matrix(runif(200 * 2, 0, 10), ncol = 2)
colnames(X2) <- c("x1", "x2")
y2 <- sin(X2[, 1]) + 0.4 * X2[, 2] + rnorm(200, 0, 0.15)

# Inspect an automatically scaled sigma grid
grid <- expand_sigma_grid(X2, grid_size = 5, tiny_sigma = 1e-20, verbose = FALSE)
grid
#> 
#> ========================================================
#> Sigma Grid
#> ========================================================
#> Grid size: 6 candidates (5 + 1 tiny)
#> Features: 2
#> 
#> First 5 rows:
#>                  x1         x2
#> 1          1.00e-20   1.00e-20
#> 2            1.1681     1.1705
#> 3            2.3362     2.3410
#> 4            3.5043     3.5116
#> 5            4.6724     4.6821
#> ... (1 more rows)
#> ========================================================

# Fit using Student's t distribution (df = 4) and the custom grid
ctrl <- pr_tree_control(
  dist = "t",
  df = 4,
  sigma_grid = grid,
  max_terminal_nodes = 8,
  prop_hold = 0.2
)
fit_t <- pr_tree(y2, X2, control = ctrl)
fit_t
#> 
#> ========================================================
#> Probabilistic Regression Tree (PRTree)
#> ========================================================
#> Number of observations: 200
#> Number of features: 2
#> Number of terminal nodes: 6
#> Distribution: t
#>   with df = 4.0000
#> 
#> Mean Squared Error:
#>   Train: 0.403950
#>   Validation: 0.414891
#>   Global: 0.406138
#> 
#> Selected sigma:
#>    3.5043, 3.5116 
#> ========================================================

Setting sigma_grid = 0 recovers crisp (hard-split) behavior while expanding automatically to match the number of predictors in X:

ctrl_crisp <- pr_tree_control(sigma_grid = 0, prop_hold = 0, max_terminal_nodes = 6)
fit_crisp <- pr_tree(y2, X2, control = ctrl_crisp)
fit_crisp$sigma
#> [1] 0 0

3. Native Handling of Missing Data (NA)

PRTree handles missing predictor values directly during tree construction and prediction without prior imputation. Two parameters control this behavior:

set.seed(2026)
X_miss <- X2
X_miss[sample(nrow(X_miss), 25), 1] <- NA
X_miss[sample(nrow(X_miss), 20), 2] <- NA

# Compare the three missing-data probability strategies
mse_by_fill <- sapply(0:2, function(ft) {
  m <- pr_tree(
    y2, X_miss,
    fill_type = ft,
    proxy_crit = "both",
    max_terminal_nodes = 8,
    prop_hold = 0.2,
    grid_size = 4
  )
  m$mse["validation"]
})
names(mse_by_fill) <- paste0("fill_type=", 0:2)
round(mse_by_fill, 4)
#> fill_type=0 fill_type=1 fill_type=2 
#>      0.4002      0.6990      0.5155

Predictions also work seamlessly when newdata contains partial or complete rows of NA:

fit_na <- pr_tree(
  y2, X_miss,
  fill_type = 2,
  max_terminal_nodes = 8,
  prop_hold = 0,
  grid_size = 4
)

X_new_na <- rbind(
  c(2.0, 5.0),
  c(NA, 5.0),
  c(2.0, NA),
  c(NA, NA)
)
colnames(X_new_na) <- colnames(X2)

predict(fit_na, newdata = X_new_na)
#> [1] 2.134978 2.134978 2.386236 2.386236

4. Cross-Validation and Data Splitting

train_test_split() creates simple random splits, missing-value stratified splits, or k-fold partitions:

set.seed(123)
is_na_row <- apply(is.na(X_miss), 1, any)
folds <- train_test_split(stratify = TRUE, is_NA = is_na_row, n_rep = 5)
summary(folds)
#> 
#> ========================================================
#> K-fold Partition Summary
#> ========================================================
#> Sampling method: Stratified (by missing values)
#> Total observations: 200
#> Number of folds: 5
#> 
#> --- Fold Statistics ---
#>  Fold Size Missing Missing (%)
#>     1   41       9       22.0%
#>     2   40       8       20.0%
#>     3   40       8       20.0%
#>     4   40       8       20.0%
#>     5   39       8       20.5%
#> ========================================================

pr_tree_cv() automates both Monte Carlo (method = "montecarlo") and k-fold (method = "kfold") cross-validation, including internal validation for selecting sigma and refitting on the full training folds (update_final = TRUE):

set.seed(123)
ctrl_cv <- pr_tree_control_cv(
  method = "kfold",
  n_rep = 5,
  prop_valid = 0.2,
  stratify = TRUE,
  max_terminal_nodes = 6,
  grid_size = 3
)

cv_res <- pr_tree_cv(y2, X_miss, control_cv = ctrl_cv, verbose = FALSE)
summary(cv_res)
#> 
#> ========================================================
#> Summary of PRTree Cross-Validation
#> ========================================================
#> Cross-Validation Configuration:
#>   Method: 5-fold CV
#>   Test set: 1 fold per iteration (~20.0% of data).
#>   Validation (for sigma): 0.20 of the training folds.
#>   Features: 2
#>   Stratification: TRUE
#> 
#> --- RMSE Summary ---
#> Validation Set (used for sigma selection):
#>        Min        Q1    Median      Mean        Q3       Max         SD N
#>  0.7165538 0.7424459 0.7895829 0.7922058 0.8066387 0.9058076 0.07300658 5
#> 
#> Test Set (unseen data):
#>        Min       Q1    Median      Mean        Q3       Max        SD N
#>  0.7022832 0.712286 0.8733391 0.8323491 0.9087283 0.9651092 0.1188187 5
#> 
#> --- Selected Sigma Summary (by feature) ---
#>         Min     Mean   Median      Max       SD
#> x1 1.954499 4.308286 3.959845 5.893186 1.628666
#> x2 1.905049 4.223338 3.837079 5.858242 1.656737
#> ========================================================
plot(cv_res)

References

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.