healthcareai (version 2.3.0)

split_train_test: Split data into training and test data frames

Description

`split_train_test` splits data into two data frames for validation of models. One data frame is meant for model training ("train") and the other is meant to assess model performance ("test"). The distribution of outcome will be preserved acrosss the train and test datasets. Additionally, if there are groups in the dataset, you can keep all observations within a in the same train/test dataset by passing the name of the group column to grouping_col; this is useful, for example, when there are multiple observations per patient, and you want to keep each patient within one dataset.

Usage

split_train_test(d, outcome, percent_train = 0.8, seed, grouping_col)

Arguments

d

Data frame

outcome

Target column, unquoted. Split will be stratified across this variable

percent_train

Proportion of rows in d to put into training. Default is 0.8

seed

Optional, if provided the function will return the same split each time it is called

grouping_col

column name that specifies grouping. Individuals in the same group are in the same training/test set.

Value

A list of two data frames with names train and test

Details

This function wraps `caret::createDataPartition`. If outcome is a factor then the test/training porportions are stratified. Otherwise they are randomly selected.

If the grouping_col is given, then the groups are divided into the test/ training porportions.

Examples

Run this code
# NOT RUN {
split_train_test(mtcars, am, .9)


# Below is an additional example of grouping. Grouping is where individuals
# in the same group are in the same training/test set. Here we group on car
# owners. Owners will be in the same training/test set.
library(dplyr)

mtcars %>%
  mutate(owner = rep(letters[1:16], each = 2)) %>%
  split_train_test(., am, grouping_col = owner)

# }

Run the code above in your browser using DataCamp Workspace