Waspadalah sampleterhadap pemisahan jika Anda mencari hasil yang dapat direproduksi. Jika data Anda sedikit berubah, perpecahan akan bervariasi bahkan jika Anda menggunakannya set.seed. Misalnya, bayangkan daftar ID yang diurutkan dalam data Anda adalah semua angka antara 1 dan 10. Jika Anda hanya membatalkan satu pengamatan, katakanlah 4, pengambilan sampel berdasarkan lokasi akan menghasilkan hasil yang berbeda karena sekarang 5 hingga 10 semua tempat yang dipindahkan.
Metode alternatif adalah dengan menggunakan fungsi hash untuk memetakan ID menjadi beberapa angka acak semu dan kemudian sampel pada mod angka-angka ini. Sampel ini lebih stabil karena tugas sekarang ditentukan oleh hash dari setiap pengamatan, dan bukan oleh posisi relatifnya.
Sebagai contoh:
require(openssl) # for md5
require(data.table) # for the demo data
set.seed(1) # this won't help `sample`
population <- as.character(1e5:(1e6-1)) # some made up ID names
N <- 1e4 # sample size
sample1 <- data.table(id = sort(sample(population, N))) # randomly sample N ids
sample2 <- sample1[-sample(N, 1)] # randomly drop one observation from sample1
# samples are all but identical
sample1
sample2
nrow(merge(sample1, sample2))
[1] 9999
# row splitting yields very different test sets, even though we've set the seed
test <- sample(N-1, N/2, replace = F)
test1 <- sample1[test, .(id)]
test2 <- sample2[test, .(id)]
nrow(test1)
[1] 5000
nrow(merge(test1, test2))
[1] 2653
# to fix that, we can use some hash function to sample on the last digit
md5_bit_mod <- function(x, m = 2L) {
# Inputs:
# x: a character vector of ids
# m: the modulo divisor (modify for split proportions other than 50:50)
# Output: remainders from dividing the first digit of the md5 hash of x by m
as.integer(as.hexmode(substr(openssl::md5(x), 1, 1)) %% m)
}
# hash splitting preserves the similarity, because the assignment of test/train
# is determined by the hash of each obs., and not by its relative location in the data
# which may change
test1a <- sample1[md5_bit_mod(id) == 0L, .(id)]
test2a <- sample2[md5_bit_mod(id) == 0L, .(id)]
nrow(merge(test1a, test2a))
[1] 5057
nrow(test1a)
[1] 5057
ukuran sampel tidak tepat 5000 karena penugasan adalah probabilistik, tetapi seharusnya tidak menjadi masalah dalam sampel besar berkat hukum angka besar.
Lihat juga: http://blog.richardweiss.org/2016/12/25/hash-splits.html
dan /crypto/20742/statribution-properties-of-hash-functions-when -menghitung-modulo
xdapat menjadi indeks (baris / kolom no. katakan) dari Andadata.sizebisa0.75*nrow(data). Cobasample(1:10, 4, replace = FALSE, prob = NULL)lihat apa fungsinya.