Root class for common functionality of this package
Root class for common functionality of this package
References
Lowsky, D. J. et al. (2013). A K-nearest neighbors survival probability prediction method. Statistics in Medicine, 32(12), 2062–2069. doi:10.1002/sim.5673
Uno, H. et al. (2011). On the C-statistics for evaluating overall adequacy of risk prediction procedures with censored survival data. Statistics in Medicine, 30(10), 1105–1117. doi:10.1002/sim.4154
Graf, E. et al. (1999). Assessment and comparison of prognostic classification schemes for survival data. Statistics in Medicine, 18(17-18), 2529–2545.
Keane, M. T. and Smyth, B. (2020). Good Counterfactuals and Where to Find Them: A Case-Based Technique for Generating Counterfactuals for Explainable AI. ICCBR 2020, LNCS 12311, 163–178. doi:10.1007/978-3-030-58342-2_11
Smyth, B. and Keane, M. T. (1995). Remembering to forget: A competence-preserving case deletion policy for case-based reasoning systems. Proceedings of IJCAI-95, 377–382.
Wilson, D. L. (1972). Asymptotic properties of nearest neighbor rules using edited data. IEEE Transactions on Systems, Man, and Cybernetics, 2(3), 408–421. doi:10.1109/TSMC.1972.4309137
Public fields
modelthe statistical model
datatraining data
model_fittrained object
formulaObject of class formula or character describing the model fit
termsterms of the formula (term labels, e.g.
"rcs(age, 3)")variablesvariables used by the terms of the formula
endpointTarget variable
dist_matrixA matrix with distances
order_matrixA matrix with the order indices for similar cases search
Active bindings
endPointDeprecated: use
endpointinstead.distMatDeprecated: use
dist_matrixinstead.orderMatDeprecated: use
order_matrixinstead.
Methods
Method get_similar_cases()
Extracts similar cases
Usage
CBRBase$get_similar_cases(
query,
k = 1,
add_distance = TRUE,
merge = FALSE,
exclude_self = FALSE
)Arguments
queryQuery data of class data.frame
knumber of similar cases
add_distanceAdd distance to result data.frame
mergeAdd query data to matched cases data.frame
exclude_selfExclude training cases whose row name equals the query's row name. Use this when the query rows are taken from the training data, so that a case is not returned as its own nearest neighbour.
Method predict_outcome()
Predict the outcome of class and numeric endpoints from the
k most similar training cases (the Reuse step of the
CBR cycle): a (weighted) majority vote with class
probabilities, or a (weighted) mean. For survival endpoints
use predict_survival().
Class endpoints are factors, characters, logicals or 0/1 variables; all other endpoints are numeric.
Method evaluate()
Evaluate predictions from retrieved cases on test data. Class
endpoints: accuracy, Brier score and log loss; numeric
endpoints: root mean squared error (RMSE) and mean absolute
error (MAE); survival endpoints: see
evaluate_survival(). Use it to choose k and the
distance method on held-out data.
Method predict_survival()
Predict survival curves from the k most similar
training cases: a Kaplan-Meier estimate over the retrieved
cases of each query (Lowsky et al., 2013). Only for survival
endpoints (Surv(time, status)).
Method evaluate_survival()
Evaluate survival predictions from retrieved cases on test
data: Uno's concordance index and the IPCW (integrated)
Brier score. Use it to choose k and the distance
method on held-out data.
Arguments
testTest data with the endpoint columns
knumber of similar cases
timestime points for the Brier score; default: 20 points between the 10% and 90% quantile of the training event times
tautime horizon for the concordance index (risk score \(1 - S(\tau)\)); default: the largest of
timesweightedpassed to
predict_survival()
Method get_counterfactual_cases()
Counterfactual cases: the nearest training cases whose outcome differs from the query's outcome ("native counterfactuals", Keane & Smyth, 2020).
The query's outcome is taken from its endpoint columns if
present, otherwise it is predicted from its k_outcome
most similar cases (majority class, mean, or majority
event status). A training case is a counterfactual if
classification endpoint: its class differs;
numeric endpoint: its outcome differs by at least
min_diff(default: standard deviation of the training outcome);survival endpoint: its event status at
horizondiffers (event beforehorizonvs. followed beyondhorizon; cases censored beforehorizonare not used).
Usage
CBRBase$get_counterfactual_cases(
query,
k = 1,
horizon = NULL,
min_diff = NULL,
k_outcome = 10
)Arguments
queryQuery data of class data.frame
knumber of counterfactual cases per query
horizontime horizon for survival endpoints (default: median training event time)
min_diffminimum outcome difference for numeric endpoints
k_outcomenumber of similar cases used to predict the query's outcome if it is not given
Method add_cases()
Add solved cases to the case base (the Retain step of the
CBR cycle). By default the model is not refitted: the new
cases are retrievable at once, and distances use the
fitted model (for GowerModel: the ranges of the
fitted cases). The new cases are checked like queries
(variables, factor levels, missing values) and need a
complete endpoint.
For RFModel the OOB and RF-GAP proximities need the
in-bag counts of all cases; refit the model to use them
after adding cases.
Method remove_cases()
Remove cases from the case base, e.g. noisy or redundant
cases found by assess_cases().
Method assess_cases()
Assess the cases of the case base for case-base maintenance.
Each case is retrieved against the other cases
(leave-one-out) with its k nearest neighbours.
Case \(j\) solves case \(i\) if \(j\) is among
the k nearest neighbours of \(i\) and has the same
outcome (same class, numeric outcomes within
tolerance, survival: same event status at
horizon). Following Smyth & Keane (1995), a case is
pivotal if no other case solves it (removing it
loses competence), and auxiliary if its coverage
(the cases it solves, and itself) is contained in the
coverage of a case that solves it (it is redundant). A case
is noisy if the majority of its neighbours predicts a
different outcome (Wilson editing, 1972); pivotal cases are
often noisy.
For RFModel with OOB or RF-GAP proximities, the
in-sample (out-of-bag) proximities are used.
Arguments
knumber of neighbours
horizontime horizon for survival endpoints (default: median event time). Cases censored before
horizonare not assessed.tolerancenumeric endpoints: largest outcome difference counted as the same outcome (default: half the standard deviation of the outcome).
Returns
A data.frame with one row per case of the case base
(row names as in get_case_base()): outcome,
agreement (share of neighbours with the same
outcome), noisy, n_reachable (number of cases
that solve the case), n_coverage (number of cases it
solves) and category ("pivotal",
"auxiliary" or "other").
Method get_case_base()
The case base: the training cases used for retrieval (complete cases, including added cases).
Method explain_match()
Explain why a training case is similar to a query. For regression models: the contribution of each term to the linear predictor of query and case, and its share of the distance. For random forests: in the trees where query and case end in different terminal nodes, the split variable at which their paths diverge.