如何在保留变量比例和分布的情况下将数据分为训练集和测试集?

4

可复现的例子:

library(caTools) #for sample.split function
set.seed(123)
#Creating example data frame
example_df <- data.frame(personID = > c(stringi::stri_rand_strings(1000, 5)),
                           sex = sample(1:2, 1000, replace=TRUE),
                           age = round(rnorm(1000, mean=50, sd=15), 0))

#Example of random splitting:
training_set <- example_df[sample.split(example_df$personID),]
test_set <- example_df[-c(training_set$personID),]

#evaluation of variables in test and training data sets:
  #Has to approximate 1 (in this case it's 1.2, which is too high)
  (sum(training_set$sex == 1) / sum(training_set$sex == 2)) / (sum(test_set$sex == 1) / sum(test_set$sex == 2)) 
  [1] 1.219139
  #Has to approximate 1 along the distribution (it's quite good, this is actually what i would expect)
  summary(training_set$age) / summary(test_set$age)
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.7143  0.9756  1.0000  1.0032  1.0169  1.0000 

虽然sample.split函数为变量age正确进行了划分(分布相符),但是男性和女性在变量sex中的比例存在显著差异。您需要使用哪个函数将数据自动均匀地分成多个集合(例如两个集合),同时保持各变量的比例和分布不变?


一般来说是个好问题。但是不完全可重现。sample.split 是从哪里来的?数据框创建中的流氓 > 是什么意思?我强烈建议您了解 reprex 包。 - JBGruber
1个回答

1

caret 包将为您构建平衡集。请查看基本内容的 vignette 包。例如:

inTrain <- createDataPartition(
  y = Sonar$Class,
  ## the outcome data are needed
  p = .75,
  ## The percentage of data in the
  ## training set
  list = FALSE
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接