Nonparametric estimation of marginal effects using an ocf
object.
marginal_effects(
object,
data = NULL,
which_covariates = c(),
eval = "atmean",
bandwitdh = 0.1,
inference = FALSE
)
An ocf
object.
Data set of class data.frame
to estimate marginal effects. It must contain at least the same covariates used to train the forests. If NULL
, marginal effects are estimated on object$full_data
.
Character vector storing the names of the covariates for which marginal effect estimation is desired. If empty (the default), marginal effects are estimated for all covariates.
Evaluation point for marginal effects. Either "mean"
, "atmean"
or "atmedian"
.
How many standard deviations x_up
and x_down
differ from x
.
Whether to extract weights and compute standard errors. The weights extraction considerably slows down the program.
Object of class ocf.marginal
.
marginal_effects
can estimate mean marginal effects, marginal effects at the mean, or marginal effects at the
median, according to the eval
argument.
The routine assumes that covariates with more than ten unique values are continuous. Otherwise, covariates are assumed to
be discrete.
Di Francesco, R. (2023). Ordered Correlation Forest. arXiv preprint arXiv:2309.08755.
## Generate synthetic data.
set.seed(1986)
data <- generate_ordered_data(100)
sample <- data$sample
Y <- sample$Y
X <- sample[, -1]
## Fit ocf.
forests <- ocf(Y, X)
## Marginal effects at the mean.
me <- marginal_effects(forests, eval = "atmean")
print(me)
#> ocf marginal effects results
#>
#> Data info:
#> Number of classes: 3
#> Sample size: 100
#>
#> 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.008 -0.167 0.159
#> x2 -0.034 0.066 -0.033
#> x3 -0.089 0.014 0.075
#> x4 -0.047 -0.148 0.194
#> x5 -0.022 0.027 -0.004
#> x6 0.009 -0.027 0.018
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.008 & -0.167 & 0.159 \\
#> \texttt{x2} & -0.034 & 0.066 & -0.033 \\
#> \texttt{x3} & -0.089 & 0.014 & 0.075 \\
#> \texttt{x4} & -0.047 & -0.148 & 0.194 \\
#> \texttt{x5} & -0.022 & 0.027 & -0.004 \\
#> \texttt{x6} & 0.009 & -0.027 & 0.018 \\
#>
#> \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_forests <- ocf(Y, X, honesty = TRUE)
honest_me <- marginal_effects(honest_forests, eval = "atmean", inference = TRUE)
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.064 & -0.117 & 0.181 \\
#> & (0.032) & (0.136) & (0.09) \\
#> \texttt{x2} & -0.004 & 0.009 & -0.005 \\
#> & (0.028) & (0.052) & (0.016) \\
#> \texttt{x3} & -0.065 & 0.003 & 0.062 \\
#> & (0.049) & (0.078) & (0.081) \\
#> \texttt{x4} & -0.025 & -0.021 & 0.045 \\
#> & (0.01) & (0.037) & (0.033) \\
#> \texttt{x5} & -0.029 & 0.028 & 0.001 \\
#> & (0.022) & (0.019) & (0.009) \\
#> \texttt{x6} & -0.002 & -0.009 & 0.011 \\
#> & (0.019) & (0.043) & (0.028) \\
#>
#> \addlinespace[3pt]
#> \\[-1.8ex]\hline
#> \hline \\[-1.8ex]
#> \end{tabular}
#> \end{adjustbox}
#> \caption{Marginal effects.}
#> \label{table:ocf.marginal.effects}
#> \end{table}
#> \endgroup