有沒有辦法在單個樣本中采樣 X 個隨機行和 X 個非隨機行?例如,我想獲取 4 行的 1,000 個樣本iris。我想隨機抽樣 3 行,iris并且每個樣本中的第四行將是相同的(這是為了模仿混合抽樣設計)。
我可以采樣 3 個隨機行 1000x 和固定行 1000x,然后將兩個資料幀合并在一起,但由于某些原因,這不是理想的情況。執行此操作的代碼如下所示:
df<- iris
fixed_sample<- iris[7,]
random<- list()
fixed<- list()
counter<- 0
for (i in 1:1000) {
# sample 4 randomly selected transects 100 time
tempsample_random<- df[sample(1:nrow(df), 3, replace=F),]
tempsample_fixed<- fixed_sample[sample(1:nrow(fixed_sample), 1, replace=F), ]
random[[i]]=tempsample_random
fixed[[i]]=tempsample_fixed
counter<- counter 1
print(counter)
}
random_results<- do.call(rbind, random)
fixed_results<- do.call(rbind, fixed)
從這里我將創建一個新列作為分組變數,然后根據該組將它們合并在一起。所以最終資料幀的每四行fixed_sample在每個樣本中都有 3 個隨機行和行號 7 ( )。
我已經研究過 using splitstackshape::stratified,但還沒有讓它按照我需要的方式作業。我將在幾個級別的采樣作業(樣本 2、3、4、5 行等,每個 1,000 倍)上執行此操作,因此能夠從同一樣本中提取固定行和隨機行是理想的開始。
任何幫助將不勝感激。
uj5u.com熱心網友回復:
我認為您可以使用lapply. 在這種情況下,我們將抽取 3 個樣本,但您可以更改seq(3)為seq(1000)獲取 1000 個樣本。我已按照您的示例選擇第 7 行作為固定行。
lapply(seq(3), function(i) iris[c(sample(seq(nrow(iris))[-7], 3), 7),])
#> [[1]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 67 5.6 3.0 4.5 1.5 versicolor
#> 105 6.5 3.0 5.8 2.2 virginica
#> 111 6.5 3.2 5.1 2.0 virginica
#> 7 4.6 3.4 1.4 0.3 setosa
#>
#> [[2]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 147 6.3 2.5 5.0 1.9 virginica
#> 131 7.4 2.8 6.1 1.9 virginica
#> 126 7.2 3.2 6.0 1.8 virginica
#> 7 4.6 3.4 1.4 0.3 setosa
#>
#> [[3]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 143 5.8 2.7 5.1 1.9 virginica
#> 145 6.7 3.3 5.7 2.5 virginica
#> 60 5.2 2.7 3.9 1.4 versicolor
#> 7 4.6 3.4 1.4 0.3 setosa
由reprex 包于 2022-05-18 創建(v2.0.1)
uj5u.com熱心網友回復:
這是一個方法:
fixed_row <- 7
set.seed(42)
random <- replicate(1000, df[c(fixed_row, sample(setdiff(seq_len(nrow(df)), fixed_row), size = 3)),], simplify = FALSE)
random[1:3]
# [[1]]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 7 4.6 3.4 1.4 0.3 setosa
# 50 5.0 3.3 1.4 0.2 setosa
# 66 6.7 3.1 4.4 1.4 versicolor
# 75 6.4 2.9 4.3 1.3 versicolor
# [[2]]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 7 4.6 3.4 1.4 0.3 setosa
# 147 6.3 2.5 5.0 1.9 virginica
# 123 7.7 2.8 6.7 2.0 virginica
# 50 5.0 3.3 1.4 0.2 setosa
# [[3]]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 7 4.6 3.4 1.4 0.3 setosa
# 129 6.4 2.8 5.6 2.1 virginica
# 48 4.6 3.2 1.4 0.2 setosa
# 25 4.8 3.4 1.9 0.2 setosa
目的是我們對除您打算包含在所有樣本中的固定行之外的所有行進行采樣,然后將其添加到行索引串列中。使用 的前提setdiff(.., fixed_row)允許您在此處使用任意集合,因此具有所需最終結果的零個或多個行索引是fixed_row可行的。
set.seed(42)
c(fixed_row, sample(setdiff(seq_len(nrow(df)), fixed_row), size = 3))
# [1] 7 50 66 75
c(fixed_row, sample(setdiff(seq_len(nrow(df)), fixed_row), size = 3))
# [1] 7 147 123 50
c(fixed_row, sample(setdiff(seq_len(nrow(df)), fixed_row), size = 3))
# [1] 7 129 48 25
(請注意,set.seed在 StackOverflow 上使用 只是為了重現性,您可能不應該在生產中使用它。)
uj5u.com熱心網友回復:
df <- iris
fixed_row = 2
resample_count = 1000
keep_rows <- unlist(
Map(1:resample_count,
f = function(x) c(fixed_row, sample(1:nrow(df),3))
)
)
resamples <- iris[keep_rows,]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/477555.html
