Skip to contents

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

model

the statistical model

data

training data

model_fit

trained object

formula

Object of class formula or character describing the model fit

terms

terms of the formula (term labels, e.g. "rcs(age, 3)")

variables

variables used by the terms of the formula

endpoint

Target variable

dist_matrix

A matrix with distances

order_matrix

A matrix with the order indices for similar cases search

Active bindings

endPoint

Deprecated: use endpoint instead.

distMat

Deprecated: use dist_matrix instead.

orderMat

Deprecated: use order_matrix instead.

Methods


Method new()

Initialize object for searching similar cases

Usage

CBRBase$new(formula, data)

Arguments

formula

Object of class formula or character describing the model fit

data

Training data of class data.frame


Method fit()

Fit the Model

Usage

CBRBase$fit()


Method calc_distance_matrix()

Calculates the distance matrix

Usage

CBRBase$calc_distance_matrix(query = NULL)

Arguments

query

Query data of class data.frame


Method get_similar_cases()

Extracts similar cases

Usage

CBRBase$get_similar_cases(
  query,
  k = 1,
  add_distance = TRUE,
  merge = FALSE,
  exclude_self = FALSE
)

Arguments

query

Query data of class data.frame

k

number of similar cases

add_distance

Add distance to result data.frame

merge

Add query data to matched cases data.frame

exclude_self

Exclude 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.

Usage

CBRBase$predict_outcome(query, k = 10, weighted = FALSE, exclude_self = FALSE)

Arguments

query

Query data of class data.frame

k

number of similar cases

weighted

If TRUE, cases are weighted with a tricube kernel of their distance to the query.

exclude_self

Exclude training cases whose row name equals the query's row name (leave-one-out predictions for in-sample queries).

Returns

A data.frame with one row per query and the column prediction. For class endpoints prediction is a factor, and there is one column prob_<level> with the share of (weighted) cases per class.


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.

Usage

CBRBase$evaluate(test, k = 10, weighted = FALSE, ...)

Arguments

test

Test data with the endpoint column(s)

k

number of similar cases

weighted

If TRUE, cases are weighted with a tricube kernel of their distance to the query.

...

further arguments: exclude_self for class and numeric endpoints, times and tau for survival endpoints.

Returns

A named list of scores and the number of test cases n (for survival endpoints the list of evaluate_survival()).


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)).

Usage

CBRBase$predict_survival(query, k = 10, times = NULL, weighted = FALSE)

Arguments

query

Query data of class data.frame

k

number of similar cases

times

time points; default: the event times of the training data

weighted

If TRUE, cases are weighted with a tricube kernel of their distance to the query.

Returns

A matrix of survival probabilities with one row per query and one column per time point (attribute times).


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.

Usage

CBRBase$evaluate_survival(
  test,
  k = 10,
  times = NULL,
  tau = NULL,
  weighted = FALSE
)

Arguments

test

Test data with the endpoint columns

k

number of similar cases

times

time points for the Brier score; default: 20 points between the 10% and 90% quantile of the training event times

tau

time horizon for the concordance index (risk score \(1 - S(\tau)\)); default: the largest of times

weighted

passed to predict_survival()

Returns

A list with c_index (Uno's C), ibs (integrated Brier score) and brier (data.frame with the Brier score at each time).


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 horizon differs (event before horizon vs. followed beyond horizon; cases censored before horizon are not used).

Usage

CBRBase$get_counterfactual_cases(
  query,
  k = 1,
  horizon = NULL,
  min_diff = NULL,
  k_outcome = 10
)

Arguments

query

Query data of class data.frame

k

number of counterfactual cases per query

horizon

time horizon for survival endpoints (default: median training event time)

min_diff

minimum outcome difference for numeric endpoints

k_outcome

number of similar cases used to predict the query's outcome if it is not given

Returns

A data.frame like get_similar_cases() with the additional column queryOutcome.


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.

Usage

CBRBase$add_cases(newdata, refit = FALSE)

Arguments

newdata

New cases (data.frame) with the variables and the endpoint of the model. Other columns of the training data are filled with NA.

refit

If TRUE, refit the model on the extended case base; new factor levels are then allowed.

Returns

The object, invisibly.


Method remove_cases()

Remove cases from the case base, e.g. noisy or redundant cases found by assess_cases().

Usage

CBRBase$remove_cases(cases, refit = FALSE)

Arguments

cases

row names (character) or positions (integer) of cases in the case base (get_case_base()).

refit

If TRUE, refit the model on the remaining cases.

Returns

The object, invisibly.


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.

Usage

CBRBase$assess_cases(k = 5, horizon = NULL, tolerance = NULL)

Arguments

k

number of neighbours

horizon

time horizon for survival endpoints (default: median event time). Cases censored before horizon are not assessed.

tolerance

numeric 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).

Usage

CBRBase$get_case_base()

Returns

A data.frame.


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.

Usage

CBRBase$explain_match(query, case)

Arguments

query

One query case (data.frame with one row)

case

One case (data.frame with one row), e.g. a row returned by get_similar_cases()

Returns

A data.frame with one row per term (regression) or variable (random forest).


Method clone()

The objects of this class are cloneable with this method.

Usage

CBRBase$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.