Nonparametric estimator for ordered non-numeric outcomes. The estimator modifies a standard random forest splitting criterion to build a collection of forests, each estimating the conditional probability of a single class.

ocf(
  y = NULL,
  X = NULL,
  honesty = FALSE,
  honesty.fraction = 0.5,
  inference = FALSE,
  alpha = 0,
  n.trees = 2000,
  mtry = ceiling(sqrt(ncol(X))),
  min.node.size = 5,
  max.depth = 0,
  replace = FALSE,
  sample.fraction = ifelse(replace, 1, 0.5),
  n.threads = 1
)

Arguments

y

Outcome vector.

X

Covariate matrix (no intercept).

honesty

Whether to grow honest forests.

honesty.fraction

Fraction of honest sample. Ignored if honesty = FALSE.

inference

Whether to extract weights and compute standard errors. The weights extraction considerably slows down the routine. honesty = TRUE is required for valid inference.

alpha

Controls the balance of each split. Each split leaves at least a fraction alpha of observations in the parent node on each side of the split.

n.trees

Number of trees.

mtry

Number of covariates to possibly split at in each node. Default is the square root of the number of covariates.

min.node.size

Minimal node size.

max.depth

Maximal tree depth. A value of 0 corresponds to unlimited depth, 1 to "stumps" (one split per tree).

replace

If TRUE, grow trees on bootstrap subsamples. Otherwise, trees are grown on random subsamples drawn without replacement.

sample.fraction

Fraction of observations to sample.

n.threads

Number of threads. Zero corresponds to the number of CPUs available.

Value

Object of class ocf.

References

  • Di Francesco, R. (2023). Ordered Correlation Forest. arXiv preprint arXiv:2309.08755.

See also

Author

Riccardo Di Francesco

Examples

## Generate synthetic data.
set.seed(1986)

data <- generate_ordered_data(1000)
sample <- data$sample
Y <- sample$Y
X <- sample[, -1]

## Training-test split.
train_idx <- sample(seq_len(length(Y)), floor(length(Y) * 0.5))

Y_tr <- Y[train_idx]
X_tr <- X[train_idx, ]

Y_test <- Y[-train_idx]
X_test <- X[-train_idx, ]

## Fit ocf on training sample.
forests <- ocf(Y_tr, X_tr)

## We have compatibility with generic S3-methods.
print(forests)
#> Call: 
#> ocf(Y_tr, X_tr) 
#> 
#> Data info: 
#> Full sample size:   500 
#> N. covariates:      6 
#> Classes:            1 2 3 
#> 
#> Relative variable importance: 
#>    x1    x2    x3    x4    x5    x6 
#> 0.339 0.099 0.251 0.064 0.213 0.035 
#> 
#> Tuning parameters: 
#> N. trees:           2000 
#> mtry:               3 
#> min.node.size       5 
#> Subsampling scheme: No replacement 
#> Honesty:            FALSE 
#> Honest fraction:    0
summary(forests)
#> Call: 
#> ocf(Y_tr, X_tr) 
#> 
#> Data info: 
#> Full sample size:   500 
#> N. covariates:      6 
#> Classes:            1 2 3 
#> 
#> Relative variable importance: 
#>    x1    x2    x3    x4    x5    x6 
#> 0.339 0.099 0.251 0.064 0.213 0.035 
#> 
#> Tuning parameters: 
#> N. trees:           2000 
#> mtry:               3 
#> min.node.size       5 
#> Subsampling scheme: No replacement 
#> Honesty:            FALSE 
#> Honest fraction:    0
predictions <- predict(forests, X_test)
head(predictions$probabilities)
#>         P(Y=1)    P(Y=2)     P(Y=3)
#> [1,] 0.3215795 0.4732135 0.20520702
#> [2,] 0.3136190 0.4936867 0.19269430
#> [3,] 0.3624392 0.2213801 0.41618073
#> [4,] 0.3627696 0.4386894 0.19854103
#> [5,] 0.7408662 0.1769811 0.08215271
#> [6,] 0.3670253 0.4038256 0.22914902
table(Y_test, predictions$classification)
#>       
#> Y_test   1   2   3
#>      1 118  35  32
#>      2  48  40  61
#>      3  32  34 100

## Compute standard errors. This requires honest forests.
honest_forests <- ocf(Y_tr, X_tr, honesty = TRUE, inference = TRUE)
head(honest_forests$predictions$standard.errors)
#>          P(Y=1)     P(Y=2)     P(Y=3)
#> [1,] 0.04611769 0.08459098 0.13421026
#> [2,] 0.08842578 0.14308939 0.02864389
#> [3,] 0.07919031 0.08602906 0.07010516
#> [4,] 0.10671703 0.13361349 0.03358193
#> [5,] 0.05545374 0.06682724 0.07569678
#> [6,] 0.13044318 0.07680194 0.04666298

