% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/RFok.R
\name{rfok}
\alias{rfok}
\title{Conformal Random Forest with Out-of-bag KNN}
\usage{
rfok(X, y, k = 5, beta = sd(y)/30, eps = 0, ...)
}
\arguments{
\item{X}{a data frame or a matrix of predictors}

\item{y}{a response vector}

\item{k}{Number of nearest neighbors to use for out-of-bag error estimaters}

\item{beta}{Sensitivity parameter}

\item{eps}{Tolerance for nearest neighbor approach. When \code{eps = 0}, exact nearest neighbors are used.}

\item{...}{additional parameters passed to randomForest}
}
\value{
An object with class "rfok"
}
\description{
A conformal RF based on Johansson et al. (2014). Rather than using a traditional calibration set, conformal intervals are constructed using out-of-bag samples for each tree. K-nearest neighbors is used to calibrate the prediction intervals for each prediction location.
}
\details{
This function fits a random forest and constructs conformal prediction
intervals using out-of-bag (OOB) predictions. For each training observation,
an OOB prediction is computed using only trees for which that observation was
not included in the bootstrap sample. These OOB predictions are used to form
residuals without requiring a separate calibration set.

Local scale estimates are computed using the \code{k} nearest neighbors in
the predictor space. For observation \eqn{i}, the local scale estimate
\eqn{\mu_i} is the average absolute OOB residual among its nearest neighbors.
The non-conformity score is

\deqn{\alpha_i = \frac{|y_i - \hat{y}^{OOB}_i|}{\mu_i + \beta}.}

The parameter \code{k} controls the locality of the scale estimate. Smaller
values make the intervals more locally adaptive, while larger values produce
smoother, more stable local scale estimates.

The parameter \code{beta} is a non-negative stabilization parameter added to
the local scale estimate. Smaller values make the intervals more sensitive to
local changes in the OOB residuals. Larger values reduce this sensitivity and
make the intervals behave more like globally scaled conformal intervals. The
default, \code{sd(y) / 30}, is a scale-aware value intended to provide mild
stabilization.

The parameter \code{eps} is passed to the nearest-neighbor search. Setting
\code{eps = 0} uses exact nearest neighbors. Larger values allow approximate
nearest-neighbor search and may be faster for large data sets.
}
\examples{
# Friedman function
f <-  function(x){
  10 * sin(pi * x[1] * x[2]) + 20 * (x[3] - 0.5)^2 + 10 * x[4] + 5 * x[5]
}
X <- matrix(runif(250), nrow=50, ncol=5)
y <- apply(X, 1, f)
fit <- rfok(X, y)

Xnew <- matrix(runif(250), nrow=50, ncol=5)
predict(fit, Xnew)
}
\references{
Johansson, U., Boström, H., Löfström, T., & Linusson, H. (2014). Regression conformal prediction with random forests. Machine learning, 97, 155-176.
}
