# How to randomly split the data set into multiple different sets:(training 70%:validation 10%(optional):testing 20%) in R?

I have a dataset with 4 predictor variables X1, X2, X3, X4, and one response variable Y. I have been asked to check the correlation between these variables and see how they are related and then use a linear model to fit them.

No split of training set: test set is given. I have one data set with 10000 samples. I was planning of splitting this data set in the ratio 80:20 for training and testing respectively.

I'd like to know how to do the same in R programming.

Also in general, we will split it into multiple combinations of training:testing set right? or? Please correct me if am wrong. I am a newbie to ML, so kindly help me out here.

Usually you choose the ratio between training and test data by how many samples you have, for example if you do not have that many you would go to something like 90:10, but with a larger one you can even co sider something like 75:25, although, 80:20 sounds good for your case.

For the random splitting that you are talking about, you should search and learn a lit k-fold cross-validation. It's a method with which you split your data in training and test sets k times in order to validate your model on different training and test sets each time.

About the R implementation i cannot really help you, but im pretty sure it should be quite easy to find a guide on how to do that.

Good luck.

# Load data
library(ISLR)


Split data into two parts (train and "rest"):

# 70% Sample size
smp_size <- floor(0.7 * nrow(cars))
# Set seed to control randomness
set.seed(123)
ind <- sample(seq_len(nrow(cars)), size = smp_size)
# Split data
train <- cars[ind, ]
rest <- cars[-ind, ]


Split "rest" of data again into two sets:

# Split the "rest" of the data into test, val
# 70% Sample size
smp_size <- floor(1/3 * nrow(rest))
# Set seed to control randomness
set.seed(123)
ind <- sample(seq_len(nrow(rest)), size = smp_size)
# Split data
val <- rest[ind, ]
test <- rest[-ind, ]