# Splitting data into training and testing sets - keep ratios of binary response

I have data with binary response (default of a client). I want to split the data into training and testing sets. I have 300 default clients and 700 non default clients. When splitting, should I make sure that the training set keeps the same ratio 3:7 of defaults? Or can I simply take a random sample?

• If you take a randam sample, then the 3:7 ratio will approximately be kept, so in my opinion you do not have to correct, So I would take a random sample.
– user83346
Commented Sep 18, 2015 at 17:41

R's createDataPartition function from package caret does that for you. It would not necessarily keep the response in the same ratio, but would split your data with least bias.

• My question was not how to do it (how to keep the ratio) but whether it is a correct way to analyse data. Commented Sep 18, 2015 at 13:40

Yes, you can take a random sample.

As the difference between the two types is significant, a random sample would obey(almost) the same distribution.

You might want to read up on random sampling, for appreciating the concept and executing it with confidence.

If you want to be sure to keep a perfect ratio without introducing a selection bias, you could select respectively all the 1 values, sample it and select the quantity you want and do the same with the 0 values.

The ultimate question would be, what do you plan to do with your data once you've done the split ?

• The reason for splitting is to develop the model (what covariates to use) and fit it on the training subset. Then I want to use the testing subset to check if the model predicts well. Anyway, I've done a random sample and the ration has remained almost the same (29.5 % : 70.5 %). I guess I can work with it. Commented Sep 26, 2015 at 16:09
• Yep, that's the simplest way and as your data set increase, the ratios are gonna be closer to the ideal one due to the Law of Large Numbers. My answer was provided in case that your sample is small to do a perfect ratio selection. Commented Sep 27, 2015 at 12:39