textTrainRandomForest: Train word embeddings to a categorical variable using random...

textTrainRandomForestR Documentation

Train word embeddings to a categorical variable using random forrest.

Description

Train word embeddings to a categorical variable using random forrest.

Usage

textTrainRandomForest(
  x,
  y,
  x_append = NULL,
  append_first = FALSE,
  cv_method = "validation_split",
  outside_folds = 10,
  outside_strata_y = "y",
  outside_breaks = 4,
  inside_folds = 3/4,
  inside_strata_y = "y",
  inside_breaks = 4,
  mode_rf = "classification",
  preprocess_step_center = FALSE,
  preprocess_scale_center = FALSE,
  preprocess_PCA = NA,
  extremely_randomised_splitrule = "extratrees",
  mtry = c(1, 10, 20, 40),
  min_n = c(1, 10, 20, 40),
  trees = c(1000),
  eval_measure = "bal_accuracy",
  model_description = "Consider writing a description of your model here",
  multi_cores = "multi_cores_sys_default",
  save_output = "all",
  seed = 2020,
  ...
)

Arguments

x

Word embeddings from textEmbed.

y

Categorical variable to predict.

x_append

Variables to be appended after the word embeddings (x); if wanting to preappend them before the word embeddings use the option first = TRUE. If not wanting to train with word embeddings, set x = NULL.

append_first

(boolean) Option to add variables before or after all word embeddings.

cv_method

Cross-validation method to use within a pipeline of nested outer and inner loops of folds (see nested_cv in rsample). Default is using cv_folds in the outside folds and "validation_split" using rsample::validation_split in the inner loop to achieve a development and assessment set (note that for validation_split the inside_folds should be a proportion, e.g., inside_folds = 3/4); whereas "cv_folds" uses rsample::vfold_cv to achieve n-folds in both the outer and inner loops.

outside_folds

Number of folds for the outer folds (default = 10).

outside_strata_y

Variable to stratify according (default "y"; can also set to NULL).

outside_breaks

The number of bins wanted to stratify a numeric stratification variable in the outer cross-validation loop.

inside_folds

Number of folds for the inner folds (default = 3/4).

inside_strata_y

Variable to stratify according (default "y"; can also set to NULL).

inside_breaks

The number of bins wanted to stratify a numeric stratification variable in the inner cross-validation loop.

mode_rf

Default is "classification" ("regression" is not supported yet).

preprocess_step_center

normalizes dimensions to have a mean of zero; default is set to TRUE. For more info see (step_center in recipes).

preprocess_scale_center

normalize dimensions to have a standard deviation of one. For more info see (step_scale in recipes).

preprocess_PCA

Pre-processing threshold for PCA. Can select amount of variance to retain (e.g., .90 or as a grid c(0.80, 0.90)); or number of components to select (e.g., 10). Default is "min_halving", which is a function that selects the number of PCA components based on number of participants and feature (word embedding dimensions) in the data. The formula is: preprocess_PCA = round(max(min(number_features/2), number_participants/2), min(50, number_features))).

extremely_randomised_splitrule

default: "extratrees", which thus implement a random forest; can also select: NULL, "gini" or "hellinger"; if these are selected your mtry settings will be overridden (see Geurts et al. (2006) Extremely randomized trees for details; and see the ranger r-package for details on implementations).

mtry

hyper parameter that may be tuned; default:c(1, 20, 40),

min_n

hyper parameter that may be tuned; default: c(1, 20, 40)

trees

Number of trees to use (default 1000).

eval_measure

Measure to evaluate the models in order to select the best hyperparameters default "roc_auc"; see also "accuracy", "bal_accuracy", "sens", "spec", "precision", "kappa", "f_measure".

model_description

Text to describe your model (optional; good when sharing the model with others).

multi_cores

If TRUE it enables the use of multiple cores if the computer system allows for it (i.e., only on unix, not windows). Hence it makes the analyses considerably faster to run. Default is "multi_cores_sys_default", where it automatically uses TRUE for Mac and Linux and FALSE for Windows.

save_output

Option not to save all output; default "all". see also "only_results" and "only_results_predictions".

seed

Set different seed.

...

For example settings in yardstick::accuracy to set event_level (e.g., event_level = "second").

Value

A list with roc_curve_data, roc_curve_plot, truth and predictions, preprocessing_recipe, final_model, model_description chisq and fishers test as well as evaluation measures, e.g., including accuracy, f_meas and roc_auc (for details on these measures see the yardstick r-package documentation).

See Also

see textTrainLists

Examples


results <- textTrainRandomForest(
  x = word_embeddings_4$texts$harmonywords,
  y = as.factor(Language_based_assessment_data_8$gender),
  trees = c(1000, 1500),
  mtry = c(1), # this is short because of testing
  min_n = c(1), # this is short because of testing
  multi_cores = FALSE # This is FALSE due to CRAN testing and Windows machines.
)


text documentation built on Aug. 9, 2023, 5:08 p.m.