## Marginal effects.
me <- marginal_effects(forests, eval = "atmean")
print(me)
#> ocf marginal effects results 
#> 
#> Data info: 
#> Number of classes:    3 
#> Sample size:          500 
#> 
#> Tuning parameters: 
#> Evaluation:           atmean 
#> Bandwidth:            0.1 
#> Number of trees:      2000 
#> Honest forests:       FALSE 
#> Honesty fraction:     0 
#> 
#> Marginal Effects: 
#>    P'(Y=1) P'(Y=2) P'(Y=3)
#> x1   0.172  -0.499   0.327
#> x2  -0.338  -0.034   0.372
#> x3   0.373  -0.074  -0.298
#> x4  -0.287   0.094   0.193
#> x5  -0.011  -0.023   0.035
#> x6  -0.067   0.035   0.032
print(me, latex = TRUE)
#> \begingroup
#>     \setlength{\tabcolsep}{8pt}
#>     \renewcommand{\arraystretch}{1.1}
#>     \begin{table}[H]
#>         \centering
#>         \begin{adjustbox}{width = 0.75\textwidth}
#>         \begin{tabular}{@{\extracolsep{5pt}}l c c c}
#>         \\[-1.8ex]\hline
#>         \hline \\[-1.8ex]
#>         & Class 1 & Class 2 & Class 3 \\
#>         \addlinespace[2pt]
#>         \hline \\[-1.8ex] 
#> 
#>         \texttt{x1} & 0.172 & -0.499 & 0.327 \\ 
#>         \texttt{x2} & -0.338 & -0.034 & 0.372 \\ 
#>         \texttt{x3} & 0.373 & -0.074 & -0.298 \\ 
#>         \texttt{x4} & -0.287 & 0.094 & 0.193 \\ 
#>         \texttt{x5} & -0.011 & -0.023 & 0.035 \\ 
#>         \texttt{x6} & -0.067 & 0.035 & 0.032 \\ 
#> 
#>         \addlinespace[3pt]
#>         \\[-1.8ex]\hline
#>         \hline \\[-1.8ex]
#>         \end{tabular}
#>         \end{adjustbox}
#>         \caption{Marginal effects.}
#>         \label{table:ocf.marginal.effects}
#>     \end{table}
#> \endgroup

## Compute standard errors. This requires honest forests.
honest_me <- marginal_effects(honest_forests, eval = "atmean", inference = TRUE)
honest_me$standard.errors
#>       P'(Y=1)    P'(Y=2)    P'(Y=3)
#> x1 0.15165395 0.20892182 0.08596515
#> x2 0.07666639 0.10019054 0.06905256
#> x3 0.13602488 0.20750917 0.07839312
#> x4 0.07933707 0.09752920 0.05244289
#> x5 0.09324607 0.09412552 0.02587149
#> x6 0.05190078 0.05855442 0.01552705
print(honest_me, latex = TRUE)
#> \begingroup
#>     \setlength{\tabcolsep}{8pt}
#>     \renewcommand{\arraystretch}{1.1}
#>     \begin{table}[H]
#>         \centering
#>         \begin{adjustbox}{width = 0.75\textwidth}
#>         \begin{tabular}{@{\extracolsep{5pt}}l c c c}
#>         \\[-1.8ex]\hline
#>         \hline \\[-1.8ex]
#>         & Class 1 & Class 2 & Class 3 \\
#>         \addlinespace[2pt]
#>         \hline \\[-1.8ex] 
#> 
#>         \texttt{x1} & 0.063 & -0.233 & 0.17 \\ 
#>                      & (0.152) & (0.209) & (0.086) \\ 
#>         \texttt{x2} & -0.107 & -0.097 & 0.204 \\ 
#>                      & (0.077) & (0.1) & (0.069) \\ 
#>         \texttt{x3} & -0.026 & -0.073 & 0.099 \\ 
#>                      & (0.136) & (0.208) & (0.078) \\ 
#>         \texttt{x4} & -0.078 & 0 & 0.077 \\ 
#>                      & (0.079) & (0.098) & (0.052) \\ 
#>         \texttt{x5} & 0.026 & -0.062 & 0.037 \\ 
#>                      & (0.093) & (0.094) & (0.026) \\ 
#>         \texttt{x6} & 0.038 & -0.04 & 0.002 \\ 
#>                      & (0.052) & (0.059) & (0.016) \\ 
#> 
#>         \addlinespace[3pt]
#>         \\[-1.8ex]\hline
#>         \hline \\[-1.8ex]
#>         \end{tabular}
#>         \end{adjustbox}
#>         \caption{Marginal effects.}
#>         \label{table:ocf.marginal.effects}
#>     \end{table}
#> \endgroup